From ed2ef0231f5b13c15566fb71a18272afd6ef255a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Sat, 14 May 2011 20:06:25 +0100 Subject: [PATCH 1/8] Have ctor functions be unnamed, so they don't show as 'ctor' prototypes in debugging. --- lib/bootstrap.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/bootstrap.js b/lib/bootstrap.js index f1632e5c..66144505 100644 --- a/lib/bootstrap.js +++ b/lib/bootstrap.js @@ -149,7 +149,7 @@ var Base = this.Base = new function() { } function extend(obj) { - function ctor(dont) { + var ctor = function(dont) { if (fix) define(this, '__proto__', { value: obj }); if (this.initialize && dont !== ctor.dont) return this.initialize.apply(this, arguments); From 064f3e05d38d9253379156a59f216b08c0046b37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Sat, 14 May 2011 20:08:14 +0100 Subject: [PATCH 2/8] Simplify paper scope switching code for now, as key handling code depends on paper object to always point to a valid one. This is a workaround, rethinking of how key handling should be distributed across multiple instances within one page is required. --- src/core/PaperScript.js | 13 ++++--------- src/tool/ToolHandler.js | 3 +-- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/src/core/PaperScript.js b/src/core/PaperScript.js index 13b6b93d..2c2e19e9 100644 --- a/src/core/PaperScript.js +++ b/src/core/PaperScript.js @@ -130,8 +130,8 @@ var PaperScript = this.PaperScript = new function() { } function run(code, scope) { - try { with (scope) { // Safe one indentation by grouping try and with - PaperScope.set(scope); + with (scope) { // Safe one indentation by grouping try and with + paper = scope; var doc = scope.document; // TODO: Add support for multiple tools var tool = scope.tool = @@ -158,7 +158,7 @@ var PaperScript = this.PaperScript = new function() { var totalTime = 0; function frame(dontSwitch) { if (!dontSwitch) - PaperScope.set(scope); + paper = scope; // Request next frame already DomEvent.requestAnimationFrame(frame, doc && doc.canvas); var time = Date.now() / 1000; @@ -174,8 +174,6 @@ var PaperScript = this.PaperScript = new function() { if (doc) doc.redraw(); lastTime = time; - if (!dontSwitch) - PaperScope.restore(); }; // Call the onFrame handler and redraw the document: frame(true); @@ -185,8 +183,6 @@ var PaperScript = this.PaperScript = new function() { doc.redraw(); } return res; - } } finally { - PaperScope.restore(); } } @@ -223,9 +219,8 @@ var PaperScript = this.PaperScript = new function() { var canvas = script.getAttribute('canvas'); if (canvas = canvas && document.getElementById(canvas)) { // Create a Document for this canvas, using the right scope - PaperScope.set(scope); + paper = scope; new Document(canvas); - PaperScope.restore(); } if (script.src) { request(script.src, scope); diff --git a/src/tool/ToolHandler.js b/src/tool/ToolHandler.js index 8a3510b2..f0db61a9 100644 --- a/src/tool/ToolHandler.js +++ b/src/tool/ToolHandler.js @@ -121,7 +121,7 @@ var ToolHandler = this.ToolHandler = Base.extend({ }, onHandleEvent: function(type, pt, event) { - PaperScope.set(this._scope); + paper = this._scope; switch (type) { case 'mousedown': this.updateEvent(type, pt, null, null, true, false, false); @@ -178,6 +178,5 @@ var ToolHandler = this.ToolHandler = Base.extend({ } break; } - PaperScope.restore(); } }); From 0d85e0941c0245f4bf93e24cb282aa25d005a204 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Sat, 14 May 2011 20:20:10 +0100 Subject: [PATCH 3/8] Add inverse matrix caching. --- src/document/DocumentView.js | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/document/DocumentView.js b/src/document/DocumentView.js index b9d4ac36..6d5d3aee 100644 --- a/src/document/DocumentView.js +++ b/src/document/DocumentView.js @@ -20,7 +20,7 @@ var DocumentView = this.DocumentView = Base.extend({ initialize: function(document) { this.document = document; this._bounds = this.document.bounds.clone(); - this.matrix = new Matrix(); + this._matrix = new Matrix(); this._zoom = 1; this._center = this._bounds.center; }, @@ -55,13 +55,21 @@ var DocumentView = this.DocumentView = Base.extend({ }, transform: function(matrix, flags) { - this.matrix.preConcatenate(matrix); + this._matrix.preConcatenate(matrix); this._bounds = null; + this._inverse = null; + }, + + _getInverse: function() { + if (!this._inverse) { + this._inverse = this._matrix.createInverse(); + } + return this._inverse; }, getBounds: function() { if (!this._bounds) { - this._bounds = this.matrix.transformBounds(this.document.bounds); + this._bounds = this._matrix.transformBounds(this.document.bounds); } return this._bounds; }, @@ -78,11 +86,10 @@ var DocumentView = this.DocumentView = Base.extend({ // TODO: getMousePoint // TODO: artworkToView(rect) artworkToView: function(point) { - return this.matrix._transformPoint(point); + return this._matrix._transformPoint(point); }, viewToArtwork: function(point) { - // TODO: cache the inverse matrix: - return this.matrix.createInverse()._transformPoint(point); + return this._getInverse()._transformPoint(point); } }); From 1dd0ee25339e6f7c41e1967f7d8eca4a6802f7be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Sat, 14 May 2011 22:38:27 +0100 Subject: [PATCH 4/8] Simplify Item#getId() --- src/item/Item.js | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/src/item/Item.js b/src/item/Item.js index 29f68ae6..5e4790c1 100644 --- a/src/item/Item.js +++ b/src/item/Item.js @@ -34,6 +34,16 @@ var Item = this.Item = Base.extend({ } }, + /** + * The unique id of the item. + */ + getId: function() { + if (this._id == null) { + this._id = Item._id = (Item._id || 0) + 1; + } + return this._id; + } + /** * When passed a document, copies the item to the document, * or duplicates it within the same document. When passed an item, @@ -996,18 +1006,4 @@ var Item = this.Item = Base.extend({ return this; } }); -}, new function() { - var id = 0; - return { - beans: true, - - /** - * The unique id of the item. - */ - getId: function() { - if (this._id === undefined) - this._id = id++; - return this._id; - } - }; }); From 3cb78db5f5ae98f3a15fe4c94ee7163c284bc932 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Sun, 15 May 2011 11:32:09 +0100 Subject: [PATCH 5/8] Fix typo. --- src/item/Item.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/item/Item.js b/src/item/Item.js index 5e4790c1..68125a90 100644 --- a/src/item/Item.js +++ b/src/item/Item.js @@ -42,7 +42,7 @@ var Item = this.Item = Base.extend({ this._id = Item._id = (Item._id || 0) + 1; } return this._id; - } + }, /** * When passed a document, copies the item to the document, From 06f16d0c2a231a74ccac27aabe08914d74479629 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Sun, 15 May 2011 11:32:42 +0100 Subject: [PATCH 6/8] Define a unique id for each PaperScript. --- src/core/PaperScope.js | 3 ++- src/core/PaperScript.js | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/core/PaperScope.js b/src/core/PaperScope.js index 48c3d3c2..8bd7177a 100644 --- a/src/core/PaperScope.js +++ b/src/core/PaperScope.js @@ -19,10 +19,11 @@ * global paper object, which simply is a pointer to the currently active scope. */ var PaperScope = this.PaperScope = Base.extend({ - initialize: function() { + initialize: function(id) { this.document = null; this.documents = []; this.tools = []; + this.id = id; }, /** diff --git a/src/core/PaperScript.js b/src/core/PaperScript.js index 2c2e19e9..e5616e71 100644 --- a/src/core/PaperScript.js +++ b/src/core/PaperScript.js @@ -207,13 +207,13 @@ var PaperScript = this.PaperScript = new function() { var scripts = document.getElementsByTagName('script'); for (var i = 0, l = scripts.length; i < l; i++) { var script = scripts[i]; - // Only load this cript if it not loaded already. + // Only load this script if it not loaded already. if (script.type === 'text/paperscript' && !script.getAttribute('loaded')) { // Produce a new PaperScope for this script now. Scopes are // cheap so let's not worry about the initial one that was // already created. - var scope = new PaperScope(); + var scope = new PaperScope(script.src || ('script-' + i)); // If a canvas id is provided, create a document for it now, // so the active document is defined. var canvas = script.getAttribute('canvas'); From 51dbd85db5a03930a3ba2bb64382f799249dfa1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Sun, 15 May 2011 11:33:09 +0100 Subject: [PATCH 7/8] Add experimental orientation detection for Paths. --- src/path/Path.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/path/Path.js b/src/path/Path.js index 46f95955..e97f2411 100644 --- a/src/path/Path.js +++ b/src/path/Path.js @@ -356,6 +356,31 @@ var Path = this.Path = PathItem.extend({ return false; }, + getOrientation: function() { + var sum = 0; + var xPre, yPre; + function edge(x, y) { + if (xPre !== undefined) { + sum += (xPre - x) * (y + yPre); + } + xPre = x; + yPre = y; + } + for (var i = 0, l = this._segments.length; i < l; i++) { + var seg1 = this._segments[i]; + var seg2 = this._segments[i + 1 < l ? i + 1 : 0]; + var point1 = seg1._point; + var handle1 = seg1._handleOut; + var handle2 = seg2._handleIn; + var point2 = seg2._point; + edge(point1._x, point1._y); + edge(point1._x + handle1._x, point1._y + handle1._y); + edge(point2._x + handle2._x, point2._y + handle2._y); + edge(point2._x, point2._y); + } + return sum; + }, + getLength: function() { if (this._length == null) { var curves = this.getCurves(); From 3078e74f2711fb9d04f0553a508fd658f916156f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Sun, 15 May 2011 11:36:10 +0100 Subject: [PATCH 8/8] Let's not use variable chaining in examples, for clearer code. --- examples/Animated/Extruded.html | 12 ++++---- examples/Animated/Flock.html | 42 ++++++++++++------------- examples/Animated/Lines.html | 8 ++--- examples/Animated/Raster.html | 8 ++--- examples/Scripts/BouncingBalls.html | 10 +++--- examples/Scripts/Chain.html | 6 ++-- examples/Scripts/RotationRaster.html | 18 +++++------ examples/Tools/MetaBalls.html | 46 ++++++++++++++-------------- examples/Tools/Stars.html | 10 +++--- 9 files changed, 80 insertions(+), 80 deletions(-) diff --git a/examples/Animated/Extruded.html b/examples/Animated/Extruded.html index 456fe092..d008138b 100644 --- a/examples/Animated/Extruded.html +++ b/examples/Animated/Extruded.html @@ -44,9 +44,9 @@ path2.position = document.bounds.center; path2.scale(2); - var length1 = path1.length, - length2 = path2.length, - count = 0; + var length1 = path1.length; + var length2 = path2.length; + var count = 0; function onFrame() { var vector = new Point({ angle: -count % 360, @@ -59,9 +59,9 @@ path2.position = document.bounds.center + vector.normalize(50); for (var i = 0; i < lineCount; i++) { - var path = lineGroup.children[i], - l1 = (length1 / lineCount * (i + count / 10)) % length1, - l2 = (length2 / lineCount * (i + count / 10)) % length2; + var path = lineGroup.children[i]; + var l1 = (length1 / lineCount * (i + count / 10)) % length1; + var l2 = (length2 / lineCount * (i + count / 10)) % length2; path.segments[0].point = path1.getPointAt(l1), path.segments[1].point = path2.getPointAt(l2); } diff --git a/examples/Animated/Flock.html b/examples/Animated/Flock.html index d61a230c..d7b60150 100644 --- a/examples/Animated/Flock.html +++ b/examples/Animated/Flock.html @@ -138,11 +138,11 @@ }, borders: function() { - var loc = this.loc, - r = this.r, - oldLoc = this.loc.clone(), - width = size.width, - height = size.height; + var loc = this.loc; + var r = this.r; + var oldLoc = this.loc.clone(); + var width = size.width; + var height = size.height; if (loc.x < -r) loc.x = width + r; if (loc.y < -r) loc.y = height + r; if (loc.x > width + r) loc.x = -r; @@ -153,9 +153,9 @@ }, separate: function(boids) { - var desiredSeperation = 60, - steer = new Point(0, 0), - count = 0; + var desiredSeperation = 60; + var steer = new Point(0, 0); + var count = 0; // For every boid in the system, check if it's too close for (var i = 0, l = boids.length; i < l; i++) { var other = boids[i]; @@ -182,11 +182,11 @@ // Alignment // For every nearby boid in the system, calculate the average velocity align: function(boids) { - var neighborDist = 25, - steer = new Point(0, 0), - count = 0, - nearest = 999, - closestPoint; + var neighborDist = 25; + var steer = new Point(0, 0); + var count = 0; + var nearest = 999; + var closestPoint; for (var i = 0, l = boids.length; i < l; i++) { var other = boids[i]; var d = this.loc.getDistance(other.loc); @@ -214,9 +214,9 @@ // Cohesion // For the average location (i.e. center) of all nearby boids, calculate steering vector towards that location cohesion: function(boids) { - var neighborDist = 100, - sum = new Point(0, 0), - count = 0; + var neighborDist = 100; + var sum = new Point(0, 0); + var count = 0; for (var i = 0, l = boids.length; i < l; i++) { var other = boids[i]; var d = this.loc.getDistance(other.loc); @@ -258,11 +258,11 @@ heartPath.strokeColor = null; heartPath.scale(1.5); - var count = 0, - groupTogether = false, - pathLength = heartPath.length, - mouseDown = false, - boids = []; + var count = 0; + var groupTogether = false; + var pathLength = heartPath.length; + var mouseDown = false; + var boids = []; // Add the boids: for (var i = 0; i < 30; i++) { diff --git a/examples/Animated/Lines.html b/examples/Animated/Lines.html index a2f43068..3c79c268 100644 --- a/examples/Animated/Lines.html +++ b/examples/Animated/Lines.html @@ -15,10 +15,10 @@ path.closed = true; } - var position = document.bounds.center, - mousePos = position, - children = document.activeLayer.children, - count = 0; + var position = document.bounds.center; + var mousePos = position; + var children = document.activeLayer.children; + var count = 0; function iterate() { count++; diff --git a/examples/Animated/Raster.html b/examples/Animated/Raster.html index a300c713..9cbdfac7 100644 --- a/examples/Animated/Raster.html +++ b/examples/Animated/Raster.html @@ -8,10 +8,10 @@