2013-04-07 12:49:34 -04:00
|
|
|
/*
|
2013-01-28 21:03:27 -05:00
|
|
|
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
|
2011-03-07 20:41:50 -05:00
|
|
|
* http://paperjs.org/
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2013-01-28 21:03:27 -05:00
|
|
|
* Copyright (c) 2011 - 2013, Juerg Lehni & Jonathan Puckey
|
2011-03-06 19:50:44 -05:00
|
|
|
* http://lehni.org/ & http://jonathanpuckey.com/
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-07-01 06:17:45 -04:00
|
|
|
* Distributed under the MIT license. See LICENSE file for details.
|
|
|
|
*
|
2011-03-07 20:41:50 -05:00
|
|
|
* All rights reserved.
|
2011-03-06 19:50:44 -05:00
|
|
|
*/
|
|
|
|
|
2011-06-22 18:56:05 -04:00
|
|
|
/**
|
|
|
|
* @name PathItem
|
2011-07-01 05:22:33 -04:00
|
|
|
*
|
|
|
|
* @class The PathItem class is the base for any items that describe paths
|
|
|
|
* and offer standardised methods for drawing and path manipulation, such as
|
|
|
|
* {@link Path} and {@link CompoundPath}.
|
|
|
|
*
|
2011-06-22 18:56:05 -04:00
|
|
|
* @extends Item
|
|
|
|
*/
|
2013-05-27 15:48:58 -04:00
|
|
|
var PathItem = Item.extend(/** @lends PathItem# */{
|
2013-05-27 18:55:44 -04:00
|
|
|
initialize: function PathItem() {
|
|
|
|
Item.apply(this, arguments);
|
|
|
|
},
|
|
|
|
|
2012-12-27 13:26:40 -05:00
|
|
|
/**
|
2013-03-03 13:47:32 -05:00
|
|
|
* Returns all intersections between two {@link PathItem} items as an array
|
2012-12-27 13:26:40 -05:00
|
|
|
* of {@link CurveLocation} objects. {@link CompoundPath} items are also
|
|
|
|
* supported.
|
|
|
|
*
|
2013-03-03 13:47:32 -05:00
|
|
|
* @param {PathItem} path the other item to find the intersections to.
|
2012-12-27 13:26:40 -05:00
|
|
|
* @return {CurveLocation[]} the locations of all intersection between the
|
|
|
|
* paths
|
2013-03-03 13:47:32 -05:00
|
|
|
* @example {@paperscript}
|
|
|
|
* // Create a rectangular path with its top-left point at
|
|
|
|
* // {x: 30, y: 25} and a size of {width: 50, height: 50}:
|
|
|
|
* var path = new Path.Rectangle(new Point(30, 25), new Size(50, 50));
|
|
|
|
* path.strokeColor = 'black';
|
2013-04-29 15:36:12 -04:00
|
|
|
*
|
2013-03-03 13:47:32 -05:00
|
|
|
* var secondPath = path.clone();
|
|
|
|
* var intersectionGroup = new Group();
|
2013-04-29 15:36:12 -04:00
|
|
|
*
|
2013-03-03 13:47:32 -05:00
|
|
|
* function onFrame(event) {
|
|
|
|
* secondPath.rotate(3);
|
2013-04-29 15:36:12 -04:00
|
|
|
*
|
2013-03-03 13:47:32 -05:00
|
|
|
* var intersections = path.getIntersections(secondPath);
|
|
|
|
* intersectionGroup.removeChildren();
|
2013-04-29 15:36:12 -04:00
|
|
|
*
|
2013-03-03 13:47:32 -05:00
|
|
|
* for (var i = 0; i < intersections.length; i++) {
|
|
|
|
* var intersectionPath = new Path.Circle({
|
|
|
|
* center: intersections[i].point,
|
|
|
|
* radius: 4,
|
|
|
|
* fillColor: 'red'
|
|
|
|
* });
|
|
|
|
* intersectionGroup.addChild(intersectionPath);
|
|
|
|
* }
|
|
|
|
* }
|
2012-12-27 13:26:40 -05:00
|
|
|
*/
|
2012-12-21 10:13:38 -05:00
|
|
|
getIntersections: function(path) {
|
|
|
|
// First check the bounds of the two paths. If they don't intersect,
|
2012-12-27 14:19:23 -05:00
|
|
|
// we don't need to iterate through their curves.
|
2013-04-09 23:27:55 -04:00
|
|
|
if (!this.getBounds().touches(path.getBounds()))
|
2012-12-21 10:13:38 -05:00
|
|
|
return [];
|
|
|
|
var locations = [],
|
|
|
|
curves1 = this.getCurves(),
|
2012-12-27 13:23:03 -05:00
|
|
|
curves2 = path.getCurves(),
|
|
|
|
length2 = curves2.length,
|
|
|
|
values2 = [];
|
|
|
|
for (var i = 0; i < length2; i++)
|
|
|
|
values2[i] = curves2[i].getValues();
|
|
|
|
for (var i = 0, l = curves1.length; i < l; i++) {
|
2013-04-30 21:41:26 -04:00
|
|
|
var curve1 = curves1[i],
|
|
|
|
values1 = curve1.getValues();
|
2012-12-27 13:23:03 -05:00
|
|
|
for (var j = 0; j < length2; j++)
|
2013-04-30 21:41:26 -04:00
|
|
|
Curve.getIntersections(values1, values2[j], curve1, curves2[j],
|
|
|
|
locations);
|
2012-12-27 13:23:03 -05:00
|
|
|
}
|
|
|
|
return locations;
|
2013-02-28 22:13:46 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
setPathData: function(data) {
|
|
|
|
// This is a very compact SVG Path Data parser that works both for Path
|
|
|
|
// and CompoundPath.
|
|
|
|
|
|
|
|
var parts = data.match(/[a-z][^a-z]*/ig),
|
|
|
|
coords,
|
|
|
|
relative = false,
|
|
|
|
control,
|
|
|
|
current = new Point(); // the current position
|
|
|
|
|
|
|
|
function getCoord(index, coord, update) {
|
2013-04-29 15:36:12 -04:00
|
|
|
var val = parseFloat(coords[index]);
|
2013-02-28 22:13:46 -05:00
|
|
|
if (relative)
|
|
|
|
val += current[coord];
|
|
|
|
if (update)
|
|
|
|
current[coord] = val;
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPoint(index, update) {
|
|
|
|
return new Point(
|
|
|
|
getCoord(index, 'x', update),
|
|
|
|
getCoord(index + 1, 'y', update)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-03-03 19:56:48 -05:00
|
|
|
// First clear the previous content
|
2013-04-06 12:07:30 -04:00
|
|
|
if (this._type === 'path')
|
2013-03-03 19:56:48 -05:00
|
|
|
this.removeSegments();
|
|
|
|
else
|
|
|
|
this.removeChildren();
|
|
|
|
|
2013-02-28 22:13:46 -05:00
|
|
|
for (var i = 0, l = parts.length; i < l; i++) {
|
2013-02-28 22:33:08 -05:00
|
|
|
var part = parts[i];
|
2013-02-28 22:13:46 -05:00
|
|
|
cmd = part[0],
|
|
|
|
lower = cmd.toLowerCase();
|
|
|
|
// Split at white-space, commas but also before signs.
|
2013-02-28 22:21:46 -05:00
|
|
|
// Use positive lookahead to include signs.
|
2013-02-28 22:33:08 -05:00
|
|
|
coords = part.slice(1).trim().split(/[\s,]+|(?=[+-])/);
|
2013-02-28 22:13:46 -05:00
|
|
|
relative = cmd === lower;
|
2013-04-20 17:54:21 -04:00
|
|
|
var length = coords.length;
|
2013-02-28 22:13:46 -05:00
|
|
|
switch (lower) {
|
|
|
|
case 'm':
|
|
|
|
case 'l':
|
2013-04-20 17:54:21 -04:00
|
|
|
for (var j = 0; j < length; j += 2)
|
2013-02-28 22:13:46 -05:00
|
|
|
this[j === 0 && lower === 'm' ? 'moveTo' : 'lineTo'](
|
|
|
|
getPoint(j, true));
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
case 'v':
|
|
|
|
var coord = lower == 'h' ? 'x' : 'y';
|
2013-04-20 17:54:21 -04:00
|
|
|
for (var j = 0; j < length; j++) {
|
2013-02-28 22:13:46 -05:00
|
|
|
getCoord(j, coord, true);
|
2013-03-01 04:18:27 -05:00
|
|
|
this.lineTo(current);
|
|
|
|
}
|
2013-02-28 22:13:46 -05:00
|
|
|
break;
|
|
|
|
case 'c':
|
2013-04-20 17:54:21 -04:00
|
|
|
for (var j = 0; j < length; j += 6) {
|
|
|
|
this.cubicCurveTo(
|
|
|
|
getPoint(j),
|
|
|
|
control = getPoint(j + 2),
|
|
|
|
getPoint(j + 4, true));
|
|
|
|
}
|
2013-02-28 22:13:46 -05:00
|
|
|
break;
|
2013-02-28 22:21:46 -05:00
|
|
|
case 's':
|
|
|
|
// Shorthand cubic bezierCurveTo, absolute
|
2013-04-20 17:54:21 -04:00
|
|
|
for (var j = 0; j < length; j += 4) {
|
|
|
|
this.cubicCurveTo(
|
|
|
|
// Calculate reflection of previous control points
|
|
|
|
current.multiply(2).subtract(control),
|
|
|
|
control = getPoint(j),
|
|
|
|
getPoint(j + 2, true));
|
|
|
|
}
|
2013-02-28 22:13:46 -05:00
|
|
|
break;
|
|
|
|
case 'q':
|
2013-04-20 17:54:21 -04:00
|
|
|
for (var j = 0; j < length; j += 4) {
|
|
|
|
this.quadraticCurveTo(
|
|
|
|
control = getPoint(j),
|
|
|
|
getPoint(j + 2, true));
|
|
|
|
}
|
2013-02-28 22:13:46 -05:00
|
|
|
break;
|
|
|
|
case 't':
|
2013-04-20 17:54:21 -04:00
|
|
|
for (var j = 0; j < length; j += 2) {
|
2013-03-01 04:18:42 -05:00
|
|
|
this.quadraticCurveTo(
|
|
|
|
// Calculate reflection of previous control points
|
|
|
|
control = current.multiply(2).subtract(control),
|
|
|
|
getPoint(j, true));
|
2013-02-28 22:13:46 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
// TODO: Implement Arcs!
|
|
|
|
break;
|
|
|
|
case 'z':
|
|
|
|
this.closePath();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-04-29 15:36:12 -04:00
|
|
|
},
|
|
|
|
|
2011-06-16 17:07:00 -04:00
|
|
|
/**
|
|
|
|
* Smooth bezier curves without changing the amount of segments or their
|
|
|
|
* points, by only smoothing and adjusting their handle points, for both
|
|
|
|
* open ended and closed paths.
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 18:37:45 -04:00
|
|
|
* @name PathItem#smooth
|
|
|
|
* @function
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* @example {@paperscript}
|
|
|
|
* // Smoothing a closed shape:
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* // Create a rectangular path with its top-left point at
|
|
|
|
* // {x: 30, y: 25} and a size of {width: 50, height: 50}:
|
|
|
|
* var path = new Path.Rectangle(new Point(30, 25), new Size(50, 50));
|
|
|
|
* path.strokeColor = 'black';
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* // Select the path, so we can see its handles:
|
2011-11-12 10:45:33 -05:00
|
|
|
* path.fullySelected = true;
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* // Create a copy of the path and move it 100pt to the right:
|
|
|
|
* var copy = path.clone();
|
|
|
|
* copy.position.x += 100;
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* // Smooth the segments of the copy:
|
|
|
|
* copy.smooth();
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* @example {@paperscript height=220}
|
|
|
|
* var path = new Path();
|
|
|
|
* path.strokeColor = 'black';
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* path.add(new Point(30, 50));
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* var y = 5;
|
|
|
|
* var x = 3;
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* for (var i = 0; i < 28; i++) {
|
|
|
|
* y *= -1.1;
|
|
|
|
* x *= 1.1;
|
|
|
|
* path.lineBy(x, y);
|
|
|
|
* }
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* // Create a copy of the path and move it 100pt down:
|
|
|
|
* var copy = path.clone();
|
|
|
|
* copy.position.y += 120;
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* // Set its stroke color to red:
|
|
|
|
* copy.strokeColor = 'red';
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* // Smooth the segments of the copy:
|
|
|
|
* copy.smooth();
|
|
|
|
*/
|
|
|
|
|
2011-06-14 18:04:32 -04:00
|
|
|
/**
|
|
|
|
* {@grouptitle Postscript Style Drawing Commands}
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2012-10-05 19:09:15 -04:00
|
|
|
* On a normal empty {@link Path}, the point is simply added as the path's
|
|
|
|
* first segment. If called on a {@link CompoundPath}, a new {@link Path} is
|
|
|
|
* created as a child and the point is added as its first segment.
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-14 18:04:32 -04:00
|
|
|
* @name PathItem#moveTo
|
2011-06-16 17:07:00 -04:00
|
|
|
* @function
|
|
|
|
* @param {Point} point
|
|
|
|
*/
|
|
|
|
|
2011-12-23 16:47:10 -05:00
|
|
|
// DOCS: Document #lineTo()
|
2011-06-16 17:07:00 -04:00
|
|
|
/**
|
|
|
|
* @name PathItem#lineTo
|
|
|
|
* @function
|
|
|
|
* @param {Point} point
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a cubic bezier curve to the path, defined by two handles and a to
|
|
|
|
* point.
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* @name PathItem#cubicCurveTo
|
|
|
|
* @function
|
|
|
|
* @param {Point} handle1
|
|
|
|
* @param {Point} handle2
|
|
|
|
* @param {Point} to
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a quadratic bezier curve to the path, defined by a handle and a to
|
|
|
|
* point.
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* @name PathItem#quadraticCurveTo
|
|
|
|
* @function
|
|
|
|
* @param {Point} handle
|
|
|
|
* @param {Point} to
|
|
|
|
*/
|
|
|
|
|
2011-12-23 16:47:10 -05:00
|
|
|
// DOCS: Document PathItem#curveTo() 'paramater' param.
|
2011-06-16 17:07:00 -04:00
|
|
|
/**
|
|
|
|
* Draws a curve from the position of the last segment point in the path
|
|
|
|
* that goes through the specified {@code through} point, to the specified
|
|
|
|
* {@code to} point by adding one segment to the path.
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* @name PathItem#curveTo
|
|
|
|
* @function
|
|
|
|
* @param {Point} through the point through which the curve should go
|
|
|
|
* @param {Point} to the point where the curve should end
|
|
|
|
* @param {Number} [parameter=0.5]
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* @example {@paperscript height=300}
|
2013-03-03 13:47:32 -05:00
|
|
|
* // Interactive example. Move your mouse around the view below:
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* var myPath;
|
2013-03-03 13:47:32 -05:00
|
|
|
* function onMouseMove(event) {
|
2011-06-16 17:07:00 -04:00
|
|
|
* // If we created a path before, remove it:
|
|
|
|
* if (myPath) {
|
2013-03-03 13:47:32 -05:00
|
|
|
* myPath.remove();
|
2011-06-16 17:07:00 -04:00
|
|
|
* }
|
2013-04-29 15:36:12 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* // Create a new path and add a segment point to it
|
|
|
|
* // at {x: 150, y: 150):
|
|
|
|
* myPath = new Path();
|
|
|
|
* myPath.add(150, 150);
|
2013-04-29 15:36:12 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* // Draw a curve through the position of the mouse to 'toPoint'
|
|
|
|
* var toPoint = new Point(350, 150);
|
|
|
|
* myPath.curveTo(event.point, toPoint);
|
2013-04-29 15:36:12 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* // Select the path, so we can see its segments:
|
|
|
|
* myPath.selected = true;
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Draws an arc from the position of the last segment point in the path that
|
|
|
|
* goes through the specified {@code through} point, to the specified
|
|
|
|
* {@code to} point by adding one or more segments to the path.
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* @name PathItem#arcTo
|
|
|
|
* @function
|
|
|
|
* @param {Point} through the point where the arc should pass through
|
|
|
|
* @param {Point} to the point where the arc should end
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* @example {@paperscript}
|
|
|
|
* var path = new Path();
|
|
|
|
* path.strokeColor = 'black';
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* var firstPoint = new Point(30, 75);
|
|
|
|
* path.add(firstPoint);
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* // The point through which we will create the arc:
|
|
|
|
* var throughPoint = new Point(40, 40);
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* // The point at which the arc will end:
|
|
|
|
* var toPoint = new Point(130, 75);
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* // Draw an arc through 'throughPoint' to 'toPoint'
|
|
|
|
* path.arcTo(throughPoint, toPoint);
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* // Add a red circle shaped path at the position of 'throughPoint':
|
|
|
|
* var circle = new Path.Circle(throughPoint, 3);
|
|
|
|
* circle.fillColor = 'red';
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* @example {@paperscript height=300}
|
|
|
|
* // Interactive example. Click and drag in the view below:
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* var myPath;
|
|
|
|
* function onMouseDrag(event) {
|
|
|
|
* // If we created a path before, remove it:
|
|
|
|
* if (myPath) {
|
|
|
|
* myPath.remove();
|
|
|
|
* }
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* // Create a new path and add a segment point to it
|
|
|
|
* // at {x: 150, y: 150):
|
|
|
|
* myPath = new Path();
|
|
|
|
* myPath.add(150, 150);
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* // Draw an arc through the position of the mouse to 'toPoint'
|
|
|
|
* var toPoint = new Point(350, 150);
|
|
|
|
* myPath.arcTo(event.point, toPoint);
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* // Select the path, so we can see its segments:
|
|
|
|
* myPath.selected = true;
|
|
|
|
* }
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* // When the mouse is released, deselect the path
|
|
|
|
* // and fill it with black.
|
|
|
|
* function onMouseUp(event) {
|
|
|
|
* myPath.selected = false;
|
|
|
|
* myPath.fillColor = 'black';
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Draws an arc from the position of the last segment point in the path to
|
|
|
|
* the specified point by adding one or more segments to the path.
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* @name PathItem#arcTo
|
|
|
|
* @function
|
2011-06-16 17:38:58 -04:00
|
|
|
* @param {Point} to the point where the arc should end
|
2011-06-16 17:07:00 -04:00
|
|
|
* @param {Boolean} [clockwise=true] specifies whether the arc should be
|
|
|
|
* drawn in clockwise direction.
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* @example {@paperscript}
|
|
|
|
* var path = new Path();
|
|
|
|
* path.strokeColor = 'black';
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* path.add(new Point(30, 75));
|
|
|
|
* path.arcTo(new Point(130, 75));
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* var path2 = new Path();
|
|
|
|
* path2.strokeColor = 'red';
|
|
|
|
* path2.add(new Point(180, 25));
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* // To draw an arc in anticlockwise direction,
|
|
|
|
* // we pass 'false' as the second argument to arcTo:
|
|
|
|
* path2.arcTo(new Point(280, 25), false);
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* @example {@paperscript height=300}
|
|
|
|
* // Interactive example. Click and drag in the view below:
|
|
|
|
* var myPath;
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* // The mouse has to move at least 20 points before
|
|
|
|
* // the next mouse drag event is fired:
|
|
|
|
* tool.minDistance = 20;
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* // When the user clicks, create a new path and add
|
|
|
|
* // the current mouse position to it as its first segment:
|
|
|
|
* function onMouseDown(event) {
|
|
|
|
* myPath = new Path();
|
|
|
|
* myPath.strokeColor = 'black';
|
|
|
|
* myPath.add(event.point);
|
|
|
|
* }
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* // On each mouse drag event, draw an arc to the current
|
|
|
|
* // position of the mouse:
|
|
|
|
* function onMouseDrag(event) {
|
|
|
|
* myPath.arcTo(event.point);
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Closes the path. When closed, Paper.js connects the first and last
|
|
|
|
* segments.
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* @name PathItem#closePath
|
|
|
|
* @function
|
|
|
|
* @see Path#closed
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@grouptitle Relative Drawing Commands}
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* If called on a {@link CompoundPath}, a new {@link Path} is created as a
|
2012-10-05 19:09:15 -04:00
|
|
|
* child and a point is added as its first segment relative to the
|
2011-06-16 17:07:00 -04:00
|
|
|
* position of the last segment of the current path.
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* @name PathItem#moveBy
|
|
|
|
* @function
|
2012-10-05 19:09:15 -04:00
|
|
|
* @param {Point} vector
|
2011-06-16 17:07:00 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a segment relative to the last segment point of the path.
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* @name PathItem#lineBy
|
|
|
|
* @function
|
|
|
|
* @param {Point} vector The vector which is added to the position of the
|
|
|
|
* last segment of the path, to become the new segment.
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* @example {@paperscript}
|
|
|
|
* var path = new Path();
|
|
|
|
* path.strokeColor = 'black';
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* // Add a segment at {x: 50, y: 50}
|
|
|
|
* path.add(25, 25);
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* // Add a segment relative to the last segment of the path.
|
|
|
|
* // 50 in x direction and 0 in y direction, becomes {x: 75, y: 25}
|
|
|
|
* path.lineBy(50, 0);
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* // 0 in x direction and 50 in y direction, becomes {x: 75, y: 75}
|
|
|
|
* path.lineBy(0, 50);
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* @example {@paperscript height=300}
|
|
|
|
* // Drawing a spiral using lineBy:
|
|
|
|
* var path = new Path();
|
|
|
|
* path.strokeColor = 'black';
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* // Add the first segment at {x: 50, y: 50}
|
|
|
|
* path.add(view.center);
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* // Loop 500 times:
|
|
|
|
* for (var i = 0; i < 500; i++) {
|
|
|
|
* // Create a vector with an ever increasing length
|
|
|
|
* // and an angle in increments of 45 degrees
|
|
|
|
* var vector = new Point({
|
|
|
|
* angle: i * 45,
|
|
|
|
* length: i / 2
|
|
|
|
* });
|
|
|
|
* // Add the vector relatively to the last segment point:
|
|
|
|
* path.lineBy(vector);
|
|
|
|
* }
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* // Smooth the handles of the path:
|
|
|
|
* path.smooth();
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-16 17:07:00 -04:00
|
|
|
* // Uncomment the following line and click on 'run' to see
|
|
|
|
* // the construction of the path:
|
|
|
|
* // path.selected = true;
|
|
|
|
*/
|
|
|
|
|
2011-12-23 16:47:10 -05:00
|
|
|
// DOCS: Document Path#curveBy()
|
2011-06-16 17:07:00 -04:00
|
|
|
/**
|
|
|
|
* @name PathItem#curveBy
|
|
|
|
* @function
|
|
|
|
* @param {Point} throughVector
|
|
|
|
* @param {Point} toVector
|
|
|
|
* @param {Number} [parameter=0.5]
|
|
|
|
*/
|
|
|
|
|
2011-12-23 16:47:10 -05:00
|
|
|
// DOCS: Document Path#arcBy()
|
2011-06-16 17:07:00 -04:00
|
|
|
/**
|
|
|
|
* @name PathItem#arcBy
|
|
|
|
* @function
|
|
|
|
* @param {Point} throughVector
|
|
|
|
* @param {Point} toVector
|
2011-06-14 18:04:32 -04:00
|
|
|
*/
|
2011-02-13 11:26:24 -05:00
|
|
|
});
|