mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-06 04:42:15 -05:00
SVG: Implement support for Shape.
This commit is contained in:
parent
8ab6c61b0d
commit
be5a73a61d
2 changed files with 33 additions and 4 deletions
|
@ -22,7 +22,7 @@ var Shape = Item.extend(/** @lends Shape# */{
|
||||||
_transformContent: false,
|
_transformContent: false,
|
||||||
_boundsSelected: true,
|
_boundsSelected: true,
|
||||||
|
|
||||||
// TODO: SVG, serialization
|
// TODO: serialization
|
||||||
|
|
||||||
initialize: function Shape(shape, point, size, radius, props) {
|
initialize: function Shape(shape, point, size, radius, props) {
|
||||||
this._shape = shape;
|
this._shape = shape;
|
||||||
|
|
|
@ -41,7 +41,7 @@ new function() {
|
||||||
return segments[index1]._point.getDistance(segments[index2]._point);
|
return segments[index1]._point.getDistance(segments[index2]._point);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getTransform(item, coordinates) {
|
function getTransform(item, coordinates, center) {
|
||||||
var matrix = item._matrix,
|
var matrix = item._matrix,
|
||||||
trans = matrix.getTranslation(),
|
trans = matrix.getTranslation(),
|
||||||
attrs = {};
|
attrs = {};
|
||||||
|
@ -54,8 +54,8 @@ new function() {
|
||||||
// in local coordinates.
|
// in local coordinates.
|
||||||
matrix = matrix.shiftless();
|
matrix = matrix.shiftless();
|
||||||
var point = matrix._inverseTransform(trans);
|
var point = matrix._inverseTransform(trans);
|
||||||
attrs.x = point.x;
|
attrs[center ? 'cx' : 'x'] = point.x;
|
||||||
attrs.y = point.y;
|
attrs[center ? 'cy' : 'y'] = point.y;
|
||||||
trans = null;
|
trans = null;
|
||||||
}
|
}
|
||||||
if (matrix.isIdentity())
|
if (matrix.isIdentity())
|
||||||
|
@ -296,6 +296,34 @@ new function() {
|
||||||
return createElement(type, attrs);
|
return createElement(type, attrs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function exportShape(item) {
|
||||||
|
var shape = item._shape,
|
||||||
|
center = item.getPosition(true),
|
||||||
|
radius = item._radius,
|
||||||
|
attrs = getTransform(item, true, shape !== 'rectangle');
|
||||||
|
if (shape === 'rectangle') {
|
||||||
|
shape = 'rect'; // SVG
|
||||||
|
var size = item._size,
|
||||||
|
width = size.width,
|
||||||
|
height = size.height;
|
||||||
|
attrs.x -= width / 2;
|
||||||
|
attrs.y -= height / 2;
|
||||||
|
attrs.width = width;
|
||||||
|
attrs.height = height;
|
||||||
|
if (radius.isZero())
|
||||||
|
radius = null;
|
||||||
|
}
|
||||||
|
if (radius) {
|
||||||
|
if (shape === 'circle') {
|
||||||
|
attrs.r = radius;
|
||||||
|
} else {
|
||||||
|
attrs.rx = radius.width;
|
||||||
|
attrs.ry = radius.height;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return createElement(shape, attrs);
|
||||||
|
}
|
||||||
|
|
||||||
function exportCompoundPath(item) {
|
function exportCompoundPath(item) {
|
||||||
var attrs = getTransform(item, true);
|
var attrs = getTransform(item, true);
|
||||||
var data = item.getPathData();
|
var data = item.getPathData();
|
||||||
|
@ -393,6 +421,7 @@ new function() {
|
||||||
layer: exportGroup,
|
layer: exportGroup,
|
||||||
raster: exportRaster,
|
raster: exportRaster,
|
||||||
path: exportPath,
|
path: exportPath,
|
||||||
|
shape: exportShape,
|
||||||
'compound-path': exportCompoundPath,
|
'compound-path': exportCompoundPath,
|
||||||
'placed-symbol': exportPlacedSymbol,
|
'placed-symbol': exportPlacedSymbol,
|
||||||
'point-text': exportText
|
'point-text': exportText
|
||||||
|
|
Loading…
Reference in a new issue