OLD | NEW |
---|---|
1 /** | 1 /** |
2 * Common JS that talks XHR back to the server and runs the code and receives | 2 * Common JS that talks XHR back to the server and runs the code and receives |
3 * the results. | 3 * the results. |
4 */ | 4 */ |
5 | 5 |
6 /** | 6 /** |
7 * A polyfill for HTML Templates. | 7 * A polyfill for HTML Templates. |
8 * | 8 * |
9 * This just adds in the content attribute, it doesn't stop scripts | 9 * This just adds in the content attribute, it doesn't stop scripts |
10 * from running nor does it stop other side-effects. | 10 * from running nor does it stop other side-effects. |
11 */ | 11 */ |
12 (function polyfillTemplates() { | 12 (function polyfillTemplates() { |
13 if('content' in document.createElement('template')) { | 13 if('content' in document.createElement('template')) { |
14 return false; | 14 return false; |
15 } | 15 } |
16 | 16 |
17 var templates = document.getElementsByTagName('template'); | 17 var templates = document.getElementsByTagName('template'); |
18 for (var i=0; i<templates.length; i++) { | 18 for (var i=0; i<templates.length; i++) { |
19 var content = document.createDocumentFragment(); | 19 var content = document.createDocumentFragment(); |
20 while (templates[i].firstChild) { | 20 while (templates[i].firstChild) { |
21 content.appendChild(templates[i].firstChild); | 21 content.appendChild(templates[i].firstChild); |
22 } | 22 } |
23 templates[i].content = content; | 23 templates[i].content = content; |
24 } | 24 } |
25 })(); | 25 })(); |
26 | 26 |
27 /** | 27 /** |
28 * Enable zooming for any images with a class of 'zoom'. | |
29 */ | |
30 (function () { | |
31 var clientX = 0; | |
32 var clientY = 0; | |
33 var lastClientX = 0; | |
34 var lastClientY = 0; | |
35 var ctx = null; // The 2D canvas context of the zoom. | |
36 var currentImage = null; // The img node we are zooming for, otherwise null. | |
37 | |
38 function zoomMove(e) { | |
39 clientX = e.clientX; | |
40 clientY = e.clientY; | |
41 } | |
42 | |
43 function zoomMouseDown(e) { | |
44 e.preventDefault(); | |
45 // Only do zooming on the primary mouse button. | |
46 if (e.button != 0) { | |
47 return | |
48 } | |
49 currentImage = e.target; | |
50 clientX = e.clientX; | |
51 clientY = e.clientY; | |
52 lastClientX = clientX-1; | |
53 lastClientY = clientY-1; | |
54 document.body.style.cursor = 'crosshair'; | |
55 canvas = document.createElement('canvas'); | |
56 canvas.width=256; | |
mtklein
2014/04/28 19:53:17
Can we grab the w/h from currentImage?
jcgregorio
2014/04/28 20:45:25
These dimensions just need to be large enough that
| |
57 canvas.height=256; | |
58 canvas.classList.add('zoomCanvas'); | |
59 ctx = canvas.getContext('2d'); | |
60 ctx.imageSmoothingEnabled = false; | |
61 this.parentNode.insertBefore(canvas, this); | |
62 | |
63 document.body.addEventListener('mousemove', zoomMove, true); | |
64 document.body.addEventListener('mouseup', zoomFinished); | |
65 document.body.addEventListener('mouseleave', zoomFinished); | |
66 | |
67 // Kick off the drawing. | |
68 setTimeout(drawZoom, 33); | |
mtklein
2014/04/28 19:53:17
Tsk. Tsk. Don't you know we're trying to make the
jcgregorio
2014/04/28 20:45:25
I doubt it would be noticeable on the 32x32 grid.
| |
69 } | |
70 | |
71 function drawZoom() { | |
72 if (currentImage) { | |
73 // Only draw if the mouse has moved from the last time we drew. | |
74 if (lastClientX != clientX || lastClientY != clientY) { | |
75 ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height); | |
76 ctx.drawImage(currentImage, clientX - currentImage.x, clientY - currentI mage.y, 32, 32, 0, 0, ctx.canvas.width, ctx.canvas.height); | |
mtklein
2014/04/28 19:53:17
Took me a while and a reference to parse this line
jcgregorio
2014/04/28 20:45:25
Done.
| |
77 lastClientX = clientX; | |
78 lastClientY = clientY; | |
79 } | |
80 setTimeout(drawZoom, 33); | |
81 } | |
82 } | |
83 | |
84 function zoomFinished() { | |
85 currentImage = null; | |
86 document.body.style.cursor = 'default'; | |
87 ctx.canvas.parentNode.removeChild(ctx.canvas); | |
88 document.body.removeEventListener('mousemove', zoomMove, true); | |
89 document.body.removeEventListener('mouseup', zoomFinished); | |
90 document.body.removeEventListener('mouseleave', zoomFinished); | |
91 } | |
92 | |
93 this.addEventListener('DOMContentLoaded', function() { | |
94 var zoomables = document.body.querySelectorAll('.zoom'); | |
95 for (var i=0; i<zoomables.length; i++) { | |
96 zoomables[i].addEventListener('mousedown', zoomMouseDown); | |
97 } | |
98 }); | |
99 })(); | |
100 | |
101 | |
102 /** | |
28 * All the functionality is wrapped up in this anonymous closure, but we need | 103 * All the functionality is wrapped up in this anonymous closure, but we need |
29 * to be told if we are on the workspace page or a normal try page, so the | 104 * to be told if we are on the workspace page or a normal try page, so the |
30 * workspaceName is passed into the closure, it must be set in the global | 105 * workspaceName is passed into the closure, it must be set in the global |
31 * namespace. If workspaceName is the empty string then we know we aren't | 106 * namespace. If workspaceName is the empty string then we know we aren't |
32 * running on a workspace page. | 107 * running on a workspace page. |
33 * | 108 * |
34 * If we are on a workspace page we also look for a 'history' | 109 * If we are on a workspace page we also look for a 'history' |
35 * variable in the global namespace which contains the list of tries | 110 * variable in the global namespace which contains the list of tries |
36 * that are included in this workspace. That variable is used to | 111 * that are included in this workspace. That variable is used to |
37 * populate the history list. | 112 * populate the history list. |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
194 | 269 |
195 | 270 |
196 // Add the images to the history if we are on a workspace page. | 271 // Add the images to the history if we are on a workspace page. |
197 if (tryHistory && history) { | 272 if (tryHistory && history) { |
198 for (var i=0; i<history.length; i++) { | 273 for (var i=0; i<history.length; i++) { |
199 addToHistory(history[i].hash, '/i/'+history[i].hash+'.png'); | 274 addToHistory(history[i].hash, '/i/'+history[i].hash+'.png'); |
200 } | 275 } |
201 } | 276 } |
202 | 277 |
203 })(workspaceName); | 278 })(workspaceName); |
OLD | NEW |