@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.
- package/README.md +5 -5
- package/dist/components/App.js +236 -423
- package/dist/components/Canvas.js +120 -139
- package/dist/components/common/Button.js +41 -50
- package/dist/components/common/Loading.js +24 -33
- package/dist/components/common/Modal.js +18 -17
- package/dist/components/common/Panel.js +20 -28
- package/dist/components/common/Snackbar.js +31 -38
- package/dist/components/settings/ColorField.js +59 -68
- package/dist/components/settings/DeleteButton.js +10 -9
- package/dist/components/settings/FontFamilyField.js +25 -20
- package/dist/components/settings/LayoutButton.js +11 -9
- package/dist/components/settings/Layouts.js +111 -111
- package/dist/components/settings/LineArrowField.js +25 -22
- package/dist/components/settings/LineTypeField.js +23 -20
- package/dist/components/settings/PointsNumberField.js +14 -13
- package/dist/components/settings/RangeField.js +35 -47
- package/dist/components/settings/SelectField.js +34 -43
- package/dist/components/settings/SettingsBar.js +100 -121
- package/dist/components/settings/ToggleField.js +18 -25
- package/dist/components/toolbox/EditTextBox.js +62 -49
- package/dist/components/toolbox/LoadFileTool.js +19 -21
- package/dist/components/toolbox/MenuGroup.js +66 -100
- package/dist/components/toolbox/PictureUrlInput.js +33 -62
- package/dist/components/toolbox/Tool.js +13 -13
- package/dist/components/toolbox/Toolbar.js +79 -113
- package/dist/components/toolbox/ToolbarGroup.js +60 -63
- package/dist/constants/app.js +51 -0
- package/dist/constants/icons.js +42 -48
- package/dist/constants/loading.js +8 -0
- package/dist/constants/shapes.js +10 -8
- package/dist/constants/snackbar.js +7 -15
- package/dist/constants/style.js +21 -18
- package/dist/constants/tools.js +99 -103
- package/dist/hooks/useComponent.js +15 -20
- package/dist/hooks/useDoubleClick.js +12 -12
- package/dist/hooks/useDrag.js +24 -32
- package/dist/hooks/useDrawableCanvas.js +106 -122
- package/dist/hooks/useKeyboard.js +80 -63
- package/dist/hooks/useResizeObserver.js +18 -16
- package/dist/hooks/useShapes.js +92 -144
- package/dist/hooks/useSnackbar.js +28 -58
- package/dist/styles.css +16 -13
- package/dist/types/Mode.js +1 -13
- package/dist/types/Shapes.js +1 -17
- package/dist/types/snackbar.js +5 -0
- package/dist/types/tools.js +1 -18
- package/dist/utils/canvas.js +49 -0
- package/dist/utils/data.js +15 -274
- package/dist/utils/dom.js +12 -0
- package/dist/utils/file.js +75 -120
- package/dist/utils/intersect.js +54 -131
- package/dist/utils/migration.js +59 -0
- package/dist/utils/selection.js +18 -20
- package/dist/utils/shapes/brush.js +135 -0
- package/dist/utils/shapes/circle.js +93 -0
- package/dist/utils/shapes/curve.js +55 -0
- package/dist/utils/shapes/default.js +73 -0
- package/dist/utils/shapes/ellipse.js +101 -0
- package/dist/utils/shapes/grid.js +36 -0
- package/dist/utils/shapes/index.js +341 -0
- package/dist/utils/shapes/line.js +179 -0
- package/dist/utils/shapes/picture.js +88 -0
- package/dist/utils/shapes/polygon.js +69 -0
- package/dist/utils/shapes/rectangle.js +100 -0
- package/dist/utils/shapes/text.js +128 -0
- package/dist/utils/shapes/triangle.js +24 -0
- package/dist/utils/string.js +10 -16
- package/dist/utils/style.js +11 -13
- package/dist/utils/toolbar.js +35 -49
- package/dist/utils/transform.js +47 -339
- package/dist/utils/trigo.js +42 -0
- package/package.json +6 -6
- package/Changelog.md +0 -119
- package/dist/types/keyboard.js +0 -28
- package/dist/utils/draw.js +0 -461
- package/dist/utils/shapeData.js +0 -173
package/dist/utils/intersect.js
CHANGED
|
@@ -5,36 +5,27 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", {
|
|
6
6
|
value: true
|
|
7
7
|
});
|
|
8
|
-
exports.
|
|
9
|
-
|
|
10
|
-
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
|
8
|
+
exports.getPointPositionBeforeCanvasTransformation = exports.getPointPositionAfterCanvasTransformation = exports.getCursorPosition = exports.checkPositionIntersection = void 0;
|
|
11
9
|
|
|
12
10
|
var _shapes = require("../constants/shapes");
|
|
13
11
|
|
|
14
12
|
var _fp = _interopRequireDefault(require("lodash/fp"));
|
|
15
13
|
|
|
16
|
-
var
|
|
17
|
-
|
|
18
|
-
var _Shapes = require("../types/Shapes");
|
|
19
|
-
|
|
20
|
-
var _shapeData = require("./shapeData");
|
|
21
|
-
|
|
22
|
-
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
|
|
14
|
+
var _shapes2 = require("./shapes");
|
|
23
15
|
|
|
24
|
-
|
|
16
|
+
var _trigo = require("./trigo");
|
|
25
17
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
var getCursorPosition = function getCursorPosition(e, canvas, givenWidth, givenHeight) {
|
|
18
|
+
const getCursorPosition = function (e, canvas, givenWidth, givenHeight) {
|
|
29
19
|
var _canvas$getBoundingCl;
|
|
30
20
|
|
|
31
|
-
|
|
21
|
+
let scaleRatio = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
|
|
32
22
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
23
|
+
const {
|
|
24
|
+
clientX,
|
|
25
|
+
clientY
|
|
26
|
+
} = _fp.default.getOr(_fp.default.getOr(e, 'changedTouches[0]', e), 'touches[0]', e);
|
|
36
27
|
|
|
37
|
-
|
|
28
|
+
const canvasBoundingRect = (_canvas$getBoundingCl = canvas === null || canvas === void 0 ? void 0 : canvas.getBoundingClientRect()) !== null && _canvas$getBoundingCl !== void 0 ? _canvas$getBoundingCl : {
|
|
38
29
|
left: 0,
|
|
39
30
|
top: 0,
|
|
40
31
|
width: givenWidth,
|
|
@@ -45,154 +36,86 @@ var getCursorPosition = function getCursorPosition(e, canvas, givenWidth, givenH
|
|
|
45
36
|
|
|
46
37
|
exports.getCursorPosition = getCursorPosition;
|
|
47
38
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
width: givenWidth,
|
|
57
|
-
height: givenHeight
|
|
58
|
-
};
|
|
59
|
-
return [canvasBoundingRect.left + positionInCanvas[0] * (canvasBoundingRect.width / givenWidth), canvasBoundingRect.top + positionInCanvas[1] * (canvasBoundingRect.height / givenHeight)];
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
exports.getSettingsPosition = getSettingsPosition;
|
|
63
|
-
|
|
64
|
-
var isPointInsideRect = function isPointInsideRect(rect, point) {
|
|
65
|
-
return point[0] >= rect.x && point[0] <= rect.x + rect.width && point[1] >= rect.y && point[1] <= rect.y + rect.height;
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
var getPointPositionAfterCanvasTransformation = function getPointPositionAfterCanvasTransformation(_ref, _ref2, _ref3, shapeRotation, _ref4) {
|
|
69
|
-
var _ref5 = (0, _slicedToArray2["default"])(_ref, 2),
|
|
70
|
-
positionX = _ref5[0],
|
|
71
|
-
positionY = _ref5[1];
|
|
72
|
-
|
|
73
|
-
var _ref6 = (0, _slicedToArray2["default"])(_ref2, 2),
|
|
74
|
-
canvasOffsetX = _ref6[0],
|
|
75
|
-
canvasOffsetY = _ref6[1];
|
|
76
|
-
|
|
77
|
-
var _ref7 = (0, _slicedToArray2["default"])(_ref3, 2),
|
|
78
|
-
translationX = _ref7[0],
|
|
79
|
-
translationY = _ref7[1];
|
|
80
|
-
|
|
81
|
-
var _ref8 = (0, _slicedToArray2["default"])(_ref4, 2),
|
|
82
|
-
centerX = _ref8[0],
|
|
83
|
-
centerY = _ref8[1];
|
|
84
|
-
|
|
85
|
-
var newX = positionX + canvasOffsetX - centerX - translationX;
|
|
86
|
-
var newY = positionY + canvasOffsetY - centerY - translationY;
|
|
87
|
-
var rotatedY = newY * Math.cos(shapeRotation) - newX * Math.sin(shapeRotation);
|
|
88
|
-
var rotatedX = newY * Math.sin(shapeRotation) + newX * Math.cos(shapeRotation);
|
|
89
|
-
var translatedX = rotatedX + centerX;
|
|
90
|
-
var translatedY = rotatedY + centerY;
|
|
91
|
-
return [translatedX, translatedY];
|
|
39
|
+
const getPointPositionAfterCanvasTransformation = (point, shapeRotation, _ref, _ref2) => {
|
|
40
|
+
let [originX, originY] = _ref;
|
|
41
|
+
let [canvasOffsetX, canvasOffsetY] = _ref2;
|
|
42
|
+
return (0, _trigo.rotatePoint)({
|
|
43
|
+
origin: [originX - canvasOffsetX, originY - canvasOffsetY],
|
|
44
|
+
point,
|
|
45
|
+
rotation: shapeRotation
|
|
46
|
+
});
|
|
92
47
|
};
|
|
93
48
|
|
|
94
49
|
exports.getPointPositionAfterCanvasTransformation = getPointPositionAfterCanvasTransformation;
|
|
95
50
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
var _ref14 = (0, _slicedToArray2["default"])(_ref11, 2),
|
|
106
|
-
centerX = _ref14[0],
|
|
107
|
-
centerY = _ref14[1];
|
|
108
|
-
|
|
109
|
-
var newX = positionX - centerX;
|
|
110
|
-
var newY = positionY - centerY;
|
|
111
|
-
var rotatedY = newY * Math.cos(-shapeRotation) - newX * Math.sin(-shapeRotation);
|
|
112
|
-
var rotatedX = newY * Math.sin(-shapeRotation) + newX * Math.cos(-shapeRotation);
|
|
113
|
-
var translatedX = rotatedX + centerX;
|
|
114
|
-
var translatedY = rotatedY + centerY;
|
|
115
|
-
return [translatedX - translationX, translatedY - translationY];
|
|
51
|
+
const getPointPositionBeforeCanvasTransformation = (point, shapeRotation, _ref3, _ref4) => {
|
|
52
|
+
let [originX, originY] = _ref3;
|
|
53
|
+
let [canvasOffsetX, canvasOffsetY] = _ref4;
|
|
54
|
+
return (0, _trigo.rotatePoint)({
|
|
55
|
+
origin: [originX - canvasOffsetX, originY - canvasOffsetY],
|
|
56
|
+
point,
|
|
57
|
+
rotation: -shapeRotation
|
|
58
|
+
});
|
|
116
59
|
};
|
|
117
60
|
|
|
118
61
|
exports.getPointPositionBeforeCanvasTransformation = getPointPositionBeforeCanvasTransformation;
|
|
119
62
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
return [rotatedX, rotatedY];
|
|
124
|
-
};
|
|
125
|
-
|
|
126
|
-
exports.applyRotationToVector = applyRotationToVector;
|
|
127
|
-
|
|
128
|
-
var checkPositionIntersection = function checkPositionIntersection(shape, position, canvasOffset, selectionPadding) {
|
|
129
|
-
var currentScale = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
|
|
130
|
-
var checkAnchors = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : false;
|
|
63
|
+
const checkPositionIntersection = function (shape, position, canvasOffset, selectionPadding) {
|
|
64
|
+
let currentScale = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
|
|
65
|
+
let checkAnchors = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : false;
|
|
131
66
|
if (shape.locked) return false;
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
var newPosition = getPointPositionAfterCanvasTransformation(position, canvasOffset, shape.translation, shape.rotation, center);
|
|
67
|
+
const scaleWithMinCap = Math.min(1, currentScale);
|
|
68
|
+
const {
|
|
69
|
+
borders,
|
|
70
|
+
center
|
|
71
|
+
} = (0, _shapes2.getShapeInfos)(shape, selectionPadding);
|
|
72
|
+
const newPosition = getPointPositionAfterCanvasTransformation(position, shape.rotation, center, canvasOffset);
|
|
139
73
|
|
|
140
74
|
if (checkAnchors) {
|
|
141
|
-
if (shape.type ===
|
|
142
|
-
for (
|
|
143
|
-
if (isPointInsideRect({
|
|
75
|
+
if (shape.type === 'line' || shape.type === 'polygon' || shape.type === 'curve') {
|
|
76
|
+
for (let i = 0; i < shape.points.length; i++) {
|
|
77
|
+
if ((0, _trigo.isPointInsideRect)({
|
|
144
78
|
x: shape.points[i][0] - _shapes.SELECTION_ANCHOR_SIZE / 2 / scaleWithMinCap,
|
|
145
79
|
y: shape.points[i][1] - _shapes.SELECTION_ANCHOR_SIZE / 2 / scaleWithMinCap,
|
|
146
80
|
width: _shapes.SELECTION_ANCHOR_SIZE / scaleWithMinCap,
|
|
147
81
|
height: _shapes.SELECTION_ANCHOR_SIZE / scaleWithMinCap
|
|
148
82
|
}, newPosition)) {
|
|
149
83
|
return {
|
|
150
|
-
mode:
|
|
84
|
+
mode: 'resize',
|
|
151
85
|
anchor: i
|
|
152
86
|
};
|
|
153
87
|
}
|
|
154
88
|
}
|
|
155
89
|
} else {
|
|
156
|
-
if (isPointInsideRect({
|
|
90
|
+
if ((0, _trigo.isPointInsideRect)({
|
|
157
91
|
x: borders.x + borders.width / 2 - _shapes.SELECTION_ANCHOR_SIZE / 2 / scaleWithMinCap,
|
|
158
92
|
y: borders.y - _shapes.SELECTION_ANCHOR_SIZE / scaleWithMinCap - _shapes.SELECTION_ROTATED_ANCHOR_POSITION / scaleWithMinCap,
|
|
159
93
|
width: _shapes.SELECTION_ANCHOR_SIZE / scaleWithMinCap,
|
|
160
94
|
height: _shapes.SELECTION_ANCHOR_SIZE / scaleWithMinCap
|
|
161
95
|
}, newPosition)) {
|
|
162
96
|
return {
|
|
163
|
-
mode:
|
|
97
|
+
mode: 'rotate'
|
|
164
98
|
};
|
|
165
99
|
}
|
|
166
100
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
height: _shapes.SELECTION_ANCHOR_SIZE / scaleWithMinCap
|
|
179
|
-
}, newPosition)) {
|
|
180
|
-
return {
|
|
181
|
-
mode: _Mode.SelectionModeLib.resize,
|
|
182
|
-
anchor: anchorPosition
|
|
183
|
-
};
|
|
184
|
-
}
|
|
101
|
+
for (const anchorPosition of _shapes.SELECTION_RESIZE_ANCHOR_POSITIONS) {
|
|
102
|
+
if ((0, _trigo.isPointInsideRect)({
|
|
103
|
+
x: borders.x + borders.width * anchorPosition[0] - _shapes.SELECTION_ANCHOR_SIZE / 2 / scaleWithMinCap,
|
|
104
|
+
y: borders.y + borders.height * anchorPosition[1] - _shapes.SELECTION_ANCHOR_SIZE / 2 / scaleWithMinCap,
|
|
105
|
+
width: _shapes.SELECTION_ANCHOR_SIZE / scaleWithMinCap,
|
|
106
|
+
height: _shapes.SELECTION_ANCHOR_SIZE / scaleWithMinCap
|
|
107
|
+
}, newPosition)) {
|
|
108
|
+
return {
|
|
109
|
+
mode: 'resize',
|
|
110
|
+
anchor: anchorPosition
|
|
111
|
+
};
|
|
185
112
|
}
|
|
186
|
-
} catch (err) {
|
|
187
|
-
_iterator.e(err);
|
|
188
|
-
} finally {
|
|
189
|
-
_iterator.f();
|
|
190
113
|
}
|
|
191
114
|
}
|
|
192
115
|
}
|
|
193
116
|
|
|
194
|
-
return isPointInsideRect(borders, newPosition) ? {
|
|
195
|
-
mode:
|
|
117
|
+
return (0, _trigo.isPointInsideRect)(borders, newPosition) ? {
|
|
118
|
+
mode: 'translate'
|
|
196
119
|
} : false;
|
|
197
120
|
};
|
|
198
121
|
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.migrateShapesV065 = void 0;
|
|
9
|
+
|
|
10
|
+
var _fp = _interopRequireDefault(require("lodash/fp"));
|
|
11
|
+
|
|
12
|
+
const migrateShapesV065 = shapes => {
|
|
13
|
+
return _fp.default.map(shape => {
|
|
14
|
+
if (!shape.translation) {
|
|
15
|
+
return shape;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const translation = shape.translation;
|
|
19
|
+
|
|
20
|
+
switch (shape.type) {
|
|
21
|
+
case 'rect':
|
|
22
|
+
case 'square':
|
|
23
|
+
case 'ellipse':
|
|
24
|
+
case 'circle':
|
|
25
|
+
case 'picture':
|
|
26
|
+
return { ..._fp.default.omit(['translation'], shape),
|
|
27
|
+
x: shape.x + translation[0],
|
|
28
|
+
y: shape.y + translation[1]
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
case 'line':
|
|
32
|
+
return { ..._fp.default.omit(['translation'], shape),
|
|
33
|
+
points: shape.points.map(_ref => {
|
|
34
|
+
let [x, y] = _ref;
|
|
35
|
+
return [x + translation[0], y + translation[1]];
|
|
36
|
+
})
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
case 'polygon':
|
|
40
|
+
case 'curve':
|
|
41
|
+
return { ..._fp.default.omit(['translation'], shape),
|
|
42
|
+
points: shape.points.map(_ref2 => {
|
|
43
|
+
let [x, y] = _ref2;
|
|
44
|
+
return [x + translation[0], y + translation[1]];
|
|
45
|
+
})
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
case 'brush':
|
|
49
|
+
return { ..._fp.default.omit(['translation'], shape),
|
|
50
|
+
points: shape.points.map(coord => coord.map(_ref3 => {
|
|
51
|
+
let [x, y] = _ref3;
|
|
52
|
+
return [x + translation[0], y + translation[1]];
|
|
53
|
+
}))
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
}, shapes);
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
exports.migrateShapesV065 = migrateShapesV065;
|
package/dist/utils/selection.js
CHANGED
|
@@ -9,33 +9,31 @@ exports.selectShape = exports.getNewSelectionData = void 0;
|
|
|
9
9
|
|
|
10
10
|
var _fp = _interopRequireDefault(require("lodash/fp"));
|
|
11
11
|
|
|
12
|
-
var _Mode = require("../types/Mode");
|
|
13
|
-
|
|
14
12
|
var _intersect = require("./intersect");
|
|
15
13
|
|
|
16
|
-
var
|
|
14
|
+
var _shapes = require("./shapes");
|
|
17
15
|
|
|
18
|
-
|
|
16
|
+
const getNewSelectionData = (hoverMode, selectedShape, cursorPosition, selectionPadding) => {
|
|
19
17
|
if (hoverMode.mode === 'translate') {
|
|
20
18
|
return {
|
|
21
|
-
mode:
|
|
19
|
+
mode: 'translate',
|
|
22
20
|
cursorStartPosition: cursorPosition,
|
|
23
21
|
originalShape: selectedShape
|
|
24
22
|
};
|
|
25
23
|
} else if (hoverMode.mode === 'rotate') {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
24
|
+
const {
|
|
25
|
+
center: centerBeforeResize
|
|
26
|
+
} = (0, _shapes.getShapeInfos)(selectedShape, selectionPadding);
|
|
27
|
+
const center = [centerBeforeResize[0], centerBeforeResize[1]];
|
|
30
28
|
return {
|
|
31
|
-
mode:
|
|
29
|
+
mode: 'rotate',
|
|
32
30
|
cursorStartPosition: cursorPosition,
|
|
33
31
|
originalShape: selectedShape,
|
|
34
|
-
center
|
|
32
|
+
center
|
|
35
33
|
};
|
|
36
34
|
} else if (hoverMode.mode === 'resize') {
|
|
37
35
|
return {
|
|
38
|
-
mode:
|
|
36
|
+
mode: 'resize',
|
|
39
37
|
cursorStartPosition: cursorPosition,
|
|
40
38
|
originalShape: selectedShape,
|
|
41
39
|
anchor: hoverMode.anchor
|
|
@@ -47,14 +45,14 @@ var getNewSelectionData = function getNewSelectionData(hoverMode, selectedShape,
|
|
|
47
45
|
|
|
48
46
|
exports.getNewSelectionData = getNewSelectionData;
|
|
49
47
|
|
|
50
|
-
|
|
48
|
+
const selectShape = (shapes, cursorPosition, currentScale, canvasOffset, selectedShape, selectionPadding) => {
|
|
51
49
|
if (selectedShape) {
|
|
52
|
-
|
|
53
|
-
mode:
|
|
50
|
+
const positionIntersection = (0, _intersect.checkPositionIntersection)(selectedShape, cursorPosition, canvasOffset, selectionPadding, currentScale, true) || {
|
|
51
|
+
mode: 'default'
|
|
54
52
|
};
|
|
55
|
-
|
|
53
|
+
const newSelectionMode = getNewSelectionData(positionIntersection, selectedShape, cursorPosition, selectionPadding);
|
|
56
54
|
|
|
57
|
-
if ((newSelectionMode === null || newSelectionMode === void 0 ? void 0 : newSelectionMode.mode) ===
|
|
55
|
+
if ((newSelectionMode === null || newSelectionMode === void 0 ? void 0 : newSelectionMode.mode) === 'resize' || (newSelectionMode === null || newSelectionMode === void 0 ? void 0 : newSelectionMode.mode) === 'rotate') {
|
|
58
56
|
return {
|
|
59
57
|
shape: selectedShape,
|
|
60
58
|
mode: newSelectionMode
|
|
@@ -62,7 +60,7 @@ var selectShape = function selectShape(shapes, cursorPosition, currentScale, can
|
|
|
62
60
|
}
|
|
63
61
|
}
|
|
64
62
|
|
|
65
|
-
|
|
63
|
+
const foundShape = _fp.default.find(shape => {
|
|
66
64
|
return !!(0, _intersect.checkPositionIntersection)(shape, cursorPosition, canvasOffset, selectionPadding, currentScale, false);
|
|
67
65
|
}, shapes);
|
|
68
66
|
|
|
@@ -70,7 +68,7 @@ var selectShape = function selectShape(shapes, cursorPosition, currentScale, can
|
|
|
70
68
|
return {
|
|
71
69
|
shape: foundShape,
|
|
72
70
|
mode: {
|
|
73
|
-
mode:
|
|
71
|
+
mode: 'translate',
|
|
74
72
|
cursorStartPosition: cursorPosition,
|
|
75
73
|
originalShape: foundShape
|
|
76
74
|
}
|
|
@@ -79,7 +77,7 @@ var selectShape = function selectShape(shapes, cursorPosition, currentScale, can
|
|
|
79
77
|
return {
|
|
80
78
|
shape: undefined,
|
|
81
79
|
mode: {
|
|
82
|
-
mode:
|
|
80
|
+
mode: 'default'
|
|
83
81
|
}
|
|
84
82
|
};
|
|
85
83
|
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.resizeBrush = exports.getBrushBorder = exports.drawBrush = exports.createBrush = exports.addNewPointToShape = exports.addNewPointGroupToShape = 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
|
+
var _rectangle = require("./rectangle");
|
|
21
|
+
|
|
22
|
+
const createBrush = (shape, cursorPosition) => {
|
|
23
|
+
return {
|
|
24
|
+
toolId: shape.id,
|
|
25
|
+
type: shape.type,
|
|
26
|
+
id: _fp.default.uniqueId("".concat(shape.type, "_")),
|
|
27
|
+
points: [[cursorPosition]],
|
|
28
|
+
rotation: 0,
|
|
29
|
+
style: {
|
|
30
|
+
globalAlpha: shape.settings.opacity.default,
|
|
31
|
+
strokeColor: shape.settings.strokeColor.default,
|
|
32
|
+
lineWidth: shape.settings.lineWidth.default,
|
|
33
|
+
lineDash: shape.settings.lineDash.default
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
exports.createBrush = createBrush;
|
|
39
|
+
|
|
40
|
+
const drawBrush = (ctx, brush) => {
|
|
41
|
+
var _brush$style;
|
|
42
|
+
|
|
43
|
+
if (brush.points.length < 1) return;
|
|
44
|
+
(0, _canvas.updateCanvasContext)(ctx, brush.style);
|
|
45
|
+
ctx.beginPath();
|
|
46
|
+
if (((_brush$style = brush.style) === null || _brush$style === void 0 ? void 0 : _brush$style.strokeColor) === 'transparent' || ctx.globalAlpha === 0) return;
|
|
47
|
+
brush.points.forEach(points => {
|
|
48
|
+
if (points.length === 1) {
|
|
49
|
+
ctx.rect(points[0][0], points[0][1], 1, 1);
|
|
50
|
+
} else {
|
|
51
|
+
ctx.moveTo(...points[0]);
|
|
52
|
+
points.slice(1).forEach(point => {
|
|
53
|
+
ctx.lineTo(...point);
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
ctx.stroke();
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
exports.drawBrush = drawBrush;
|
|
61
|
+
|
|
62
|
+
const getBrushBorder = (brush, selectionPadding) => {
|
|
63
|
+
const brushPoints = _fp.default.flatMap(points => points, brush.points);
|
|
64
|
+
|
|
65
|
+
const minX = _fp.default.flow(_fp.default.map(point => point[0]), _fp.default.min, _fp.default.add(-selectionPadding))(brushPoints);
|
|
66
|
+
|
|
67
|
+
const maxX = _fp.default.flow(_fp.default.map(point => point[0]), _fp.default.max, _fp.default.add(selectionPadding))(brushPoints);
|
|
68
|
+
|
|
69
|
+
const minY = _fp.default.flow(_fp.default.map(point => point[1]), _fp.default.min, _fp.default.add(-selectionPadding))(brushPoints);
|
|
70
|
+
|
|
71
|
+
const maxY = _fp.default.flow(_fp.default.map(point => point[1]), _fp.default.max, _fp.default.add(selectionPadding))(brushPoints);
|
|
72
|
+
|
|
73
|
+
return {
|
|
74
|
+
x: minX,
|
|
75
|
+
width: maxX - minX,
|
|
76
|
+
y: minY,
|
|
77
|
+
height: maxY - minY
|
|
78
|
+
};
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
exports.getBrushBorder = getBrushBorder;
|
|
82
|
+
|
|
83
|
+
const resizeBrush = (cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding, keepRatio) => {
|
|
84
|
+
const {
|
|
85
|
+
center,
|
|
86
|
+
borders
|
|
87
|
+
} = (0, _2.getShapeInfos)(originalShape, selectionPadding);
|
|
88
|
+
const cursorPositionBeforeResize = (0, _intersect.getPointPositionAfterCanvasTransformation)(cursorPosition, originalShape.rotation, center, canvasOffset);
|
|
89
|
+
const scaledWidth = selectionMode.anchor[0] === 0.5 ? 1 : selectionMode.anchor[0] === 0 ? (borders.x + borders.width - cursorPositionBeforeResize[0] + selectionPadding * -2) / (borders.width + selectionPadding * -2) : (cursorPositionBeforeResize[0] - borders.x - selectionPadding * 2) / (borders.width - selectionPadding * 2);
|
|
90
|
+
const scaledHeight = selectionMode.anchor[1] === 0.5 ? 1 : selectionMode.anchor[1] === 0 ? (borders.y + borders.height - cursorPositionBeforeResize[1] + selectionPadding * -2) / (borders.height + selectionPadding * -2) : (cursorPositionBeforeResize[1] - borders.y - selectionPadding * 2) / (borders.height - selectionPadding * 2);
|
|
91
|
+
const [widthWithRatio, heightWithRatio] = keepRatio ? (0, _transform.getNormalizedSize)(1, 1, scaledWidth, scaledHeight) : [scaledWidth, scaledHeight];
|
|
92
|
+
const shapeWithNewDimensions = { ...originalShape,
|
|
93
|
+
points: originalShape.points.map(points => {
|
|
94
|
+
return points.map(point => [(point[0] - borders.x) * widthWithRatio + borders.x, (point[1] - borders.y) * heightWithRatio + borders.y]);
|
|
95
|
+
})
|
|
96
|
+
};
|
|
97
|
+
const {
|
|
98
|
+
center: shapeWithNewDimensionsCenter,
|
|
99
|
+
borders: shapeWithNewDimensionsBorders
|
|
100
|
+
} = (0, _2.getShapeInfos)(shapeWithNewDimensions, selectionPadding);
|
|
101
|
+
const [oppTrueX, oppTrueY] = (0, _rectangle.getRectOppositeAnchorAbsolutePosition)(selectionMode.anchor, center, { ...originalShape,
|
|
102
|
+
...borders
|
|
103
|
+
}, canvasOffset);
|
|
104
|
+
const [newOppTrueX, newOppTrueY] = (0, _rectangle.getRectOppositeAnchorAbsolutePosition)(selectionMode.anchor, shapeWithNewDimensionsCenter, { ...shapeWithNewDimensions,
|
|
105
|
+
...shapeWithNewDimensionsBorders
|
|
106
|
+
}, canvasOffset, [widthWithRatio < 0, heightWithRatio < 0]);
|
|
107
|
+
return { ...shapeWithNewDimensions,
|
|
108
|
+
points: shapeWithNewDimensions.points.map(coord => coord.map(_ref => {
|
|
109
|
+
let [x, y] = _ref;
|
|
110
|
+
return [x - (newOppTrueX - oppTrueX), y - (newOppTrueY - oppTrueY)];
|
|
111
|
+
}))
|
|
112
|
+
};
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
exports.resizeBrush = resizeBrush;
|
|
116
|
+
|
|
117
|
+
const addNewPointToShape = (shape, cursorPosition) => {
|
|
118
|
+
return { ...shape,
|
|
119
|
+
...{
|
|
120
|
+
points: _fp.default.set(shape.points.length - 1, [...shape.points[shape.points.length - 1], [Math.round(cursorPosition[0]), Math.round(cursorPosition[1])]], shape.points)
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
exports.addNewPointToShape = addNewPointToShape;
|
|
126
|
+
|
|
127
|
+
const addNewPointGroupToShape = (shape, cursorPosition) => {
|
|
128
|
+
return { ...shape,
|
|
129
|
+
...{
|
|
130
|
+
points: _fp.default.set(shape.points.length, [[Math.round(cursorPosition[0]), Math.round(cursorPosition[1])]], shape.points)
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
exports.addNewPointGroupToShape = addNewPointGroupToShape;
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.resizeCircle = exports.getCircleBorder = exports.drawCircle = exports.createCircle = 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 _2 = require(".");
|
|
17
|
+
|
|
18
|
+
const createCircle = (shape, cursorPosition) => {
|
|
19
|
+
return {
|
|
20
|
+
toolId: shape.id,
|
|
21
|
+
type: shape.type,
|
|
22
|
+
id: _fp.default.uniqueId("".concat(shape.type, "_")),
|
|
23
|
+
x: cursorPosition[0],
|
|
24
|
+
y: cursorPosition[1],
|
|
25
|
+
radius: 1,
|
|
26
|
+
rotation: 0,
|
|
27
|
+
style: {
|
|
28
|
+
globalAlpha: shape.settings.opacity.default,
|
|
29
|
+
fillColor: shape.settings.fillColor.default,
|
|
30
|
+
strokeColor: shape.settings.strokeColor.default,
|
|
31
|
+
lineWidth: shape.settings.lineWidth.default,
|
|
32
|
+
lineDash: shape.settings.lineDash.default
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
exports.createCircle = createCircle;
|
|
38
|
+
|
|
39
|
+
const drawCircle = (ctx, circle) => {
|
|
40
|
+
var _circle$style, _circle$style2;
|
|
41
|
+
|
|
42
|
+
(0, _canvas.updateCanvasContext)(ctx, circle.style);
|
|
43
|
+
ctx.beginPath();
|
|
44
|
+
ctx.arc(circle.x, circle.y, circle.radius, 0, 2 * Math.PI);
|
|
45
|
+
((_circle$style = circle.style) === null || _circle$style === void 0 ? void 0 : _circle$style.fillColor) !== 'transparent' && ctx.fill();
|
|
46
|
+
((_circle$style2 = circle.style) === null || _circle$style2 === void 0 ? void 0 : _circle$style2.strokeColor) !== 'transparent' && ctx.stroke();
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
exports.drawCircle = drawCircle;
|
|
50
|
+
|
|
51
|
+
const getCircleBorder = (circle, selectionPadding) => {
|
|
52
|
+
return {
|
|
53
|
+
x: circle.x - circle.radius - selectionPadding,
|
|
54
|
+
width: (circle.radius + selectionPadding) * 2,
|
|
55
|
+
y: circle.y - circle.radius - selectionPadding,
|
|
56
|
+
height: (circle.radius + selectionPadding) * 2
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
exports.getCircleBorder = getCircleBorder;
|
|
61
|
+
|
|
62
|
+
const getCircleOppositeAnchorAbsolutePosition = function (anchor, center, shape, canvasOffset) {
|
|
63
|
+
let [negW, negH] = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : [false, false];
|
|
64
|
+
const oppositeX = anchor[0] === 0.5 ? shape.x : anchor[0] === 0 ? shape.x + (negW ? -shape.radius : shape.radius) : shape.x + (negW ? shape.radius : -shape.radius);
|
|
65
|
+
const oppositeY = anchor[1] === 0.5 ? shape.y : anchor[1] === 0 ? shape.y + (negH ? -shape.radius : shape.radius) : shape.y + (negH ? shape.radius : -shape.radius);
|
|
66
|
+
return (0, _intersect.getPointPositionBeforeCanvasTransformation)([oppositeX, oppositeY], shape.rotation, center, canvasOffset);
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const resizeCircle = (cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding) => {
|
|
70
|
+
const {
|
|
71
|
+
center,
|
|
72
|
+
borders
|
|
73
|
+
} = (0, _2.getShapeInfos)(originalShape, selectionPadding);
|
|
74
|
+
const cursorPositionBeforeResize = (0, _intersect.getPointPositionAfterCanvasTransformation)(cursorPosition, originalShape.rotation, center, canvasOffset);
|
|
75
|
+
const newCursorPosition = [cursorPositionBeforeResize[0], cursorPositionBeforeResize[1]];
|
|
76
|
+
const scaledRadius = selectionMode.anchor[1] === 0.5 ? (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 : (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;
|
|
77
|
+
const shapeWithNewDimensions = { ...originalShape,
|
|
78
|
+
...{
|
|
79
|
+
radius: Math.abs(scaledRadius)
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
const {
|
|
83
|
+
center: shapeWithNewDimensionsCenter
|
|
84
|
+
} = (0, _2.getShapeInfos)(shapeWithNewDimensions, selectionPadding);
|
|
85
|
+
const [oppTrueX, oppTrueY] = getCircleOppositeAnchorAbsolutePosition(selectionMode.anchor, center, originalShape, canvasOffset);
|
|
86
|
+
const [newOppTrueX, newOppTrueY] = getCircleOppositeAnchorAbsolutePosition(selectionMode.anchor, shapeWithNewDimensionsCenter, shapeWithNewDimensions, canvasOffset, [scaledRadius < 0, scaledRadius < 0]);
|
|
87
|
+
return { ...shapeWithNewDimensions,
|
|
88
|
+
x: shapeWithNewDimensions.x - (newOppTrueX - oppTrueX),
|
|
89
|
+
y: shapeWithNewDimensions.y - (newOppTrueY - oppTrueY)
|
|
90
|
+
};
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
exports.resizeCircle = resizeCircle;
|