-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.html
265 lines (241 loc) · 11 KB
/
index.html
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<html>
<head>
<link rel="icon" type="image/png" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAABxUlEQVRYR+1WPU/CUBQ9r8ENIiNs1dlEGJmssxL5Hy46GN2Uf4KJBhMHSOQH6OYmJA5Otk7qJCQshvqur6YWXmnLayGwtGuTe887H/dehhV/bMX9kQJIGfAYeEfW0ICdSVNy2JdFvdmHljkCNPL+cd6DVW0vwsAegC8g/42cJYqujwvTfQHDXWx02sKuB1JDzsqw9rrzgpA88IlcTTyzJRflxwX9qgFtTQZH6MLcLy8UgFPsA1lBLZt8bZ8wKhf12xI05gNHdbxWL+YBMZWCZUsRGMNlShE6B+JKQTdnbXDtSUkOojqrPP9JFwogiRTUPK2BsfOZIFQAOEWSSEGPWyKabDsShCqAJKmg65MGkNGjWbAtVnlxYj17GwZ4YSBiWQqOpagoBhS1Di2McuEs/IzelABESsAyptA7P/VSd0BFSqEigWtCUzTwmhDooYihETiaJSRUj0yFCoCY1A/kHeJKEZaKWQBiUU/UA9lG0l0RNorVqf/fivpdLcmumAIQj3rfMkqwttXXsd/1DvVmtSR5T2/l40rhP0jiU+/PYUwppJNM1DLkenZjfJJN/OHiGIk6yTY74oQjeUaE3A3pVZwykDLwC4/VJDBDudEqAAAAAElFTkSuQmCC"/>
<title>Blank Canvas</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery-json.js"></script>
</head>
<body style="padding: 0px; margin: 0px; border:0px">
<div id="canvas"></div>
<script type="text/javascript">
var session = 0; // global session number
var images = [];
var sounds = [];
var patterns = [];
var canvasbuffers = [];
var c = null;
var myDevicePixelRatio = 1; // "my" because this would nameclass with the window.* version
var urlQuery = {};
// From http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript
location.search.substr(1).split("&").forEach(function(item) {
urlQuery[item.split("=")[0]] = item.split("=")[1];
});
function reply(e) {
return function(u) { return $.kc.reply(u,e); }
}
function getContext() {
var canvas = document.getElementById("canvas");
if (canvas.getContext) {
return canvas.getContext("2d");
}
alert("no canvas");
}
// ##############################################################
// This is the sync-ing with framerate-based output
var futureFrameCode = [];
var indent = 0;
var old_n = 0;
function indenter(i) {
return new Array(i+1).join(".");
}
// run the given argument after the frame sync, using requestAnimationFrame.
function nextFrame(f) {
// console.log(indenter(indent++),"-> calling nextFrame");
if (futureFrameCode.length != 0) {
// suspend the call inside the callback list
futureFrameCode.push(function(){ nextFrame(f); });
$.kc.event({metaKey:false,type:"frame"});
console.log("double nextFrame",futureFrameCode.length)
return;
}
futureFrameCode.push(f); // Whole new list
requestAnimationFrame(function (n) {
var d = n - old_n;
old_n = n;
if (d > 20) { console.log("timing",d); }
// Clone so we can blank the futureFrameCode before starting the frame
// console.log("requestAnimationFrame futureFrameCode",futureFrameCode);
var fns = futureFrameCode.slice(0);
futureFrameCode = [];
// console.log(indenter(indent++),"-> calling requestAnimationFrame callback",fns.length);
// console.log("requestAnimationFrame fns",fns);
$.each(fns,function(ix,f) {
// Do all the callbacks.
// console.log("callback",f);
try {
f();
} catch (e) {
alert('JavaScript Failure: '+e.message);
}
});
// console.log(indenter(--indent),"<- done requestAnimationFrame callback");
});
// console.log(indenter(--indent),"<- done nextFrame");
}
// syncToFrame syncs the argument with any pause-for-frame requirements
// The callback code itself should never contain a syncToFrame.
function syncToFrame(f) {
if (futureFrameCode.length == 0) {
try {
f(); // Call code *now*, because there is no future
// requestAnimationFrame pending.
} catch (e) {
alert('JavaScript Failure: '+e.message);
}
} else {
// Perform the callback *after* the future frame list, by appending to it.
// console.log(indenter(indent),"pushing futureFrameCode",futureFrameCode.length);
futureFrameCode.push(f);
}
}
// All code generated and sent from Haskell should use
// syncToFrame(function() { ... })
// as wrapper.
// ##############################################################
// These are the Queries, taking 2 arguments, then second one
// being the (graphics) context.
Device = () => (c) =>
[c.canvas.width, c.canvas.height,myDevicePixelRatio];
function Size(c) {
return [c.canvas.width, c.canvas.height];
}
ToDataURL = () => (c) =>
c.canvas.toDataURL()
function NewImage(src) {
return function (c) {
return new Promise( (good,bad) => {
var img = new Image();
var count = images.push(img);
img.onload = function() {
good([count - 1,img.width,img.height])
};
img.onerror = function() { bad("Image " + src + " not found.\nAdd as a static file to fix this."); };
img.src = src;
})
}
}
function MeasureText(txt) {
return function (c) {
// If we try return the object directly, via json, we
// get different results on different browsers.
// So we build an object explicity.
return { width: c.measureText(txt).width };
}
}
function IsPointInPath(x,y) {
return function (c) {
return c.isPointInPath(x,y);
}
}
function NewCanvas(w,h) {
return function (c) {
var canvas = document.createElement('canvas');
canvas.width = w;
canvas.height = h;
return canvas.getContext("2d");
}
}
function GetImageData(sx,sy,sw,sh) {
return function (c) {
var img = c.getImageData(sx,sy,sw,sh);
var arr = [];
for (var i = 0;i < img.data.length;i++) {
arr[i] = img.data[i];
}
return { width: img.width
, height: img.height
, data: arr
};
}
}
Sync = () => (c) => [];
function Trigger(e) {
var o = {};
o.metaKey = e.metaKey;
o.type = e.type;
if (e.pageXY != undefined) {
o.pageXY = e.pageXY;
}
if (e.pageX != undefined && e.pageY != undefined) {
o.pageXY = [e.pageX,e.pageY];
}
if (e.which != undefined) {
o.which = e.which;
}
jsb.event(o);
}
function register(name) {
$(document).bind(name,Trigger);
}
function redraw() {
$.ajax({ url: "/canvas/" + session,
type: "GET",
dataType: "script",
success: function success() {
redraw();
}
});
}
function ImageData(w,h,dat) {
var imgData = canvasbuffers[0].createImageData(w,h);
for(var i = 0;i < dat.length;i ++) {
imgData.data[i] = dat[i];
}
return imgData;
}
function NewAudio(src) {
return function (u,c) {
var audio = new Audio();
var count = sounds.push(audio);
// I commented this out because there's a chance that the browser won't
// know the audio duration until it's "loaded" (i.e., it can be played
// through). Further testing might be wise.
// dur itself isn't used beyond sending the duration back through $.kc.reply,
// the current setup will probably be sufficient
audio.oncanplaythrough = function () {
$.kc.reply(u,[count - 1,audio.duration]);
};
audio.onerror = function() { alert("Audio " + src + " not found."); };
audio.src = src;
}
}
function CurrentTimeAudio(aud) {
return function (u,c) {
var currTime = sounds[aud].currentTime;
$.kc.reply(u,currTime);
}
}
$(document).ready(function() {
// Make the canvas the size of the window
var w = $(window).width();
var h = $(window).height();
var b = "0";
if (urlQuery.border && !isNaN(parseInt(urlQuery.border))) {
b = parseInt(urlQuery.border);
w -= b * 2;
h -= b * 2;
}
if (urlQuery.width) {
w = urlQuery.width;
}
if (urlQuery.height) {
h = urlQuery.height;
}
if ('hd' in urlQuery && 'devicePixelRatio' in window) {
myDevicePixelRatio = window.devicePixelRatio;
}
$("#canvas").replaceWith(
'<canvas id="canvas" width="' + (w * myDevicePixelRatio) + '" height="' + (h * myDevicePixelRatio) +
'" style="border: black solid ' + b + 'px; padding: 0px; margin: 0px"></canvas>');
$("#canvas").css("width",w + "px");
$("#canvas").css("height",h + "px");
c = getContext("2d");
canvasbuffers.push(c);
// bootstrap the web socket connection
window.jsb = {ws: new WebSocket('ws://' + location.host)};
jsb.ws.onmessage = (evt) => { console.log(evt,evt.data) ; eval(evt.data); }
jsb.debug = true;
});
</script>
</body>
</html>