forked from uva-cs/pdr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
canvas.js
356 lines (311 loc) · 13.8 KB
/
canvas.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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// this code adapted from
// http://stackoverflow.com/questions/2368784/draw-by-mouse-with-html5-canvas
// and
// http://stackoverflow.com/questions/4037212/html-canvas-full-screen
var canvas, ctx, flag = false,
prevX = 0,
currX = 0,
prevY = 0,
currY = 0,
dot_flag = false;
var x = "red",
y = 2,
w = 0,
h = 0;
// These are the offsets from where the mouse is to where the canvas
// mousedown point is. If the offsets are BOTH set to zero, then the
// canvasinit() function will try to calibrate them (likely
// unsuccessfully). If EITHER are set to non-zero, then it will
// assume those are the initial values to start with; these values can
// be determined by selecting the 'notify' checkbox when doing a
// calibrate
var xoffset = -60, // the x offset for canvas drawing, computed in canvasinit()
yoffset = -66; // the x offset for canvas drawing, computed in canvasinit()
var calibrateimg = new Image(); // the calibrate image target
var canvases = new Array(); // holds a map of the canvas IDs to their CTX's
var canvas_border = 100; // how much wider than the slide itselt to allow drawing
var colors = new Array("red","orange","yellow","green","blue","purple","white","black"); // drawing colors
var numCanvases = 0;
function canvasinit() {
for ( var id in canvases ) {
canvas = document.getElementById(id);
canvases[id] = canvas.getContext("2d");
canvas.width = 960+2*canvas_border; // the width of the reveal.js slides
canvas.height = 700+2*canvas_border; // the height of the reveal.js slides
w = canvas.width; // all are assumed to be the same size
h = canvas.height; // all are assumed to be the same size
canvas.addEventListener("mousemove", function (e) {
findxy('move', e, this.id)
}, false);
canvas.addEventListener("mousedown", function (e) {
findxy('down', e, this.id)
}, false);
canvas.addEventListener("mouseup", function (e) {
findxy('up', e, this.id)
}, false);
canvas.addEventListener("mouseout", function (e) {
findxy('out', e, this.id)
}, false);
calibrateimg.src = "../slides/images/calibrate.png";
if ( (xoffset == 0) && (yoffset == 0) ) {
var pos = getPosition(canvas);
yoffset = pos.offsetTop + canvas_border;
xoffset = pos.offsetLeft + canvas_border;
}
}
//alert("offsets: (x,y) = (" + xoffset + "," + yoffset + ")");
}
function color(obj, which) {
switch (obj.id) {
case "green":
x = "green";
break;
case "blue":
x = "blue";
break;
case "red":
x = "red";
break;
case "yellow":
x = "yellow";
break;
case "orange":
x = "orange";
break;
case "black":
x = "black";
break;
case "white":
x = "white";
break;
}
y = 2;
//if (x == "white") y = 14; // enable if the eraser is set
menutoggle(which);
}
function draw(which) {
canvases[which].beginPath();
canvases[which].moveTo(prevX-xoffset, prevY-yoffset);
canvases[which].lineTo(currX-xoffset, currY-yoffset);
canvases[which].strokeStyle = x;
canvases[which].lineWidth = y;
canvases[which].stroke();
canvases[which].closePath();
}
function erase(which) {
//var m = confirm("Want to clear?");
//if (m) {
canvases['canvas_'+which].clearRect(0, 0, w, h);
//document.getElementById("canvasimg").style.display = "none";
//}
menutoggle(which);
}
function save(which) {
// NOT WORKING, as I broke it when allowing multiple canvases
document.getElementById("canvasimg").style.border = "2px solid";
var dataURL = canvas.toDataURL();
document.getElementById("canvasimg").src = dataURL;
document.getElementById("canvasimg").style.display = "inline";
}
function findxy(res, e, which) {
var canvas = document.getElementById(which);
//canvas.offsetTop = canvas_border;
//canvas.offsetLeft = canvas_border;
//console.log("findxy("+res+","+e+","+which+"): canvas offsets ("+canvas.offsetLeft+","+canvas.offsetTop+"), (x,y) offsets (" + xoffset +","+yoffset+") and canvas border "+canvas_border);
if (res == 'down') {
prevX = currX;
prevY = currY;
currX = e.clientX; // - xoffset; //canvas.offsetLeft;
currY = e.clientY; // - yoffset; //canvas.offsetTop;
flag = true;
dot_flag = false; // this was true, but was causing a stray pixel to be drawn from the natural (i.e., not offset) position, so it was turned to false
if (dot_flag) {
canvases[which].beginPath();
canvases[which].fillStyle = x;
canvases[which].fillRect(currX, currY, 2, 2);
canvases[which].closePath();
dot_flag = false;
}
}
if (res == 'up' || res == "out") {
flag = false;
}
if (res == 'move') {
if (flag) {
prevX = currX;
prevY = currY;
currX = e.clientX; // - xoffset; //canvas.offsetLeft;
currY = e.clientY; // - yoffset; //canvas.offsetTop;
draw(which);
}
}
}
// this function from
// http://stackoverflow.com/questions/9040768/getting-coordinates-of-objects-in-js
function getPosition(elem) {
var dims = {offsetLeft:0, offsetTop:0};
do {
dims.offsetLeft += elem.offsetLeft;
dims.offsetTop += elem.offsetTop;
}
while ( (elem = elem.offsetParent) );
return dims;
}
//----------------------------------------
// calibrate functions
//----------------------------------------
var calibratewin;
var which_canvas_id;
function on_mousedown_for_calibrate(e) {
// determine the vertical and horizontal offset
var top = parseInt(calibratewin.style.top, 10);
var left = parseInt(calibratewin.style.left, 10);
// for the spot that was clicked:
// * e.clientY is the distance from that to the top of the browser window (example: 437)
// * top (from above) is the distance between the top of the dynamic window and the top of the browser window (example: 265)
// * (calibrateimg.height+1) is the height of the image (example: 300)
// * there are 22 pixels above the image (the "Calibrate" title window)
//
// so in the example above, the click was in the center because 437-265=300/2+22
// or because 437-265-300/2-22 = 0
// so the vertical offset is e.clientY - top - (calibrateimg.height+1)/2 - 22
//
// likewise with the horizontal, but the extra is 4 instead of 22
var vertoff = e.clientY - top - (calibrateimg.height+1) / 2 - 22;
var horizoff = e.clientX - left - (calibrateimg.width+1) / 2 - 4;
var pos = getPosition(document.getElementById('canvas_'+which_canvas_id));
yoffset = pos.offsetTop + vertoff;
xoffset = pos.offsetLeft + horizoff;
//console.log("calibrate: horizoff = " + horizoff + ", vertoff = " + vertoff + ", new offsets: (" + xoffset + "," + yoffset + "), pos.offset: (" + pos.offsetLeft+","+pos.offsetTop+")");
//console.log("vert: " +e.clientY+" "+top+" "+(calibrateimg.height+1)+" "+vertoff +" "+canvas.height + " / horiz: " + e.clientX+" "+left+" "+(calibrateimg.width+1)+" "+horizoff+" "+canvas.width + " -> (" + xoffset + ", " + yoffset + "); canvas at top:" + getPosition(document.getElementById('canvas_'+which_canvas_id)).offsetTop + ", left:" + getPosition(document.getElementById('canvas_'+which_canvas_id)).offsetLeft);
// all done! close window. This also un-grays out the window
calibratewin.close();
if ( document.getElementById('cal_notify_'+which_canvas_id).checked ) {
alert ("calibration set to (" + xoffset + "," + yoffset + ")");
}
}
function calibrate(which) {
grayOut(true);
// create the window: the window must be created BEFORE one modifies the objects in the window.
calibratewin = dhtmlwindow.open('divbox', 'div', 'calibratediv', 'Calibrate', 'width=' + ((calibrateimg.width+1) + 6) + 'px,height=' + ((calibrateimg.height+1) + 60) + 'px,center=1,resize=0,scrolling=0');
calibratewin.onclose = function () {
grayOut(false);
return true;
};
// set which canvas ID we are on, so the event listener knows where to grab the position from
which_canvas_id = which;
// get the elements, and set the mousedown listener
//var caldiv = document.getElementById("calibratecanvasdiv");
var calcan = document.getElementById("calibratecanvas");
calcan.addEventListener("mousedown", on_mousedown_for_calibrate, false);
// display the image in the canvas
// the image is loaded in init()
//while ( !calibrateimg.complete );
var ctx2 = calcan.getContext("2d");
ctx2.clearRect(0, 0, (calibrateimg.width+1), (calibrateimg.height+1));
ctx2.drawImage(calibrateimg, 0, 0, (calibrateimg.width+1), (calibrateimg.height+1));
menutoggle(which);
}
function grayOut(vis, optionsparam) {
// this function from http://www.hunlock.com/blogs/Snippets:_Howto_Grey-Out_The_Screen
//
// Pass true to gray out screen, false to ungray
// options are optional. This is a JSON object with the following (optional) properties
// opacity:0-100 // Lower number = less grayout higher = more of a blackout
// zindex: # // HTML elements with a higher zindex appear on top of the gray out
// bgcolor: (#xxxxxx) // Standard RGB Hex color code
// grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});
// Because options is JSON opacity/zindex/bgcolor are all optional and can appear
// in any order. Pass only the properties you need to set.
var options = optionsparam || {};
var zindex = options.zindex || 50;
var opacity = options.opacity || 70;
var opaque = (opacity / 100);
var bgcolor = options.bgcolor || '#000000';
var dark = document.getElementById('darkenScreenObject');
if (!dark) {
// The dark layer doesn't exist, it's never been created. So we'll
// create it here and apply some basic styles.
// If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917
var tbody = document.getElementsByTagName("body")[0];
var tnode = document.createElement('div'); // Create the layer.
tnode.style.position = 'absolute'; // Position absolutely
tnode.style.top = '0px'; // In the top
tnode.style.left = '0px'; // Left corner of the page
tnode.style.overflow = 'hidden'; // Try to avoid making scroll bars
tnode.style.display = 'none'; // Start out Hidden
tnode.id = 'darkenScreenObject'; // Name it so we can find it later
tbody.appendChild(tnode); // Add it to the web page
dark = document.getElementById('darkenScreenObject'); // Get the object.
}
if (vis) {
var pageWidth, pageHeight;
// Calculate the page width and height
if (document.body && (document.body.scrollWidth || document.body.scrollHeight)) {
pageWidth = document.body.scrollWidth + 'px';
pageHeight = document.body.scrollHeight + 'px';
} else if (document.body.offsetWidth) {
pageWidth = document.body.offsetWidth + 'px';
pageHeight = document.body.offsetHeight + 'px';
} else {
pageWidth = '100%';
pageHeight = '100%';
}
//set the shader to cover the entire page and make it visible.
dark.style.opacity = opaque;
dark.style.MozOpacity = opaque;
dark.style.filter = 'alpha(opacity=' + opacity + ')';
dark.style.zIndex = zindex;
dark.style.backgroundColor = bgcolor;
dark.style.width = pageWidth;
dark.style.height = pageHeight;
dark.style.display = 'block';
} else {
dark.style.display = 'none';
}
}
//----------------------------------------
// canvas insertion function
//----------------------------------------
function insertCanvas() {
var which = ++numCanvases;
canvases['canvas_'+which] = false;
document.write('\
<canvas id="canvas_'+which+'" width="1000" height="1000" style="position:absolute;top:-'+canvas_border+'px;left:-'+canvas_border+'px;border:0px solid"></canvas> \
<!-- <table class="default" style="position:absolute;bottom:-15%"> --> \
<table class="transparent" style="position:fixed;bottom:-150px"><tr> \
<td><input type="image" src="../slides/images/menu-icon.png" id="menu" onclick="menutoggle('+which+')"></td> \
<td><div id="menu_'+which+'" style="display:none"> \
<table class="default"> \
<tr> \
<td><input type="button" value="clear" id="clr" onclick="erase('+which+')"></td> \
<td><input type="button" value="calibrate" id="cal" onclick="calibrate('+which+')"></td> \
<td><input type="checkbox" value="notify" id="cal_notify_' + which + '"><span style="margin-top:20px;font-size:50%">notify</span></td> \
<td><input type="button" value="close" id="cal" onclick="menutoggle('+which+')"></td> \
<td style="width:10px"></td> \
');
for ( var i in colors ) {
document.write('<td><div style="width:15px;height:15px;background:' + colors[i] + ';" id="' + colors[i] + '" onclick="color(this,' + which + ')"></div></td>');
}
document.write('\
<td style="width:50px"></td> \
</tr> \
</table> \
</div></td> \
</tr></table> \
');
// stuff not included in the above:
// <td><input type="button" value="save" id="btn" size="30" onclick="save('+which+')"></td>
// <td>Eraser: </td>
// <td><div style="width:30px;height:30px;background:white;border:2px solid;" id="white" onclick="color(this)"></div> -->
}
//----------------------------------------
// menu pop-up
//----------------------------------------
function menutoggle(which) {
//alert("offsets: (x,y) = (" + xoffset + "," + yoffset + ")");
if ( document.getElementById('menu_'+which).style.display == 'none' )
document.getElementById('menu_'+which).style.display = 'block';
else
document.getElementById('menu_'+which).style.display = 'none';
}