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. |
(...skipping 10 matching lines...) Expand all Loading... | |
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'. | 28 * Enable zooming for any images with a class of 'zoom'. |
29 */ | 29 */ |
30 (function () { | 30 (function () { |
31 var PIXELS = 20; // The number of pixels in width and height in a zoom. | |
31 var clientX = 0; | 32 var clientX = 0; |
32 var clientY = 0; | 33 var clientY = 0; |
33 var lastClientX = 0; | 34 var lastClientX = 0; |
34 var lastClientY = 0; | 35 var lastClientY = 0; |
35 var ctx = null; // The 2D canvas context of the zoom. | 36 var ctx = null; // The 2D canvas context of the zoom. |
36 var currentImage = null; // The img node we are zooming for, otherwise null. | 37 var currentImage = null; // The img node we are zooming for, otherwise null. |
38 var hex = document.getElementById('zoomHex'); | |
39 var canvasCopy = null; | |
37 | 40 |
38 function zoomMove(e) { | 41 function zoomMove(e) { |
39 clientX = e.clientX; | 42 clientX = e.clientX; |
40 clientY = e.clientY; | 43 clientY = e.clientY; |
41 } | 44 } |
42 | 45 |
43 function zoomMouseDown(e) { | 46 function zoomMouseDown(e) { |
44 e.preventDefault(); | 47 e.preventDefault(); |
45 // Only do zooming on the primary mouse button. | 48 // Only do zooming on the primary mouse button. |
46 if (e.button != 0) { | 49 if (e.button != 0) { |
47 return | 50 return |
48 } | 51 } |
49 currentImage = e.target; | 52 currentImage = e.target; |
50 clientX = e.clientX; | 53 clientX = e.clientX; |
51 clientY = e.clientY; | 54 clientY = e.clientY; |
52 lastClientX = clientX-1; | 55 lastClientX = clientX-1; |
53 lastClientY = clientY-1; | 56 lastClientY = clientY-1; |
54 document.body.style.cursor = 'crosshair'; | 57 document.body.style.cursor = 'crosshair'; |
55 canvas = document.createElement('canvas'); | 58 canvas = document.createElement('canvas'); |
56 canvas.width=256; | 59 canvas.width = 1024; |
57 canvas.height=256; | 60 canvas.height = 1024; |
58 canvas.classList.add('zoomCanvas'); | 61 canvas.classList.add('zoomCanvas'); |
59 ctx = canvas.getContext('2d'); | 62 ctx = canvas.getContext('2d'); |
60 ctx.imageSmoothingEnabled = false; | 63 ctx.imageSmoothingEnabled = false; |
61 this.parentNode.insertBefore(canvas, this); | 64 this.parentNode.insertBefore(canvas, this); |
62 | 65 |
66 // Copy the image over to a canvas so we can read RGBA values for each point . | |
67 if (hex) { | |
68 canvasCopy = document.createElement('canvas'); | |
69 canvasCopy.width = currentImage.width; | |
70 canvasCopy.height = currentImage.height; | |
71 canvasCopy.id = 'zoomCopy'; | |
72 canvasCopy.getContext('2d').drawImage(currentImage, 0, 0, currentImage.wid th, currentImage.height); | |
73 this.parentNode.insertBefore(canvasCopy, this); | |
74 } | |
75 | |
63 document.body.addEventListener('mousemove', zoomMove, true); | 76 document.body.addEventListener('mousemove', zoomMove, true); |
64 document.body.addEventListener('mouseup', zoomFinished); | 77 document.body.addEventListener('mouseup', zoomFinished); |
65 document.body.addEventListener('mouseleave', zoomFinished); | 78 document.body.addEventListener('mouseleave', zoomFinished); |
66 | 79 |
67 // Kick off the drawing. | 80 // Kick off the drawing. |
68 setTimeout(drawZoom, 1); | 81 setTimeout(drawZoom, 1); |
69 } | 82 } |
70 | 83 |
84 function hexify(i) { | |
85 var s = i.toString(16).toUpperCase(); | |
86 if (s.length < 2) { | |
mtklein
2014/04/30 21:14:19
// i is a single component, 1 or 2 hex digits. Pa
jcgregorio
2014/05/01 12:47:04
Done.
| |
87 s = '0' + s; | |
88 } | |
89 return s; | |
90 } | |
91 | |
71 function drawZoom() { | 92 function drawZoom() { |
72 if (currentImage) { | 93 if (currentImage) { |
73 // Only draw if the mouse has moved from the last time we drew. | 94 // Only draw if the mouse has moved from the last time we drew. |
74 if (lastClientX != clientX || lastClientY != clientY) { | 95 if (lastClientX != clientX || lastClientY != clientY) { |
75 ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height); | 96 ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height); |
97 var x = clientX - currentImage.x; | |
98 var y = clientY - currentImage.y; | |
76 ctx.drawImage(currentImage, | 99 ctx.drawImage(currentImage, |
77 clientX - currentImage.x, clientY - currentImage.y, // src zero | 100 x, y, // src zero |
78 32, 32, // src dimensio ns | 101 PIXELS, PIXELS, // src dimensions |
79 0, 0, // dst zero | 102 0, 0, // dst zero |
80 ctx.canvas.width, ctx.canvas.height); // dst dimensio ns | 103 ctx.canvas.width, ctx.canvas.height); // dst dimensions |
81 lastClientX = clientX; | 104 lastClientX = clientX; |
82 lastClientY = clientY; | 105 lastClientY = clientY; |
106 if (hex) { | |
mtklein
2014/04/30 21:14:19
// Box and label the pixel we're moused over.
?
jcgregorio
2014/05/01 12:47:04
Done.
| |
107 ctx.lineWidth = 1; | |
108 ctx.strokeStyle = '#000'; | |
109 var dx = ctx.canvas.width/PIXELS; | |
110 var dy = ctx.canvas.height/PIXELS; | |
111 ctx.strokeRect((PIXELS/2)*dx, (PIXELS/2)*dy, dx, dy); | |
112 var p = canvasCopy.getContext('2d').getImageData(x+PIXELS/2, y+PIXELS/ 2, 1, 1).data; | |
113 hex.textContent = 'rgba(' | |
114 + p[0] + ', ' | |
115 + p[1] + ', ' | |
116 + p[2] + ', ' | |
117 + p[3] + ') ' | |
118 + hexify(p[0]) | |
119 + hexify(p[1]) | |
120 + hexify(p[2]) | |
121 + hexify(p[3]); | |
122 } | |
83 } | 123 } |
84 setTimeout(drawZoom, 1000/30); | 124 setTimeout(drawZoom, 1000/30); |
85 } | 125 } |
86 } | 126 } |
87 | 127 |
88 function zoomFinished() { | 128 function zoomFinished() { |
89 currentImage = null; | 129 currentImage = null; |
130 if (hex) { | |
131 hex.textContent = ''; | |
132 } | |
90 document.body.style.cursor = 'default'; | 133 document.body.style.cursor = 'default'; |
91 ctx.canvas.parentNode.removeChild(ctx.canvas); | 134 ctx.canvas.parentNode.removeChild(ctx.canvas); |
135 canvasCopy.parentNode.removeChild(canvasCopy); | |
92 document.body.removeEventListener('mousemove', zoomMove, true); | 136 document.body.removeEventListener('mousemove', zoomMove, true); |
93 document.body.removeEventListener('mouseup', zoomFinished); | 137 document.body.removeEventListener('mouseup', zoomFinished); |
94 document.body.removeEventListener('mouseleave', zoomFinished); | 138 document.body.removeEventListener('mouseleave', zoomFinished); |
95 } | 139 } |
96 | 140 |
97 this.addEventListener('DOMContentLoaded', function() { | 141 this.addEventListener('DOMContentLoaded', function() { |
98 var zoomables = document.body.querySelectorAll('.zoom'); | 142 var zoomables = document.body.querySelectorAll('.zoom'); |
99 for (var i=0; i<zoomables.length; i++) { | 143 for (var i=0; i<zoomables.length; i++) { |
100 zoomables[i].addEventListener('mousedown', zoomMouseDown); | 144 zoomables[i].addEventListener('mousedown', zoomMouseDown); |
101 } | 145 } |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
273 | 317 |
274 | 318 |
275 // Add the images to the history if we are on a workspace page. | 319 // Add the images to the history if we are on a workspace page. |
276 if (tryHistory && history) { | 320 if (tryHistory && history) { |
277 for (var i=0; i<history.length; i++) { | 321 for (var i=0; i<history.length; i++) { |
278 addToHistory(history[i].hash, '/i/'+history[i].hash+'.png'); | 322 addToHistory(history[i].hash, '/i/'+history[i].hash+'.png'); |
279 } | 323 } |
280 } | 324 } |
281 | 325 |
282 })(workspaceName); | 326 })(workspaceName); |
OLD | NEW |