Browse Source

Better color change, naive mouse velocity

Tyler Hallada 8 years ago
parent
commit
ac2dc8efa8
1 changed files with 63 additions and 22 deletions
  1. 63 22
      js/stuff.js

+ 63 - 22
js/stuff.js

@@ -28,12 +28,41 @@ Stuff.prototype.getDocumentDimensions = function () {
28 28
     return {"height": height, "width": width};
29 29
 };
30 30
 
31
-/* Choose a random RGB color to begin the color gradient with */
31
+/* Choose a random RGB color */
32 32
 Stuff.prototype.pickRandomColor = function() {
33 33
     var normal = {"r": Math.floor(Math.random() * (255 + 1)),
34 34
                   "g": Math.floor(Math.random() * (255 + 1)),
35 35
                   "b": Math.floor(Math.random() * (255 + 1))};
36
-    var inverse_color = (normal.r + normal.g + normal.b) / 3 > 0.5 ? 20 : 235;
36
+    var gamma = 2.2;
37
+    var L = 0.2126 * Math.pow(normal.r / 255, gamma) + 0.7152 * Math.pow(normal.g / 255, gamma) +
38
+            0.0722 * Math.pow(normal.b / 255, gamma);
39
+    var inverse = 'black';
40
+    if (L < 0.5) {
41
+        inverse = 'white';
42
+    }
43
+    return {'normal': normal, 'inverse': inverse};
44
+};
45
+
46
+Stuff.prototype.changeColorValue = function(color_value) {
47
+    if (color_value === 255) {
48
+        return color_value - this.getRandomInRange(5, 50);
49
+    } else if (color_value === 0) {
50
+        return color_value + this.getRandomInRange(5, 50);
51
+    }
52
+    var new_color_value = color_value + this.getRandomInRange(5, 100, true);
53
+    if (new_color_value > 255) {
54
+        return 255;
55
+    } else if (new_color_value < 0) {
56
+        return 0;
57
+    }
58
+    return new_color_value;
59
+};
60
+
61
+/* Mutate RGB color by random increment */
62
+Stuff.prototype.changeColor = function(color) {
63
+    var normal = {"r": this.changeColorValue(color.normal.r),
64
+                  "g": this.changeColorValue(color.normal.g),
65
+                  "b": this.changeColorValue(color.normal.b)};
37 66
     var gamma = 2.2;
38 67
     var L = 0.2126 * Math.pow(normal.r / 255, gamma) + 0.7152 * Math.pow(normal.g / 255, gamma) +
39 68
             0.0722 * Math.pow(normal.b / 255, gamma);
@@ -116,24 +145,29 @@ Stuff.prototype.drawPolygon = function(context) {
116 145
             } else {
117 146
                 this.points[i].y = this.points[i].y + pull_amount;
118 147
             }
119
-        } else if (this.counter % 10 === 0) {
148
+        } else if ((this.mouse_position) && (this.old_mouse_position) && (this.distance(this.mouse_position, this.points[i]) < 50)) {
149
+            // var push_amount = this.getRandomInRange(this.mouse_velocity * 0.75, this.mouse_velocity, false);
150
+            // if (this.points[i].x >= this.mouse_position.x) {
151
+                // this.points[i].x = this.points[i].x + push_amount;
152
+            // } else {
153
+                // this.points[i].x = this.points[i].x - push_amount;
154
+            // }
155
+            // if (this.points[i].y >= this.mouse_position.y) {
156
+                // this.points[i].y = this.points[i].y + push_amount;
157
+            // } else {
158
+                // this.points[i].y = this.points[i].y - push_amount;
159
+            // }
160
+            var push_amount_x = this.old_mouse_position.x - this.mouse_position.x;
161
+            var push_amount_y = this.old_mouse_position.y - this.mouse_position.y;
162
+            this.points[i].x = this.points[i].x - this.getRandomInRange(push_amount_x * 0.5, push_amount_x * 1.5);
163
+            this.points[i].y = this.points[i].y - this.getRandomInRange(push_amount_y * 0.5, push_amount_y * 1.5);
164
+        }
165
+        if (this.counter % 10 === 0) {
120 166
             if (Math.random() < 0.5) {
121 167
                 this.points[i].y = this.points[i].y - this.getRandomInRange(3, 6, false);
122 168
             } else {
123 169
                 this.points[i].y = this.points[i].y + this.getRandomInRange(3, 6, false);
124 170
             }
125
-        } else if ((this.mouse_position) && (this.distance(this.mouse_position, this.points[i]) < 50)) {
126
-            var push_amount = this.getRandomInRange(2, 7, false);
127
-            if (this.points[i].x >= this.mouse_position.x) {
128
-                this.points[i].x = this.points[i].x + push_amount;
129
-            } else {
130
-                this.points[i].x = this.points[i].x - push_amount;
131
-            }
132
-            if (this.points[i].y >= this.mouse_position.y) {
133
-                this.points[i].y = this.points[i].y + push_amount;
134
-            } else {
135
-                this.points[i].y = this.points[i].y - push_amount;
136
-            }
137 171
         } else {
138 172
             this.points[i].x = this.points[i].x + this.getRandomInRange(0, 2, true);
139 173
             this.points[i].y = this.points[i].y + this.getRandomInRange(0, 2, true);
@@ -159,10 +193,7 @@ Stuff.prototype.drawPolygon = function(context) {
159 193
  Stuff.prototype.drawThing = function(context, sort, prune, chain) {
160 194
     var AnimationFrame = window.AnimationFrame;
161 195
     AnimationFrame.shim();
162
-    var animationFrame = new AnimationFrame(30),
163
-        timeCounter = 0,
164
-        color,
165
-        gradient;
196
+    var animationFrame = new AnimationFrame(30);
166 197
     context.lineWidth = 0.5;
167 198
     
168 199
     var self = this;
@@ -195,15 +226,25 @@ Stuff.prototype.canvasClickHandler = function(event) {
195 226
     var x = event.pageX;
196 227
     var y = event.pageY;
197 228
     this.converge_point = {'x': x, 'y': y};
198
-    color = this.pickRandomColor();
199
-    this.canvas.style.backgroundColor = "rgb(" + color.normal.r + "," + color.normal.g + "," + color.normal.b + ")";
200
-    this.context.strokeStyle = color.inverse;
229
+    if (this.color) {
230
+        this.color = this.changeColor(this.color);
231
+    } else {
232
+        this.color = this.pickRandomColor();
233
+    }
234
+    this.canvas.style.backgroundColor = "rgb(" + this.color.normal.r + "," + this.color.normal.g + "," + this.color.normal.b + ")";
235
+    this.context.strokeStyle = this.color.inverse;
201 236
 };
202 237
 
203 238
 Stuff.prototype.canvasMouseMoveHandler = function(event) {
204 239
     var x = event.pageX;
205 240
     var y = event.pageY;
241
+    this.old_mouse_position = this.mouse_position;
206 242
     this.mouse_position = {'x': x, 'y': y};
243
+    if (this.old_mouse_position) {
244
+        this.mouse_velocity = this.distance(this.mouse_position, this.old_mouse_position);
245
+    } else {
246
+        this.mouse_velocity = 0.0;
247
+    }
207 248
 };
208 249
 
209 250
 Stuff.prototype.draw = function() {