From 5a0491f1112b2f8373250f89c9672561877d8e8d Mon Sep 17 00:00:00 2001 From: SebastianCrow Date: Wed, 4 Mar 2015 23:40:42 +0100 Subject: [PATCH 1/2] Added entityClicked event + nearestEntity performance improvements MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Event “entityClicked” is triggered after clicking entity 2. selectionRadiusSquare introduced (to avoid computations in every loop) 3. Traditional loops instead of for…in (performance gain) 4. Break final loop after finding matching PinConstraint (to avoid not needed further processing) --- js/verlet-1.0.0.js | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/js/verlet-1.0.0.js b/js/verlet-1.0.0.js index bdf342e..88e2874 100644 --- a/js/verlet-1.0.0.js +++ b/js/verlet-1.0.0.js @@ -504,6 +504,15 @@ function VerletJS(width, height, canvas) { e.preventDefault(); }; + this.canvas.onclick = function(e) { + var nearest = _this.nearestEntity(); + if (nearest) { + var event = new CustomEvent("entityClicked"); + event.entity = nearest; + this.dispatchEvent(event); + } + }; + this.canvas.onmousedown = function(e) { _this.mouseDown = true; var nearest = _this.nearestEntity(); @@ -639,13 +648,16 @@ VerletJS.prototype.nearestEntity = function() { var d2Nearest = 0; var entity = null; var constraintsNearest = null; - + var selectionRadiusSquare = this.selectionRadius * this.selectionRadius; + // find nearest point - for (c in this.composites) { + var compositesLength = this.composites.length; + for (var c = 0; c < compositesLength; ++c) { var particles = this.composites[c].particles; - for (i in particles) { + var particlesLength = particles.length; + for (var i = 0; i < particlesLength; ++i) { var d2 = particles[i].pos.dist2(this.mouse); - if (d2 <= this.selectionRadius*this.selectionRadius && (entity == null || d2 < d2Nearest)) { + if (d2 <= selectionRadiusSquare && (entity == null || d2 < d2Nearest)) { entity = particles[i]; constraintsNearest = this.composites[c].constraints; d2Nearest = d2; @@ -654,9 +666,11 @@ VerletJS.prototype.nearestEntity = function() { } // search for pinned constraints for this entity - for (i in constraintsNearest) - if (constraintsNearest[i] instanceof PinConstraint && constraintsNearest[i].a == entity) + for (var i = constraintsNearest.length-1; i >= 0; --i) + if (constraintsNearest[i] instanceof PinConstraint && constraintsNearest[i].a == entity) { entity = constraintsNearest[i]; + break; + } return entity; } From e0b576fe3a155b2588afd009ec63691d8bac5f7a Mon Sep 17 00:00:00 2001 From: SebastianCrow Date: Wed, 4 Mar 2015 23:45:36 +0100 Subject: [PATCH 2/2] Styling refactoring Tabs fix --- js/verlet-1.0.0.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/js/verlet-1.0.0.js b/js/verlet-1.0.0.js index 88e2874..f627008 100644 --- a/js/verlet-1.0.0.js +++ b/js/verlet-1.0.0.js @@ -504,14 +504,14 @@ function VerletJS(width, height, canvas) { e.preventDefault(); }; - this.canvas.onclick = function(e) { - var nearest = _this.nearestEntity(); - if (nearest) { - var event = new CustomEvent("entityClicked"); - event.entity = nearest; - this.dispatchEvent(event); - } - }; + this.canvas.onclick = function(e) { + var nearest = _this.nearestEntity(); + if (nearest) { + var event = new CustomEvent("entityClicked"); + event.entity = nearest; + this.dispatchEvent(event); + } + }; this.canvas.onmousedown = function(e) { _this.mouseDown = true;