From 7abf73c5f7fd83cc8e5a6a1d979c39bb73b188b9 Mon Sep 17 00:00:00 2001 From: Tyler Hallada Date: Sat, 2 Apr 2016 18:48:41 -0400 Subject: [PATCH] More progress, more colors --- js/stuff.js | 41 ++++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/js/stuff.js b/js/stuff.js index 4c95ae3..f1973b9 100644 --- a/js/stuff.js +++ b/js/stuff.js @@ -30,9 +30,18 @@ Stuff.prototype.getDocumentDimensions = function () { /* Choose a random RGB color to begin the color gradient with */ Stuff.prototype.pickRandomColor = function() { - return {"r": Math.floor(Math.random() * (255 + 1)), - "g": Math.floor(Math.random() * (255 + 1)), - "b": Math.floor(Math.random() * (255 + 1))}; + var normal = {"r": Math.floor(Math.random() * (255 + 1)), + "g": Math.floor(Math.random() * (255 + 1)), + "b": Math.floor(Math.random() * (255 + 1))}; + var inverse_color = (normal.r + normal.g + normal.b) / 3 > 0.5 ? 20 : 235; + var gamma = 2.2; + var L = 0.2126 * Math.pow(normal.r / 255, gamma) + 0.7152 * Math.pow(normal.g / 255, gamma) + + 0.0722 * Math.pow(normal.b / 255, gamma); + var inverse = 'black'; + if (L < 0.5) { + inverse = 'white'; + } + return {'normal': normal, 'inverse': inverse}; }; Stuff.prototype.clear = function() { @@ -96,32 +105,34 @@ Stuff.prototype.distance = function(point1, point2) { Stuff.prototype.drawPolygon = function(context) { for (i = 0; i < this.points.length; i++) { if (this.converge_point) { + var pull_amount = this.getRandomInRange(2, 7, false); if (this.points[i].x >= this.converge_point.x) { - this.points[i].x = this.points[i].x - 3; + this.points[i].x = this.points[i].x - pull_amount; } else { - this.points[i].x = this.points[i].x + 3; + this.points[i].x = this.points[i].x + pull_amount; } if (this.points[i].y >= this.converge_point.y) { - this.points[i].y = this.points[i].y - 3; + this.points[i].y = this.points[i].y - pull_amount; } else { - this.points[i].y = this.points[i].y + 3; + this.points[i].y = this.points[i].y + pull_amount; } } else if (this.counter % 10 === 0) { if (Math.random() < 0.5) { - this.points[i].y = this.points[i].y - 10; + this.points[i].y = this.points[i].y - this.getRandomInRange(3, 6, false); } else { - this.points[i].y = this.points[i].y + 10; + this.points[i].y = this.points[i].y + this.getRandomInRange(3, 6, false); } } else if ((this.mouse_position) && (this.distance(this.mouse_position, this.points[i]) < 50)) { + var push_amount = this.getRandomInRange(2, 7, false); if (this.points[i].x >= this.mouse_position.x) { - this.points[i].x = this.points[i].x + 3; + this.points[i].x = this.points[i].x + push_amount; } else { - this.points[i].x = this.points[i].x - 3; + this.points[i].x = this.points[i].x - push_amount; } if (this.points[i].y >= this.mouse_position.y) { - this.points[i].y = this.points[i].y + 3; + this.points[i].y = this.points[i].y + push_amount; } else { - this.points[i].y = this.points[i].y - 3; + this.points[i].y = this.points[i].y - push_amount; } } else { this.points[i].x = this.points[i].x + this.getRandomInRange(0, 2, true); @@ -133,7 +144,6 @@ Stuff.prototype.drawPolygon = function(context) { // this.sortedInsert(this.y_sorted, this.points[i].y, point); context.beginPath(); - context.strokeStyle = 'black'; context.arc(this.points[i].x, this.points[i].y, 1, 0, 2*Math.PI); context.stroke(); context.closePath(); @@ -186,7 +196,8 @@ Stuff.prototype.canvasClickHandler = function(event) { var y = event.pageY; this.converge_point = {'x': x, 'y': y}; color = this.pickRandomColor(); - this.canvas.style.backgroundColor = "rgb(" + color.r + "," + color.g + "," + color.b + ")"; + this.canvas.style.backgroundColor = "rgb(" + color.normal.r + "," + color.normal.g + "," + color.normal.b + ")"; + this.context.strokeStyle = color.inverse; }; Stuff.prototype.canvasMouseMoveHandler = function(event) {