-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.js
229 lines (188 loc) · 5.85 KB
/
helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
"use strict";
/**
Just some handy global functions.
By Matthew Aitchison
2016/04/25
Please feel free to copy / use the code as you see fit.
*/
// This just gives me a basic string.format
if (!String.prototype.format) {
String.prototype.format = function () {
var args = arguments;
return this.replace(/{(\d+)}/g, function (match, number) {
return typeof args[number] != 'undefined'
? args[number]
: match
;
});
};
}
// Creates a new array of given size using the fastest format, and zeros the data.
// This used tobe Float32Array, but a standard array is much faster
function createArray(size) {
var result = new Array(size);
for (var i = 0; i < size; i++)
result[i] = 0;
return result;
}
// Functions to convert rgb to hex color string (from stackoverflow):
function componentToHex(c) {
var hex = c.toString(16);
return hex.length == 1 ? "0" + hex : hex;
}
function rgbToHex(r, g, b) {
return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
}
// Convert page coordinates to canvas coordinates:
function pageToCanvas(pageX, pageY, canvas) {
var canvasX = pageX - canvas.offsetLeft;
var canvasY = pageY - canvas.offsetTop;
// this simple subtraction may not work when the canvas is nested in other elements
return { x: canvasX, y: canvasY };
}
function clip(value, min, max) {
if (value < min)
return min;
if (value > max)
return max;
return value;
}
// Fetches an XML document
// Will block execution till file loads. 30 second timeout, after which null is returned. Really bad idea 'busy sleep'.
function fetch(filename) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
myFunction(xhttp);
}
};
var xmlDoc = null;
function myFunction(xml) {
xmlDoc = xml.responseXML;
}
xhttp.open("GET", filename, true);
xhttp.send();
startTime = new Date().getTime();
while (xmlDoc == null) {
elapsed = (new Date().getTime()-startTime) / 1000;
console.log("waiting {0} ".format(elapsed.toFixed(1)));
if (elapsed > 10) {
window.alert("Timeout");
return null;
}
}
return xmlDoc;
}
// Serializes an array of floats to a (quite long) string.
// optional RLE compression.
function serialize(data, compress) {
if (compress == null) compress == true;
var len = data.length;
var stringData = []
var lastValue = data[0];
if (lastValue == null) lastValue = 0;
var runlength = 0;
function write(value, length) {
// write the value.
if (length > 1)
stringData.push(value.toString() + ":" + length.toString())
else
stringData.push(value.toString())
}
for (var i = 1; i < len; i++) {
runlength++;
var value = data[i];
// not sure why, but I'm getting undefinied in my array for some reason?
if (value == null) value = 0;
if (!compress || value != lastValue || i == (len - 1)) {
write(lastValue, runlength);
runlength = 0;
}
lastValue = value;
}
// write the final byte.
if (runlength >= 1)
write(lastValue, runlength);
return stringData.join(",");
}
// Deserializes a string into an array of floats.
function deserialize(dataString) {
var stringParts = dataString.split(",");
var len = stringParts.length;
var data = []
for (var i = 0; i < len; i++) {
var entry = stringParts[i].split(':');
var value = Number(entry[0]);
if (entry.length == 2)
var count = entry[1]
else
var count = 1;
for (var j = 0; j < count; j++)
data.push(value);
}
return data;
}
// -------------------------------------------------------------
// Mouse stuff
// -------------------------------------------------------------
function doMouseDown(e) {
mouse.isButtonDown = true;
}
function doMouseUp(e) {
mouse.isButtonDown = false;
}
function doMouseMove(e) {
e.preventDefault();
var canvasLoc = pageToCanvas(e.pageX, e.pageY, canvas);
mouse.x = canvasLoc.x;
mouse.y = canvasLoc.y;
}
var mouse = {
x: 0,
y: 0,
isButtonDown: false
}
// -------------------------------------------------------------
// Keyboard stuff
// -------------------------------------------------------------
// track keypresses.
function doKeyDown(e) {
var keycode;
var isShift;
if (window.event) {
keycode = window.event.keyCode;
isShift = !!window.event.shiftKey; // typecast to boolean
} else {
keycode = ev.which;
isShift = !!ev.shiftKey;
}
key.shift = isShift;
}
// track keypresses.
function doKeyUp(e) {
var keycode;
var isShift;
if (window.event) {
keycode = window.event.keyCode;
isShift = !!window.event.shiftKey; // typecast to boolean
} else {
keycode = ev.which;
isShift = !!ev.shiftKey;
}
key.shift = isShift;
}
var key = {
shift: false,
crtl: false
}
// Hooks.
var canvas = document.getElementById('theCanvas');
canvas.addEventListener('mousedown', doMouseDown, false);
canvas.addEventListener('mousemove', doMouseMove, false);
document.body.addEventListener('mouseup', doMouseUp, false); // button release could occur outside canvas
document.body.addEventListener('mousemove', doMouseMove, false);
canvas.addEventListener('touchstart', doMouseDown, false);
canvas.addEventListener('touchmove', doMouseMove, false);
document.body.addEventListener('touchend', doMouseUp, false);
document.body.addEventListener('keydown', doKeyDown, false);
document.body.addEventListener('keyup', doKeyUp, false);