Initial commit, v1.0.0
[Hacks.git] / hacks / Munch / Munch.js
1 /* Copyright 2011 The Dominion of Awesome
2  * See COPYING for licensing information */
3 enyo.kind({
4         name: 'Munch',
5         kind: 'Hack',
6         pack: "center",
7         align: "center",
8         style: "background-color: black",
9         components: [
10                 {kind: enyo.Control, nodeTag: "canvas", name: "display", width: "512px", height: "512px"}
11         ],
12
13         t: 0,
14         timer: null,
15
16         rendered: function() {
17                 var node = this.$.display.hasNode();
18                 node.width = 256;
19                 node.height = 256;
20                 this.ctx = node.getContext('2d');
21                 this.ctx.fillStyle = 'black';
22                 this.ctx.fillRect(0, 0, 255, 255);
23                 this.ctx.fillStyle = 'rgba(0,0,0,0.075)';
24                 this.ctx.strokeStyle = '#00FF00';
25                 this.draw();
26         },
27
28         draw: function() {
29                 this.ctx.fillRect(0, 0, 256, 256);
30                 this.ctx.beginPath();
31                 for (var x = 0; x < 256; x++) {
32                         var y = x ^ this.t;
33                         this.ctx.moveTo(x, y);
34                         this.ctx.lineTo(x+1, y+1);
35                 }
36                 this.ctx.stroke();
37                 this.t = (this.t + 1) % 255;
38         },
39
40         start: function() {
41                 if (this.timer) return;
42                 this.timer = setInterval(this.draw.bind(this), 20);
43         },
44
45         stop: function() {
46                 clearInterval(this.timer);
47                 this.timer = null;
48         }
49 });