@yorab/react-paint 0.6.6 → 0.7.2

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 (64) hide show
  1. package/README.md +3 -5
  2. package/dist/components/App.js +30 -57
  3. package/dist/components/Canvas.js +32 -40
  4. package/dist/components/common/Button.js +4 -4
  5. package/dist/components/common/Loading.js +8 -5
  6. package/dist/components/common/Modal.js +3 -3
  7. package/dist/components/common/Panel.js +7 -11
  8. package/dist/components/common/Snackbar.js +6 -6
  9. package/dist/components/settings/ColorField.js +3 -3
  10. package/dist/components/settings/FontFamilyField.js +1 -1
  11. package/dist/components/settings/Layouts.js +36 -26
  12. package/dist/components/settings/LineArrowField.js +3 -3
  13. package/dist/components/settings/LineTypeField.js +2 -2
  14. package/dist/components/settings/RangeField.js +1 -1
  15. package/dist/components/settings/SelectField.js +1 -1
  16. package/dist/components/settings/SettingsBar.js +9 -7
  17. package/dist/components/toolbox/EditTextBox.js +19 -9
  18. package/dist/components/toolbox/MenuGroup.js +9 -7
  19. package/dist/components/toolbox/Toolbar.js +7 -9
  20. package/dist/components/toolbox/ToolbarGroup.js +24 -11
  21. package/dist/constants/app.js +51 -0
  22. package/dist/constants/icons.js +2 -8
  23. package/dist/constants/loading.js +8 -0
  24. package/dist/constants/shapes.js +4 -2
  25. package/dist/constants/snackbar.js +6 -14
  26. package/dist/constants/style.js +12 -9
  27. package/dist/constants/tools.js +20 -24
  28. package/dist/hooks/useComponent.js +3 -1
  29. package/dist/hooks/useDrawableCanvas.js +35 -37
  30. package/dist/hooks/useKeyboard.js +60 -33
  31. package/dist/hooks/useShapes.js +2 -2
  32. package/dist/styles.css +16 -13
  33. package/dist/types/Mode.js +1 -13
  34. package/dist/types/Shapes.js +1 -17
  35. package/dist/types/snackbar.js +5 -0
  36. package/dist/types/tools.js +1 -18
  37. package/dist/utils/canvas.js +49 -0
  38. package/dist/utils/data.js +5 -248
  39. package/dist/utils/dom.js +12 -0
  40. package/dist/utils/file.js +12 -7
  41. package/dist/utils/intersect.js +29 -66
  42. package/dist/utils/migration.js +59 -0
  43. package/dist/utils/selection.js +9 -11
  44. package/dist/utils/shapes/brush.js +135 -0
  45. package/dist/utils/shapes/circle.js +93 -0
  46. package/dist/utils/shapes/curve.js +55 -0
  47. package/dist/utils/shapes/default.js +73 -0
  48. package/dist/utils/shapes/ellipse.js +101 -0
  49. package/dist/utils/shapes/grid.js +36 -0
  50. package/dist/utils/shapes/index.js +341 -0
  51. package/dist/utils/shapes/line.js +179 -0
  52. package/dist/utils/shapes/picture.js +88 -0
  53. package/dist/utils/shapes/polygon.js +69 -0
  54. package/dist/utils/shapes/rectangle.js +100 -0
  55. package/dist/utils/shapes/text.js +128 -0
  56. package/dist/utils/shapes/triangle.js +24 -0
  57. package/dist/utils/style.js +10 -12
  58. package/dist/utils/toolbar.js +9 -11
  59. package/dist/utils/transform.js +25 -261
  60. package/dist/utils/trigo.js +42 -0
  61. package/package.json +7 -8
  62. package/dist/types/keyboard.js +0 -28
  63. package/dist/utils/draw.js +0 -431
  64. package/dist/utils/shapeData.js +0 -155
@@ -5,26 +5,24 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
5
5
  Object.defineProperty(exports, "__esModule", {
6
6
  value: true
7
7
  });
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;
8
+ exports.updatePolygonLinesCount = exports.transformShape = exports.roundForGrid = exports.getNormalizedSize = exports.fitContentInsideContainer = void 0;
9
9
 
10
10
  var _fp = _interopRequireDefault(require("lodash/fp"));
11
11
 
12
- var _shapeData = require("./shapeData");
13
-
14
- var _Mode = require("../types/Mode");
12
+ var _style = require("../constants/style");
15
13
 
16
- var _intersect = require("./intersect");
14
+ var _shapes = require("./shapes");
17
15
 
18
- var _style = require("../constants/style");
16
+ var _brush = require("./shapes/brush");
19
17
 
20
- const getNormalizedSize = (originalShape, width, height) => {
21
- const originalRatio = originalShape.width / originalShape.height;
18
+ const getNormalizedSize = (originalWidth, originalHeight, width, height) => {
19
+ const originalRatio = originalWidth / originalHeight;
22
20
  const newRatio = width / height;
23
21
 
24
22
  if (newRatio > originalRatio || height < 0) {
25
- return width > originalShape.width ? [width, width / originalRatio] : [height * originalRatio, height];
23
+ return width > originalWidth ? [width, width / originalRatio] : [height * originalRatio, height];
26
24
  } else if (newRatio < originalRatio) {
27
- return height > originalShape.height ? [height * originalRatio, height] : [width, width / originalRatio];
25
+ return height > originalHeight ? [height * originalRatio, height] : [width, width / originalRatio];
28
26
  }
29
27
 
30
28
  return [width, height];
@@ -32,95 +30,12 @@ const getNormalizedSize = (originalShape, width, height) => {
32
30
 
33
31
  exports.getNormalizedSize = getNormalizedSize;
34
32
 
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
- };
41
- };
42
-
43
- exports.translateShape = translateShape;
44
-
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
- };
56
- };
57
-
58
- exports.rotateShape = rotateShape;
59
-
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
- };
33
+ const roundForGrid = (value, gridFormat) => {
34
+ const step = value >= 0 ? _style.GRID_STEP[gridFormat - 1] : -_style.GRID_STEP[gridFormat - 1];
35
+ return gridFormat ? value + step / 2 - (value + step / 2) % step : Math.round(value);
66
36
  };
67
37
 
68
- exports.paintNewPointToShape = paintNewPointToShape;
69
-
70
- const createNewPointGroupToShape = (shape, cursorPosition) => {
71
- return { ...shape,
72
- ...{
73
- points: _fp.default.set(shape.points.length, [cursorPosition], shape.points)
74
- }
75
- };
76
- };
77
-
78
- exports.createNewPointGroupToShape = createNewPointGroupToShape;
79
-
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]);
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
99
- });
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
- };
108
- };
109
-
110
- exports.resizeBrush = resizeBrush;
111
-
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);
117
-
118
- const updatedShape = _fp.default.set(['points', selectionMode.anchor], cursorPositionBeforeResize, originalShape);
119
-
120
- return updatedShape;
121
- };
122
-
123
- exports.resizeLine = resizeLine;
38
+ exports.roundForGrid = roundForGrid;
124
39
 
125
40
  const updatePolygonLinesCount = (shape, newPointsCount) => {
126
41
  const currentPointsCount = shape.points.length;
@@ -152,179 +67,28 @@ const updatePolygonLinesCount = (shape, newPointsCount) => {
152
67
 
153
68
  exports.updatePolygonLinesCount = updatePolygonLinesCount;
154
69
 
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
- };
178
- };
179
-
180
- exports.resizeCircle = resizeCircle;
181
-
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
- };
207
- };
208
-
209
- exports.resizeEllipse = resizeEllipse;
210
-
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,
222
- width: Math.abs(widthWithRatio),
223
- height: Math.abs(heightWithRatio)
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
- };
235
- };
236
-
237
- exports.resizeRect = resizeRect;
238
-
239
- const resizeText = (ctx, cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding) => {
240
- var _newRect$style$fontBo, _newRect$style, _newRect$style$fontIt, _newRect$style2, _newRect$style3;
241
-
242
- const newRect = resizeRect(cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding, true);
243
- return { ...newRect,
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)
245
- };
246
- };
247
-
248
- exports.resizeText = resizeText;
249
-
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];
254
- return (0, _intersect.getPointPositionBeforeCanvasTransformation)([oppositeX, oppositeY], shape.translation, shape.rotation, center);
255
- };
256
-
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];
261
- return (0, _intersect.getPointPositionBeforeCanvasTransformation)([oppositeX, oppositeY], shape.translation, shape.rotation, center);
262
- };
70
+ const transformShape = (ctx, shape, cursorPosition, gridFormat, canvasOffset, selectionMode, selectionPadding, isShiftPressed) => {
71
+ switch (selectionMode.mode) {
72
+ case 'brush':
73
+ return (0, _brush.addNewPointToShape)(shape, cursorPosition);
263
74
 
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];
268
- return (0, _intersect.getPointPositionBeforeCanvasTransformation)([oppositeX, oppositeY], shape.translation, shape.rotation, center);
269
- };
75
+ case 'translate':
76
+ return (0, _shapes.translateShape)(cursorPosition, selectionMode.originalShape, selectionMode.cursorStartPosition, gridFormat);
270
77
 
271
- const resizePicture = (cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding) => {
272
- return resizeRect(cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding, true);
273
- };
274
-
275
- exports.resizePicture = resizePicture;
276
-
277
- const resizeShape = (ctx, shape, cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding) => {
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);
279
- return shape;
280
- };
78
+ case 'rotate':
79
+ return (0, _shapes.rotateShape)(shape, cursorPosition, selectionMode.originalShape, selectionMode.cursorStartPosition, selectionMode.center, gridFormat);
281
80
 
282
- exports.resizeShape = resizeShape;
81
+ case 'resize':
82
+ const roundCursorPosition = [roundForGrid(cursorPosition[0], gridFormat), roundForGrid(cursorPosition[1], gridFormat)];
83
+ return (0, _shapes.resizeShape)(ctx, shape, roundCursorPosition, canvasOffset, selectionMode.originalShape, selectionMode, selectionPadding, isShiftPressed);
283
84
 
284
- const transformShape = (ctx, shape, cursorPosition, canvasOffset, selectionMode, selectionPadding) => {
285
- if (selectionMode.mode === _Mode.SelectionModeLib.brush) {
286
- return paintNewPointToShape(shape, cursorPosition);
287
- } else if (selectionMode.mode === _Mode.SelectionModeLib.translate) {
288
- return translateShape(shape, cursorPosition, selectionMode.originalShape, selectionMode.cursorStartPosition);
289
- } else if (selectionMode.mode === _Mode.SelectionModeLib.rotate) {
290
- return rotateShape(shape, cursorPosition, selectionMode.originalShape, selectionMode.cursorStartPosition, selectionMode.center);
291
- } else if (selectionMode.mode === _Mode.SelectionModeLib.resize) {
292
- return resizeShape(ctx, shape, cursorPosition, canvasOffset, selectionMode.originalShape, selectionMode, selectionPadding);
85
+ default:
86
+ return shape;
293
87
  }
294
-
295
- return shape;
296
88
  };
297
89
 
298
90
  exports.transformShape = transformShape;
299
91
 
300
- const calculateTextFontSize = function (ctx, text, maxWidth, fontBold, fontItalic) {
301
- var _$flow;
302
-
303
- let fontFamily = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : _style.STYLE_FONT_DEFAULT;
304
- ctx.font = "".concat(fontItalic ? 'italic' : '', " ").concat(fontBold ? 'bold' : '', " 1px ").concat(fontFamily);
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;
306
- };
307
-
308
- exports.calculateTextFontSize = calculateTextFontSize;
309
-
310
- const calculateTextWidth = function (ctx, text, fontSize, fontBold, fontItalic) {
311
- var _$flow2;
312
-
313
- let fontFamily = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : _style.STYLE_FONT_DEFAULT;
314
- ctx.font = "".concat(fontItalic ? 'italic' : '', " ").concat(fontBold ? 'bold' : '', " ").concat(fontSize, "px ").concat(fontFamily);
315
- return (_$flow2 = _fp.default.flow(_fp.default.map(value => ctx.measureText(value).width), _fp.default.max)(text)) !== null && _$flow2 !== void 0 ? _$flow2 : 20;
316
- };
317
-
318
- exports.calculateTextWidth = calculateTextWidth;
319
-
320
- const degreesToRadians = degrees => degrees * (Math.PI / 180);
321
-
322
- exports.degreesToRadians = degreesToRadians;
323
-
324
- const radiansToDegrees = radians => radians * 180 / Math.PI;
325
-
326
- exports.radiansToDegrees = radiansToDegrees;
327
-
328
92
  const fitContentInsideContainer = function (contentWidth, contentHeight, containerWidth, containerHeight) {
329
93
  let shouldFillContainer = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;
330
94
  const contentRatio = contentWidth / contentHeight;
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.rotatePoint = exports.radiansToDegrees = exports.isPointInsideRect = exports.getAngleFromVector = exports.degreesToRadians = void 0;
7
+
8
+ const degreesToRadians = degrees => degrees * (Math.PI / 180);
9
+
10
+ exports.degreesToRadians = degreesToRadians;
11
+
12
+ const radiansToDegrees = radians => radians * 180 / Math.PI;
13
+
14
+ exports.radiansToDegrees = radiansToDegrees;
15
+
16
+ const isPointInsideRect = (rect, point) => {
17
+ return point[0] >= rect.x && point[0] <= rect.x + rect.width && point[1] >= rect.y && point[1] <= rect.y + rect.height;
18
+ };
19
+
20
+ exports.isPointInsideRect = isPointInsideRect;
21
+
22
+ const rotatePoint = _ref => {
23
+ let {
24
+ origin = [0, 0],
25
+ point,
26
+ rotation
27
+ } = _ref;
28
+ const pointFromOrigin = [point[0] - origin[0], point[1] - origin[1]];
29
+ const rotatedPointY = pointFromOrigin[1] * Math.cos(rotation) - pointFromOrigin[0] * Math.sin(rotation);
30
+ const rotatedPointX = pointFromOrigin[1] * Math.sin(rotation) + pointFromOrigin[0] * Math.cos(rotation);
31
+ return [rotatedPointX + origin[0], rotatedPointY + origin[1]];
32
+ };
33
+
34
+ exports.rotatePoint = rotatePoint;
35
+
36
+ const getAngleFromVector = function (point1, point2) {
37
+ let radian = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
38
+ const radianAngle = Math.atan2(point2[1] - point1[1], point2[0] - point1[0]);
39
+ return radian ? radianAngle : radiansToDegrees(radianAngle);
40
+ };
41
+
42
+ exports.getAngleFromVector = getAngleFromVector;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yorab/react-paint",
3
- "version": "0.6.6",
3
+ "version": "0.7.2",
4
4
  "keywords": [],
5
5
  "author": "Yohann Rabatel",
6
6
  "license": "MIT",
@@ -12,7 +12,8 @@
12
12
  "dist/"
13
13
  ],
14
14
  "dependencies": {
15
- "@linaria/react": "^3.0.0-beta.15"
15
+ "@linaria/react": "3.0.0-beta.22",
16
+ "lodash": "^4.17.20"
16
17
  },
17
18
  "devDependencies": {
18
19
  "@babel/cli": "7.12.8",
@@ -22,10 +23,10 @@
22
23
  "@babel/preset-react": "^7.16.7",
23
24
  "@babel/preset-typescript": "^7.16.7",
24
25
  "@babel/runtime": "^7.16.7",
25
- "@linaria/babel-preset": "^3.0.0-beta.15",
26
- "@linaria/core": "^3.0.0-beta.15",
27
- "@linaria/shaker": "^3.0.0-beta.15",
28
- "@linaria/webpack-loader": "^3.0.0-beta.16",
26
+ "@linaria/babel-preset": "3.0.0-beta.22",
27
+ "@linaria/core": "3.0.0-beta.22",
28
+ "@linaria/webpack-loader": "3.0.0-beta.22",
29
+ "@linaria/shaker": "^3.0.0-beta.22",
29
30
  "@testing-library/jest-dom": "^5.11.4",
30
31
  "@testing-library/react": "^11.1.0",
31
32
  "@testing-library/user-event": "^12.1.10",
@@ -48,7 +49,6 @@
48
49
  "eslint-plugin-react-hooks": "^4.2.0",
49
50
  "html-webpack-plugin": "^5.5.0",
50
51
  "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,7 +58,6 @@
58
58
  "webpack-dev-server": "^4.7.3"
59
59
  },
60
60
  "peerDependencies": {
61
- "lodash" : "*",
62
61
  "react": "*",
63
62
  "react-dom": "*"
64
63
  },
@@ -1,28 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.KeyboardCommand = exports.KeyboardCode = void 0;
7
- let KeyboardCode;
8
- exports.KeyboardCode = KeyboardCode;
9
-
10
- (function (KeyboardCode) {
11
- KeyboardCode["ArrowUp"] = "ArrowUp";
12
- KeyboardCode["ArrowDown"] = "ArrowDown";
13
- KeyboardCode["ArrowLeft"] = "ArrowLeft";
14
- KeyboardCode["ArrowRight"] = "ArrowRight";
15
- KeyboardCode["Delete"] = "Delete";
16
- KeyboardCode["Backspace"] = "Backspace";
17
- KeyboardCode["Escape"] = "Escape";
18
- KeyboardCode["Z"] = "z";
19
- KeyboardCode["Y"] = "y";
20
- })(KeyboardCode || (exports.KeyboardCode = KeyboardCode = {}));
21
-
22
- let KeyboardCommand;
23
- exports.KeyboardCommand = KeyboardCommand;
24
-
25
- (function (KeyboardCommand) {
26
- KeyboardCommand["Copy"] = "copy";
27
- KeyboardCommand["Paste"] = "paste";
28
- })(KeyboardCommand || (exports.KeyboardCommand = KeyboardCommand = {}));