-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathshape.js
562 lines (501 loc) · 17.2 KB
/
shape.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/*************************************************
* Shape Experiment
* MIT License
* Copyright 2013, Max Irwin
* https://github.com/binarymax/shape
*************************************************/
"use strict";
// ==============================================================
var shape = (function() {
var _gap = 20;
var _keyid = 1;
var _color = "#333333";
var _shapes = [];
//------------------------------------------------------------------
//Random number helpers
var rand1 = function(max){ return Math.floor(Math.random()*max); };
var rand2 = function(min,max){ return Math.floor(Math.random() * (max - min + 1)) + min; };
//------------------------------------------------------------------
//DOM initializers
var canvas = function(name,width,height,scale) {
var canvas = document.createElement("canvas");
scale = scale || 1;
canvas.width = width;
canvas.height = height;
canvas.style.width = width * scale + 'px';
canvas.style.height = height * scale + 'px';
canvas.style.backgroundColor = "#ffffff";
canvas.style.display = "none";
canvas.setAttribute("id","canvas"+name);
canvas.setAttribute("name",name);
canvas.setAttribute("data-scale",1);
document.getElementById("shapes").appendChild(canvas);
return canvas;
};
var context = function(cvs) {
var ctx = cvs.getContext("2d");
var scale = parseInt(cvs.getAttribute("data-scale",scale)) || 1;
ctx.width = cvs.width;
ctx.height = cvs.height;
ctx.scaled = scale;
ctx.scale(scale,scale);
//Clear method
ctx.clear = function() { ctx.clearRect(0, 0, ctx.width, ctx.height); return ctx; };
return ctx;
};
var button = function(key,name,icon){
var div = document.createElement("div");
var btn = document.createElement("input");
btn.style.backgroundImage = "url("+icon+")";
btn.style.backgroundPosition = "22px 3px";
btn.setAttribute("type","button");
btn.setAttribute("id","button"+name);
btn.setAttribute("name",name);
btn.className = "button";
div.appendChild(btn);
document.getElementById("buttons").appendChild(div);
return btn;
};
//------------------------------------------------------------------
//Shape class
var Shape = function(name,draw){
var self = this;
self.name = name;
self.width = 100;
self.height = 100;
self.scale = 4;
self.key = _keyid;
self.canvas = canvas(name, self.width, self.height, self.scale);
self.context = context(self.canvas);
self.draw = draw;
self.pixels = [];
self.makebutton().redraw();
self.total = self.pixels.length;
_shapes[name] = self;
_keyid++;
};
Shape.prototype.eachPixel = function(callback) {
var self = this;
var image = self.context.getImageData(0,0,self.width,self.height);
var data = image.data;
var length = data.length;
for(var y=0;y<self.height;y++) {
for(var x=0;x<self.width;x++) {
var i = (x+y*self.width)*4;
callback(x,y,data[i],data[i+1],data[i+2],data[i+3]);
}
}
return image;
};
Shape.prototype.cachePixels = function(){
var self = this;
self.pixels = [];
self.eachPixel(function(x,y,r,g,b,a) {
if (r<255&&g<255&&b<255 && r>0&&g>0&&b>0&&a>0.0) self.pixels.push({x:x,y:y});
});
};
Shape.prototype.randPixel = function(){
var self = this;
var i = rand1(self.pixels.length);
var p = self.pixels[i];
self.pixels.splice(i,1);
return p;
};
Shape.prototype.putPixel = function(r,g,b,a){
var self = this;
var xy = self.randPixel();
var i = (xy.x+xy.y*self.width)*4;
var image = self.image;
image.data[i+0]=r;
image.data[i+1]=g;
image.data[i+2]=b;
image.data[i+3]=a||255;
self.context.putImageData(image,0,0);
};
Shape.prototype.makebutton = function(){
var self = this;
self.context.scale(0.5,0.5);
self.draw.call(self,_color);
self.icon = self.canvas.toDataURL();
self.context.scale(2,2);
self.context.clear();
self.button = button(self.key,self.name,self.icon);
return self;
};
Shape.prototype.redraw = function(){
var self = this;
self.draw.call(self,_color);
self.cachePixels();
self.context.clear();
self.image = self.context.getImageData(0,0,self.width,self.height);
clearInterval(self.interval);
return self;
};
Shape.prototype.start = function(gap){
var self = this;
self.started = (new Date()) - 0;
self.interval = setInterval(function(){
if(self.pixels.length) self.putPixel(0,0,0);
else self.stop();
},gap||_gap);
return self;
};
Shape.prototype.stop = function(){
clearInterval(this.interval);
return this;
};
Shape.prototype.test = function(guess){
return this.name.toLowerCase()===guess.toLowerCase();
};
Shape.prototype.done = function(){
return this.total - this.pixels.length;
};
Shape.prototype.percent = function(){
return this.done()/this.total;
};
Shape.prototype.time = function(){
return (new Date()) - this.started;
};
Shape.prototype.show = function(){
this.canvas.style.display = "block";
return this;
};
Shape.prototype.hide = function(){
this.canvas.style.display = "none";
return this;
};
//------------------------------------------------------------------
//Public
function addShape(name,draw,test) {
return new Shape(name,draw,test);
};
function testShape(name,guess) {
return _shapes[name].test(guess);
};
function listShapes() {
var list = [];
for(var i in _shapes) if (_shapes.hasOwnProperty(i)) list.push(i);
return list;
};
function listShapeKeys() {
var list = {};
for(var i in _shapes) if (_shapes.hasOwnProperty(i)) list[_shapes[i].key] = i;
return list;
};
function eachShape(callback) {
for(var i in _shapes) if (_shapes.hasOwnProperty(i)) callback.call(_shapes[i]);
};
function randShape(){
var lst = listShapes();
var i = rand1(lst.length);
return _shapes[lst[i]];
};
return {
add:addShape,
test:testShape,
list:listShapes,
keys:listShapeKeys,
each:eachShape,
random:randShape,
rand1:rand1,
rand2:rand2
};
})();
//=============================================================
var model = (function() {
var ls = function(a,b){return b?{get:function(c){return a[c]&&b.parse(a[c])},set:function(c,d){a[c]=b.stringify(d)}}:{}}(window.localStorage||{},JSON);
var _totals = {};
var _logs = ls.get("_logs") || [];
var _uaid = 'UA-22107593-7';
var _uadm = 'shapex.org';
//Initialize Google Analytics
var init = function() {
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
if (ga) { ga('create', _uaid, _uadm); ga('send', 'pageview'); }
};
//Custom UA Event
var result = function(shape,guess,correct,total,done,time,image) {
image = image.substr('data:image/png;base64,'.length).replace(/\+/g,'-').replace(/\//g,'_') + ".shape";
var url = ["/",shape,"/",guess,"/",total,"/",done,"/",time,"/",image].join('');
if (ga) ga('send', 'event', 'click', shape + '_' + guess + '_' + total + '_' + done + '_' + time, correct, done);
if (ga) ga('send', {'hitType':'pageview', 'page':shape + '_' + guess});
return url;
};
var total = function(shape, guess, correct, total, done, time, image) {
var amend = function(tshape) {
tshape[guess] = tshape[guess] ? tshape[guess] + 1 : 1;
if(!correct) {
tshape.incorrect[guess] = tshape.incorrect[guess]?tshape.incorrect[guess]+1:1;
tshape.incorrect.total++;
} else {
var shapecount = ++tshape.count - tshape.incorrect.total;
tshape.done+=done, tshape.total+=total, tshape.done+=done, tshape.time+=time;
tshape.avg_time = tshape.time/shapecount;
tshape.avg_done = tshape.done/shapecount;
tshape.avg_percent = tshape.done/tshape.total;
if(!tshape.best_done || tshape.best_done>done) tshape.best_done = done;
if(!tshape.best_time || tshape.best_time>time) tshape.best_time = time;
if(!tshape.best_percent || tshape.best_done>done) tshape.best_percent = tshape.avg_percent;
}
};
_totals.grand = _totals.grand || {count:0,done:0,total:0,time:0,best_done:0,best_percent:0,best_time:0,incorrect:{total:0}};
_totals[shape] = _totals[shape] || {count:0,done:0,total:0,time:0,best_done:0,best_percent:0,best_time:0,incorrect:{total:0}};
amend(_totals.grand);
amend(_totals[shape]);
ls.set("_totals",_totals);
return _totals;
};
var log = function(shape, guess, correct, total, done, time, image) {
var timestamp = ((new Date())-0);
var item = {shape:shape, guess:guess, correct:correct, total:total, done:done, time:time, image:image, timestamp: timestamp };
_logs.push(item);
ls.set("_logs",_logs);
return item
};
return { init:init, result:result, log:log, total:total, logs:function(){return _logs;},totals:function(){return _totals;} };
})();
//==============================================================
var ui = (function() {
var _begin = document.getElementById("begin"),
_accept = document.getElementById("accept"),
_social = document.getElementById("social"),
_message = document.getElementById("message"),
_cookies = document.getElementById("cookies"),
_experiment = document.getElementById("experiment"),
_results = document.getElementById("results"),
_total = document.getElementById("total"),
_totals = document.getElementById("totals"),
_facebook = document.getElementById("facebook"),
_twitter = document.getElementById("twitter"),
_about = document.getElementById("about"),
_url = "http://shapex.org",
_shape = null;
var polygon = function(ctx,color,vrt) {
ctx.beginPath();
ctx.fillStyle=color;
ctx.moveTo.apply(ctx,vrt[0]);
for(var i=1,l=vrt.length;i<l;i++) ctx.lineTo.apply(ctx,vrt[i]);
ctx.lineTo.apply(ctx,vrt[0]);
ctx.fill();
ctx.closePath();
}
var circle = shape.add("circle",function(color){
var ctr = this.width/2;
var rad = this.width/2-11;
var ctx = this.context;
ctx.beginPath();
ctx.fillStyle=color;
ctx.arc(ctr, ctr, rad, 0, Math.PI*2, true);
ctx.closePath();
ctx.fill();
});
var triangle = shape.add("triangle",function(color){
polygon(this.context,color,[[15,85],[50,15],[85,85]]);
});
var square = shape.add("square",function(color){
var margin = 15;
this.context.fillStyle=color;
this.context.fillRect(margin, margin, this.width-margin*2, this.width-margin*2);
});
var pentagon = shape.add("pentagon",function(color){
polygon(this.context,color,[[25,85],[15,40],[50,15],[85,40],[75,85]]);
});
var donut = shape.add("donut",function(color){
var ctr = this.width/2;
var rad = this.width/2-11;
var ctx = this.context;
ctx.beginPath();
ctx.fillStyle=color;
ctx.arc(ctr, ctr, rad, 0, Math.PI*2, true);
ctx.closePath();
ctx.fill();
ctx.beginPath();
ctx.fillStyle="rgba(255,255,255,1.0)";
ctx.arc(ctr, ctr, rad/3.7, 0, Math.PI*2, true);
ctx.closePath();
ctx.fill();
});
var star = shape.add("star",function(color){
polygon(this.context,color,[[50,10],[35,35],[10,40],[28,58],[20,85],[50,72],[80,85],[72,58],[90,40],[65,35]]);
});
var bars = shape.add("bars",function(color){
var margin = 15;
var height = Math.floor((this.height-margin)/2.6);
var width = this.width-margin*2;
this.context.fillStyle=color;
this.context.fillRect(margin, margin, width, height);
this.context.fillRect(margin, this.height-height-margin, width, height);
});
var diamond = shape.add("diamond",function(color){
polygon(this.context,color,[[50,15],[85,50],[50,85],[15,50]]);
});
var record = function(shape, guess, correct, total, done, time, image) {
var img = document.createElement("img");
var url = model.result.apply(this,arguments);
img.className = 'pixel';
img.src = url;
return img;
};
var tally = function(shape,bpct,btime,apct,atime) {
_total.style.display="block";
var tr = document.getElementById("total_"+shape);
var isnew = tr?0:1;
if(isnew) {
tr = document.createElement("tr");
tr.setAttribute("id","total_"+shape);
_totals.appendChild(tr);
}
for(var i=0,l=arguments.length;i<l;i++) {
var tag = !i?'':i%2?'%':'s';
var td = isnew ? document.createElement("td") : tr.cells[i];
td.textContent = (((i>0) ? parseInt(arguments[i]*100)/100:arguments[i])||0) + tag;
isnew && tr.appendChild(td);
}
};
var display = function(shape, guess, correct, total, done, time, image, nolog) {
var li = document.createElement("li");
var classes = [correct?'correct':'incorrect',guess,'log'];
var percent = done/total;
var container = document.createElement("div");
var info1 = document.createElement("div");
var info2 = document.createElement("div");
var thumb = document.createElement("img");
thumb.src = image;
info1.textContent = '| ' + shape + ' | ' + (parseInt(percent * 10000)/100) + '% | ' + (parseInt(percent * 10000)/100) + ' seconds | ';
info2.textContent = '( ' + guess + ' ' +done + ' )';
container.appendChild(info2);
container.appendChild(thumb);
container.className = "hidden";
if(!nolog) li.appendChild(record.apply(this,arguments));
li.setAttribute("class",classes.join(' '));
li.appendChild(info1);
li.appendChild(container);
if(_results.getElementsByTagName("li").length) {
_results.insertBefore(li,_results.firstChild);
} else {
_results.appendChild(li);
}
var toggle = function(){ container.style.display=container.style.display==='block'?'none':'block'; };
li.onclick = toggle;
li.touchend = toggle;
};
var facebook = function() {
//Show the like button:
var root = document.getElementById("fb-root");
var div = document.createElement("div");
//Initialize Facebook
(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
//Show like button
div.className = "fb-like";
div.setAttribute("data-href",_url);
div.setAttribute("data-send","false");
div.setAttribute("data-layout","button_count");
div.setAttribute("data-width","450");
div.setAttribute("data-show-faces","false");
root.appendChild(div);
root.style.display = "inline-block";
return false;
};
var twitter = function() {
var root = document.getElementById("tw-root");
var link = document.createElement("a");
link.className = "twitter-share-button";
link.href="https://twitter.com/share";
link.setAttribute("data-url",_url);
link.setAttribute("data-hashtags","shapex");
link.textContent = "Tweet";
root.appendChild(link);
root.style.display="inline-block";
!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');
return false;
};
//Restarts the shape sequence
var restart = function(){
if(_shape) {
_shape.stop().redraw().hide();
_shape = shape.random().show().start();
}
};
//Window keypress event handler
var keypress = function(e) {
var key = (e.keyCode||e.which) - 48;
if (key>0 && key<9) answer(shape.keys()[key]);
};
//------------------------------------------------------------------
//Public
var about = function() {
var root = document.getElementById("ab-root");
root.style.display="inline-block";
return false;
};
var social = function() {
facebook();
twitter();
about();
_social.style.display = "none";
};
var info = function(){
var style = document.getElementById("info").style;
style.display = style.display==="block"?"none":"block";
};
var init = function(){
model.init();
var logs = model.logs();
var totals = model.totals();
_accept.style.display = "none";
_message.style.display = "none";
_experiment.style.display = "block";
_shape = shape.random().show().start();
window.addEventListener("keypress",keypress);
for(var i=0,l=logs.length;i<l;i++) {
var lg = logs[i]; display(lg.shape, lg.guess, lg.correct, lg.total, lg.done, lg.time, lg.image, true);
model.total.call(this,lg.shape, lg.guess, lg.correct, lg.total, lg.done, lg.time, lg.image);
};
for(var i in totals) {
if(totals.hasOwnProperty(i)) {
var t = totals[i];
tally(i,t.best_percent*100,t.best_time/1000,t.avg_percent*100,t.avg_time/1000);
}
}
social();
};
//Marks the shape as incorrect or correct sends to the
var answer = function(guess){
if (_shape) {
var name = _shape.name;
var image = _shape.canvas.toDataURL(0,0,_shape.width,_shape.height);
var correct = _shape.test(guess);
var args = [name, guess, correct, _shape.total, _shape.done(), _shape.time(), image];
var totals = model.total.apply(this,args);
model.log.apply(this,args);
display.apply(this,args);
var t = totals[name], g = totals.grand;
tally('grand',g.best_percent*100,g.best_time/1000,g.avg_percent*100,g.avg_time/1000);
tally(name,t.best_percent*100,t.best_time/1000,t.avg_percent*100,t.avg_time/1000);
restart();
}
};
shape.each(function(){ var self = this; self.button.onclick = function(){ answer(self.name); }; });
//Begin button!
_begin.onclick = init;
//JS Links
_cookies.onclick= info;
_facebook.onclick = social;
_twitter.onclick = social;
_about.onclick = social;
return {
answer:answer,
about:about,
info:info,
social:social
};
})();