Merge remote branch 'origin/master'

This commit is contained in:
Jonathan Puckey 2011-05-05 11:42:35 +01:00
commit 19a767e6f6
23 changed files with 151 additions and 186 deletions

View file

@ -1,20 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Example</title>
<script type="text/javascript">var root = '../../'</script>
<script type="text/javascript" src="../../src/load.js"></script>
<script type="text/paperscript" canvas="canvas">
var path = new Path.Circle(new Point(), 100);
path.strokeWidth = 30;
path.fillColor = null;
path.strokeColor = 'black';
var symbol = new PlacedSymbol(path);
symbol.position = new Point(200, 200);
symbol.scale(1, 0.5);
</script>
</head>
<body>
<canvas id='canvas' width=1024 height=768></canvas>
</body>

View file

@ -38,7 +38,6 @@
path.strokeWidth = 30;
path.strokeJoin = 'miter';
path.strokeCap = 'butt';
console.log(path.segments);
</script>
</head>
<body>

View file

@ -193,9 +193,11 @@ var Matrix = this.Matrix = Base.extend({
* @return {string} A string representation of this transform.
*/
toString: function() {
return '[['
+ [this._m00, this._m01, this._m02].join(', ') + '], ['
+ [this._m10, this._m11, this._m12].join(', ') + ']]';
var format = Base.formatNumber;
return '[[' + [format(this._m00), format(this._m01),
format(this._m02)].join(', ') + '], ['
+ [format(this._m10), format(this._m11),
format(this._m12)].join(', ') + ']]';
},
/**

View file

@ -454,7 +454,8 @@ var Point = this.Point = Base.extend({
},
toString: function() {
return '{ x: ' + this.x + ', y: ' + this.y + ' }';
var format = Base.formatNumber;
return '{ x: ' + format(this.x) + ', y: ' + format(this.y) + ' }';
},
statics: {

View file

@ -221,10 +221,11 @@ var Rectangle = this.Rectangle = Base.extend({
},
toString: function() {
return '{ x: ' + this.x
+ ', y: ' + this.y
+ ', width: ' + this.width
+ ', height: ' + this.height
var format = Base.formatNumber;
return '{ x: ' + format(this.x)
+ ', y: ' + format(this.y)
+ ', width: ' + format(this.width)
+ ', height: ' + format(this.height)
+ ' }';
},

View file

@ -86,7 +86,9 @@ var Size = this.Size = Base.extend({
},
toString: function() {
return '{ x: ' + this.width + ', y: ' + this.height + ' }';
var format = Base.formatNumber;
return '{ x: ' + format(this.width)
+ ', y: ' + format(this.height) + ' }';
},
statics: {

View file

@ -161,7 +161,7 @@ var Color = this.Color = Base.extend(new function() {
* @return {@true if the GrayColor is the same}
*/
equals: function(color) {
if (color._colorType == this._colorType) {
if (color && color._colorType == this._colorType) {
for (var i = 0, l = this._components; i < l; i++) {
var component = '_' + this._components[i];
if (this[component] !== color[component])
@ -173,13 +173,14 @@ var Color = this.Color = Base.extend(new function() {
},
toString: function() {
var parts = [];
var parts = [],
format = Base.formatNumber;
for (var i = 0, l = this._components.length; i < l; i++) {
var component = this._components[i];
var value = this['_' + component];
var component = this._components[i],
value = this['_' + component];
if (component === 'alpha' && value == null)
value = 1;
parts.push(component + ': ' + value);
parts.push(component + ': ' + format(value));
}
return '{ ' + parts.join(', ') + ' }';
},

View file

@ -62,7 +62,7 @@ var Document = this.Document = Base.extend({
},
setCurrentStyle: function(style) {
this._currentStyle = new PathStyle(this, style);
this._currentStyle = PathStyle.create(this, style);
},
activate: function() {

View file

@ -439,9 +439,10 @@ var Item = this.Item = Base.extend({
_getBounds: function(includeStroke) {
var children = this.children;
if (children && children.length) {
var x1, x2;
var y1 = x1 = Infinity;
var y2 = x2 = -Infinity;
var x1 = Infinity,
x2 = -Infinity,
y1 = x1,
y2 = x2;
for (var i = 0, l = children.length; i < l; i++) {
var child = children[i],
rect = includeStroke
@ -673,7 +674,7 @@ var Item = this.Item = Base.extend({
},
setStyle: function(style) {
this._style = new PathStyle(this, style);
this._style = PathStyle.create(this, style);
},
// TODO: toString

View file

@ -22,43 +22,69 @@ var PathStyle = this.PathStyle = Base.extend(new function() {
var fields = {
beans: true,
initialize: function(item, style) {
this._item = item;
initialize: function(style) {
if (style) {
for (var i = 0, l = keys.length; i < l; i++) {
var key = keys[i];
if (style[key] !== undefined)
this[key] = style[key];
var key = keys[i],
value = style[key];
if (value !== undefined)
this[key] = value;
}
}
},
clone: function() {
return new PathStyle(this);
},
statics: {
create: function(item, other) {
var style = new PathStyle(PathStyle.dont);
style._item = item;
style.initialize(other);
return style;
}
}
};
Item.inject(Base.each(keys, function(key) {
var isColor = !!(key.match(/Color$/)),
set = 'set' + Base.capitalize(key),
get = 'get' + Base.capitalize(key);
var isColor = !!key.match(/Color$/),
part = Base.capitalize(key),
set = 'set' + part,
get = 'get' + part;
fields[set] = function(value) {
if (this._item && this._item.children) {
for (var i = 0, l = this._item.children.length; i < l; i++) {
this._item.children[i]._style[set](value);
}
var children = this._item && this._item.children;
value = isColor ? Color.read(arguments) : value;
if (children) {
for (var i = 0, l = children.length; i < l; i++)
children[i]._style[set](value);
} else {
this['_' + key] = isColor ? Color.read(arguments) : value;
var old = this['_' + key];
if (old != value && !(old && old.equals && old.equals(value))) {
this['_' + key] = value;
// TODO: Tell _item what exactly has changed. Maybe introduce
// ChangeFlags, e.g. STROKE, COLOR, FILL, GEOMETRY, etc?
if (this._item && this._item._changed)
this._item._changed();
}
}
return this;
};
fields[get] = function() {
if (this._item && this._item.children) {
var style;
for (var i = 0, l = this._item.children.length; i < l; i++) {
var childStyle = this._item.children[i]._style[get]();
var children = this._item && this._item.children,
style;
// If this item has children, walk through all of them and see if
// they all have the same style.
if (children) {
for (var i = 0, l = children.length; i < l; i++) {
var childStyle = children[i]._style[get]();
if (!style) {
style = childStyle;
} else if (style != childStyle) {
// If there is another item with a different style:
// If there is another item with a different style,
// the style is not defined:
return undefined;
}
}
@ -68,6 +94,8 @@ var PathStyle = this.PathStyle = Base.extend(new function() {
}
};
// 'this' = the Base.each() side-car = the object that is injected into
// Item above:
this[set] = function(value) {
this._style[set](value);
return this;

View file

@ -43,7 +43,7 @@ var PlacedSymbol = this.PlacedSymbol = Item.extend({
},
getBounds: function() {
var bounds = this.symbol._definition.getStrokeBounds(this.matrix, true);
var bounds = this.symbol._definition.getStrokeBounds(this.matrix);
return LinkedRectangle.create(this, 'setBounds',
bounds.x, bounds.y, bounds.width, bounds.height);
},

View file

@ -83,6 +83,10 @@ Base.inject({
return str.replace(/\b[a-z]/g, function(match) {
return match.toUpperCase();
});
},
formatNumber: function(num) {
return (Math.round(num * 100000) / 100000).toString();
}
});

View file

@ -129,8 +129,8 @@ CurveLocation = Base.extend({
},
toString: function() {
var parts = [];
var point = this.getPoint();
var parts = [],
point = this.getPoint();
if (point)
parts.push('point: ' + point);
var index = this.getIndex();
@ -138,7 +138,7 @@ CurveLocation = Base.extend({
parts.push('index: ' + index);
var parameter = this.getParameter();
if (parameter != null)
parts.push('parameter: ' + parameter);
parts.push('parameter: ' + Base.formatNumber(parameter));
return '{ ' + parts.join(', ') + ' }';
}
});

View file

@ -52,7 +52,7 @@ var Path = this.Path = PathItem.extend({
if (this._curves)
this._curves = null;
}
this._add(Segment.readAll(segments));
this._add(Segment.readAll(segments), true);
},
getFirstSegment: function() {
@ -136,7 +136,7 @@ var Path = this.Path = PathItem.extend({
* If a curves list was requested, it will kept in sync with the segments
* list automatically.
*/
_add: function(segs, index) {
_add: function(segs, notify, index) {
// Local short-cuts:
var segments = this._segments,
curves = this._curves,
@ -175,18 +175,19 @@ var Path = this.Path = PathItem.extend({
if (curve)
curve._segment1 = segments[index + amount];
}
this._changed();
if (notify)
this._changed();
return segs;
},
// TODO: Port back support for adding multiple segments at once to Sg
add: function(segment1 /*, segment2, ... */) {
return this._add(Segment.readAll(arguments));
return this._add(Segment.readAll(arguments), true);
},
// TODO: Port back support for adding multiple segments at once to Sg
insert: function(index, segment1 /*, segment2, ... */) {
return this._add(Segment.readAll(arguments, 1), index);
return this._add(Segment.readAll(arguments, 1), true, index);
},
// TODO: Port back to Sg
@ -310,6 +311,7 @@ var Path = this.Path = PathItem.extend({
if (last1._point.equals(first1._point)) {
first1.setHandleIn(last1._handleIn);
last1.remove();
// TODO: Don't notify in setClosed... Use internal _setClosed?
this.setClosed(true);
}
this._changed();
@ -441,8 +443,8 @@ var Path = this.Path = PathItem.extend({
ctx.lineTo(handleX, handleY);
ctx.stroke();
ctx.beginPath();
ctx.rect(handleX - 1, handleY - 1, 2, 2);
ctx.stroke();
ctx.arc(handleX, handleY, 1.75, 0, Math.PI * 2, true);
ctx.fill();
}
}
@ -679,12 +681,12 @@ var Path = this.Path = PathItem.extend({
// Let's not be picky about calling moveTo() when not at the
// beginning of a path, just bail out:
if (!this._segments.length)
this._add([new Segment(Point.read(arguments))]);
this._add([ new Segment(Point.read(arguments)) ], true);
},
lineTo: function() {
// Let's not be picky about calling moveTo() first:
this._add([new Segment(Point.read(arguments))]);
this._add([ new Segment(Point.read(arguments)) ], true);
},
/**
@ -700,7 +702,7 @@ var Path = this.Path = PathItem.extend({
// Convert to relative values:
current.setHandleOut(handle1.subtract(current._point));
// And add the new segment, with handleIn set to c2
this._add([new Segment(to, handle2.subtract(to), new Point())]);
this._add([ new Segment(to, handle2.subtract(to)) ], true);
},
/**
@ -835,7 +837,7 @@ var Path = this.Path = PathItem.extend({
angle += inc;
}
// Add all segments at once at the end for higher performance
this._add(segments);
this._add(segments, true);
},
lineBy: function(vector) {
@ -1011,10 +1013,13 @@ var Path = this.Path = PathItem.extend({
* @param matrix optional
*/
getBounds: function(/* matrix */) {
var useCache = arguments.length == 0;
// Pass the matrix hidden from Bootstrap, so it still inject
// getBounds as bean too.
if (!this._bounds) {
if (!useCache || !this._bounds) {
var bounds = getBounds(this, arguments[0]);
if (!useCache)
return bounds;
this._bounds = LinkedRectangle.create(this, 'setBounds',
bounds.x, bounds.y, bounds.width, bounds.height);
}
@ -1025,6 +1030,9 @@ var Path = this.Path = PathItem.extend({
* The bounding rectangle of the item including stroke width.
*/
getStrokeBounds: function(/* matrix */) {
var useCache = arguments.length == 0;
if (this._strokeBounds && useCache)
return this._strokeBounds;
var matrix = arguments[0], // set #getBounds()
width = this.getStrokeWidth(),
radius = width / 2,
@ -1124,7 +1132,8 @@ var Path = this.Path = PathItem.extend({
addCap(segments[0], cap, 0);
addCap(segments[length - 1], cap, 1);
}
if (useCache)
this._strokeBounds = bounds;
return bounds;
},

View file

@ -42,10 +42,10 @@ var ToolEvent = this.ToolEvent = Base.extend({
toString: function() {
return '{ type: ' + this.type
+ ', point: ' + this.point
+ ', count: ' + this.count
+ ', modifiers: ' + this.modifiers
+ ' }';
+ ', point: ' + this.point
+ ', count: ' + this.count
+ ', modifiers: ' + this.modifiers
+ ' }';
},
/**

View file

@ -1,25 +1,3 @@
function compareSegmentLists(list1, list2) {
equals(list1.length, list2.length, 'segment count');
if (list1.length == list2.length) {
for (var i = 0, l = list1.length; i < l; i++) {
compareSegments(list1[i], list2[i]);
}
}
}
function compareSegments(segment1, segment2) {
// Convert comparison value through Segment.read, to not have to provide
// all handles all the time.
segment2 = Segment.read([segment2]);
var points = ['point', 'handleIn', 'handleOut'];
for (var i = 0; i < 3; i++) {
equals(!!segment1[points[i]], !!segment2[points[i]],
'have ' + points[i]);
if (segment1[points[i]] && segment2[points[i]])
comparePoints(segment1[points[i]], segment2[points[i]], points[i]);
}
}
function compareNumbers(number1, number2, message) {
if (number1 !== 0)
number1 = Math.round(number1 * 100) / 100;

View file

@ -11,8 +11,7 @@ test('path.join(path)', function() {
path2.add([20, 10]);
path.join(path2);
compareSegmentLists(path.segments, [new Segment(new Point(0, 0)),
new Segment(new Point(10, 0)), new Segment(new Point(20, 10))]);
equals(path.segments.toString(), '{ point: { x: 0, y: 0 } },{ point: { x: 10, y: 0 } },{ point: { x: 20, y: 10 } }');
equals(doc.activeLayer.children.length, 1);
var path = new Path();
@ -23,8 +22,7 @@ test('path.join(path)', function() {
path2.add([20, 10]);
path2.add([10, 0]);
path.join(path2);
compareSegmentLists(path.segments, [new Segment(new Point(0, 0)),
new Segment(new Point(10, 0)), new Segment(new Point(20, 10))]);
equals(path.segments.toString(), '{ point: { x: 0, y: 0 } },{ point: { x: 10, y: 0 } },{ point: { x: 20, y: 10 } }');
var path = new Path();
path.add([0, 0]);
@ -34,10 +32,8 @@ test('path.join(path)', function() {
path2.add([30, 10]);
path2.add([40, 0]);
path.join(path2);
compareSegmentLists(path.segments, [new Segment(new Point(0, 0)),
new Segment(new Point(10, 0)), new Segment(new Point(30, 10)),
new Segment(new Point(40, 0))]);
equals(path.segments.toString(), '{ point: { x: 0, y: 0 } },{ point: { x: 10, y: 0 } },{ point: { x: 30, y: 10 } },{ point: { x: 40, y: 0 } }');
var path = new Path();
path.add([0, 0]);
path.add([10, 0]);
@ -49,9 +45,8 @@ test('path.join(path)', function() {
path2.add([20, 10]);
path.join(path2);
compareSegmentLists(path.segments, [new Segment(new Point(0, 0)),
new Segment(new Point(10, 0)), new Segment(new Point(20, 10)),
new Segment(new Point(10, 5))]);
equals(path.segments.toString(), '{ point: { x: 0, y: 0 } },{ point: { x: 10, y: 0 } },{ point: { x: 20, y: 10 } },{ point: { x: 10, y: 5 } }');
equals(path.closed, true);
});
@ -94,12 +89,6 @@ test('Path#reverse', function() {
var doc = new Document();
var path = new Path.Circle([100, 100], 30);
path.reverse();
compareSegmentLists(path.segments, [new Segment(new Point(100, 130),
new Point(-16.568359375, 0), new Point(16.568359375, 0)),
new Segment(new Point(130, 100), new Point(0, 16.568359375),
new Point(0, -16.568359375)), new Segment(new Point(100, 70),
new Point(16.568359375, 0), new Point(-16.568359375, 0)),
new Segment(new Point(70, 100), new Point(0, -16.568359375),
new Point(0, 16.568359375))]);
equals(path.segments.toString(), '{ point: { x: 100, y: 130 }, handleIn: { x: -16.56854, y: 0 }, handleOut: { x: 16.56854, y: 0 } },{ point: { x: 130, y: 100 }, handleIn: { x: 0, y: 16.56854 }, handleOut: { x: 0, y: -16.56854 } },{ point: { x: 100, y: 70 }, handleIn: { x: 16.56854, y: 0 }, handleOut: { x: -16.56854, y: 0 } },{ point: { x: 70, y: 100 }, handleIn: { x: 0, y: -16.56854 }, handleOut: { x: 0, y: 16.56854 } }');
});

View file

@ -27,11 +27,11 @@ test('path.bounds', function() {
// Set new bounds and check segment list as result of resizing / positioning
path.bounds = { x: 100, y: 100, width: 200, height: 200 };
compareSegmentLists(path.segments, [{ point: { x: 107.93066, y: 179.56982 }, handleIn: { x: -24.41211, y: 51.30664 }, handleOut: { x: 39.52734, y: -83.08447 } }, { point: { x: 271.10107, y: 160.66553 }, handleIn: { x: -53.96289, y: -99.9126 }, handleOut: { x: 53.96143, y: 99.91406 } }, { point: { x: 215.85303, y: 296.96045 }, handleIn: { x: 85.81299, y: -17.18555 }, handleOut: { x: -101.49854, y: 20.32861 } }])
equals(path.segments.toString(), '{ point: { x: 107.93077, y: 179.56917 }, handleIn: { x: -24.41127, y: 51.30707 }, handleOut: { x: 39.52904, y: -83.08194 } },{ point: { x: 271.10084, y: 160.66656 }, handleIn: { x: -53.96176, y: -99.91377 }, handleOut: { x: 53.96176, y: 99.91377 } },{ point: { x: 215.85428, y: 296.96086 }, handleIn: { x: 85.81084, y: -17.18521 }, handleOut: { x: -101.49949, y: 20.32729 } }');
// Now rotate by 40 degrees and test bounds and segments again.
path.rotate(40);
compareRectangles(path.bounds, { x: 92.38109, y: 106.78957, width: 191.4803, height: 203.66878 });
compareSegmentLists(path.segments, [{ point: { x: 142.60352, y: 125.16797 }, handleIn: { x: -51.67969, y: 23.6123 }, handleOut: { x: 83.68555, y: -38.23584 } }, { point: { x: 279.74902, y: 215.57178 }, handleIn: { x: 22.88672, y: -111.22461 }, handleOut: { x: -22.88623, y: 111.22412 } }, { point: { x: 149.82031, y: 284.4668 }, handleIn: { x: 76.78076, y: 41.99414 }, handleOut: { x: -90.81982, y: -49.66992 } }]);
equals(path.segments.toString(), '{ point: { x: 142.60356, y: 125.16811 }, handleIn: { x: -51.67967, y: 23.61224 }, handleOut: { x: 83.68504, y: -38.23568 } },{ point: { x: 279.74945, y: 215.57158 }, handleIn: { x: 22.88623, y: -111.22434 }, handleOut: { x: -22.88623, y: 111.22434 } },{ point: { x: 149.81984, y: 284.46726 }, handleIn: { x: 76.78135, y: 41.99351 }, handleOut: { x: -90.81925, y: -49.67101 } }');
});

View file

@ -6,22 +6,22 @@ test('path.curves Synchronisation', function() {
var path = new Path();
path.add(new Point(0, 100));
equals(path.segments.toString(), '{ point: { x: 0, y: 100 } }', 'path.segments: path.add(new Point(0, 100));');
equals(path.curves.toString(), '', 'path.curves: path.add(new Point(0, 100));');
equals(path.segments.toString(), "{ point: { x: 0, y: 100 } }", "path.segments: path.add(new Point(0, 100));");
equals(path.curves.toString(), "", "path.curves: path.add(new Point(0, 100));");
path.add(new Point(100, 100));
equals(path.segments.toString(), '{ point: { x: 0, y: 100 } },{ point: { x: 100, y: 100 } }', 'path.segments: path.add(new Point(100, 100));');
equals(path.curves.toString(), '{ point1: { x: 0, y: 100 }, point2: { x: 100, y: 100 } }', 'path.curves: path.add(new Point(100, 100));');
equals(path.segments.toString(), "{ point: { x: 0, y: 100 } },{ point: { x: 100, y: 100 } }", "path.segments: path.add(new Point(100, 100));");
equals(path.curves.toString(), "{ point1: { x: 0, y: 100 }, point2: { x: 100, y: 100 } }", "path.curves: path.add(new Point(100, 100));");
path.insert(1, {point:[50, 0], handleIn:[-25, 0], handleOut:[25, 0]});
equals(path.segments.toString(), '{ point: { x: 0, y: 100 } },{ point: { x: 50, y: 0 }, handleIn: { x: -25, y: 0 }, handleOut: { x: 25, y: 0 } },{ point: { x: 100, y: 100 } }', 'path.segments: path.insert(1, {point:[50, 0], handleIn:[-25, 0], handleOut:[25, 0]});');
equals(path.curves.toString(), '{ point1: { x: 0, y: 100 }, handle2: { x: -25, y: 0 }, point2: { x: 50, y: 0 } },{ point1: { x: 50, y: 0 }, handle1: { x: 25, y: 0 }, point2: { x: 100, y: 100 } }', 'path.curves: path.insert(1, {point:[50, 0], handleIn:[-25, 0], handleOut:[25, 0]});');
equals(path.segments.toString(), "{ point: { x: 0, y: 100 } },{ point: { x: 50, y: 0 }, handleIn: { x: -25, y: 0 }, handleOut: { x: 25, y: 0 } },{ point: { x: 100, y: 100 } }", "path.segments: path.insert(1, {point:[50, 0], handleIn:[-25, 0], handleOut:[25, 0]});");
equals(path.curves.toString(), "{ point1: { x: 0, y: 100 }, handle2: { x: -25, y: 0 }, point2: { x: 50, y: 0 } },{ point1: { x: 50, y: 0 }, handle1: { x: 25, y: 0 }, point2: { x: 100, y: 100 } }", "path.curves: path.insert(1, {point:[50, 0], handleIn:[-25, 0], handleOut:[25, 0]});");
path.closed = true;
equals(path.segments.toString(), '{ point: { x: 0, y: 100 } },{ point: { x: 50, y: 0 }, handleIn: { x: -25, y: 0 }, handleOut: { x: 25, y: 0 } },{ point: { x: 100, y: 100 } }', 'path.segments: path.closed = true;');
equals(path.curves.toString(), '{ point1: { x: 0, y: 100 }, handle2: { x: -25, y: 0 }, point2: { x: 50, y: 0 } },{ point1: { x: 50, y: 0 }, handle1: { x: 25, y: 0 }, point2: { x: 100, y: 100 } },{ point1: { x: 100, y: 100 }, point2: { x: 0, y: 100 } }', 'path.curves: path.closed = true;');
equals(path.segments.toString(), "{ point: { x: 0, y: 100 } },{ point: { x: 50, y: 0 }, handleIn: { x: -25, y: 0 }, handleOut: { x: 25, y: 0 } },{ point: { x: 100, y: 100 } }", "path.segments: path.closed = true;");
equals(path.curves.toString(), "{ point1: { x: 0, y: 100 }, handle2: { x: -25, y: 0 }, point2: { x: 50, y: 0 } },{ point1: { x: 50, y: 0 }, handle1: { x: 25, y: 0 }, point2: { x: 100, y: 100 } },{ point1: { x: 100, y: 100 }, point2: { x: 0, y: 100 } }", "path.curves: path.closed = true;");
path.removeSegments(2, 3);
equals(path.segments.toString(), '{ point: { x: 0, y: 100 } },{ point: { x: 50, y: 0 }, handleIn: { x: -25, y: 0 }, handleOut: { x: 25, y: 0 } }', 'path.segments: path.removeSegments(2, 3);');
equals(path.curves.toString(), '{ point1: { x: 0, y: 100 }, handle2: { x: -25, y: 0 }, point2: { x: 50, y: 0 } },{ point1: { x: 50, y: 0 }, handle1: { x: 25, y: 0 }, point2: { x: 0, y: 100 } }', 'path.curves: path.removeSegments(2, 3);');
equals(path.segments.toString(), "{ point: { x: 0, y: 100 } },{ point: { x: 50, y: 0 }, handleIn: { x: -25, y: 0 }, handleOut: { x: 25, y: 0 } }", "path.segments: path.removeSegments(2, 3);");
equals(path.curves.toString(), "{ point1: { x: 0, y: 100 }, handle2: { x: -25, y: 0 }, point2: { x: 50, y: 0 } },{ point1: { x: 50, y: 0 }, handle1: { x: 25, y: 0 }, point2: { x: 0, y: 100 } }", "path.curves: path.removeSegments(2, 3);");
path.add(new Point(100, 100));
path.removeSegments(1, 2);
equals(path.segments.toString(), '{ point: { x: 0, y: 100 } },{ point: { x: 100, y: 100 } }', 'path.segments: path.add(new Point(100, 100));\n path.removeSegments(1, 2);');
equals(path.curves.toString(), '{ point1: { x: 0, y: 100 }, point2: { x: 100, y: 100 } },{ point1: { x: 100, y: 100 }, point2: { x: 0, y: 100 } }', 'path.curves: path.add(new Point(100, 100));\n path.removeSegments(1, 2);');
equals(path.segments.toString(), "{ point: { x: 0, y: 100 } },{ point: { x: 100, y: 100 } }", "path.segments: path.add(new Point(100, 100));\npath.removeSegments(1, 2);");
equals(path.curves.toString(), "{ point1: { x: 0, y: 100 }, point2: { x: 100, y: 100 } },{ point1: { x: 100, y: 100 }, point2: { x: 0, y: 100 } }", "path.curves: path.add(new Point(100, 100));\npath.removeSegments(1, 2);");
});

View file

@ -4,14 +4,12 @@ test('path.lineTo(point);', function() {
var path = new Path();
path.moveTo([50, 50]);
path.lineTo([100, 100]);
var expectedSegments = [{ point: { x: 50, y: 50 } }, { point: { x: 100, y: 100 } }];
compareSegmentLists(path.segments, expectedSegments);
equals(path.segments.toString(), '{ point: { x: 50, y: 50 } },{ point: { x: 100, y: 100 } }');
});
test('path.arcTo(from, through, to);', function() {
var path = new Path();
path.moveTo([50, 50]);
path.arcTo([100, 100], [75, 75]);
var expectedSegments = [{ point: { x: 50, y: 50 }, handleOut: { x: 10.11156, y: -10.11156 } }, { point: { x: 88.5299, y: 42.33593 }, handleIn: { x: -13.21138, y: -5.47233 }, handleOut: { x: 13.21138, y: 5.47233 } }, { point: { x: 110.35534, y: 75 }, handleIn: { x: 0, y: -14.2999 } }];
compareSegmentLists(path.segments, expectedSegments);
equals(path.segments.toString(), '{ point: { x: 50, y: 50 }, handleOut: { x: 10.11156, y: -10.11156 } },{ point: { x: 88.5299, y: 42.33593 }, handleIn: { x: -13.21138, y: -5.47233 }, handleOut: { x: 13.21138, y: 5.47233 } },{ point: { x: 110.35534, y: 75 }, handleIn: { x: 0, y: -14.2999 } }');
});

View file

@ -6,30 +6,11 @@ test('path.length', function() {
new Segment(new Point(121, 334), new Point(-19, 38), new Point(30.7666015625, -61.53369140625)),
new Segment(new Point(248, 320), new Point(-42, -74), new Point(42, 74))
]);
var t = Date.now(), c = 1000;
for (var i = 0; i < c; i++) {
var length = path.length;
}
window.console.log(Date.now() - t, length);
// ai: 172.10122680664062
// 8: 172.10094440399325
// 7: 172.10190407418446
// 6: 172.09823801587845
// 5: 172.1100076851322
var length = path.length;
compareNumbers(length, 172.10122680664062);
var curve = path.curves[0];
var t = Date.now(), c = 1000;
for (var i = 0; i < c; i++) {
var param = curve.getParameter(length / 4);
}
window.console.log(Date.now() - t, param);
var param = curve.getParameter(length / 4);
// ai: 0.2255849553116685
// 8: 0.22558507711602457
// 5: 0.22558507714028128
// 4: 0.22558508917324532
var param = path.curves[0].getParameter(length / 4);
compareNumbers(param, 0.2255849553116685);
});

View file

@ -2,62 +2,61 @@ module('Predefined Path Shapes');
test('new Path.Rectangle([50, 50], [100, 100])', function() {
var path = new Path.Rectangle([50, 50], [100, 100]);
var expectedSegments = [new Segment(new Point(50, 150)), new Segment(new Point(50, 50)), new Segment(new Point(150, 50)), new Segment(new Point(150, 150))];
compareSegmentLists(path.segments, expectedSegments);
equals(path.segments.toString(), '{ point: { x: 50, y: 150 } },{ point: { x: 50, y: 50 } },{ point: { x: 150, y: 50 } },{ point: { x: 150, y: 150 } }');
});
test('new Path.Circle([100, 100], 50)', function() {
var path = new Path.Circle([100, 100], 50);
var expectedSegments = [new Segment(new Point(50, 100), new Point(0, 27.6142578125), new Point(0, -27.6142578125)), new Segment(new Point(100, 50), new Point(-27.6142578125, 0), new Point(27.6142578125, 0)), new Segment(new Point(150, 100), new Point(0, -27.6142578125), new Point(0, 27.6142578125)), new Segment(new Point(100, 150), new Point(27.6142578125, 0), new Point(-27.6142578125, 0))];
compareSegmentLists(path.segments, expectedSegments);
equals(path.segments.toString(), '{ point: { x: 50, y: 100 }, handleIn: { x: 0, y: 27.61424 }, handleOut: { x: 0, y: -27.61424 } },{ point: { x: 100, y: 50 }, handleIn: { x: -27.61424, y: 0 }, handleOut: { x: 27.61424, y: 0 } },{ point: { x: 150, y: 100 }, handleIn: { x: 0, y: -27.61424 }, handleOut: { x: 0, y: 27.61424 } },{ point: { x: 100, y: 150 }, handleIn: { x: 27.61424, y: 0 }, handleOut: { x: -27.61424, y: 0 } }');
});
test('new Path.Oval(rect)', function() {
var rect = new Rectangle([500, 500], [1000, 750])
var path = new Path.Oval(rect);
var expectedSegments = [{ point: { x: 500, y: 875 }, handleIn: { x: 0, y: 207.10645 }, handleOut: { x: 0, y: -207.10645 } }, { point: { x: 1000, y: 500 }, handleIn: { x: -276.14258, y: 0 }, handleOut: { x: 276.14258, y: 0 } }, { point: { x: 1500, y: 875 }, handleIn: { x: 0, y: -207.10645 }, handleOut: { x: 0, y: 207.10645 } }, { point: { x: 1000, y: 1250 }, handleIn: { x: 276.14258, y: 0 }, handleOut: { x: -276.14258, y: 0 } }];
compareSegmentLists(path.segments, expectedSegments);
equals(path.segments.toString(), '{ point: { x: 500, y: 875 }, handleIn: { x: 0, y: 207.10678 }, handleOut: { x: 0, y: -207.10678 } },{ point: { x: 1000, y: 500 }, handleIn: { x: -276.14237, y: 0 }, handleOut: { x: 276.14237, y: 0 } },{ point: { x: 1500, y: 875 }, handleIn: { x: 0, y: -207.10678 }, handleOut: { x: 0, y: 207.10678 } },{ point: { x: 1000, y: 1250 }, handleIn: { x: 276.14237, y: 0 }, handleOut: { x: -276.14237, y: 0 } }');
});
test('new Path.RoundRectangle(rect, size)', function() {
var rect = new Rectangle([50, 50], [200, 100])
var path = new Path.RoundRectangle(rect, 20);
var expectedSegments = [{ point: { x: 70, y: 150 }, handleOut: { x: -11.0459, y: 0 } }, { point: { x: 50, y: 130 }, handleIn: { x: 0, y: 11.0459 } }, { point: { x: 50, y: 70 }, handleOut: { x: 0, y: -11.0459 } }, { point: { x: 70, y: 50 }, handleIn: { x: -11.0459, y: 0 } }, { point: { x: 230, y: 50 }, handleOut: { x: 11.0459, y: 0 } }, { point: { x: 250, y: 70 }, handleIn: { x: 0, y: -11.0459 } }, { point: { x: 250, y: 130 }, handleOut: { x: 0, y: 11.0459 } }, { point: { x: 230, y: 150 }, handleIn: { x: 11.0459, y: 0 } }];
compareSegmentLists(path.segments, expectedSegments);
equals(path.segments.toString(), '{ point: { x: 70, y: 150 }, handleOut: { x: -11.04569, y: 0 } },{ point: { x: 50, y: 130 }, handleIn: { x: 0, y: 11.04569 } },{ point: { x: 50, y: 70 }, handleOut: { x: 0, y: -11.04569 } },{ point: { x: 70, y: 50 }, handleIn: { x: -11.04569, y: 0 } },{ point: { x: 230, y: 50 }, handleOut: { x: 11.04569, y: 0 } },{ point: { x: 250, y: 70 }, handleIn: { x: 0, y: -11.04569 } },{ point: { x: 250, y: 130 }, handleOut: { x: 0, y: 11.04569 } },{ point: { x: 230, y: 150 }, handleIn: { x: 11.04569, y: 0 } }');
});
test('new Path.RoundRectangle(rect, size) - too large size', function() {
var rect = new Rectangle([50, 50], [200, 100])
var path = new Path.RoundRectangle(rect, 200);
var expectedSegments = [{ point: { x: 150, y: 150 }, handleOut: { x: -55.22852, y: 0 } }, { point: { x: 50, y: 100 }, handleIn: { x: 0, y: 27.61426 } }, { point: { x: 50, y: 100 }, handleOut: { x: 0, y: -27.61426 } }, { point: { x: 150, y: 50 }, handleIn: { x: -55.22852, y: 0 } }, { point: { x: 150, y: 50 }, handleOut: { x: 55.22852, y: 0 } }, { point: { x: 250, y: 100 }, handleIn: { x: 0, y: -27.61426 } }, { point: { x: 250, y: 100 }, handleOut: { x: 0, y: 27.61426 } }, { point: { x: 150, y: 150 }, handleIn: { x: 55.22852, y: 0 } }];
compareSegmentLists(path.segments, expectedSegments);
equals(path.segments.toString(), '{ point: { x: 150, y: 150 }, handleOut: { x: -55.22847, y: 0 } },{ point: { x: 50, y: 100 }, handleIn: { x: 0, y: 27.61424 } },{ point: { x: 50, y: 100 }, handleOut: { x: 0, y: -27.61424 } },{ point: { x: 150, y: 50 }, handleIn: { x: -55.22847, y: 0 } },{ point: { x: 150, y: 50 }, handleOut: { x: 55.22847, y: 0 } },{ point: { x: 250, y: 100 }, handleIn: { x: 0, y: -27.61424 } },{ point: { x: 250, y: 100 }, handleOut: { x: 0, y: 27.61424 } },{ point: { x: 150, y: 150 }, handleIn: { x: 55.22847, y: 0 } }');
});
test('new Path.Arc(from, through, to)', function() {
var path = new Path.Arc([50, 50], [100, 100], [75, 75]);
var expectedSegments = [{ point: { x: 50, y: 50 }, handleOut: { x: 10.11156, y: -10.11156 } }, { point: { x: 88.5299, y: 42.33593 }, handleIn: { x: -13.21138, y: -5.47233 }, handleOut: { x: 13.21138, y: 5.47233 } }, { point: { x: 110.35534, y: 75 }, handleIn: { x: 0, y: -14.2999 } }];
compareSegmentLists(path.segments, expectedSegments);
equals(path.segments.toString(), '{ point: { x: 50, y: 50 }, handleOut: { x: 10.11156, y: -10.11156 } },{ point: { x: 88.5299, y: 42.33593 }, handleIn: { x: -13.21138, y: -5.47233 }, handleOut: { x: 13.21138, y: 5.47233 } },{ point: { x: 110.35534, y: 75 }, handleIn: { x: 0, y: -14.2999 } }');
});
test('new Path.RegularPolygon(center, numSides, radius)', function() {
var doc = new Document();
var path = new Path.RegularPolygon(new Point(50, 50), 3, 10);
var expectedSegments = [{ point: { x: 41.33984, y: 55 } }, { point: { x: 50, y: 40 } }, { point: { x: 58.66016, y: 55 } }];
compareSegmentLists(path.segments, expectedSegments);
equals(path.segments.toString(), '{ point: { x: 41.33975, y: 55 } },{ point: { x: 50, y: 40 } },{ point: { x: 58.66025, y: 55 } }');
var path = new Path.RegularPolygon(new Point(250, 250), 10, 100);
var expectedSegments = [{ point: { x: 219.09814, y: 345.10547 } }, { point: { x: 169.09814, y: 308.77832 } }, { point: { x: 150, y: 250 } }, { point: { x: 169.09814, y: 191.22168 } }, { point: { x: 219.09814, y: 154.89453 } }, { point: { x: 280.90186, y: 154.89453 } }, { point: { x: 330.90186, y: 191.22168 } }, { point: { x: 350, y: 250 } }, { point: { x: 330.90186, y: 308.77832 } }, { point: { x: 280.90186, y: 345.10547 } }];
compareSegmentLists(path.segments, expectedSegments);
equals(path.segments.toString(), '{ point: { x: 219.0983, y: 345.10565 } },{ point: { x: 169.0983, y: 308.77853 } },{ point: { x: 150, y: 250 } },{ point: { x: 169.0983, y: 191.22147 } },{ point: { x: 219.0983, y: 154.89435 } },{ point: { x: 280.9017, y: 154.89435 } },{ point: { x: 330.9017, y: 191.22147 } },{ point: { x: 350, y: 250 } },{ point: { x: 330.9017, y: 308.77853 } },{ point: { x: 280.9017, y: 345.10565 } }');
});
test('new Path.Star(center, numSides, radius1, radius2)', function() {
var doc = new Document();
var path = new Path.Star(new Point(100, 100), 10, 10, 20);
var expectedSegments = [new Segment(new Point(100, 90)), new Segment(new Point(106.18017578125, 80.97900390625)), new Segment(new Point(105.8779296875, 91.90966796875)), new Segment(new Point(116.18017578125, 88.244140625)), new Segment(new Point(109.5107421875, 96.90966796875)), new Segment(new Point(120, 100)), new Segment(new Point(109.5107421875, 103.09033203125)), new Segment(new Point(116.18017578125, 111.755859375)), new Segment(new Point(105.8779296875, 108.09033203125)), new Segment(new Point(106.18017578125, 119.02099609375)), new Segment(new Point(100, 110)), new Segment(new Point(93.81982421875, 119.02099609375)), new Segment(new Point(94.1220703125, 108.09033203125)), new Segment(new Point(83.81982421875, 111.755859375)), new Segment(new Point(90.4892578125, 103.09033203125)), new Segment(new Point(80, 100)), new Segment(new Point(90.4892578125, 96.90966796875)), new Segment(new Point(83.81982421875, 88.244140625)), new Segment(new Point(94.1220703125, 91.90966796875)), new Segment(new Point(93.81982421875, 80.97900390625))];
compareSegmentLists(path.segments, expectedSegments);
equals(path.segments.toString(), '{ point: { x: 100, y: 90 } },{ point: { x: 106.18034, y: 80.97887 } },{ point: { x: 105.87785, y: 91.90983 } },{ point: { x: 116.18034, y: 88.24429 } },{ point: { x: 109.51057, y: 96.90983 } },{ point: { x: 120, y: 100 } },{ point: { x: 109.51057, y: 103.09017 } },{ point: { x: 116.18034, y: 111.75571 } },{ point: { x: 105.87785, y: 108.09017 } },{ point: { x: 106.18034, y: 119.02113 } },{ point: { x: 100, y: 110 } },{ point: { x: 93.81966, y: 119.02113 } },{ point: { x: 94.12215, y: 108.09017 } },{ point: { x: 83.81966, y: 111.75571 } },{ point: { x: 90.48943, y: 103.09017 } },{ point: { x: 80, y: 100 } },{ point: { x: 90.48943, y: 96.90983 } },{ point: { x: 83.81966, y: 88.24429 } },{ point: { x: 94.12215, y: 91.90983 } },{ point: { x: 93.81966, y: 80.97887 } }');
var doc = new Document();
var path = new Path.Star(new Point(100, 100), 5, 20, 10);
var expectedSegments = [new Segment(new Point(100, 80)), new Segment(new Point(105.8779296875, 91.90966796875)), new Segment(new Point(119.02099609375, 93.81982421875)), new Segment(new Point(109.5107421875, 103.09033203125)), new Segment(new Point(111.755859375, 116.18017578125)), new Segment(new Point(100, 110)), new Segment(new Point(88.244140625, 116.18017578125)), new Segment(new Point(90.4892578125, 103.09033203125)), new Segment(new Point(80.97900390625, 93.81982421875)), new Segment(new Point(94.1220703125, 91.90966796875))];
compareSegmentLists(path.segments, expectedSegments);
equals(path.segments.toString(), '{ point: { x: 100, y: 80 } },{ point: { x: 105.87785, y: 91.90983 } },{ point: { x: 119.02113, y: 93.81966 } },{ point: { x: 109.51057, y: 103.09017 } },{ point: { x: 111.75571, y: 116.18034 } },{ point: { x: 100, y: 110 } },{ point: { x: 88.24429, y: 116.18034 } },{ point: { x: 90.48943, y: 103.09017 } },{ point: { x: 80.97887, y: 93.81966 } },{ point: { x: 94.12215, y: 91.90983 } }');
});

View file

@ -1,57 +1,49 @@
module('Segment');
test('new Segment(point)', function() {
var segment = new Segment(new Point(10, 10));
var expected = { point: { x: 10, y: 10 } };
compareSegments(segment, expected);
equals(segment.toString(), '{ point: { x: 10, y: 10 } }');
});
test('new Segment(x, y)', function() {
var segment = new Segment(10, 10);
var expected = { point: { x: 10, y: 10 } };
compareSegments(segment, expected);
equals(segment.toString(), '{ point: { x: 10, y: 10 } }');
});
test('new Segment(object)', function() {
var segment = new Segment({ point: { x: 10, y: 10 }, handleIn: { x: 5, y: 5 }, handleOut: { x: 15, y: 15 } });
var expected = { point: { x: 10, y: 10 }, handleIn: { x: 5, y: 5 }, handleOut: { x: 15, y: 15 } };
compareSegments(segment, expected);
equals(segment.toString(), '{ point: { x: 10, y: 10 }, handleIn: { x: 5, y: 5 }, handleOut: { x: 15, y: 15 } }');
});
test('new Segment(point, handleIn, handleOut)', function() {
var segment = new Segment(new Point(10, 10), new Point(5, 5), new Point(15, 15));
var expected = { point: { x: 10, y: 10 }, handleIn: { x: 5, y: 5 }, handleOut: { x: 15, y: 15 } };
compareSegments(segment, expected);
equals(segment.toString(), '{ point: { x: 10, y: 10 }, handleIn: { x: 5, y: 5 }, handleOut: { x: 15, y: 15 } }');
});
test('new Segment(x, y, inX, inY, outX, outY)', function() {
var segment = new Segment(10, 10, 5, 5, 15, 15);
var expected = { point: { x: 10, y: 10 }, handleIn: { x: 5, y: 5 }, handleOut: { x: 15, y: 15 } };
compareSegments(segment, expected);
equals(segment.toString(), '{ point: { x: 10, y: 10 }, handleIn: { x: 5, y: 5 }, handleOut: { x: 15, y: 15 } }');
});
test('segment.reverse()', function() {
var segment = new Segment(new Point(10, 10), new Point(5, 5), new Point(15, 15));
segment = segment.reverse();
var expected = { point: { x: 10, y: 10 }, handleIn: { x: 15, y: 15 }, handleOut: { x: 5, y: 5 } };
compareSegments(segment, expected);
equals(segment.toString(), '{ point: { x: 10, y: 10 }, handleIn: { x: 15, y: 15 }, handleOut: { x: 5, y: 5 } }');
});
test('segment.clone()', function() {
var segment = new Segment(new Point(10, 10), new Point(5, 5), new Point(15, 15));
var clone = segment.clone();
equals(segment == clone, false);
compareSegments(segment, clone);
equals(segment.toString(), clone.toString());
});
test('segment.remove()', function() {
var doc = new Document();
var path = new Path([10, 10], [5, 5], [10, 10]);
path.segments[1].remove();
equals(path.segments.length, 2);
equals(path.segments.toString(), '{ point: { x: 10, y: 10 } },{ point: { x: 10, y: 10 } }');
});
test('segment.selected', function() {
var doc = new Document();
var path = new Path([10, 20], [50, 100]);
path.segments[0].point.selected = true;
equals(path.segments[0].point.selected, true);