@yorab/react-paint 0.6.4 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/README.md +5 -5
  2. package/dist/components/App.js +236 -423
  3. package/dist/components/Canvas.js +120 -139
  4. package/dist/components/common/Button.js +41 -50
  5. package/dist/components/common/Loading.js +24 -33
  6. package/dist/components/common/Modal.js +18 -17
  7. package/dist/components/common/Panel.js +20 -28
  8. package/dist/components/common/Snackbar.js +31 -38
  9. package/dist/components/settings/ColorField.js +59 -68
  10. package/dist/components/settings/DeleteButton.js +10 -9
  11. package/dist/components/settings/FontFamilyField.js +25 -20
  12. package/dist/components/settings/LayoutButton.js +11 -9
  13. package/dist/components/settings/Layouts.js +111 -111
  14. package/dist/components/settings/LineArrowField.js +25 -22
  15. package/dist/components/settings/LineTypeField.js +23 -20
  16. package/dist/components/settings/PointsNumberField.js +14 -13
  17. package/dist/components/settings/RangeField.js +35 -47
  18. package/dist/components/settings/SelectField.js +34 -43
  19. package/dist/components/settings/SettingsBar.js +100 -121
  20. package/dist/components/settings/ToggleField.js +18 -25
  21. package/dist/components/toolbox/EditTextBox.js +62 -49
  22. package/dist/components/toolbox/LoadFileTool.js +19 -21
  23. package/dist/components/toolbox/MenuGroup.js +66 -100
  24. package/dist/components/toolbox/PictureUrlInput.js +33 -62
  25. package/dist/components/toolbox/Tool.js +13 -13
  26. package/dist/components/toolbox/Toolbar.js +79 -113
  27. package/dist/components/toolbox/ToolbarGroup.js +60 -63
  28. package/dist/constants/app.js +51 -0
  29. package/dist/constants/icons.js +42 -48
  30. package/dist/constants/loading.js +8 -0
  31. package/dist/constants/shapes.js +10 -8
  32. package/dist/constants/snackbar.js +7 -15
  33. package/dist/constants/style.js +21 -18
  34. package/dist/constants/tools.js +99 -103
  35. package/dist/hooks/useComponent.js +15 -20
  36. package/dist/hooks/useDoubleClick.js +12 -12
  37. package/dist/hooks/useDrag.js +24 -32
  38. package/dist/hooks/useDrawableCanvas.js +106 -122
  39. package/dist/hooks/useKeyboard.js +80 -63
  40. package/dist/hooks/useResizeObserver.js +18 -16
  41. package/dist/hooks/useShapes.js +92 -144
  42. package/dist/hooks/useSnackbar.js +28 -58
  43. package/dist/styles.css +16 -13
  44. package/dist/types/Mode.js +1 -13
  45. package/dist/types/Shapes.js +1 -17
  46. package/dist/types/snackbar.js +5 -0
  47. package/dist/types/tools.js +1 -18
  48. package/dist/utils/canvas.js +49 -0
  49. package/dist/utils/data.js +15 -274
  50. package/dist/utils/dom.js +12 -0
  51. package/dist/utils/file.js +75 -120
  52. package/dist/utils/intersect.js +54 -131
  53. package/dist/utils/migration.js +59 -0
  54. package/dist/utils/selection.js +18 -20
  55. package/dist/utils/shapes/brush.js +135 -0
  56. package/dist/utils/shapes/circle.js +93 -0
  57. package/dist/utils/shapes/curve.js +55 -0
  58. package/dist/utils/shapes/default.js +73 -0
  59. package/dist/utils/shapes/ellipse.js +101 -0
  60. package/dist/utils/shapes/grid.js +36 -0
  61. package/dist/utils/shapes/index.js +341 -0
  62. package/dist/utils/shapes/line.js +179 -0
  63. package/dist/utils/shapes/picture.js +88 -0
  64. package/dist/utils/shapes/polygon.js +69 -0
  65. package/dist/utils/shapes/rectangle.js +100 -0
  66. package/dist/utils/shapes/text.js +128 -0
  67. package/dist/utils/shapes/triangle.js +24 -0
  68. package/dist/utils/string.js +10 -16
  69. package/dist/utils/style.js +11 -13
  70. package/dist/utils/toolbar.js +35 -49
  71. package/dist/utils/transform.js +47 -339
  72. package/dist/utils/trigo.js +42 -0
  73. package/package.json +6 -6
  74. package/Changelog.md +0 -119
  75. package/dist/types/keyboard.js +0 -28
  76. package/dist/utils/draw.js +0 -461
  77. package/dist/utils/shapeData.js +0 -173
@@ -0,0 +1,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.4",
3
+ "version": "0.7.1",
4
4
  "keywords": [],
5
5
  "author": "Yohann Rabatel",
6
6
  "license": "MIT",
@@ -12,8 +12,7 @@
12
12
  "dist/"
13
13
  ],
14
14
  "dependencies": {
15
- "@linaria/core": "^3.0.0-beta.15",
16
- "@linaria/react": "^3.0.0-beta.15",
15
+ "@linaria/react": "3.0.0-beta.22",
17
16
  "lodash": "^4.17.20"
18
17
  },
19
18
  "devDependencies": {
@@ -24,9 +23,10 @@
24
23
  "@babel/preset-react": "^7.16.7",
25
24
  "@babel/preset-typescript": "^7.16.7",
26
25
  "@babel/runtime": "^7.16.7",
27
- "@linaria/babel-preset": "^3.0.0-beta.15",
28
- "@linaria/shaker": "^3.0.0-beta.15",
29
- "@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",
30
30
  "@testing-library/jest-dom": "^5.11.4",
31
31
  "@testing-library/react": "^11.1.0",
32
32
  "@testing-library/user-event": "^12.1.10",
package/Changelog.md DELETED
@@ -1,119 +0,0 @@
1
- # V0.6.4
2
-
3
- - fix TextEdit copy/paste containing html
4
- - add clearCallback in opts to override clear button behavior
5
-
6
- # V0.6.3
7
-
8
- - Rename 'lib' prop in 'label'
9
- - update dash settings
10
- - update clear icon
11
- - update color settings UI
12
-
13
- # V0.6.2
14
-
15
- - Lock : can't select shape by click on it (only via layers panel)
16
- - Clear : reset state to shapes prop by default if not undefined
17
- - update icons
18
- - Add responsive to settings UI
19
-
20
- # V0.6.1
21
-
22
- - Fix shapes prop not refreshing component (introduced by 0.6.0)
23
-
24
- # V0.6.0
25
-
26
- - Fix text opacity range dancing in edit mode
27
- - Fix opacity not visually shown in edit mode
28
- - add delete button in layouts panel and update style
29
- - update shape selection priority : use layers order, unless an anchor is
30
- - stored data format updated
31
-
32
- # V0.5.1
33
-
34
- - add possibility to update default settings for a tool
35
- - fix png export not working with url pic
36
- - fix pictures missing id
37
- - fix text tool
38
- - double click for editing text is now working on mobile
39
- - add bold and italic for text
40
-
41
- # V0.5.0
42
-
43
- - Add tool presets feature
44
-
45
- # V.0.4.2
46
-
47
- - fix toolgroup not displayed
48
- - add passive prop to event listener
49
-
50
- # V.0.4.1
51
-
52
- - prevent mounting selection canvas when viewer mode
53
- - prevent drawing selection when not needed
54
- - Click on toolbar tool group always enable a tool
55
- - fix input and select options not being impacted by
56
- - new reponsive for toolbar
57
-
58
- # V.0.4.0
59
-
60
- - Add possibility to customize UI from props
61
-
62
- # V.0.3.1
63
-
64
- - Add prop to change canvas background color
65
- - Move some props to options
66
- - fix Picture shape not being saved and not triggering onDataChanged
67
- - small picture are upscaled to fit container when imported
68
- - Add prop to choose to display save/load/export
69
-
70
- # V.0.3.0 :
71
-
72
- - prevent picture from url submit to reload page
73
- - add gridVisible prop and default to false
74
- - rename withLayouts to layersManipulation and reduce possible values
75
- - move every options inside options prop
76
- - remove ajv and any kind of json validation
77
- - limit snackbar use on success and always log errors
78
- - fit size and center uploaded picture
79
- - Add new viewer mode
80
- - add borders on bars
81
- - display shape id in layer panel
82
- - Export : round values to 2 decimal points
83
- - Export and import : remove temporary values (id) from stored file
84
- - Clean unknown shape format from imported data
85
- - Use ref for onDataChanged prop to avoid rerender if callback changes
86
- - Calculate container size to resize canvas instead of CSS
87
- - use requestAnimationFrame to improve perf
88
- - fix TextEdit position and size not working with canvas resized
89
- - fix anchors area becoming too small after rezsize
90
- - add MIT license to project
91
- - other minor fixes
92
- - minor design improvements
93
-
94
- # V.0.2.2 :
95
-
96
- - Migrate to react 18
97
- - fix Picture type
98
- - change pen join and cap to round
99
- - replace loading snackbar with proper loader
100
- - support for canGrow and canShrink prop
101
- - rename project
102
- - release on npm
103
-
104
- # V.0.2.1 :
105
-
106
- - better ui for input modal
107
- - lock functionnality
108
- - toggle grid
109
- - layouts panel UI
110
-
111
- # V.0.2.0 :
112
-
113
- - fix for scaling brush
114
- - fix snackbar
115
- - refonte UI toolbar
116
- - update examples
117
- - button with icons
118
- - mini toolbar to fix
119
- - provide tool for url only
@@ -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
- var 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
- var KeyboardCommand;
23
- exports.KeyboardCommand = KeyboardCommand;
24
-
25
- (function (KeyboardCommand) {
26
- KeyboardCommand["Copy"] = "copy";
27
- KeyboardCommand["Paste"] = "paste";
28
- })(KeyboardCommand || (exports.KeyboardCommand = KeyboardCommand = {}));
@@ -1,461 +0,0 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
-
5
- Object.defineProperty(exports, "__esModule", {
6
- value: true
7
- });
8
- exports.drawTriangle = exports.drawText = exports.drawShape = exports.drawSelection = exports.drawRect = exports.drawPolygon = exports.drawPicture = exports.drawLine = exports.drawEllipse = exports.drawCurve = exports.drawCircle = exports.drawBrush = void 0;
9
-
10
- var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
-
12
- var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
13
-
14
- var _shapes = require("../constants/shapes");
15
-
16
- var _style = require("../constants/style");
17
-
18
- var _intersect = require("./intersect");
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; } } }; }
23
-
24
- function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
25
-
26
- function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
27
-
28
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
29
-
30
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
31
-
32
- var applyShapeTransformations = function applyShapeTransformations(ctx, marker, responsiveScale, canvasOffset, selectionPadding) {
33
- ctx.save();
34
- ctx.scale(responsiveScale, responsiveScale);
35
- ctx.translate(marker.translation[0] + canvasOffset[0], marker.translation[1] + canvasOffset[1]);
36
-
37
- if (marker.rotation !== 0) {
38
- var _getShapeInfos = (0, _shapeData.getShapeInfos)(marker, selectionPadding),
39
- center = _getShapeInfos.center;
40
-
41
- ctx.translate(center[0], center[1]);
42
- ctx.rotate(marker.rotation);
43
- ctx.translate(-center[0], -center[1]);
44
- }
45
- };
46
-
47
- var restoreShapeTransformations = function restoreShapeTransformations(ctx) {
48
- ctx.restore();
49
- };
50
-
51
- var updateDrawStyle = function updateDrawStyle(ctx) {
52
- var style = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
53
- var _style$fillColor = style.fillColor,
54
- fillColor = _style$fillColor === void 0 ? 'transparent' : _style$fillColor,
55
- _style$globalAlpha = style.globalAlpha,
56
- globalAlpha = _style$globalAlpha === void 0 ? 100 : _style$globalAlpha,
57
- _style$strokeColor = style.strokeColor,
58
- strokeColor = _style$strokeColor === void 0 ? _shapes.SELECTION_DEFAULT_COLOR : _style$strokeColor,
59
- _style$lineWidth = style.lineWidth,
60
- lineWidth = _style$lineWidth === void 0 ? _shapes.SELECTION_DEFAULT_WIDTH : _style$lineWidth,
61
- _style$lineDash = style.lineDash,
62
- lineDash = _style$lineDash === void 0 ? 0 : _style$lineDash;
63
- ctx.lineCap = 'round';
64
- ctx.lineJoin = 'round';
65
- globalAlpha !== undefined && (ctx.globalAlpha = globalAlpha / 100);
66
- fillColor && (ctx.fillStyle = fillColor);
67
- strokeColor && (ctx.strokeStyle = strokeColor);
68
- lineWidth && (ctx.lineWidth = lineWidth);
69
- lineDash !== undefined && lineWidth && ctx.setLineDash([_style.LINE_DASH_DATA[lineDash][0] * lineWidth, _style.LINE_DASH_DATA[lineDash][1] * lineWidth]);
70
- };
71
-
72
- var drawBrush = function drawBrush(ctx, brush) {
73
- var _brush$style;
74
-
75
- if (brush.points.length < 1) return;
76
- updateDrawStyle(ctx, brush.style);
77
- ctx.beginPath();
78
- if (((_brush$style = brush.style) === null || _brush$style === void 0 ? void 0 : _brush$style.strokeColor) === 'transparent' || ctx.globalAlpha === 0) return;
79
- brush.points.forEach(function (points) {
80
- if (points.length === 1) {
81
- ctx.rect(points[0][0], points[0][1], 1, 1);
82
- } else {
83
- ctx.moveTo.apply(ctx, (0, _toConsumableArray2["default"])(points[0]));
84
- points.slice(1).forEach(function (point) {
85
- ctx.lineTo.apply(ctx, (0, _toConsumableArray2["default"])(point));
86
- });
87
- }
88
- });
89
- ctx.stroke();
90
- };
91
-
92
- exports.drawBrush = drawBrush;
93
-
94
- var drawTriangle = function drawTriangle(ctx, triangle) {
95
- var _triangle$style, _triangle$style2;
96
-
97
- updateDrawStyle(ctx, triangle.style);
98
- if (ctx.globalAlpha === 0) return;
99
- ctx.beginPath();
100
- ctx.moveTo.apply(ctx, (0, _toConsumableArray2["default"])(triangle.points[0]));
101
- ctx.lineTo.apply(ctx, (0, _toConsumableArray2["default"])(triangle.points[1]));
102
- ctx.lineTo.apply(ctx, (0, _toConsumableArray2["default"])(triangle.points[2]));
103
- ctx.lineTo.apply(ctx, (0, _toConsumableArray2["default"])(triangle.points[0]));
104
- ((_triangle$style = triangle.style) === null || _triangle$style === void 0 ? void 0 : _triangle$style.fillColor) !== 'transparent' && ctx.fill();
105
- ((_triangle$style2 = triangle.style) === null || _triangle$style2 === void 0 ? void 0 : _triangle$style2.strokeColor) !== 'transparent' && ctx.stroke();
106
- };
107
-
108
- exports.drawTriangle = drawTriangle;
109
-
110
- var buildTriangleOnLine = function buildTriangleOnLine(center, angle, lineStyle) {
111
- var _lineStyle$style$line, _lineStyle$style, _lineStyle$style$line2, _lineStyle$style2, _lineStyle$style$line3, _lineStyle$style3, _lineStyle$style$line4, _lineStyle$style4, _lineStyle$style$line5, _lineStyle$style5, _lineStyle$style6;
112
-
113
- var trianglePoints = [(0, _intersect.applyRotationToVector)([0, -(10 + ((_lineStyle$style$line = (_lineStyle$style = lineStyle.style) === null || _lineStyle$style === void 0 ? void 0 : _lineStyle$style.lineWidth) !== null && _lineStyle$style$line !== void 0 ? _lineStyle$style$line : 0) * 1)], angle), (0, _intersect.applyRotationToVector)([-(5 + ((_lineStyle$style$line2 = (_lineStyle$style2 = lineStyle.style) === null || _lineStyle$style2 === void 0 ? void 0 : _lineStyle$style2.lineWidth) !== null && _lineStyle$style$line2 !== void 0 ? _lineStyle$style$line2 : 0) * 1), 5 + ((_lineStyle$style$line3 = (_lineStyle$style3 = lineStyle.style) === null || _lineStyle$style3 === void 0 ? void 0 : _lineStyle$style3.lineWidth) !== null && _lineStyle$style$line3 !== void 0 ? _lineStyle$style$line3 : 0) * 2], angle), (0, _intersect.applyRotationToVector)([5 + ((_lineStyle$style$line4 = (_lineStyle$style4 = lineStyle.style) === null || _lineStyle$style4 === void 0 ? void 0 : _lineStyle$style4.lineWidth) !== null && _lineStyle$style$line4 !== void 0 ? _lineStyle$style$line4 : 0) * 1, 5 + ((_lineStyle$style$line5 = (_lineStyle$style5 = lineStyle.style) === null || _lineStyle$style5 === void 0 ? void 0 : _lineStyle$style5.lineWidth) !== null && _lineStyle$style$line5 !== void 0 ? _lineStyle$style$line5 : 0) * 2], angle)];
114
- return {
115
- points: [[center[0] + trianglePoints[0][0], center[1] + trianglePoints[0][1]], [center[0] + trianglePoints[1][0], center[1] + trianglePoints[1][1]], [center[0] + trianglePoints[2][0], center[1] + trianglePoints[2][1]]],
116
- style: _objectSpread(_objectSpread({}, lineStyle.style), {}, {
117
- fillColor: (_lineStyle$style6 = lineStyle.style) === null || _lineStyle$style6 === void 0 ? void 0 : _lineStyle$style6.strokeColor,
118
- strokeColor: 'transparent'
119
- })
120
- };
121
- };
122
-
123
- var drawLine = function drawLine(ctx, line) {
124
- var _line$style, _line$style2, _line$style3, _line$style4, _line$style5, _line$style6;
125
-
126
- updateDrawStyle(ctx, line.style);
127
- if (ctx.globalAlpha === 0) return;
128
- ctx.beginPath();
129
- ctx.moveTo.apply(ctx, (0, _toConsumableArray2["default"])(line.points[0]));
130
- ctx.lineTo.apply(ctx, (0, _toConsumableArray2["default"])(line.points[1]));
131
- ((_line$style = line.style) === null || _line$style === void 0 ? void 0 : _line$style.fillColor) !== 'transparent' && ctx.fill();
132
- ((_line$style2 = line.style) === null || _line$style2 === void 0 ? void 0 : _line$style2.strokeColor) !== 'transparent' && ctx.stroke();
133
-
134
- if (((_line$style3 = line.style) === null || _line$style3 === void 0 ? void 0 : _line$style3.lineArrow) === 1 || ((_line$style4 = line.style) === null || _line$style4 === void 0 ? void 0 : _line$style4.lineArrow) === 3) {
135
- var rotation = Math.PI / 2 - Math.atan2(line.points[1][1] - line.points[0][1], line.points[1][0] - line.points[0][0]);
136
- drawTriangle(ctx, buildTriangleOnLine(line.points[0], rotation, {
137
- style: line.style
138
- }));
139
- }
140
-
141
- if (((_line$style5 = line.style) === null || _line$style5 === void 0 ? void 0 : _line$style5.lineArrow) === 2 || ((_line$style6 = line.style) === null || _line$style6 === void 0 ? void 0 : _line$style6.lineArrow) === 3) {
142
- var _rotation = Math.PI / 2 - Math.atan2(line.points[0][1] - line.points[1][1], line.points[0][0] - line.points[1][0]);
143
-
144
- drawTriangle(ctx, buildTriangleOnLine(line.points[1], _rotation, {
145
- style: line.style
146
- }));
147
- }
148
- };
149
-
150
- exports.drawLine = drawLine;
151
-
152
- var drawPolygon = function drawPolygon(ctx, polygon) {
153
- var _polygon$style, _polygon$style2;
154
-
155
- if (polygon.points.length < 1) return;
156
- updateDrawStyle(ctx, polygon.style);
157
- if (ctx.globalAlpha === 0) return;
158
- ctx.beginPath();
159
- ctx.moveTo.apply(ctx, (0, _toConsumableArray2["default"])(polygon.points[0]));
160
- polygon.points.slice(1).forEach(function (point) {
161
- ctx.lineTo.apply(ctx, (0, _toConsumableArray2["default"])(point));
162
- });
163
- ctx.lineTo.apply(ctx, (0, _toConsumableArray2["default"])(polygon.points[0]));
164
- ((_polygon$style = polygon.style) === null || _polygon$style === void 0 ? void 0 : _polygon$style.fillColor) !== 'transparent' && ctx.fill();
165
- ((_polygon$style2 = polygon.style) === null || _polygon$style2 === void 0 ? void 0 : _polygon$style2.strokeColor) !== 'transparent' && ctx.stroke();
166
- };
167
-
168
- exports.drawPolygon = drawPolygon;
169
-
170
- var drawCurve = function drawCurve(ctx, curve) {
171
- var _curve$style, _curve$style2;
172
-
173
- if (curve.points.length < 3) return;
174
- updateDrawStyle(ctx, curve.style);
175
- if (ctx.globalAlpha === 0) return;
176
- ctx.beginPath();
177
- ctx.moveTo.apply(ctx, (0, _toConsumableArray2["default"])(curve.points[0]));
178
-
179
- for (var i = 1; i < curve.points.length - 1; i++) {
180
- ctx.quadraticCurveTo.apply(ctx, (0, _toConsumableArray2["default"])(curve.points[i]).concat([curve.points.length - 2 === i ? curve.points[i + 1][0] : (curve.points[i + 1][0] - curve.points[i][0]) / 2 + curve.points[i][0], curve.points.length - 2 === i ? curve.points[i + 1][1] : (curve.points[i + 1][1] - curve.points[i][1]) / 2 + curve.points[i][1]]));
181
- }
182
-
183
- ((_curve$style = curve.style) === null || _curve$style === void 0 ? void 0 : _curve$style.fillColor) !== 'transparent' && ctx.fill();
184
- ((_curve$style2 = curve.style) === null || _curve$style2 === void 0 ? void 0 : _curve$style2.strokeColor) !== 'transparent' && ctx.stroke();
185
- };
186
-
187
- exports.drawCurve = drawCurve;
188
-
189
- var drawCircle = function drawCircle(ctx, circle) {
190
- var _circle$style, _circle$style2;
191
-
192
- updateDrawStyle(ctx, circle.style);
193
- ctx.beginPath();
194
- ctx.arc(circle.x, circle.y, circle.radius, 0, 2 * Math.PI);
195
- ((_circle$style = circle.style) === null || _circle$style === void 0 ? void 0 : _circle$style.fillColor) !== 'transparent' && ctx.fill();
196
- ((_circle$style2 = circle.style) === null || _circle$style2 === void 0 ? void 0 : _circle$style2.strokeColor) !== 'transparent' && ctx.stroke();
197
- };
198
-
199
- exports.drawCircle = drawCircle;
200
-
201
- var drawEllipse = function drawEllipse(ctx, ellipse) {
202
- var _ellipse$style, _ellipse$style2;
203
-
204
- updateDrawStyle(ctx, ellipse.style);
205
- if (ctx.globalAlpha === 0) return;
206
- ctx.beginPath();
207
- ctx.ellipse(ellipse.x, ellipse.y, ellipse.radiusX, ellipse.radiusY, 0, 0, 2 * Math.PI);
208
- ((_ellipse$style = ellipse.style) === null || _ellipse$style === void 0 ? void 0 : _ellipse$style.fillColor) !== 'transparent' && ctx.fill();
209
- ((_ellipse$style2 = ellipse.style) === null || _ellipse$style2 === void 0 ? void 0 : _ellipse$style2.strokeColor) !== 'transparent' && ctx.stroke();
210
- };
211
-
212
- exports.drawEllipse = drawEllipse;
213
-
214
- var drawRect = function drawRect(ctx, rect) {
215
- var _rect$style, _rect$style2;
216
-
217
- updateDrawStyle(ctx, rect.style);
218
- if (ctx.globalAlpha === 0) return;
219
- ctx.beginPath();
220
- ctx.rect(rect.x, rect.y, rect.width, rect.height);
221
- ((_rect$style = rect.style) === null || _rect$style === void 0 ? void 0 : _rect$style.fillColor) !== 'transparent' && ctx.fill();
222
- ((_rect$style2 = rect.style) === null || _rect$style2 === void 0 ? void 0 : _rect$style2.strokeColor) !== 'transparent' && ctx.stroke();
223
- };
224
-
225
- exports.drawRect = drawRect;
226
-
227
- var drawText = function drawText(ctx, text) {
228
- var _text$style, _text$style2, _text$style3, _text$style$fontFamil, _text$style4;
229
-
230
- updateDrawStyle(ctx, text.style);
231
- if (ctx.globalAlpha === 0 || !((_text$style = text.style) !== null && _text$style !== void 0 && _text$style.strokeColor) || text.style.strokeColor === 'transparent') return;
232
- ctx.font = "".concat((_text$style2 = text.style) !== null && _text$style2 !== void 0 && _text$style2.fontItalic ? 'italic' : '', " ").concat((_text$style3 = text.style) !== null && _text$style3 !== void 0 && _text$style3.fontBold ? 'bold' : '', " ").concat(text.fontSize, "px ").concat((_text$style$fontFamil = (_text$style4 = text.style) === null || _text$style4 === void 0 ? void 0 : _text$style4.fontFamily) !== null && _text$style$fontFamil !== void 0 ? _text$style$fontFamil : _style.STYLE_FONT_DEFAULT);
233
- ctx.textBaseline = 'top';
234
- ctx.fillStyle = text.style.strokeColor;
235
-
236
- for (var i = 0; i < text.value.length; i++) {
237
- ctx.fillText(text.value[i], text.x, text.y + i * text.fontSize, text.width);
238
- }
239
- };
240
-
241
- exports.drawText = drawText;
242
-
243
- var drawPicture = function drawPicture(ctx, picture) {
244
- updateDrawStyle(ctx, picture.style);
245
- if (ctx.globalAlpha === 0) return;
246
- ctx.beginPath();
247
- ctx.drawImage(picture.img, picture.x, picture.y, picture.width, picture.height);
248
- };
249
-
250
- exports.drawPicture = drawPicture;
251
-
252
- var drawShape = function drawShape(ctx, shape, responsiveScale, canvasOffset, selectionPadding) {
253
- if (shape.visible === false) return;
254
- applyShapeTransformations(ctx, shape, responsiveScale, canvasOffset, selectionPadding);
255
-
256
- switch (shape.type) {
257
- case 'brush':
258
- drawBrush(ctx, shape);
259
- break;
260
-
261
- case 'text':
262
- drawText(ctx, shape);
263
- break;
264
-
265
- case 'line':
266
- drawLine(ctx, shape);
267
- break;
268
-
269
- case 'polygon':
270
- drawPolygon(ctx, shape);
271
- break;
272
-
273
- case 'curve':
274
- drawCurve(ctx, shape);
275
- break;
276
-
277
- case 'circle':
278
- drawCircle(ctx, shape);
279
- break;
280
-
281
- case 'ellipse':
282
- drawEllipse(ctx, shape);
283
- break;
284
-
285
- case 'rect':
286
- case 'square':
287
- drawRect(ctx, shape);
288
- break;
289
-
290
- case 'picture':
291
- drawPicture(ctx, shape);
292
- break;
293
- }
294
-
295
- restoreShapeTransformations(ctx);
296
- };
297
-
298
- exports.drawShape = drawShape;
299
-
300
- var drawSelectionDefault = function drawSelectionDefault(_ref) {
301
- var ctx = _ref.ctx,
302
- shape = _ref.shape,
303
- withAnchors = _ref.withAnchors,
304
- selectionPadding = _ref.selectionPadding,
305
- selectionWidth = _ref.selectionWidth,
306
- selectionColor = _ref.selectionColor,
307
- _ref$currentScale = _ref.currentScale,
308
- currentScale = _ref$currentScale === void 0 ? 1 : _ref$currentScale;
309
-
310
- var _getShapeInfos2 = (0, _shapeData.getShapeInfos)(shape, selectionPadding / currentScale),
311
- borders = _getShapeInfos2.borders;
312
-
313
- drawRect(ctx, _objectSpread(_objectSpread({}, borders), {}, {
314
- style: {
315
- fillColor: 'transparent',
316
- strokeColor: selectionColor,
317
- lineWidth: selectionWidth / currentScale
318
- }
319
- }));
320
- if (!withAnchors || shape.locked) return;
321
- drawLine(ctx, {
322
- points: [[borders.x + borders.width / 2, borders.y], [borders.x + borders.width / 2, borders.y - _shapes.SELECTION_ANCHOR_SIZE / 2 - _shapes.SELECTION_ROTATED_ANCHOR_POSITION / currentScale]],
323
- style: {
324
- fillColor: 'transparent',
325
- strokeColor: selectionColor,
326
- lineWidth: selectionWidth / currentScale
327
- }
328
- });
329
-
330
- var _iterator = _createForOfIteratorHelper(_shapes.SELECTION_RESIZE_ANCHOR_POSITIONS),
331
- _step;
332
-
333
- try {
334
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
335
- var anchorPosition = _step.value;
336
- drawCircle(ctx, {
337
- x: borders.x + borders.width * anchorPosition[0],
338
- y: borders.y + borders.height * anchorPosition[1],
339
- radius: _shapes.SELECTION_ANCHOR_SIZE / 2 / currentScale,
340
- style: {
341
- fillColor: 'rgb(255,255,255)',
342
- strokeColor: 'rgb(150,150,150)',
343
- lineWidth: selectionWidth / currentScale
344
- }
345
- });
346
- }
347
- } catch (err) {
348
- _iterator.e(err);
349
- } finally {
350
- _iterator.f();
351
- }
352
-
353
- drawCircle(ctx, {
354
- x: borders.x + borders.width / 2,
355
- y: borders.y - _shapes.SELECTION_ANCHOR_SIZE / 2 - _shapes.SELECTION_ROTATED_ANCHOR_POSITION / currentScale,
356
- radius: _shapes.SELECTION_ANCHOR_SIZE / 2 / currentScale,
357
- style: {
358
- fillColor: 'rgb(255,255,255)',
359
- strokeColor: 'rgb(150,150,150)',
360
- lineWidth: selectionWidth / currentScale
361
- }
362
- });
363
- };
364
-
365
- var drawLineSelection = function drawLineSelection(_ref2) {
366
- var ctx = _ref2.ctx,
367
- shape = _ref2.shape,
368
- withAnchors = _ref2.withAnchors,
369
- selectionPadding = _ref2.selectionPadding,
370
- selectionWidth = _ref2.selectionWidth,
371
- selectionColor = _ref2.selectionColor,
372
- _ref2$currentScale = _ref2.currentScale,
373
- currentScale = _ref2$currentScale === void 0 ? 1 : _ref2$currentScale;
374
-
375
- var _getShapeInfos3 = (0, _shapeData.getShapeInfos)(shape, selectionPadding),
376
- borders = _getShapeInfos3.borders;
377
-
378
- drawRect(ctx, _objectSpread(_objectSpread({}, borders), {}, {
379
- style: {
380
- fillColor: 'transparent',
381
- strokeColor: selectionColor,
382
- lineWidth: selectionWidth / currentScale
383
- }
384
- }));
385
- if (!withAnchors || shape.locked) return;
386
-
387
- for (var i = 0; i < shape.points.length; i++) {
388
- var coordinate = shape.points[i];
389
-
390
- if (shape.type === 'curve' && i > 0 && i < shape.points.length - 1) {
391
- drawRect(ctx, {
392
- x: coordinate[0] - _shapes.SELECTION_ANCHOR_SIZE / 2,
393
- y: coordinate[1] - _shapes.SELECTION_ANCHOR_SIZE / 2,
394
- width: _shapes.SELECTION_ANCHOR_SIZE / currentScale,
395
- height: _shapes.SELECTION_ANCHOR_SIZE / currentScale,
396
- style: {
397
- fillColor: 'rgb(255,255,255)',
398
- strokeColor: 'rgb(150,150,150)',
399
- lineWidth: selectionWidth / currentScale
400
- }
401
- });
402
- } else {
403
- drawCircle(ctx, {
404
- x: coordinate[0],
405
- y: coordinate[1],
406
- radius: _shapes.SELECTION_ANCHOR_SIZE / 2 / currentScale,
407
- style: {
408
- fillColor: 'rgb(255,255,255)',
409
- strokeColor: 'rgb(150,150,150)',
410
- lineWidth: selectionWidth / currentScale
411
- }
412
- });
413
- }
414
- }
415
- };
416
-
417
- var drawSelection = function drawSelection(_ref3) {
418
- var ctx = _ref3.ctx,
419
- shape = _ref3.shape,
420
- _ref3$scaleRatio = _ref3.scaleRatio,
421
- scaleRatio = _ref3$scaleRatio === void 0 ? 1 : _ref3$scaleRatio,
422
- canvasOffset = _ref3.canvasOffset,
423
- selectionPadding = _ref3.selectionPadding,
424
- selectionWidth = _ref3.selectionWidth,
425
- selectionColor = _ref3.selectionColor,
426
- _ref3$withAnchors = _ref3.withAnchors,
427
- withAnchors = _ref3$withAnchors === void 0 ? true : _ref3$withAnchors;
428
- applyShapeTransformations(ctx, shape, scaleRatio, canvasOffset, selectionPadding);
429
-
430
- switch (shape.type) {
431
- case 'polygon':
432
- case 'line':
433
- case 'curve':
434
- drawLineSelection({
435
- ctx: ctx,
436
- shape: shape,
437
- withAnchors: withAnchors,
438
- selectionPadding: selectionPadding,
439
- selectionWidth: selectionWidth,
440
- selectionColor: selectionColor,
441
- currentScale: scaleRatio
442
- });
443
- break;
444
-
445
- default:
446
- drawSelectionDefault({
447
- ctx: ctx,
448
- shape: shape,
449
- withAnchors: withAnchors,
450
- selectionPadding: selectionPadding,
451
- selectionWidth: selectionWidth,
452
- selectionColor: selectionColor,
453
- currentScale: scaleRatio
454
- });
455
- break;
456
- }
457
-
458
- restoreShapeTransformations(ctx);
459
- };
460
-
461
- exports.drawSelection = drawSelection;