mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-22 07:19:57 -05:00
Correctly take Item#matrix into account in #getPathData().
This commit is contained in:
parent
94bc6427c2
commit
4464950301
3 changed files with 52 additions and 33 deletions
|
@ -225,12 +225,15 @@ var CompoundPath = PathItem.extend(/** @lends CompoundPath# */{
|
||||||
// Enforce bean creation for getPathData(), as it has hidden parameters.
|
// Enforce bean creation for getPathData(), as it has hidden parameters.
|
||||||
beans: true,
|
beans: true,
|
||||||
|
|
||||||
getPathData: function(_precision) {
|
getPathData: function(_matrix, _precision) {
|
||||||
// NOTE: #setPathData() is defined in PathItem.
|
// NOTE: #setPathData() is defined in PathItem.
|
||||||
var children = this._children,
|
var children = this._children,
|
||||||
paths = [];
|
paths = [];
|
||||||
for (var i = 0, l = children.length; i < l; i++)
|
for (var i = 0, l = children.length; i < l; i++) {
|
||||||
paths.push(children[i].getPathData(_precision));
|
var child = children[i],
|
||||||
|
mx = child._matrix;
|
||||||
|
paths.push(child.getPathData(_matrix ? _matrix.clone().concatenate(mx) : mx, _precision));
|
||||||
|
}
|
||||||
return paths.join(' ');
|
return paths.join(' ');
|
||||||
}
|
}
|
||||||
}, /** @lends CompoundPath# */{
|
}, /** @lends CompoundPath# */{
|
||||||
|
|
|
@ -296,41 +296,57 @@ var Path = PathItem.extend(/** @lends Path# */{
|
||||||
// Enforce bean creation for getPathData(), as it has hidden parameters.
|
// Enforce bean creation for getPathData(), as it has hidden parameters.
|
||||||
beans: true,
|
beans: true,
|
||||||
|
|
||||||
getPathData: function(_precision) {
|
getPathData: function(_matrix, _precision) {
|
||||||
// NOTE: #setPathData() is defined in PathItem.
|
// NOTE: #setPathData() is defined in PathItem.
|
||||||
var segments = this._segments,
|
var segments = this._segments,
|
||||||
f = Formatter.instance,
|
length = segments.length,
|
||||||
|
f = new Formatter(_precision),
|
||||||
|
coords = new Array(6),
|
||||||
|
first = true,
|
||||||
|
curX, curY,
|
||||||
|
prevX, prevY,
|
||||||
|
inX, inY,
|
||||||
|
outX, outY,
|
||||||
parts = [];
|
parts = [];
|
||||||
|
|
||||||
// TODO: Add support for H/V and/or relative commands, where appropriate
|
function addSegment(segment, skipLine) {
|
||||||
// and resulting in shorter strings
|
segment._transformCoordinates(_matrix, coords, false);
|
||||||
function addCurve(seg1, seg2, skipLine) {
|
curX = coords[0];
|
||||||
var point1 = seg1._point,
|
curY = coords[1];
|
||||||
point2 = seg2._point,
|
if (first) {
|
||||||
handle1 = seg1._handleOut,
|
parts.push('M' + f.pair(curX, curY));
|
||||||
handle2 = seg2._handleIn;
|
first = false;
|
||||||
if (handle1.isZero() && handle2.isZero()) {
|
|
||||||
if (!skipLine) {
|
|
||||||
// L = absolute lineto: moving to a point with drawing
|
|
||||||
parts.push('L' + f.point(point2, _precision));
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// c = relative curveto: handle1, handle2 + end - start,
|
inX = coords[2];
|
||||||
// end - start
|
inY = coords[3];
|
||||||
var end = point2.subtract(point1);
|
// TODO: Add support for H/V and/or relative commands, where
|
||||||
parts.push('c' + f.point(handle1, _precision)
|
// appropriate and resulting in shorter strings.
|
||||||
+ ' ' + f.point(end.add(handle2), _precision)
|
if (inX === curX && inY === curY
|
||||||
+ ' ' + f.point(end, _precision));
|
&& outX === prevX && outY === prevY) {
|
||||||
|
// l = relative lineto:
|
||||||
|
if (!skipLine)
|
||||||
|
parts.push('l' + f.pair(curX - prevX, curY - prevY));
|
||||||
|
} else {
|
||||||
|
// c = relative curveto:
|
||||||
|
parts.push('c' + f.pair(outX - prevX, outY - prevY)
|
||||||
|
+ ' ' + f.pair(inX - prevX, inY - prevY)
|
||||||
|
+ ' ' + f.pair(curX - prevX, curY - prevY));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
prevX = curX;
|
||||||
|
prevY = curY;
|
||||||
|
outX = coords[4];
|
||||||
|
outY = coords[5];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (segments.length === 0)
|
if (length === 0)
|
||||||
return '';
|
return '';
|
||||||
parts.push('M' + f.point(segments[0]._point));
|
|
||||||
for (var i = 0, l = segments.length - 1; i < l; i++)
|
for (var i = 0; i < length; i++)
|
||||||
addCurve(segments[i], segments[i + 1], false);
|
addSegment(segments[i]);
|
||||||
if (this._closed) {
|
// Close path by drawing first segment again
|
||||||
addCurve(segments[segments.length - 1], segments[0], true);
|
if (this._closed && length > 0) {
|
||||||
|
addSegment(segments[0], true);
|
||||||
parts.push('z');
|
parts.push('z');
|
||||||
}
|
}
|
||||||
return parts.join('');
|
return parts.join('');
|
||||||
|
|
|
@ -145,7 +145,7 @@ new function() {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
type = 'path';
|
type = 'path';
|
||||||
var data = item.getPathData();
|
var data = item.getPathData(null, options.precision);
|
||||||
attrs = data && { d: data };
|
attrs = data && { d: data };
|
||||||
}
|
}
|
||||||
return createElement(type, getTransform(item, attrs));
|
return createElement(type, getTransform(item, attrs));
|
||||||
|
@ -178,9 +178,9 @@ new function() {
|
||||||
return createElement(type, attrs);
|
return createElement(type, attrs);
|
||||||
}
|
}
|
||||||
|
|
||||||
function exportCompoundPath(item) {
|
function exportCompoundPath(item, options) {
|
||||||
var attrs = getTransform(item, {}, true);
|
var attrs = getTransform(item, {});
|
||||||
var data = item.getPathData();
|
var data = item.getPathData(null, options.precision);
|
||||||
if (data)
|
if (data)
|
||||||
attrs.d = data;
|
attrs.d = data;
|
||||||
return createElement('path', attrs);
|
return createElement('path', attrs);
|
||||||
|
|
Loading…
Reference in a new issue