Merge pull request #984 from adroitwhiz/fix-empty-svg-center

Set rotation center of empty SVGs to (0, 0)
This commit is contained in:
DD Liu 2020-05-15 14:57:39 -04:00 committed by GitHub
commit 9e70c96044
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -134,6 +134,14 @@ const UpdateImageHOC = function (WrappedComponent) {
scaleWithStrokes(paper.project.activeLayer, .5, new paper.Point()); scaleWithStrokes(paper.project.activeLayer, .5, new paper.Point());
const bounds = paper.project.activeLayer.drawnBounds; const bounds = paper.project.activeLayer.drawnBounds;
// `bounds.x` and `bounds.y` are relative to the top left corner,
// but if there is no content in the active layer, they default to 0,
// making the "Scratch space" rotation center ((SVG_ART_BOARD_WIDTH / 2), (SVG_ART_BOARD_HEIGHT / 2)),
// aka the upper left corner. Special-case this to be (0, 0), which is the center of the art board.
const centerX = bounds.width === 0 ? 0 : (SVG_ART_BOARD_WIDTH / 2) - bounds.x;
const centerY = bounds.height === 0 ? 0 : (SVG_ART_BOARD_HEIGHT / 2) - bounds.y;
this.props.onUpdateImage( this.props.onUpdateImage(
true /* isVector */, true /* isVector */,
paper.project.exportSVG({ paper.project.exportSVG({
@ -141,8 +149,8 @@ const UpdateImageHOC = function (WrappedComponent) {
bounds: 'content', bounds: 'content',
matrix: new paper.Matrix().translate(-bounds.x, -bounds.y) matrix: new paper.Matrix().translate(-bounds.x, -bounds.y)
}), }),
(SVG_ART_BOARD_WIDTH / 2) - bounds.x, centerX,
(SVG_ART_BOARD_HEIGHT / 2) - bounds.y); centerY);
scaleWithStrokes(paper.project.activeLayer, 2, new paper.Point()); scaleWithStrokes(paper.project.activeLayer, 2, new paper.Point());
paper.project.activeLayer.applyMatrix = true; paper.project.activeLayer.applyMatrix = true;