@yorab/react-paint 0.6.4 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/README.md +5 -5
  2. package/dist/components/App.js +236 -423
  3. package/dist/components/Canvas.js +120 -139
  4. package/dist/components/common/Button.js +41 -50
  5. package/dist/components/common/Loading.js +24 -33
  6. package/dist/components/common/Modal.js +18 -17
  7. package/dist/components/common/Panel.js +20 -28
  8. package/dist/components/common/Snackbar.js +31 -38
  9. package/dist/components/settings/ColorField.js +59 -68
  10. package/dist/components/settings/DeleteButton.js +10 -9
  11. package/dist/components/settings/FontFamilyField.js +25 -20
  12. package/dist/components/settings/LayoutButton.js +11 -9
  13. package/dist/components/settings/Layouts.js +111 -111
  14. package/dist/components/settings/LineArrowField.js +25 -22
  15. package/dist/components/settings/LineTypeField.js +23 -20
  16. package/dist/components/settings/PointsNumberField.js +14 -13
  17. package/dist/components/settings/RangeField.js +35 -47
  18. package/dist/components/settings/SelectField.js +34 -43
  19. package/dist/components/settings/SettingsBar.js +100 -121
  20. package/dist/components/settings/ToggleField.js +18 -25
  21. package/dist/components/toolbox/EditTextBox.js +62 -49
  22. package/dist/components/toolbox/LoadFileTool.js +19 -21
  23. package/dist/components/toolbox/MenuGroup.js +66 -100
  24. package/dist/components/toolbox/PictureUrlInput.js +33 -62
  25. package/dist/components/toolbox/Tool.js +13 -13
  26. package/dist/components/toolbox/Toolbar.js +79 -113
  27. package/dist/components/toolbox/ToolbarGroup.js +60 -63
  28. package/dist/constants/app.js +51 -0
  29. package/dist/constants/icons.js +42 -48
  30. package/dist/constants/loading.js +8 -0
  31. package/dist/constants/shapes.js +10 -8
  32. package/dist/constants/snackbar.js +7 -15
  33. package/dist/constants/style.js +21 -18
  34. package/dist/constants/tools.js +99 -103
  35. package/dist/hooks/useComponent.js +15 -20
  36. package/dist/hooks/useDoubleClick.js +12 -12
  37. package/dist/hooks/useDrag.js +24 -32
  38. package/dist/hooks/useDrawableCanvas.js +106 -122
  39. package/dist/hooks/useKeyboard.js +80 -63
  40. package/dist/hooks/useResizeObserver.js +18 -16
  41. package/dist/hooks/useShapes.js +92 -144
  42. package/dist/hooks/useSnackbar.js +28 -58
  43. package/dist/styles.css +16 -13
  44. package/dist/types/Mode.js +1 -13
  45. package/dist/types/Shapes.js +1 -17
  46. package/dist/types/snackbar.js +5 -0
  47. package/dist/types/tools.js +1 -18
  48. package/dist/utils/canvas.js +49 -0
  49. package/dist/utils/data.js +15 -274
  50. package/dist/utils/dom.js +12 -0
  51. package/dist/utils/file.js +75 -120
  52. package/dist/utils/intersect.js +54 -131
  53. package/dist/utils/migration.js +59 -0
  54. package/dist/utils/selection.js +18 -20
  55. package/dist/utils/shapes/brush.js +135 -0
  56. package/dist/utils/shapes/circle.js +93 -0
  57. package/dist/utils/shapes/curve.js +55 -0
  58. package/dist/utils/shapes/default.js +73 -0
  59. package/dist/utils/shapes/ellipse.js +101 -0
  60. package/dist/utils/shapes/grid.js +36 -0
  61. package/dist/utils/shapes/index.js +341 -0
  62. package/dist/utils/shapes/line.js +179 -0
  63. package/dist/utils/shapes/picture.js +88 -0
  64. package/dist/utils/shapes/polygon.js +69 -0
  65. package/dist/utils/shapes/rectangle.js +100 -0
  66. package/dist/utils/shapes/text.js +128 -0
  67. package/dist/utils/shapes/triangle.js +24 -0
  68. package/dist/utils/string.js +10 -16
  69. package/dist/utils/style.js +11 -13
  70. package/dist/utils/toolbar.js +35 -49
  71. package/dist/utils/transform.js +47 -339
  72. package/dist/utils/trigo.js +42 -0
  73. package/package.json +6 -6
  74. package/Changelog.md +0 -119
  75. package/dist/types/keyboard.js +0 -28
  76. package/dist/utils/draw.js +0 -461
  77. package/dist/utils/shapeData.js +0 -173
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.getCurveBorder = exports.drawCurve = exports.createCurve = void 0;
9
+
10
+ var _fp = _interopRequireDefault(require("lodash/fp"));
11
+
12
+ var _canvas = require("../canvas");
13
+
14
+ var _polygon = require("./polygon");
15
+
16
+ const createCurve = (shape, cursorPosition) => {
17
+ return {
18
+ toolId: shape.id,
19
+ type: shape.type,
20
+ id: _fp.default.uniqueId("".concat(shape.type, "_")),
21
+ points: _fp.default.flow(_fp.default.range(0), _fp.default.map(() => cursorPosition))(shape.settings.pointsCount.default),
22
+ rotation: 0,
23
+ style: {
24
+ globalAlpha: shape.settings.opacity.default,
25
+ fillColor: shape.settings.fillColor.default,
26
+ strokeColor: shape.settings.strokeColor.default,
27
+ lineWidth: shape.settings.lineWidth.default,
28
+ lineDash: shape.settings.lineDash.default,
29
+ pointsCount: shape.settings.pointsCount.default
30
+ }
31
+ };
32
+ };
33
+
34
+ exports.createCurve = createCurve;
35
+
36
+ const drawCurve = (ctx, curve) => {
37
+ var _curve$style, _curve$style2;
38
+
39
+ if (curve.points.length < 3) return;
40
+ (0, _canvas.updateCanvasContext)(ctx, curve.style);
41
+ if (ctx.globalAlpha === 0) return;
42
+ ctx.beginPath();
43
+ ctx.moveTo(...curve.points[0]);
44
+
45
+ for (let i = 1; i < curve.points.length - 1; i++) {
46
+ ctx.quadraticCurveTo(...curve.points[i], curve.points.length - 2 === i ? curve.points[i + 1][0] : (curve.points[i + 1][0] - curve.points[i][0]) / 2 + curve.points[i][0], curve.points.length - 2 === i ? curve.points[i + 1][1] : (curve.points[i + 1][1] - curve.points[i][1]) / 2 + curve.points[i][1]);
47
+ }
48
+
49
+ ((_curve$style = curve.style) === null || _curve$style === void 0 ? void 0 : _curve$style.fillColor) !== 'transparent' && ctx.fill();
50
+ ((_curve$style2 = curve.style) === null || _curve$style2 === void 0 ? void 0 : _curve$style2.strokeColor) !== 'transparent' && ctx.stroke();
51
+ };
52
+
53
+ exports.drawCurve = drawCurve;
54
+ const getCurveBorder = _polygon.getPolygonBorder;
55
+ exports.getCurveBorder = getCurveBorder;
@@ -0,0 +1,73 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.drawSelectionDefault = void 0;
7
+
8
+ var _shapes = require("../../constants/shapes");
9
+
10
+ var _ = require(".");
11
+
12
+ var _circle = require("./circle");
13
+
14
+ var _line = require("./line");
15
+
16
+ var _rectangle = require("./rectangle");
17
+
18
+ const drawSelectionDefault = _ref => {
19
+ let {
20
+ ctx,
21
+ shape,
22
+ withAnchors,
23
+ selectionPadding,
24
+ selectionWidth,
25
+ selectionColor,
26
+ currentScale = 1
27
+ } = _ref;
28
+ const {
29
+ borders
30
+ } = (0, _.getShapeInfos)(shape, selectionPadding / currentScale);
31
+ (0, _rectangle.drawRect)(ctx, { ...borders,
32
+ style: {
33
+ fillColor: 'transparent',
34
+ strokeColor: selectionColor,
35
+ lineWidth: selectionWidth / currentScale
36
+ }
37
+ });
38
+ if (!withAnchors || shape.locked) return;
39
+ (0, _line.drawLine)(ctx, {
40
+ points: [[borders.x + borders.width / 2, borders.y], [borders.x + borders.width / 2, borders.y - _shapes.SELECTION_ANCHOR_SIZE / 2 - _shapes.SELECTION_ROTATED_ANCHOR_POSITION / currentScale]],
41
+ style: {
42
+ fillColor: 'transparent',
43
+ strokeColor: selectionColor,
44
+ lineWidth: selectionWidth / currentScale
45
+ }
46
+ });
47
+
48
+ for (const anchorPosition of _shapes.SELECTION_RESIZE_ANCHOR_POSITIONS) {
49
+ (0, _circle.drawCircle)(ctx, {
50
+ x: borders.x + borders.width * anchorPosition[0],
51
+ y: borders.y + borders.height * anchorPosition[1],
52
+ radius: _shapes.SELECTION_ANCHOR_SIZE / 2 / currentScale,
53
+ style: {
54
+ fillColor: 'rgb(255,255,255)',
55
+ strokeColor: 'rgb(150,150,150)',
56
+ lineWidth: selectionWidth / currentScale
57
+ }
58
+ });
59
+ }
60
+
61
+ (0, _circle.drawCircle)(ctx, {
62
+ x: borders.x + borders.width / 2,
63
+ y: borders.y - _shapes.SELECTION_ANCHOR_SIZE / 2 - _shapes.SELECTION_ROTATED_ANCHOR_POSITION / currentScale,
64
+ radius: _shapes.SELECTION_ANCHOR_SIZE / 2 / currentScale,
65
+ style: {
66
+ fillColor: 'rgb(255,255,255)',
67
+ strokeColor: 'rgb(150,150,150)',
68
+ lineWidth: selectionWidth / currentScale
69
+ }
70
+ });
71
+ };
72
+
73
+ exports.drawSelectionDefault = drawSelectionDefault;
@@ -0,0 +1,101 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.resizeEllipse = exports.getEllipseBorder = exports.drawEllipse = exports.createEllipse = void 0;
9
+
10
+ var _fp = _interopRequireDefault(require("lodash/fp"));
11
+
12
+ var _canvas = require("../canvas");
13
+
14
+ var _intersect = require("../intersect");
15
+
16
+ var _transform = require("../transform");
17
+
18
+ var _2 = require(".");
19
+
20
+ const createEllipse = (shape, cursorPosition) => {
21
+ return {
22
+ toolId: shape.id,
23
+ type: shape.type,
24
+ id: _fp.default.uniqueId("".concat(shape.type, "_")),
25
+ x: cursorPosition[0],
26
+ y: cursorPosition[1],
27
+ radiusX: 1,
28
+ radiusY: 1,
29
+ rotation: 0,
30
+ style: {
31
+ globalAlpha: shape.settings.opacity.default,
32
+ fillColor: shape.settings.fillColor.default,
33
+ strokeColor: shape.settings.strokeColor.default,
34
+ lineWidth: shape.settings.lineWidth.default,
35
+ lineDash: shape.settings.lineDash.default
36
+ }
37
+ };
38
+ };
39
+
40
+ exports.createEllipse = createEllipse;
41
+
42
+ const drawEllipse = (ctx, ellipse) => {
43
+ var _ellipse$style, _ellipse$style2;
44
+
45
+ (0, _canvas.updateCanvasContext)(ctx, ellipse.style);
46
+ if (ctx.globalAlpha === 0) return;
47
+ ctx.beginPath();
48
+ ctx.ellipse(ellipse.x, ellipse.y, ellipse.radiusX, ellipse.radiusY, 0, 0, 2 * Math.PI);
49
+ ((_ellipse$style = ellipse.style) === null || _ellipse$style === void 0 ? void 0 : _ellipse$style.fillColor) !== 'transparent' && ctx.fill();
50
+ ((_ellipse$style2 = ellipse.style) === null || _ellipse$style2 === void 0 ? void 0 : _ellipse$style2.strokeColor) !== 'transparent' && ctx.stroke();
51
+ };
52
+
53
+ exports.drawEllipse = drawEllipse;
54
+
55
+ const getEllipseBorder = (ellipse, selectionPadding) => {
56
+ return {
57
+ x: ellipse.x - ellipse.radiusX - selectionPadding,
58
+ width: (ellipse.radiusX + selectionPadding) * 2,
59
+ y: ellipse.y - ellipse.radiusY - selectionPadding,
60
+ height: (ellipse.radiusY + selectionPadding) * 2
61
+ };
62
+ };
63
+
64
+ exports.getEllipseBorder = getEllipseBorder;
65
+
66
+ const getEllipseOppositeAnchorAbsolutePosition = function (anchor, center, shape, canvasOffset) {
67
+ let [negW, negH] = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : [false, false];
68
+ const oppositeX = anchor[0] === 0.5 ? shape.x : anchor[0] === 0 ? shape.x + (negW ? -shape.radiusX : shape.radiusX) : shape.x + (negW ? shape.radiusX : -shape.radiusX);
69
+ const oppositeY = anchor[1] === 0.5 ? shape.y : anchor[1] === 0 ? shape.y + (negH ? -shape.radiusY : shape.radiusY) : shape.y + (negH ? shape.radiusY : -shape.radiusY);
70
+ return (0, _intersect.getPointPositionBeforeCanvasTransformation)([oppositeX, oppositeY], shape.rotation, center, canvasOffset);
71
+ };
72
+
73
+ const resizeEllipse = function (cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding) {
74
+ let keepRatio = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : false;
75
+ const {
76
+ center,
77
+ borders
78
+ } = (0, _2.getShapeInfos)(originalShape, selectionPadding);
79
+ const cursorPositionBeforeResize = (0, _intersect.getPointPositionAfterCanvasTransformation)(cursorPosition, originalShape.rotation, center, canvasOffset);
80
+ const newCursorPosition = [cursorPositionBeforeResize[0], cursorPositionBeforeResize[1]];
81
+ const scaledRadiusX = selectionMode.anchor[0] === 0.5 ? originalShape.radiusX : (selectionMode.anchor[0] === 0 ? borders.x + borders.width - newCursorPosition[0] + selectionPadding * (selectionMode.anchor[0] === 0 ? -2 : 2) : newCursorPosition[0] - borders.x - selectionPadding * (selectionMode.anchor[0] === 0 ? -2 : 2)) / 2;
82
+ const scaledRadiusY = selectionMode.anchor[1] === 0.5 ? originalShape.radiusY : (selectionMode.anchor[1] === 0 ? borders.y + borders.height - newCursorPosition[1] + selectionPadding * (selectionMode.anchor[1] === 0 ? -2 : 2) : newCursorPosition[1] - borders.y - selectionPadding * (selectionMode.anchor[1] === 0 ? -2 : 2)) / 2;
83
+ const [radiusXWithRatio, radiusYWithRatio] = keepRatio ? (0, _transform.getNormalizedSize)(originalShape.radiusX, originalShape.radiusY, scaledRadiusX, scaledRadiusY) : [scaledRadiusX, scaledRadiusY];
84
+ const shapeWithNewDimensions = { ...originalShape,
85
+ ...{
86
+ radiusX: Math.abs(radiusXWithRatio),
87
+ radiusY: Math.abs(radiusYWithRatio)
88
+ }
89
+ };
90
+ const {
91
+ center: shapeWithNewDimensionsCenter
92
+ } = (0, _2.getShapeInfos)(shapeWithNewDimensions, selectionPadding);
93
+ const [oppTrueX, oppTrueY] = getEllipseOppositeAnchorAbsolutePosition(selectionMode.anchor, center, originalShape, canvasOffset);
94
+ const [newOppTrueX, newOppTrueY] = getEllipseOppositeAnchorAbsolutePosition(selectionMode.anchor, shapeWithNewDimensionsCenter, shapeWithNewDimensions, canvasOffset, [radiusXWithRatio < 0, radiusYWithRatio < 0]);
95
+ return { ...shapeWithNewDimensions,
96
+ x: shapeWithNewDimensions.x - (newOppTrueX - oppTrueX),
97
+ y: shapeWithNewDimensions.y - (newOppTrueY - oppTrueY)
98
+ };
99
+ };
100
+
101
+ exports.resizeEllipse = resizeEllipse;
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.drawGrid = void 0;
7
+
8
+ var _style = require("../../constants/style");
9
+
10
+ const drawGrid = (ctx, width, height, responsiveScale, canvasOffset, gridFormat) => {
11
+ ctx.strokeStyle = 'rgb(220,220,220)';
12
+ ctx.lineWidth = 1;
13
+ ctx.save();
14
+ ctx.scale(responsiveScale, responsiveScale);
15
+ ctx.beginPath();
16
+ const step = _style.GRID_STEP[gridFormat - 1];
17
+ const horizontalOffset = -canvasOffset[0] % step;
18
+ const verticalOffset = -canvasOffset[1] % step;
19
+ const nbCols = Math.ceil(width / responsiveScale / step);
20
+ const nbRows = Math.ceil(height / responsiveScale / step);
21
+
22
+ for (let i = 0; i < nbCols; i++) {
23
+ ctx.moveTo(horizontalOffset + (i + 1) * step, verticalOffset);
24
+ ctx.lineTo(horizontalOffset + (i + 1) * step, verticalOffset + height / responsiveScale);
25
+ }
26
+
27
+ for (let i = 0; i < nbRows; i++) {
28
+ ctx.moveTo(horizontalOffset, verticalOffset + (i + 1) * step);
29
+ ctx.lineTo(horizontalOffset + width / responsiveScale, verticalOffset + (i + 1) * step);
30
+ }
31
+
32
+ ctx.stroke();
33
+ ctx.restore();
34
+ };
35
+
36
+ exports.drawGrid = drawGrid;
@@ -0,0 +1,341 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.translateShape = exports.rotateShape = exports.resizeShape = exports.getShapeInfos = exports.drawShapeSelection = exports.drawShape = exports.createShape = exports.copyShape = void 0;
9
+
10
+ var _fp = _interopRequireDefault(require("lodash/fp"));
11
+
12
+ var _transform = require("../transform");
13
+
14
+ var _brush = require("./brush");
15
+
16
+ var _line = require("./line");
17
+
18
+ var _polygon = require("./polygon");
19
+
20
+ var _curve = require("./curve");
21
+
22
+ var _rectangle = require("./rectangle");
23
+
24
+ var _text = require("./text");
25
+
26
+ var _ellipse = require("./ellipse");
27
+
28
+ var _circle = require("./circle");
29
+
30
+ var _picture = require("./picture");
31
+
32
+ var _style = require("../../constants/style");
33
+
34
+ var _default = require("./default");
35
+
36
+ var _canvas = require("../canvas");
37
+
38
+ const createShape = (ctx, shape, cursorPosition, gridFormat) => {
39
+ const roundCursorPosition = [(0, _transform.roundForGrid)(cursorPosition[0], gridFormat), (0, _transform.roundForGrid)(cursorPosition[1], gridFormat)];
40
+
41
+ switch (shape.type) {
42
+ case 'brush':
43
+ return (0, _brush.createBrush)(shape, roundCursorPosition);
44
+
45
+ case 'line':
46
+ return (0, _line.createLine)(shape, roundCursorPosition);
47
+
48
+ case 'polygon':
49
+ return (0, _polygon.createPolygon)(shape, roundCursorPosition);
50
+
51
+ case 'curve':
52
+ return (0, _curve.createCurve)(shape, roundCursorPosition);
53
+
54
+ case 'rect':
55
+ case 'square':
56
+ return (0, _rectangle.createRectangle)(shape, roundCursorPosition);
57
+
58
+ case 'text':
59
+ return (0, _text.createText)(ctx, shape, roundCursorPosition);
60
+
61
+ case 'ellipse':
62
+ return (0, _ellipse.createEllipse)(shape, roundCursorPosition);
63
+
64
+ case 'circle':
65
+ return (0, _circle.createCircle)(shape, roundCursorPosition);
66
+ }
67
+ };
68
+
69
+ exports.createShape = createShape;
70
+
71
+ const drawShape = (ctx, shape, responsiveScale, canvasOffset, selectionPadding) => {
72
+ if (shape.visible === false) return;
73
+ const {
74
+ center
75
+ } = getShapeInfos(shape, selectionPadding);
76
+ (0, _canvas.transformCanvas)(ctx, responsiveScale, canvasOffset, shape.rotation, center);
77
+
78
+ switch (shape.type) {
79
+ case 'brush':
80
+ (0, _brush.drawBrush)(ctx, shape);
81
+ break;
82
+
83
+ case 'text':
84
+ (0, _text.drawText)(ctx, shape);
85
+ break;
86
+
87
+ case 'line':
88
+ (0, _line.drawLine)(ctx, shape);
89
+ break;
90
+
91
+ case 'polygon':
92
+ (0, _polygon.drawPolygon)(ctx, shape);
93
+ break;
94
+
95
+ case 'curve':
96
+ (0, _curve.drawCurve)(ctx, shape);
97
+ break;
98
+
99
+ case 'circle':
100
+ (0, _circle.drawCircle)(ctx, shape);
101
+ break;
102
+
103
+ case 'ellipse':
104
+ (0, _ellipse.drawEllipse)(ctx, shape);
105
+ break;
106
+
107
+ case 'rect':
108
+ case 'square':
109
+ (0, _rectangle.drawRect)(ctx, shape);
110
+ break;
111
+
112
+ case 'picture':
113
+ (0, _picture.drawPicture)(ctx, shape);
114
+ break;
115
+ }
116
+
117
+ ctx.restore();
118
+ };
119
+
120
+ exports.drawShape = drawShape;
121
+
122
+ const getShapeBorders = (marker, selectionPadding) => {
123
+ switch (marker.type) {
124
+ case 'brush':
125
+ return (0, _brush.getBrushBorder)(marker, selectionPadding);
126
+
127
+ case 'line':
128
+ return (0, _line.getLineBorder)(marker, selectionPadding);
129
+
130
+ case 'polygon':
131
+ return (0, _polygon.getPolygonBorder)(marker, selectionPadding);
132
+
133
+ case 'curve':
134
+ return (0, _curve.getCurveBorder)(marker, selectionPadding);
135
+
136
+ case 'circle':
137
+ return (0, _circle.getCircleBorder)(marker, selectionPadding);
138
+
139
+ case 'ellipse':
140
+ return (0, _ellipse.getEllipseBorder)(marker, selectionPadding);
141
+
142
+ case 'rect':
143
+ case 'square':
144
+ default:
145
+ return (0, _rectangle.getRectBorder)(marker, selectionPadding);
146
+
147
+ case 'text':
148
+ return (0, _text.getTextBorder)(marker, selectionPadding);
149
+
150
+ case 'picture':
151
+ return (0, _picture.getPictureBorder)(marker, selectionPadding);
152
+ }
153
+ };
154
+
155
+ const getShapeCenter = borders => {
156
+ return [borders.x + borders.width / 2, borders.y + borders.height / 2];
157
+ };
158
+
159
+ const getShapeInfos = (shape, selectionPadding) => {
160
+ const borders = getShapeBorders(shape, selectionPadding);
161
+ const center = getShapeCenter(borders);
162
+ return {
163
+ borders,
164
+ center
165
+ };
166
+ };
167
+
168
+ exports.getShapeInfos = getShapeInfos;
169
+
170
+ const rotateShape = (shape, cursorPosition, originalShape, originalCursorPosition, shapeCenter, gridFormat) => {
171
+ const p1x = shapeCenter[0] - originalCursorPosition[0];
172
+ const p1y = shapeCenter[1] - originalCursorPosition[1];
173
+ const p2x = shapeCenter[0] - cursorPosition[0];
174
+ const p2y = shapeCenter[1] - cursorPosition[1];
175
+ const rotation = originalShape.rotation + Math.atan2(p2y, p2x) - Math.atan2(p1y, p1x);
176
+ return { ...shape,
177
+ ...{
178
+ rotation: gridFormat ? rotation + Math.PI / _style.GRID_ROTATION_STEPS / 2 - (rotation + Math.PI / _style.GRID_ROTATION_STEPS / 2) % (Math.PI / _style.GRID_ROTATION_STEPS) : rotation
179
+ }
180
+ };
181
+ };
182
+
183
+ exports.rotateShape = rotateShape;
184
+
185
+ const resizeShape = (ctx, shape, cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding, isShiftPressed) => {
186
+ switch (shape.type) {
187
+ case 'line':
188
+ case 'polygon':
189
+ case 'curve':
190
+ return (0, _line.resizeLine)(cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding);
191
+
192
+ case 'brush':
193
+ return (0, _brush.resizeBrush)(cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding, isShiftPressed);
194
+
195
+ case 'circle':
196
+ return (0, _circle.resizeCircle)(cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding);
197
+
198
+ case 'ellipse':
199
+ return (0, _ellipse.resizeEllipse)(cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding, isShiftPressed);
200
+
201
+ case 'rect':
202
+ case 'square':
203
+ return (0, _rectangle.resizeRect)(cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding, shape.type === 'square' || isShiftPressed);
204
+
205
+ case 'text':
206
+ return (0, _text.resizeText)(ctx, cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding);
207
+
208
+ case 'picture':
209
+ return (0, _picture.resizePicture)(cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding, !isShiftPressed);
210
+ }
211
+ };
212
+
213
+ exports.resizeShape = resizeShape;
214
+
215
+ const translateShape = (cursorPosition, originalShape, originalCursorPosition, gridFormat) => {
216
+ switch (originalShape.type) {
217
+ case 'rect':
218
+ case 'square':
219
+ case 'ellipse':
220
+ case 'circle':
221
+ case 'picture':
222
+ default:
223
+ return { ...originalShape,
224
+ x: (0, _transform.roundForGrid)(originalShape.x + cursorPosition[0] - originalCursorPosition[0], gridFormat),
225
+ y: (0, _transform.roundForGrid)(originalShape.y + cursorPosition[1] - originalCursorPosition[1], gridFormat)
226
+ };
227
+
228
+ case 'line':
229
+ return { ...originalShape,
230
+ points: originalShape.points.map(_ref => {
231
+ let [x, y] = _ref;
232
+ return [(0, _transform.roundForGrid)(x + cursorPosition[0] - originalCursorPosition[0], gridFormat), (0, _transform.roundForGrid)(y + cursorPosition[1] - originalCursorPosition[1], gridFormat)];
233
+ })
234
+ };
235
+
236
+ case 'polygon':
237
+ case 'curve':
238
+ if (gridFormat) {
239
+ const {
240
+ borders
241
+ } = getShapeInfos(originalShape, 0);
242
+ const translationX = (0, _transform.roundForGrid)(borders.x + cursorPosition[0] - originalCursorPosition[0], gridFormat) - borders.x;
243
+ const translationY = (0, _transform.roundForGrid)(borders.y + cursorPosition[1] - originalCursorPosition[1], gridFormat) - borders.y;
244
+ return { ...originalShape,
245
+ points: originalShape.points.map(_ref2 => {
246
+ let [x, y] = _ref2;
247
+ return [x + translationX, y + translationY];
248
+ })
249
+ };
250
+ } else {
251
+ return { ...originalShape,
252
+ points: originalShape.points.map(_ref3 => {
253
+ let [x, y] = _ref3;
254
+ return [(0, _transform.roundForGrid)(x + cursorPosition[0] - originalCursorPosition[0], gridFormat), (0, _transform.roundForGrid)(y + cursorPosition[1] - originalCursorPosition[1], gridFormat)];
255
+ })
256
+ };
257
+ }
258
+
259
+ case 'brush':
260
+ if (gridFormat) {
261
+ const {
262
+ borders
263
+ } = getShapeInfos(originalShape, 0);
264
+ const translationX = (0, _transform.roundForGrid)(borders.x + cursorPosition[0] - originalCursorPosition[0], gridFormat) - borders.x;
265
+ const translationY = (0, _transform.roundForGrid)(borders.y + cursorPosition[1] - originalCursorPosition[1], gridFormat) - borders.y;
266
+ return { ...originalShape,
267
+ points: originalShape.points.map(coord => coord.map(_ref4 => {
268
+ let [x, y] = _ref4;
269
+ return [x + translationX, y + translationY];
270
+ }))
271
+ };
272
+ } else {
273
+ return { ...originalShape,
274
+ points: originalShape.points.map(coord => coord.map(_ref5 => {
275
+ let [x, y] = _ref5;
276
+ return [x + cursorPosition[0] - originalCursorPosition[0], y + cursorPosition[1] - originalCursorPosition[1]];
277
+ }))
278
+ };
279
+ }
280
+
281
+ }
282
+ };
283
+
284
+ exports.translateShape = translateShape;
285
+
286
+ const drawShapeSelection = _ref6 => {
287
+ let {
288
+ ctx,
289
+ shape,
290
+ scaleRatio = 1,
291
+ canvasOffset,
292
+ selectionPadding,
293
+ selectionWidth,
294
+ selectionColor,
295
+ withAnchors = true
296
+ } = _ref6;
297
+ const {
298
+ center
299
+ } = getShapeInfos(shape, selectionPadding);
300
+ (0, _canvas.transformCanvas)(ctx, scaleRatio, canvasOffset, shape.rotation, center);
301
+
302
+ switch (shape.type) {
303
+ case 'polygon':
304
+ case 'line':
305
+ case 'curve':
306
+ (0, _line.drawLineSelection)({
307
+ ctx,
308
+ shape,
309
+ withAnchors,
310
+ selectionPadding,
311
+ selectionWidth,
312
+ selectionColor,
313
+ currentScale: scaleRatio
314
+ });
315
+ break;
316
+
317
+ default:
318
+ (0, _default.drawSelectionDefault)({
319
+ ctx,
320
+ shape,
321
+ withAnchors,
322
+ selectionPadding,
323
+ selectionWidth,
324
+ selectionColor,
325
+ currentScale: scaleRatio
326
+ });
327
+ break;
328
+ }
329
+
330
+ ctx.restore();
331
+ };
332
+
333
+ exports.drawShapeSelection = drawShapeSelection;
334
+
335
+ const copyShape = (shape, gridFormat) => {
336
+ return { ...translateShape([20, 20], shape, [0, 0], gridFormat),
337
+ id: _fp.default.uniqueId("".concat(shape.type, "_"))
338
+ };
339
+ };
340
+
341
+ exports.copyShape = copyShape;