From 4b2341b3c2e149f5d11e82384fd1a4e532aaf239 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Sun, 13 Feb 2011 13:52:51 +0000 Subject: [PATCH] Add gettter/setters for Path#segments, and fix bug in Segment#previous. --- src/path/Path.js | 6 ++--- src/path/PathItem.js | 57 ++++++++++++++++++++++++++++---------------- src/path/Segment.js | 21 ++++++++-------- 3 files changed, 50 insertions(+), 34 deletions(-) diff --git a/src/path/Path.js b/src/path/Path.js index a94bda23..1ca1a357 100644 --- a/src/path/Path.js +++ b/src/path/Path.js @@ -89,7 +89,7 @@ Path = PathItem.extend({ segment.handleIn = segment.handleIn.multiply(size); segment.handleOut = segment.handleOut.multiply(size); segment.point = segment.point.multiply(size).add(topLeft); - path.segments.push(segment); + path._segments.push(segment); } path.closed = true; return path; @@ -112,8 +112,8 @@ Path = PathItem.extend({ path.moveTo(left); path.arcTo(center.add(radius, 0), true); path.arcTo(left, true); - var last = path.segments.pop(); - path.segments[0].handleIn = last.handleIn; + var last = path._segments.pop(); + path._segments[0].handleIn = last.handleIn; path.closed = true; return path; } diff --git a/src/path/PathItem.js b/src/path/PathItem.js index 9db9b2e1..86f626ed 100644 --- a/src/path/PathItem.js +++ b/src/path/PathItem.js @@ -37,11 +37,11 @@ PathItem = Item.extend(new function() { return { beans: true, - + initialize: function() { this.base(); this.closed = false; - this.segments = [];//new SegmentList(this); + this._segments = [];//new SegmentList(this); this.bounds = new Rectangle(); for(var i = 0, l = arguments.length; i < l; i++) { var segment = new Segment(arguments[i]); @@ -49,9 +49,20 @@ PathItem = Item.extend(new function() { } }, + /** + * The segments contained within the path. + */ + getSegments: function() { + return this._segments; + }, + + setSegments: function(segments) { + this._segments = segments; + }, + addSegment: function(segment) { segment.path = this; - this.segments.push(segment); + this._segments.push(segment); }, add: function() { @@ -61,28 +72,32 @@ PathItem = Item.extend(new function() { }, insert: function(index, segment) { - this.segments.splice(index, 0, new Segment(segment)); + this._segments.splice(index, 0, new Segment(segment)); }, + /** + * PostScript-style drawing commands + */ + /** * Helper method that returns the current segment and checks if we need to * execute a moveTo() command first. */ getCurrentSegment: function() { - if (this.segments.length == 0) + if (this._segments.length == 0) throw('Use a moveTo() command first'); - return this.segments[this.segments.length - 1]; + return this._segments[this._segments.length - 1]; }, moveTo: function() { var segment = Segment.read(arguments); - if(segment && !this.segments.length) + if(segment && !this._segments.length) this.addSegment(segment); }, lineTo: function() { var segment = Segment.read(arguments); - if(segment && this.segments.length) + if(segment && this._segments.length) this.addSegment(segment); }, @@ -113,7 +128,7 @@ PathItem = Item.extend(new function() { // and the cubic is A B C D, // B = E + 1/3 (A - E) // C = E + 1/3 (D - E) - var current = this.segments[this.segments.length - 1]; + var current = this._segments[this._segments.length - 1]; var x1 = current.point.x; var y1 = current.point.y; cubicCurveTo( @@ -128,7 +143,7 @@ PathItem = Item.extend(new function() { to = new Point(to); if(parameter == null) parameter = 0.5; - var current = this.segments[this.segments.length - 1]; + var current = this._segments[this._segments.length - 1]; // handle = (through - (1 - t)^2 * current - t^2 * to) / (2 * (1 - t) * t) var t1 = 1 - parameter; var handle = through.subtract( @@ -150,7 +165,7 @@ PathItem = Item.extend(new function() { } else { if(clockwise === null) clockwise = true; - var current = this.segments[this.segments.length - 1].point; + var current = this._segments[this._segments.length - 1].point; var middle = current.add(to).divide(2); var step = middle.subtract(current); through = clockwise @@ -159,7 +174,7 @@ PathItem = Item.extend(new function() { } // Get the start point: - var current = this.segments[this.segments.length - 1]; + var current = this._segments[this._segments.length - 1]; var x1 = current.point.x, x2 = through.x, x3 = to.x; var y1 = current.point.y, y2 = through.y, y3 = to.y; @@ -235,13 +250,13 @@ PathItem = Item.extend(new function() { lineBy: function() { var vector = Point.read(arguments); if(vector) { - var current = this.segments[this.segments.length - 1]; + var current = this._segments[this._segments.length - 1]; this.lineTo(current.point.add(vector)); } }, smooth: function() { - var segments = this.segments; + var segments = this._segments; // This code is based on the work by Oleg V. Polikarpotchkin, // http://ov-p.spaces.live.com/blog/cns!39D56F0C7A08D703!147.entry @@ -334,7 +349,7 @@ PathItem = Item.extend(new function() { } } if (closed && handleIn != null) { - var segment = this.segments[0]; + var segment = this._segments[0]; segment.handleIn = handleIn.subtract(segment.point); } }, @@ -342,14 +357,14 @@ PathItem = Item.extend(new function() { curveBy: function(throughVector, toVector, parameter) { throughVector = Point.read(throughVector); toVector = Point.read(toVector); - var current = this.segments[this.segments.length - 1].point; + var current = this._segments[this._segments.length - 1].point; this.curveTo(current.add(throughVector), current.add(toVector), parameter); }, arcBy: function(throughVector, toVector) { throughVector = Point.read(throughVector); toVector = Point.read(toVector); - var current = this.segments[this.segments.length - 1].point; + var current = this._segments[this._segments.length - 1].point; this.arcBy(current.add(throughVector), current.add(toVector)); }, @@ -365,8 +380,8 @@ PathItem = Item.extend(new function() { if(!this.visible) return; ctx.beginPath(); var cp1; - for(var i = 0, l = this.segments.length; i < l; i++) { - var segment = this.segments[i]; + for(var i = 0, l = this._segments.length; i < l; i++) { + var segment = this._segments[i]; var point = segment.point; var handleIn = segment.handleIn ? segment.handleIn.add(point) : point; var handleOut = segment.handleOut ? segment.handleOut.add(point) : point; @@ -378,8 +393,8 @@ PathItem = Item.extend(new function() { } cp1 = handleOut; } - if(this.closed && this.segments.length > 1) { - var segment = this.segments[0]; + if(this.closed && this._segments.length > 1) { + var segment = this._segments[0]; var point = segment.point; var handleIn = segment.handleIn ? segment.handleIn.add(point) : point; ctx.bezierCurveTo(cp1.x, cp1.y, handleIn.x, handleIn.y, diff --git a/src/path/Segment.js b/src/path/Segment.js index ef6e67d5..0ace1332 100644 --- a/src/path/Segment.js +++ b/src/path/Segment.js @@ -32,8 +32,8 @@ Segment = Base.extend({ // TODO: // insert: function() { - // if(this.segments && this.segments.path) { - // var path = this.segments.path; + // if(this._segments && this._segments.path) { + // var path = this._segments.path; // path.checkValid(); // // } @@ -68,7 +68,7 @@ Segment = Base.extend({ }, getIndex: function() { - var segments = this.path.segments; + var segments = this.path._segments; for(var i = 0, l = segments.length; i < l; i++) { if(segments[i] == this) return i; @@ -81,8 +81,8 @@ Segment = Base.extend({ // todo // getCurve: function() { - // if(this.segments && this.segments.path) { - // var curves = this.segments.path.getCurves(); + // if(this._segments && this._segments.path) { + // var curves = this._segments.path.getCurves(); // // The curves list handles closing curves, so the curves.size // // is adjusted accordingly. just check to be in the boundaries here: // return index < curves.length ? curves[index] : null; @@ -91,12 +91,13 @@ Segment = Base.extend({ getNext: function() { var index = this.index; - return this.path && index < this.path.segments.length - 1 - ? this.path.segments[index + 1] : null; + return this.path && index < this.path._segments.length - 1 + ? this.path._segments[index + 1] : null; }, getPrevious: function() { - return this.path != null && index > 0 ? this.segments[this.index - 1] : null; + return this.path != null && index > 0 + ? this.path._segments[this.index - 1] : null; }, // todo @@ -115,8 +116,8 @@ Segment = Base.extend({ }, remove: function() { - if(this.path && this.path.segments) - return this.path.segments.splice(this.index, 1); + if(this.path && this.path._segments) + return this.path._segments.splice(this.index, 1); return false; },