@yorab/react-paint 0.6.3 → 0.6.6
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 +2 -1
- package/dist/components/App.js +235 -387
- package/dist/components/Canvas.js +104 -114
- package/dist/components/common/Button.js +41 -50
- package/dist/components/common/Loading.js +21 -33
- package/dist/components/common/Modal.js +18 -17
- package/dist/components/common/Panel.js +20 -24
- package/dist/components/common/Snackbar.js +27 -34
- 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 +93 -103
- 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 +95 -118
- package/dist/components/settings/ToggleField.js +18 -25
- package/dist/components/toolbox/EditTextBox.js +65 -40
- package/dist/components/toolbox/LoadFileTool.js +19 -21
- package/dist/components/toolbox/MenuGroup.js +60 -96
- package/dist/components/toolbox/PictureUrlInput.js +33 -62
- package/dist/components/toolbox/Tool.js +13 -13
- package/dist/components/toolbox/Toolbar.js +74 -106
- package/dist/components/toolbox/ToolbarGroup.js +41 -57
- package/dist/constants/icons.js +43 -43
- package/dist/constants/shapes.js +6 -6
- package/dist/constants/snackbar.js +3 -3
- package/dist/constants/style.js +13 -13
- package/dist/constants/tools.js +80 -80
- package/dist/hooks/useComponent.js +12 -19
- package/dist/hooks/useDoubleClick.js +12 -12
- package/dist/hooks/useDrag.js +24 -32
- package/dist/hooks/useDrawableCanvas.js +86 -100
- package/dist/hooks/useKeyboard.js +26 -36
- package/dist/hooks/useResizeObserver.js +18 -16
- package/dist/hooks/useShapes.js +91 -143
- package/dist/hooks/useSnackbar.js +28 -58
- package/dist/styles.css +55 -55
- package/dist/types/Mode.js +1 -1
- package/dist/types/Shapes.js +1 -1
- package/dist/types/keyboard.js +2 -2
- package/dist/types/tools.js +1 -1
- package/dist/utils/data.js +91 -107
- package/dist/utils/draw.js +116 -146
- package/dist/utils/file.js +69 -119
- package/dist/utils/intersect.js +57 -97
- package/dist/utils/selection.js +12 -12
- package/dist/utils/shapeData.js +33 -51
- package/dist/utils/string.js +10 -16
- package/dist/utils/style.js +1 -1
- package/dist/utils/toolbar.js +26 -38
- package/dist/utils/transform.js +183 -239
- package/package.json +5 -4
package/dist/utils/transform.js
CHANGED
|
@@ -7,12 +7,6 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
7
7
|
});
|
|
8
8
|
exports.updatePolygonLinesCount = exports.translateShape = exports.transformShape = exports.rotateShape = exports.resizeText = exports.resizeShape = exports.resizeRect = exports.resizePicture = exports.resizeLine = exports.resizeEllipse = exports.resizeCircle = exports.resizeBrush = exports.radiansToDegrees = exports.paintNewPointToShape = exports.getNormalizedSize = exports.fitContentInsideContainer = exports.degreesToRadians = exports.createNewPointGroupToShape = exports.calculateTextWidth = exports.calculateTextFontSize = void 0;
|
|
9
9
|
|
|
10
|
-
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
|
11
|
-
|
|
12
|
-
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
|
13
|
-
|
|
14
|
-
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
15
|
-
|
|
16
10
|
var _fp = _interopRequireDefault(require("lodash/fp"));
|
|
17
11
|
|
|
18
12
|
var _shapeData = require("./shapeData");
|
|
@@ -23,13 +17,9 @@ var _intersect = require("./intersect");
|
|
|
23
17
|
|
|
24
18
|
var _style = require("../constants/style");
|
|
25
19
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
var getNormalizedSize = function getNormalizedSize(originalShape, width, height) {
|
|
31
|
-
var originalRatio = originalShape.width / originalShape.height;
|
|
32
|
-
var newRatio = width / height;
|
|
20
|
+
const getNormalizedSize = (originalShape, width, height) => {
|
|
21
|
+
const originalRatio = originalShape.width / originalShape.height;
|
|
22
|
+
const newRatio = width / height;
|
|
33
23
|
|
|
34
24
|
if (newRatio > originalRatio || height < 0) {
|
|
35
25
|
return width > originalShape.width ? [width, width / originalRatio] : [height * originalRatio, height];
|
|
@@ -42,294 +32,256 @@ var getNormalizedSize = function getNormalizedSize(originalShape, width, height)
|
|
|
42
32
|
|
|
43
33
|
exports.getNormalizedSize = getNormalizedSize;
|
|
44
34
|
|
|
45
|
-
|
|
46
|
-
return
|
|
47
|
-
|
|
48
|
-
|
|
35
|
+
const translateShape = (shape, cursorPosition, originalShape, originalCursorPosition) => {
|
|
36
|
+
return { ...shape,
|
|
37
|
+
...{
|
|
38
|
+
translation: [originalShape.translation[0] + cursorPosition[0] - originalCursorPosition[0], originalShape.translation[1] + cursorPosition[1] - originalCursorPosition[1]]
|
|
39
|
+
}
|
|
40
|
+
};
|
|
49
41
|
};
|
|
50
42
|
|
|
51
43
|
exports.translateShape = translateShape;
|
|
52
44
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
return
|
|
60
|
-
|
|
61
|
-
|
|
45
|
+
const rotateShape = (shape, cursorPosition, originalShape, originalCursorPosition, shapeCenter) => {
|
|
46
|
+
const p1x = shapeCenter[0] + originalShape.translation[0] - originalCursorPosition[0];
|
|
47
|
+
const p1y = shapeCenter[1] + originalShape.translation[1] - originalCursorPosition[1];
|
|
48
|
+
const p2x = shapeCenter[0] + originalShape.translation[0] - cursorPosition[0];
|
|
49
|
+
const p2y = shapeCenter[1] + originalShape.translation[1] - cursorPosition[1];
|
|
50
|
+
const rotation = originalShape.rotation + Math.atan2(p2y, p2x) - Math.atan2(p1y, p1x);
|
|
51
|
+
return { ...shape,
|
|
52
|
+
...{
|
|
53
|
+
rotation
|
|
54
|
+
}
|
|
55
|
+
};
|
|
62
56
|
};
|
|
63
57
|
|
|
64
58
|
exports.rotateShape = rotateShape;
|
|
65
59
|
|
|
66
|
-
|
|
67
|
-
return
|
|
68
|
-
|
|
69
|
-
|
|
60
|
+
const paintNewPointToShape = (shape, cursorPosition) => {
|
|
61
|
+
return { ...shape,
|
|
62
|
+
...{
|
|
63
|
+
points: _fp.default.set(shape.points.length - 1, [...shape.points[shape.points.length - 1], cursorPosition], shape.points)
|
|
64
|
+
}
|
|
65
|
+
};
|
|
70
66
|
};
|
|
71
67
|
|
|
72
68
|
exports.paintNewPointToShape = paintNewPointToShape;
|
|
73
69
|
|
|
74
|
-
|
|
75
|
-
return
|
|
76
|
-
|
|
77
|
-
|
|
70
|
+
const createNewPointGroupToShape = (shape, cursorPosition) => {
|
|
71
|
+
return { ...shape,
|
|
72
|
+
...{
|
|
73
|
+
points: _fp.default.set(shape.points.length, [cursorPosition], shape.points)
|
|
74
|
+
}
|
|
75
|
+
};
|
|
78
76
|
};
|
|
79
77
|
|
|
80
78
|
exports.createNewPointGroupToShape = createNewPointGroupToShape;
|
|
81
79
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
return points.map(function (point) {
|
|
94
|
-
return [(point[0] - borders.x) * scaledWidth + borders.x, (point[1] - borders.y) * scaledHeight + borders.y];
|
|
95
|
-
});
|
|
80
|
+
const resizeBrush = (cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding) => {
|
|
81
|
+
const {
|
|
82
|
+
center,
|
|
83
|
+
borders
|
|
84
|
+
} = (0, _shapeData.getShapeInfos)(originalShape, selectionPadding);
|
|
85
|
+
const cursorPositionBeforeResize = (0, _intersect.getPointPositionAfterCanvasTransformation)(cursorPosition, canvasOffset, originalShape.translation, originalShape.rotation, center);
|
|
86
|
+
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);
|
|
87
|
+
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);
|
|
88
|
+
const shapeWithNewDimensions = { ...originalShape,
|
|
89
|
+
points: originalShape.points.map(points => {
|
|
90
|
+
return points.map(point => [(point[0] - borders.x) * scaledWidth + borders.x, (point[1] - borders.y) * scaledHeight + borders.y]);
|
|
96
91
|
})
|
|
92
|
+
};
|
|
93
|
+
const {
|
|
94
|
+
center: shapeWithNewDimensionsCenter,
|
|
95
|
+
borders: shapeWithNewDimensionsBorders
|
|
96
|
+
} = (0, _shapeData.getShapeInfos)(shapeWithNewDimensions, selectionPadding);
|
|
97
|
+
const [oppTrueX, oppTrueY] = getRectOppositeAnchorAbsolutePosition(selectionMode.anchor, center, { ...originalShape,
|
|
98
|
+
...borders
|
|
97
99
|
});
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
oppTrueY = _getRectOppositeAncho2[1];
|
|
107
|
-
|
|
108
|
-
var _getRectOppositeAncho3 = getRectOppositeAnchorAbsolutePosition(selectionMode.anchor, shapeWithNewDimensionsCenter, _objectSpread(_objectSpread({}, shapeWithNewDimensions), shapeWithNewDimensionsBorders), [scaledWidth < 0, scaledHeight < 0]),
|
|
109
|
-
_getRectOppositeAncho4 = (0, _slicedToArray2["default"])(_getRectOppositeAncho3, 2),
|
|
110
|
-
newOppTrueX = _getRectOppositeAncho4[0],
|
|
111
|
-
newOppTrueY = _getRectOppositeAncho4[1];
|
|
112
|
-
|
|
113
|
-
return _objectSpread(_objectSpread({}, shapeWithNewDimensions), {
|
|
114
|
-
translation: [originalShape.translation[0] - (newOppTrueX - oppTrueX), originalShape.translation[1] - (newOppTrueY - oppTrueY)]
|
|
115
|
-
});
|
|
100
|
+
const [newOppTrueX, newOppTrueY] = getRectOppositeAnchorAbsolutePosition(selectionMode.anchor, shapeWithNewDimensionsCenter, { ...shapeWithNewDimensions,
|
|
101
|
+
...shapeWithNewDimensionsBorders
|
|
102
|
+
}, [scaledWidth < 0, scaledHeight < 0]);
|
|
103
|
+
return { ...shapeWithNewDimensions,
|
|
104
|
+
...{
|
|
105
|
+
translation: [originalShape.translation[0] - (newOppTrueX - oppTrueX), originalShape.translation[1] - (newOppTrueY - oppTrueY)]
|
|
106
|
+
}
|
|
107
|
+
};
|
|
116
108
|
};
|
|
117
109
|
|
|
118
110
|
exports.resizeBrush = resizeBrush;
|
|
119
111
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
112
|
+
const resizeLine = (cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding) => {
|
|
113
|
+
const {
|
|
114
|
+
center
|
|
115
|
+
} = (0, _shapeData.getShapeInfos)(originalShape, selectionPadding);
|
|
116
|
+
const cursorPositionBeforeResize = (0, _intersect.getPointPositionAfterCanvasTransformation)(cursorPosition, canvasOffset, originalShape.translation, originalShape.rotation, center);
|
|
125
117
|
|
|
126
|
-
|
|
118
|
+
const updatedShape = _fp.default.set(['points', selectionMode.anchor], cursorPositionBeforeResize, originalShape);
|
|
127
119
|
|
|
128
120
|
return updatedShape;
|
|
129
121
|
};
|
|
130
122
|
|
|
131
123
|
exports.resizeLine = resizeLine;
|
|
132
124
|
|
|
133
|
-
|
|
134
|
-
|
|
125
|
+
const updatePolygonLinesCount = (shape, newPointsCount) => {
|
|
126
|
+
const currentPointsCount = shape.points.length;
|
|
135
127
|
if (currentPointsCount === newPointsCount) return shape;
|
|
136
128
|
|
|
137
129
|
if (currentPointsCount > newPointsCount) {
|
|
138
|
-
|
|
139
|
-
return
|
|
130
|
+
const totalPoints = shape.points.slice(0, newPointsCount);
|
|
131
|
+
return { ...shape,
|
|
140
132
|
points: totalPoints,
|
|
141
|
-
style:
|
|
133
|
+
style: { ...shape.style,
|
|
142
134
|
pointsCount: totalPoints.length
|
|
143
|
-
}
|
|
144
|
-
}
|
|
135
|
+
}
|
|
136
|
+
};
|
|
145
137
|
} else {
|
|
146
138
|
//TODO : better distribution for new points
|
|
147
|
-
|
|
139
|
+
const nbPointsToAdd = newPointsCount - currentPointsCount;
|
|
148
140
|
|
|
149
|
-
|
|
150
|
-
return [shape.points[0][0] + (shape.points[1][0] - shape.points[0][0]) * (index + 1) / (nbPointsToAdd + 1), shape.points[0][1] + (shape.points[1][1] - shape.points[0][1]) * (index + 1) / (nbPointsToAdd + 1)];
|
|
151
|
-
}))(nbPointsToAdd);
|
|
141
|
+
const newPoints = _fp.default.flow(_fp.default.range(0), _fp.default.map(index => [shape.points[0][0] + (shape.points[1][0] - shape.points[0][0]) * (index + 1) / (nbPointsToAdd + 1), shape.points[0][1] + (shape.points[1][1] - shape.points[0][1]) * (index + 1) / (nbPointsToAdd + 1)]))(nbPointsToAdd);
|
|
152
142
|
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
});
|
|
143
|
+
const totalPoints = [shape.points[0], ...newPoints, ...shape.points.slice(1, shape.points.length)];
|
|
144
|
+
return { ...shape,
|
|
145
|
+
points: totalPoints,
|
|
146
|
+
style: { ...shape.style,
|
|
147
|
+
pointsCount: totalPoints.length
|
|
148
|
+
}
|
|
149
|
+
};
|
|
161
150
|
}
|
|
162
151
|
};
|
|
163
152
|
|
|
164
153
|
exports.updatePolygonLinesCount = updatePolygonLinesCount;
|
|
165
154
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
newOppTrueX = _getCircleOppositeAnc4[0],
|
|
190
|
-
newOppTrueY = _getCircleOppositeAnc4[1];
|
|
191
|
-
|
|
192
|
-
return _objectSpread(_objectSpread({}, shapeWithNewDimensions), {
|
|
193
|
-
translation: [originalShape.translation[0] - (newOppTrueX - oppTrueX), originalShape.translation[1] - (newOppTrueY - oppTrueY)]
|
|
194
|
-
});
|
|
155
|
+
const resizeCircle = (cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding) => {
|
|
156
|
+
const {
|
|
157
|
+
center,
|
|
158
|
+
borders
|
|
159
|
+
} = (0, _shapeData.getShapeInfos)(originalShape, selectionPadding);
|
|
160
|
+
const cursorPositionBeforeResize = (0, _intersect.getPointPositionAfterCanvasTransformation)(cursorPosition, canvasOffset, originalShape.translation, originalShape.rotation, center);
|
|
161
|
+
const newCursorPosition = [cursorPositionBeforeResize[0], cursorPositionBeforeResize[1]];
|
|
162
|
+
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;
|
|
163
|
+
const shapeWithNewDimensions = { ...originalShape,
|
|
164
|
+
...{
|
|
165
|
+
radius: Math.abs(scaledRadius)
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
const {
|
|
169
|
+
center: shapeWithNewDimensionsCenter
|
|
170
|
+
} = (0, _shapeData.getShapeInfos)(shapeWithNewDimensions, selectionPadding);
|
|
171
|
+
const [oppTrueX, oppTrueY] = getCircleOppositeAnchorAbsolutePosition(selectionMode.anchor, center, originalShape);
|
|
172
|
+
const [newOppTrueX, newOppTrueY] = getCircleOppositeAnchorAbsolutePosition(selectionMode.anchor, shapeWithNewDimensionsCenter, shapeWithNewDimensions, [scaledRadius < 0, scaledRadius < 0]);
|
|
173
|
+
return { ...shapeWithNewDimensions,
|
|
174
|
+
...{
|
|
175
|
+
translation: [originalShape.translation[0] - (newOppTrueX - oppTrueX), originalShape.translation[1] - (newOppTrueY - oppTrueY)]
|
|
176
|
+
}
|
|
177
|
+
};
|
|
195
178
|
};
|
|
196
179
|
|
|
197
180
|
exports.resizeCircle = resizeCircle;
|
|
198
181
|
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
newOppTrueX = _getEllipseOppositeAn4[0],
|
|
225
|
-
newOppTrueY = _getEllipseOppositeAn4[1];
|
|
226
|
-
|
|
227
|
-
return _objectSpread(_objectSpread({}, shapeWithNewDimensions), {
|
|
228
|
-
translation: [originalShape.translation[0] - (newOppTrueX - oppTrueX), originalShape.translation[1] - (newOppTrueY - oppTrueY)]
|
|
229
|
-
});
|
|
182
|
+
const resizeEllipse = (cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding) => {
|
|
183
|
+
const {
|
|
184
|
+
center,
|
|
185
|
+
borders
|
|
186
|
+
} = (0, _shapeData.getShapeInfos)(originalShape, selectionPadding);
|
|
187
|
+
const cursorPositionBeforeResize = (0, _intersect.getPointPositionAfterCanvasTransformation)(cursorPosition, canvasOffset, originalShape.translation, originalShape.rotation, center);
|
|
188
|
+
const newCursorPosition = [cursorPositionBeforeResize[0], cursorPositionBeforeResize[1]];
|
|
189
|
+
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;
|
|
190
|
+
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;
|
|
191
|
+
const shapeWithNewDimensions = { ...originalShape,
|
|
192
|
+
...{
|
|
193
|
+
radiusX: Math.abs(scaledRadiusX),
|
|
194
|
+
radiusY: Math.abs(scaledRadiusY)
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
const {
|
|
198
|
+
center: shapeWithNewDimensionsCenter
|
|
199
|
+
} = (0, _shapeData.getShapeInfos)(shapeWithNewDimensions, selectionPadding);
|
|
200
|
+
const [oppTrueX, oppTrueY] = getEllipseOppositeAnchorAbsolutePosition(selectionMode.anchor, center, originalShape);
|
|
201
|
+
const [newOppTrueX, newOppTrueY] = getEllipseOppositeAnchorAbsolutePosition(selectionMode.anchor, shapeWithNewDimensionsCenter, shapeWithNewDimensions, [scaledRadiusX < 0, scaledRadiusY < 0]);
|
|
202
|
+
return { ...shapeWithNewDimensions,
|
|
203
|
+
...{
|
|
204
|
+
translation: [originalShape.translation[0] - (newOppTrueX - oppTrueX), originalShape.translation[1] - (newOppTrueY - oppTrueY)]
|
|
205
|
+
}
|
|
206
|
+
};
|
|
230
207
|
};
|
|
231
208
|
|
|
232
209
|
exports.resizeEllipse = resizeEllipse;
|
|
233
210
|
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
var _ref = keepRatio ? getNormalizedSize(originalShape, scaledWidth, scaledHeight) : [scaledWidth, scaledHeight],
|
|
246
|
-
_ref2 = (0, _slicedToArray2["default"])(_ref, 2),
|
|
247
|
-
widthWithRatio = _ref2[0],
|
|
248
|
-
heightWithRatio = _ref2[1];
|
|
249
|
-
|
|
250
|
-
var shapeWithNewDimensions = _objectSpread(_objectSpread({}, originalShape), {}, {
|
|
211
|
+
const resizeRect = function (cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding) {
|
|
212
|
+
let keepRatio = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : false;
|
|
213
|
+
const {
|
|
214
|
+
center,
|
|
215
|
+
borders
|
|
216
|
+
} = (0, _shapeData.getShapeInfos)(originalShape, selectionPadding);
|
|
217
|
+
const cursorPositionBeforeResize = (0, _intersect.getPointPositionAfterCanvasTransformation)(cursorPosition, canvasOffset, originalShape.translation, originalShape.rotation, center);
|
|
218
|
+
const scaledWidth = selectionMode.anchor[0] === 0.5 ? originalShape.width : selectionMode.anchor[0] === 0 ? borders.x + borders.width - cursorPositionBeforeResize[0] + selectionPadding * -2 : cursorPositionBeforeResize[0] - borders.x - selectionPadding * 2;
|
|
219
|
+
const scaledHeight = selectionMode.anchor[1] === 0.5 ? originalShape.height : selectionMode.anchor[1] === 0 ? borders.y + borders.height - cursorPositionBeforeResize[1] + selectionPadding * -2 : cursorPositionBeforeResize[1] - borders.y - selectionPadding * 2;
|
|
220
|
+
const [widthWithRatio, heightWithRatio] = keepRatio ? getNormalizedSize(originalShape, scaledWidth, scaledHeight) : [scaledWidth, scaledHeight];
|
|
221
|
+
const shapeWithNewDimensions = { ...originalShape,
|
|
251
222
|
width: Math.abs(widthWithRatio),
|
|
252
223
|
height: Math.abs(heightWithRatio)
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
_getRectOppositeAncho8 = (0, _slicedToArray2["default"])(_getRectOppositeAncho7, 2),
|
|
265
|
-
newOppTrueX = _getRectOppositeAncho8[0],
|
|
266
|
-
newOppTrueY = _getRectOppositeAncho8[1];
|
|
267
|
-
|
|
268
|
-
return _objectSpread(_objectSpread({}, shapeWithNewDimensions), {
|
|
269
|
-
translation: [originalShape.translation[0] - (newOppTrueX - oppTrueX), originalShape.translation[1] - (newOppTrueY - oppTrueY)]
|
|
270
|
-
});
|
|
224
|
+
};
|
|
225
|
+
const {
|
|
226
|
+
center: shapeWithNewDimensionsCenter
|
|
227
|
+
} = (0, _shapeData.getShapeInfos)(shapeWithNewDimensions, selectionPadding);
|
|
228
|
+
const [oppTrueX, oppTrueY] = getRectOppositeAnchorAbsolutePosition(selectionMode.anchor, center, originalShape);
|
|
229
|
+
const [newOppTrueX, newOppTrueY] = getRectOppositeAnchorAbsolutePosition(selectionMode.anchor, shapeWithNewDimensionsCenter, shapeWithNewDimensions, [widthWithRatio < 0, heightWithRatio < 0]);
|
|
230
|
+
return { ...shapeWithNewDimensions,
|
|
231
|
+
...{
|
|
232
|
+
translation: [originalShape.translation[0] - (newOppTrueX - oppTrueX), originalShape.translation[1] - (newOppTrueY - oppTrueY)]
|
|
233
|
+
}
|
|
234
|
+
};
|
|
271
235
|
};
|
|
272
236
|
|
|
273
237
|
exports.resizeRect = resizeRect;
|
|
274
238
|
|
|
275
|
-
|
|
239
|
+
const resizeText = (ctx, cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding) => {
|
|
276
240
|
var _newRect$style$fontBo, _newRect$style, _newRect$style$fontIt, _newRect$style2, _newRect$style3;
|
|
277
241
|
|
|
278
|
-
|
|
279
|
-
return
|
|
242
|
+
const newRect = resizeRect(cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding, true);
|
|
243
|
+
return { ...newRect,
|
|
280
244
|
fontSize: calculateTextFontSize(ctx, newRect.value, newRect.width, (_newRect$style$fontBo = (_newRect$style = newRect.style) === null || _newRect$style === void 0 ? void 0 : _newRect$style.fontBold) !== null && _newRect$style$fontBo !== void 0 ? _newRect$style$fontBo : false, (_newRect$style$fontIt = (_newRect$style2 = newRect.style) === null || _newRect$style2 === void 0 ? void 0 : _newRect$style2.fontItalic) !== null && _newRect$style$fontIt !== void 0 ? _newRect$style$fontIt : false, (_newRect$style3 = newRect.style) === null || _newRect$style3 === void 0 ? void 0 : _newRect$style3.fontFamily)
|
|
281
|
-
}
|
|
245
|
+
};
|
|
282
246
|
};
|
|
283
247
|
|
|
284
248
|
exports.resizeText = resizeText;
|
|
285
249
|
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
negH = _ref4[1];
|
|
291
|
-
|
|
292
|
-
var oppositeX = (anchor[0] === 0.5 ? shape.x + shape.width / 2 : anchor[0] === 0 ? shape.x + (negW ? 0 : shape.width) : shape.x + (negW ? shape.width : 0)) + shape.translation[0];
|
|
293
|
-
var oppositeY = (anchor[1] === 0.5 ? shape.y + shape.height / 2 : anchor[1] === 0 ? shape.y + (negH ? 0 : shape.height) : shape.y + (negH ? shape.height : 0)) + shape.translation[1];
|
|
250
|
+
const getRectOppositeAnchorAbsolutePosition = function (anchor, center, shape) {
|
|
251
|
+
let [negW, negH] = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [false, false];
|
|
252
|
+
const oppositeX = (anchor[0] === 0.5 ? shape.x + shape.width / 2 : anchor[0] === 0 ? shape.x + (negW ? 0 : shape.width) : shape.x + (negW ? shape.width : 0)) + shape.translation[0];
|
|
253
|
+
const oppositeY = (anchor[1] === 0.5 ? shape.y + shape.height / 2 : anchor[1] === 0 ? shape.y + (negH ? 0 : shape.height) : shape.y + (negH ? shape.height : 0)) + shape.translation[1];
|
|
294
254
|
return (0, _intersect.getPointPositionBeforeCanvasTransformation)([oppositeX, oppositeY], shape.translation, shape.rotation, center);
|
|
295
255
|
};
|
|
296
256
|
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
negH = _ref6[1];
|
|
302
|
-
|
|
303
|
-
var oppositeX = (anchor[0] === 0.5 ? shape.x + shape.radiusX / 2 : anchor[0] === 0 ? shape.x + (negW ? -shape.radiusX : shape.radiusX) : shape.x + (negW ? shape.radiusX : -shape.radiusX)) + shape.translation[0];
|
|
304
|
-
var oppositeY = (anchor[1] === 0.5 ? shape.y + shape.radiusY / 2 : anchor[1] === 0 ? shape.y + (negH ? -shape.radiusY : shape.radiusY) : shape.y + (negH ? shape.radiusY : -shape.radiusY)) + shape.translation[1];
|
|
257
|
+
const getEllipseOppositeAnchorAbsolutePosition = function (anchor, center, shape) {
|
|
258
|
+
let [negW, negH] = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [false, false];
|
|
259
|
+
const oppositeX = (anchor[0] === 0.5 ? shape.x + shape.radiusX / 2 : anchor[0] === 0 ? shape.x + (negW ? -shape.radiusX : shape.radiusX) : shape.x + (negW ? shape.radiusX : -shape.radiusX)) + shape.translation[0];
|
|
260
|
+
const oppositeY = (anchor[1] === 0.5 ? shape.y + shape.radiusY / 2 : anchor[1] === 0 ? shape.y + (negH ? -shape.radiusY : shape.radiusY) : shape.y + (negH ? shape.radiusY : -shape.radiusY)) + shape.translation[1];
|
|
305
261
|
return (0, _intersect.getPointPositionBeforeCanvasTransformation)([oppositeX, oppositeY], shape.translation, shape.rotation, center);
|
|
306
262
|
};
|
|
307
263
|
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
negH = _ref8[1];
|
|
313
|
-
|
|
314
|
-
var oppositeX = (anchor[0] === 0.5 ? shape.x : anchor[0] === 0 ? shape.x + (negW ? -shape.radius : shape.radius) : shape.x + (negW ? shape.radius : -shape.radius)) + shape.translation[0];
|
|
315
|
-
var oppositeY = (anchor[1] === 0.5 ? shape.y : anchor[1] === 0 ? shape.y + (negH ? -shape.radius : shape.radius) : shape.y + (negH ? shape.radius : -shape.radius)) + shape.translation[1];
|
|
264
|
+
const getCircleOppositeAnchorAbsolutePosition = function (anchor, center, shape) {
|
|
265
|
+
let [negW, negH] = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [false, false];
|
|
266
|
+
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)) + shape.translation[0];
|
|
267
|
+
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)) + shape.translation[1];
|
|
316
268
|
return (0, _intersect.getPointPositionBeforeCanvasTransformation)([oppositeX, oppositeY], shape.translation, shape.rotation, center);
|
|
317
269
|
};
|
|
318
270
|
|
|
319
|
-
|
|
271
|
+
const resizePicture = (cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding) => {
|
|
320
272
|
return resizeRect(cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding, true);
|
|
321
273
|
};
|
|
322
274
|
|
|
323
275
|
exports.resizePicture = resizePicture;
|
|
324
276
|
|
|
325
|
-
|
|
277
|
+
const resizeShape = (ctx, shape, cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding) => {
|
|
326
278
|
if (shape.type === 'line' || shape.type === 'polygon' || shape.type === 'curve') return resizeLine(cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding);else if (shape.type === 'brush') return resizeBrush(cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding);else if (shape.type === 'circle') return resizeCircle(cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding);else if (shape.type === 'ellipse') return resizeEllipse(cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding);else if (shape.type === 'rect') return resizeRect(cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding);else if (shape.type === 'square') return resizeRect(cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding, true);else if (shape.type === 'text') return resizeText(ctx, cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding);else if (shape.type === 'picture') return resizePicture(cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding);
|
|
327
279
|
return shape;
|
|
328
280
|
};
|
|
329
281
|
|
|
330
282
|
exports.resizeShape = resizeShape;
|
|
331
283
|
|
|
332
|
-
|
|
284
|
+
const transformShape = (ctx, shape, cursorPosition, canvasOffset, selectionMode, selectionPadding) => {
|
|
333
285
|
if (selectionMode.mode === _Mode.SelectionModeLib.brush) {
|
|
334
286
|
return paintNewPointToShape(shape, cursorPosition);
|
|
335
287
|
} else if (selectionMode.mode === _Mode.SelectionModeLib.translate) {
|
|
@@ -345,52 +297,44 @@ var transformShape = function transformShape(ctx, shape, cursorPosition, canvasO
|
|
|
345
297
|
|
|
346
298
|
exports.transformShape = transformShape;
|
|
347
299
|
|
|
348
|
-
|
|
300
|
+
const calculateTextFontSize = function (ctx, text, maxWidth, fontBold, fontItalic) {
|
|
349
301
|
var _$flow;
|
|
350
302
|
|
|
351
|
-
|
|
303
|
+
let fontFamily = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : _style.STYLE_FONT_DEFAULT;
|
|
352
304
|
ctx.font = "".concat(fontItalic ? 'italic' : '', " ").concat(fontBold ? 'bold' : '', " 1px ").concat(fontFamily);
|
|
353
|
-
return (_$flow = _fp
|
|
354
|
-
return maxWidth / ctx.measureText(value).width;
|
|
355
|
-
}), _fp["default"].min)(text)) !== null && _$flow !== void 0 ? _$flow : _style.STYLE_FONT_SIZE_DEFAULT;
|
|
305
|
+
return (_$flow = _fp.default.flow(_fp.default.map(value => maxWidth / ctx.measureText(value).width), _fp.default.min)(text)) !== null && _$flow !== void 0 ? _$flow : _style.STYLE_FONT_SIZE_DEFAULT;
|
|
356
306
|
};
|
|
357
307
|
|
|
358
308
|
exports.calculateTextFontSize = calculateTextFontSize;
|
|
359
309
|
|
|
360
|
-
|
|
310
|
+
const calculateTextWidth = function (ctx, text, fontSize, fontBold, fontItalic) {
|
|
361
311
|
var _$flow2;
|
|
362
312
|
|
|
363
|
-
|
|
313
|
+
let fontFamily = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : _style.STYLE_FONT_DEFAULT;
|
|
364
314
|
ctx.font = "".concat(fontItalic ? 'italic' : '', " ").concat(fontBold ? 'bold' : '', " ").concat(fontSize, "px ").concat(fontFamily);
|
|
365
|
-
return (_$flow2 = _fp
|
|
366
|
-
return ctx.measureText(value).width;
|
|
367
|
-
}), _fp["default"].max)(text)) !== null && _$flow2 !== void 0 ? _$flow2 : 20;
|
|
315
|
+
return (_$flow2 = _fp.default.flow(_fp.default.map(value => ctx.measureText(value).width), _fp.default.max)(text)) !== null && _$flow2 !== void 0 ? _$flow2 : 20;
|
|
368
316
|
};
|
|
369
317
|
|
|
370
318
|
exports.calculateTextWidth = calculateTextWidth;
|
|
371
319
|
|
|
372
|
-
|
|
373
|
-
return degrees * (Math.PI / 180);
|
|
374
|
-
};
|
|
320
|
+
const degreesToRadians = degrees => degrees * (Math.PI / 180);
|
|
375
321
|
|
|
376
322
|
exports.degreesToRadians = degreesToRadians;
|
|
377
323
|
|
|
378
|
-
|
|
379
|
-
return radians * 180 / Math.PI;
|
|
380
|
-
};
|
|
324
|
+
const radiansToDegrees = radians => radians * 180 / Math.PI;
|
|
381
325
|
|
|
382
326
|
exports.radiansToDegrees = radiansToDegrees;
|
|
383
327
|
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
328
|
+
const fitContentInsideContainer = function (contentWidth, contentHeight, containerWidth, containerHeight) {
|
|
329
|
+
let shouldFillContainer = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;
|
|
330
|
+
const contentRatio = contentWidth / contentHeight;
|
|
331
|
+
const containerRatio = containerWidth / containerHeight;
|
|
388
332
|
|
|
389
333
|
if (contentRatio > containerRatio) {
|
|
390
|
-
|
|
334
|
+
const newWidth = shouldFillContainer ? containerWidth : Math.min(contentWidth, containerWidth);
|
|
391
335
|
return [newWidth, newWidth / contentRatio];
|
|
392
336
|
} else {
|
|
393
|
-
|
|
337
|
+
const newHeight = shouldFillContainer ? containerHeight : Math.min(contentHeight, containerHeight);
|
|
394
338
|
return [newHeight * contentRatio, newHeight];
|
|
395
339
|
}
|
|
396
340
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yorab/react-paint",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.6",
|
|
4
4
|
"keywords": [],
|
|
5
5
|
"author": "Yohann Rabatel",
|
|
6
6
|
"license": "MIT",
|
|
@@ -12,9 +12,7 @@
|
|
|
12
12
|
"dist/"
|
|
13
13
|
],
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@linaria/
|
|
16
|
-
"@linaria/react": "^3.0.0-beta.15",
|
|
17
|
-
"lodash": "^4.17.20"
|
|
15
|
+
"@linaria/react": "^3.0.0-beta.15"
|
|
18
16
|
},
|
|
19
17
|
"devDependencies": {
|
|
20
18
|
"@babel/cli": "7.12.8",
|
|
@@ -25,6 +23,7 @@
|
|
|
25
23
|
"@babel/preset-typescript": "^7.16.7",
|
|
26
24
|
"@babel/runtime": "^7.16.7",
|
|
27
25
|
"@linaria/babel-preset": "^3.0.0-beta.15",
|
|
26
|
+
"@linaria/core": "^3.0.0-beta.15",
|
|
28
27
|
"@linaria/shaker": "^3.0.0-beta.15",
|
|
29
28
|
"@linaria/webpack-loader": "^3.0.0-beta.16",
|
|
30
29
|
"@testing-library/jest-dom": "^5.11.4",
|
|
@@ -49,6 +48,7 @@
|
|
|
49
48
|
"eslint-plugin-react-hooks": "^4.2.0",
|
|
50
49
|
"html-webpack-plugin": "^5.5.0",
|
|
51
50
|
"mini-css-extract-plugin": "^2.6.0",
|
|
51
|
+
"lodash": "^4.17.20",
|
|
52
52
|
"prettier": "^2.3.2",
|
|
53
53
|
"react": "18.1.0",
|
|
54
54
|
"react-dom": "18.1.0",
|
|
@@ -58,6 +58,7 @@
|
|
|
58
58
|
"webpack-dev-server": "^4.7.3"
|
|
59
59
|
},
|
|
60
60
|
"peerDependencies": {
|
|
61
|
+
"lodash" : "*",
|
|
61
62
|
"react": "*",
|
|
62
63
|
"react-dom": "*"
|
|
63
64
|
},
|