1 /* Copyright 2011 The Dominion of Awesome
2 * See COPYING for licensing information */
5 constructor: function(canvas) {
6 this.x = Math.random() * canvas.width;
7 this.y = Math.random() * canvas.height;
8 this.vx = Math.random() * 20.0 - 10.0;
9 this.vy = Math.random() * 20.0 - 10.0;
10 this.lastupdate = (new Date()).getTime();
12 accel: function(x, y) {
16 else if (this.vx < -200.0)
21 else if (this.vy < -200.0)
25 var now = (new Date()).getTime();
26 var dt = (now - this.lastupdate) / 75.0;
27 this.x += this.vx * dt;
28 this.y += this.vy * dt;
29 this.lastupdate = now;
33 ctx.moveTo(this.x,this.y);
34 ctx.lineTo(this.x - this.vx / 4.0, this.y - this.vy / 4.0);
46 {name: "canvas", nodeTag: "canvas", style: "background-color: gray",
47 onmousemove: "mouseMove", onmousedown: "mouseDown"}
51 rendered: function() {
52 this.canvas = this.$.canvas.hasNode();
54 this.mx = this.canvas.width / 2;
55 this.my = this.canvas.width / 2;
56 for (var x = 0; x < this.npixels; x++)
57 this.pixels.push(new Pixel(this.canvas));
58 //this.stopTime = (new Date()).getTime();
63 var delta = (new Date()).getTime() - this.stopTime;
64 for (var i = 0; i < this.npixels; i++)
65 this.pixels[i].lastupdate += delta;
69 this.timer = setInterval(this.engine.bind(this), 20);
72 this.stopTime = (new Date()).getTime();
73 clearInterval(this.timer);
76 resize: function(w, h) {
77 this.canvas.width = w;
78 this.canvas.height = h;
79 //this.canvas.style.width = w + 'px';
80 //this.canvas.style.height = h + 'px';
81 this.context = this.canvas.getContext('2d');
82 this.context.strokeStyle = '#000000';
83 this.context.lineCap = 'round';
84 this.context.lineWidth = 1.5;
86 mouseMove: function(inSender, inEvent) {
87 this.mx = inEvent.clientX;
88 this.my = inEvent.clientY;
90 mouseDown: function(inSender, inEvent) {
91 this.mouseMove(inSender, inEvent);
92 // Apply force to each pixel outwards from the mouse
93 for (var i = 0; i < this.npixels; i++) {
95 var dx = this.pixels[i].x - inEvent.clientX;
96 var dy = this.pixels[i].y - inEvent.clientY;
97 var mag = Math.sqrt(dx*dx + dy*dy);
102 this.pixels[i].accel(dx * 40.0, dy * 40.0);
107 this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
108 // Update all pixel objects
109 for (var i = 0; i < this.npixels; i++) {
111 var dx = this.mx - this.pixels[i].x;
112 var dy = this.my - this.pixels[i].y;
113 var d = Math.sqrt(dx*dx + dy*dy);
115 // Apply acceleration to pixels towards the mouse cursor
119 // Apply a drag proportional to the distance from the cursor
121 ax += -this.pixels[i].vx * (d - 100.0) / 2000.0;
122 ay += -this.pixels[i].vy * (d - 100.0) / 2000.0;
125 // And a "chaotic" acceleration inversely proportional
126 // to the distance from the cursor
127 ax += (Math.random() * 160.0 - 80.0) / d;
128 ay += (Math.random() * 160.0 - 80.0) / d;
130 this.pixels[i].accel(ax,ay);
131 this.pixels[i].update();
132 this.pixels[i].draw(this.context);
135 this.context.strokeRect(this.mx - 10, this.my - 10, 20, 20);