From 66b376b98d0994d5f0a5b7d4a0dcc3e12c4d29b1 Mon Sep 17 00:00:00 2001 From: adroitwhiz Date: Fri, 14 Feb 2020 19:25:40 -0500 Subject: [PATCH] Fix rotation center of empty SVGs --- src/hocs/update-image-hoc.jsx | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/hocs/update-image-hoc.jsx b/src/hocs/update-image-hoc.jsx index f966aeff..0286c827 100644 --- a/src/hocs/update-image-hoc.jsx +++ b/src/hocs/update-image-hoc.jsx @@ -134,6 +134,14 @@ const UpdateImageHOC = function (WrappedComponent) { scaleWithStrokes(paper.project.activeLayer, .5, new paper.Point()); 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( true /* isVector */, paper.project.exportSVG({ @@ -141,8 +149,8 @@ const UpdateImageHOC = function (WrappedComponent) { bounds: 'content', matrix: new paper.Matrix().translate(-bounds.x, -bounds.y) }), - (SVG_ART_BOARD_WIDTH / 2) - bounds.x, - (SVG_ART_BOARD_HEIGHT / 2) - bounds.y); + centerX, + centerY); scaleWithStrokes(paper.project.activeLayer, 2, new paper.Point()); paper.project.activeLayer.applyMatrix = true;