-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvas_plus.js
380 lines (321 loc) · 9.62 KB
/
canvas_plus.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
((window)=> {
var newWorker = function (funcObj) {
// TODO: also should work with multiple functions and external file
var blobURL = URL.createObjectURL(new Blob(['(',
funcObj.toString(),
')()'], {
type: 'application/javascript'
})),
worker = new Worker(blobURL);
URL.revokeObjectURL(blobURL);
return worker;
}
//http://www.hyperlounge.net/blog/up-the-workers-2/
//https://www.html5rocks.com/en/tutorials/workers/basics/
//https://developers.google.com/web/updates/2011/12/Transferable-Objects-Lightning-Fast
//http://www.hyperlounge.net/blog/up-the-workers-2/
// TODO: use this to do the heavy lifting
// var w = newWorker(function () {
// var i = 0;
// function timedCount() {
// i = i + 1;
// postMessage(i);
// setTimeout(timedCount, 500);
// }
// timedCount();
// });
// w.onmessage = function (event) {
// document.getElementById("result").innerHTML = event.data;
// }
const defaultCSS = (dimensions = { zoom: 1}) => `
@media (min-width:320px) { /* smartphones, portrait iPhone, portrait 480x320 phones (Android) */ }
@media (min-width:480px) { /* smartphones, Android phones, landscape iPhone */ }
@media (min-width:600px) { /* portrait tablets, portrait iPad, e-readers (Nook/Kindle), landscape 800x480 phones (Android) */ }
@media (min-width:801px) { /* tablet, landscape iPad, lo-res laptops ands desktops */ }
@media (min-width:1025px) { /* big landscape tablets, laptops, and desktops */ }
@media (min-width:1281px) { /* hi-res laptops and desktops */ }
body {
background-color: #222;
font-family: verdana;
}
#canvas-container {
display: flex;
flex-direction: column;
}
#rotateMessage {
display: none;
color: white;
width: 90%;
margin: auto;
text-align: center;
font-size: 5em;
color: #00BCD4;
}
@media screen and (orientation: portrait) and (min-height: 650px) and (max-height: 985px) {
#canvas-container { display: none; }
#rotateMessage { display: block; }
}
#canvas-container canvas {
zoom: ${5*dimensions.zoom};
margin: auto;
border: 0px solid #8a8a8a;
/* box-shadow: 1px -4px 12px 0px #d4d4d4; */
background-color: #fefefe;
margin-top: 5%;
image-rendering: pixelated;
border-radius: 0.5px;
position: absolute;
top: 0px;
left: 0px;
right: 0px;
}
@media screen and (max-width: 1400px) {
#canvas-container canvas{
zoom: ${4*dimensions.zoom};
margin-top: 2%;
}
}
@media screen and (max-width: 750px) {
#canvas-container canvas{
zoom: ${3*dimensions.zoom};
}
}
@media screen and (max-width: 750px)
and (orientation: landscape) and (max-height: 450px) {
#canvas-container canvas{
zoom: ${2.5*dimensions.zoom};
}
}
@media screen and (max-width: 500px) {
#canvas-container canvas{
zoom: ${2*dimensions.zoom};
}
}
@media screen and (max-width: 350px) {
#canvas-container canvas{
zoom: ${1*dimensions.zoom};
}
}
#button-container {
margin: auto;
position: absolute;
left: 0px;
right: 0px;
bottom: 15px;
text-align: center;
}
button {
border-radius: 50px;
color: #333333;
font-size: 20px;
background: rgba(255, 255, 255, 0.1);
padding: 9.5px 15px 11px 15px;
text-decoration: none;
border: 1px solid transparent;
width: 42px;
min-height: 42px;
color: transparent;
margin: 10px 10px;
transition: background-color 1s, color 0s;
overflow: hidden;
min-width: 48px;
}
button:hover {
background-color: #7eccfc;
color: #111;
width: auto;
transition: color 0.5s;
}
:focus {
display: inline-block;
border: 1px solid red;
outline: none;
border-radius: 45px;
}
@media screen and (max-width: 1400px) {
#button-container {
margin-top: 2em;
}
}
@media screen and (max-width: 750px) {
#button-container {
margin-top: 1em;
}
}
@media screen and (max-width: 500px) {
#button-container {
margin-top: 1em;
}
}
`;
const defaultButtons = [];
const _default = ({ dims }) => ({
css: defaultCSS(dims),
dimensions: {
x: 160,
y: 120
},
buttons: defaultButtons,
id: 'myCanvas',
parent: 'body'
});
var CanvasPlus = function(options={}) {
//TODO: validate options
this.options = options;
//MAYBE: wrap all buttons' onclick with throttle
};
CanvasPlus.prototype.start = function start() {
const dims = this.options.dimensions;
const def = _default({dims});
this.styleSheet = addcss(this.options.css || def.css);
this.dimensions = this.options.dimensions || def.dimensions;
if (!this.options.init){
def.buttons = [{
text: 'random',
onClick: throttle(() => randomPixels.call(this, setImageDataPixel))
}];
}
var buttons;
if (def.buttons.length === 0 && this.options.buttons){
buttons = this.options.buttons.map(button => ({
text: button.text,
onClick: throttle(() => button.onClick.call(this, setImageDataPixel))
}))
}
this.canvas = createCanvas({
id: this.options.id || def.id,
dimensions: this.dimensions,
parent: this.options.parent || def.parent,
buttons: buttons || def.buttons
});
(this.options.init || randomPixels).call(this, setImageDataPixel);
};
CanvasPlus.prototype.stop = function stop() {
//TODO: teardown
};
// dynamically append css
function addcss(css){
var head = document.getElementsByTagName('head')[0];
var s = document.createElement('style');
s.setAttribute('type', 'text/css');
if (s.styleSheet) { // IE
s.styleSheet.cssText = css;
} else { // the world
s.appendChild(document.createTextNode(css));
}
head.appendChild(s);
return s;
}
function createCanvas({id, dimensions, parent, buttons}) {
var div = document.getElementById('canvas-container') || document.createElement('div');
div.id = 'canvas-container';
var canvas = makeCanvas({id, dimensions, parent});
div.appendChild(canvas);
var buttonDiv = makeButtons({ buttons });
div.appendChild(buttonDiv);
if(!document.getElementById('canvas-container')){
document.querySelector(parent).appendChild(div);
}
var firstButton = div.querySelectorAll('button')[0];
if (firstButton){
firstButton.focus();
}
if(document.getElementById('rotateMessage')){
return canvas;
}
var rotMess = document.createElement('div');
rotMess.id = 'rotateMessage';
rotMess.textContent = 'Rotate your device!';
document.querySelector(parent).appendChild(rotMess);
return canvas;
}
function makeCanvas({id, dimensions}){
var canvas = document.createElement('canvas');
canvas.width = dimensions.x;
canvas.height = dimensions.y;
canvas.id = id;
canvas.textContent = 'Your browser does not support the HTML5 canvas tag.';
//canvas.style.filter = 'url(#myFilter)';
return canvas;
}
function makeButtons({buttons = [] } = {}){
var buttonDiv = document.getElementById('button-container') || document.createElement('div');
buttonDiv.id = 'button-container';
if(buttons.length === 0){
return buttonDiv;
}
var first, last;
buttons.forEach((button,index) => {
var el = document.createElement('button');
el.textContent = button.text;
el.onclick = button.onClick;
el.tabindex = index;
if (index === 0){
first = el;
}
if (index === buttons.length - 1){
last = el;
el.onkeydown = function(e){
if( e.keyCode === 9 ) {
first.focus();
return false;
}
};
}
buttonDiv.appendChild(el);
});
buttonDiv.querySelectorAll('button')[0].onkeydown = function(e){
if( e.keyCode === 9 && e.shiftKey) {
last.focus();
return false;
}
};
return buttonDiv;
}
// https://remysharp.com/2010/07/21/throttling-function-calls
function throttle(fn, threshhold, scope) {
threshhold || (threshhold = 250);
var last,
deferTimer;
return function () {
var context = scope || this;
var now = +new Date,
args = arguments;
if (last && now < last + threshhold) {
// hold on to it
clearTimeout(deferTimer);
deferTimer = setTimeout(function () {
last = now;
fn.apply(context, args);
}, threshhold);
} else {
last = now;
fn.apply(context, args);
}
};
}
var randomImageData;
function randomPixels(setter){
if (!setter) return;
var ctx = this.canvas.getContext('2d');
randomImageData = randomImageData
? randomImageData
: ctx.createImageData(this.dimensions.x, this.dimensions.y);
setter(randomImageData, {r:255}, {x:0, y:0, xmax:1});
requestAnimationFrame(
() => ctx.putImageData( randomImageData, 0, 0 )
);
}
// used for setting entire page at a time
function setImageDataPixel(imageData, {r=0, g=0, b=0, a=255}, {x=0, y=0, xmax=1}){
const ad = imageData.data;
const rowOffset = y * 4 * xmax;
const colOffset = x * 4;
const offset = rowOffset + colOffset;
ad[offset + 0] = r;
ad[offset + 1] = g;
ad[offset + 2] = b;
ad[offset + 3] = a;
}
window.CanvasPlus = CanvasPlus;
})(window);