@yorab/react-paint 0.3.0 → 0.4.0

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.
@@ -17,8 +17,6 @@ var _fp = _interopRequireDefault(require("lodash/fp"));
17
17
 
18
18
  var _shapeData = require("./shapeData");
19
19
 
20
- var _shapes = require("../constants/shapes");
21
-
22
20
  var _Mode = require("../types/Mode");
23
21
 
24
22
  var _intersect = require("./intersect");
@@ -81,14 +79,14 @@ var createNewPointGroupToShape = function createNewPointGroupToShape(shape, curs
81
79
 
82
80
  exports.createNewPointGroupToShape = createNewPointGroupToShape;
83
81
 
84
- var resizeBrush = function resizeBrush(cursorPosition, canvasOffset, originalShape, selectionMode) {
85
- var _getShapeInfos = (0, _shapeData.getShapeInfos)(originalShape),
82
+ var resizeBrush = function resizeBrush(cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding) {
83
+ var _getShapeInfos = (0, _shapeData.getShapeInfos)(originalShape, selectionPadding),
86
84
  center = _getShapeInfos.center,
87
85
  borders = _getShapeInfos.borders;
88
86
 
89
87
  var cursorPositionBeforeResize = (0, _intersect.getPointPositionAfterCanvasTransformation)(cursorPosition, canvasOffset, originalShape.translation, originalShape.rotation, center);
90
- var scaledWidth = selectionMode.anchor[0] === 0.5 ? 1 : selectionMode.anchor[0] === 0 ? (borders.x + borders.width - cursorPositionBeforeResize[0] + _shapes.SELECTION_PADDING * -2) / (borders.width + _shapes.SELECTION_PADDING * -2) : (cursorPositionBeforeResize[0] - borders.x - _shapes.SELECTION_PADDING * 2) / (borders.width - _shapes.SELECTION_PADDING * 2);
91
- var scaledHeight = selectionMode.anchor[1] === 0.5 ? 1 : selectionMode.anchor[1] === 0 ? (borders.y + borders.height - cursorPositionBeforeResize[1] + _shapes.SELECTION_PADDING * -2) / (borders.height + _shapes.SELECTION_PADDING * -2) : (cursorPositionBeforeResize[1] - borders.y - _shapes.SELECTION_PADDING * 2) / (borders.height - _shapes.SELECTION_PADDING * 2);
88
+ var 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);
89
+ var 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);
92
90
 
93
91
  var shapeWithNewDimensions = _objectSpread(_objectSpread({}, originalShape), {}, {
94
92
  points: originalShape.points.map(function (points) {
@@ -98,7 +96,7 @@ var resizeBrush = function resizeBrush(cursorPosition, canvasOffset, originalSha
98
96
  })
99
97
  });
100
98
 
101
- var _getShapeInfos2 = (0, _shapeData.getShapeInfos)(shapeWithNewDimensions),
99
+ var _getShapeInfos2 = (0, _shapeData.getShapeInfos)(shapeWithNewDimensions, selectionPadding),
102
100
  shapeWithNewDimensionsCenter = _getShapeInfos2.center,
103
101
  shapeWithNewDimensionsBorders = _getShapeInfos2.borders;
104
102
 
@@ -119,8 +117,8 @@ var resizeBrush = function resizeBrush(cursorPosition, canvasOffset, originalSha
119
117
 
120
118
  exports.resizeBrush = resizeBrush;
121
119
 
122
- var resizeLine = function resizeLine(cursorPosition, canvasOffset, originalShape, selectionMode) {
123
- var _getShapeInfos3 = (0, _shapeData.getShapeInfos)(originalShape),
120
+ var resizeLine = function resizeLine(cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding) {
121
+ var _getShapeInfos3 = (0, _shapeData.getShapeInfos)(originalShape, selectionPadding),
124
122
  center = _getShapeInfos3.center;
125
123
 
126
124
  var cursorPositionBeforeResize = (0, _intersect.getPointPositionAfterCanvasTransformation)(cursorPosition, canvasOffset, originalShape.translation, originalShape.rotation, center);
@@ -152,20 +150,20 @@ var updatePolygonLinesCount = function updatePolygonLinesCount(shape, newPointsC
152
150
 
153
151
  exports.updatePolygonLinesCount = updatePolygonLinesCount;
154
152
 
155
- var resizeCircle = function resizeCircle(cursorPosition, canvasOffset, originalShape, selectionMode) {
156
- var _getShapeInfos4 = (0, _shapeData.getShapeInfos)(originalShape),
153
+ var resizeCircle = function resizeCircle(cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding) {
154
+ var _getShapeInfos4 = (0, _shapeData.getShapeInfos)(originalShape, selectionPadding),
157
155
  center = _getShapeInfos4.center,
158
156
  borders = _getShapeInfos4.borders;
159
157
 
160
158
  var cursorPositionBeforeResize = (0, _intersect.getPointPositionAfterCanvasTransformation)(cursorPosition, canvasOffset, originalShape.translation, originalShape.rotation, center);
161
159
  var newCursorPosition = [cursorPositionBeforeResize[0], cursorPositionBeforeResize[1]];
162
- var scaledRadius = selectionMode.anchor[1] === 0.5 ? (selectionMode.anchor[0] === 0 ? borders.x + borders.width - newCursorPosition[0] + _shapes.SELECTION_PADDING * (selectionMode.anchor[0] === 0 ? -2 : 2) : newCursorPosition[0] - borders.x - _shapes.SELECTION_PADDING * (selectionMode.anchor[0] === 0 ? -2 : 2)) / 2 : (selectionMode.anchor[1] === 0 ? borders.y + borders.height - newCursorPosition[1] + _shapes.SELECTION_PADDING * (selectionMode.anchor[1] === 0 ? -2 : 2) : newCursorPosition[1] - borders.y - _shapes.SELECTION_PADDING * (selectionMode.anchor[1] === 0 ? -2 : 2)) / 2;
160
+ var 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
161
 
164
162
  var shapeWithNewDimensions = _objectSpread(_objectSpread({}, originalShape), {
165
163
  radius: Math.abs(scaledRadius)
166
164
  });
167
165
 
168
- var _getShapeInfos5 = (0, _shapeData.getShapeInfos)(shapeWithNewDimensions),
166
+ var _getShapeInfos5 = (0, _shapeData.getShapeInfos)(shapeWithNewDimensions, selectionPadding),
169
167
  shapeWithNewDimensionsCenter = _getShapeInfos5.center;
170
168
 
171
169
  var _getCircleOppositeAnc = getCircleOppositeAnchorAbsolutePosition(selectionMode.anchor, center, originalShape),
@@ -185,22 +183,22 @@ var resizeCircle = function resizeCircle(cursorPosition, canvasOffset, originalS
185
183
 
186
184
  exports.resizeCircle = resizeCircle;
187
185
 
188
- var resizeEllipse = function resizeEllipse(cursorPosition, canvasOffset, originalShape, selectionMode) {
189
- var _getShapeInfos6 = (0, _shapeData.getShapeInfos)(originalShape),
186
+ var resizeEllipse = function resizeEllipse(cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding) {
187
+ var _getShapeInfos6 = (0, _shapeData.getShapeInfos)(originalShape, selectionPadding),
190
188
  center = _getShapeInfos6.center,
191
189
  borders = _getShapeInfos6.borders;
192
190
 
193
191
  var cursorPositionBeforeResize = (0, _intersect.getPointPositionAfterCanvasTransformation)(cursorPosition, canvasOffset, originalShape.translation, originalShape.rotation, center);
194
192
  var newCursorPosition = [cursorPositionBeforeResize[0], cursorPositionBeforeResize[1]];
195
- var scaledRadiusX = selectionMode.anchor[0] === 0.5 ? originalShape.radiusX : (selectionMode.anchor[0] === 0 ? borders.x + borders.width - newCursorPosition[0] + _shapes.SELECTION_PADDING * (selectionMode.anchor[0] === 0 ? -2 : 2) : newCursorPosition[0] - borders.x - _shapes.SELECTION_PADDING * (selectionMode.anchor[0] === 0 ? -2 : 2)) / 2;
196
- var scaledRadiusY = selectionMode.anchor[1] === 0.5 ? originalShape.radiusY : (selectionMode.anchor[1] === 0 ? borders.y + borders.height - newCursorPosition[1] + _shapes.SELECTION_PADDING * (selectionMode.anchor[1] === 0 ? -2 : 2) : newCursorPosition[1] - borders.y - _shapes.SELECTION_PADDING * (selectionMode.anchor[1] === 0 ? -2 : 2)) / 2;
193
+ var 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;
194
+ var 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;
197
195
 
198
196
  var shapeWithNewDimensions = _objectSpread(_objectSpread({}, originalShape), {
199
197
  radiusX: Math.abs(scaledRadiusX),
200
198
  radiusY: Math.abs(scaledRadiusY)
201
199
  });
202
200
 
203
- var _getShapeInfos7 = (0, _shapeData.getShapeInfos)(shapeWithNewDimensions),
201
+ var _getShapeInfos7 = (0, _shapeData.getShapeInfos)(shapeWithNewDimensions, selectionPadding),
204
202
  shapeWithNewDimensionsCenter = _getShapeInfos7.center;
205
203
 
206
204
  var _getEllipseOppositeAn = getEllipseOppositeAnchorAbsolutePosition(selectionMode.anchor, center, originalShape),
@@ -220,16 +218,16 @@ var resizeEllipse = function resizeEllipse(cursorPosition, canvasOffset, origina
220
218
 
221
219
  exports.resizeEllipse = resizeEllipse;
222
220
 
223
- var resizeRect = function resizeRect(cursorPosition, canvasOffset, originalShape, selectionMode) {
224
- var keepRatio = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;
221
+ var resizeRect = function resizeRect(cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding) {
222
+ var keepRatio = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : false;
225
223
 
226
- var _getShapeInfos8 = (0, _shapeData.getShapeInfos)(originalShape),
224
+ var _getShapeInfos8 = (0, _shapeData.getShapeInfos)(originalShape, selectionPadding),
227
225
  center = _getShapeInfos8.center,
228
226
  borders = _getShapeInfos8.borders;
229
227
 
230
228
  var cursorPositionBeforeResize = (0, _intersect.getPointPositionAfterCanvasTransformation)(cursorPosition, canvasOffset, originalShape.translation, originalShape.rotation, center);
231
- var scaledWidth = selectionMode.anchor[0] === 0.5 ? originalShape.width : selectionMode.anchor[0] === 0 ? borders.x + borders.width - cursorPositionBeforeResize[0] + _shapes.SELECTION_PADDING * -2 : cursorPositionBeforeResize[0] - borders.x - _shapes.SELECTION_PADDING * 2;
232
- var scaledHeight = selectionMode.anchor[1] === 0.5 ? originalShape.height : selectionMode.anchor[1] === 0 ? borders.y + borders.height - cursorPositionBeforeResize[1] + _shapes.SELECTION_PADDING * -2 : cursorPositionBeforeResize[1] - borders.y - _shapes.SELECTION_PADDING * 2;
229
+ var 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;
230
+ var 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;
233
231
 
234
232
  var _ref = keepRatio ? getNormalizedSize(originalShape, scaledWidth, scaledHeight) : [scaledWidth, scaledHeight],
235
233
  _ref2 = (0, _slicedToArray2["default"])(_ref, 2),
@@ -241,7 +239,7 @@ var resizeRect = function resizeRect(cursorPosition, canvasOffset, originalShape
241
239
  height: Math.abs(heightWithRatio)
242
240
  });
243
241
 
244
- var _getShapeInfos9 = (0, _shapeData.getShapeInfos)(shapeWithNewDimensions),
242
+ var _getShapeInfos9 = (0, _shapeData.getShapeInfos)(shapeWithNewDimensions, selectionPadding),
245
243
  shapeWithNewDimensionsCenter = _getShapeInfos9.center;
246
244
 
247
245
  var _getRectOppositeAncho5 = getRectOppositeAnchorAbsolutePosition(selectionMode.anchor, center, originalShape),
@@ -261,10 +259,10 @@ var resizeRect = function resizeRect(cursorPosition, canvasOffset, originalShape
261
259
 
262
260
  exports.resizeRect = resizeRect;
263
261
 
264
- var resizeText = function resizeText(ctx, cursorPosition, canvasOffset, originalShape, selectionMode) {
262
+ var resizeText = function resizeText(ctx, cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding) {
265
263
  var _newRect$style;
266
264
 
267
- var newRect = resizeRect(cursorPosition, canvasOffset, originalShape, selectionMode, true);
265
+ var newRect = resizeRect(cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding, true);
268
266
  return _objectSpread(_objectSpread({}, newRect), {}, {
269
267
  fontSize: calculateTextFontSize(ctx, newRect.value, newRect.width, (_newRect$style = newRect.style) === null || _newRect$style === void 0 ? void 0 : _newRect$style.fontFamily)
270
268
  });
@@ -305,20 +303,20 @@ var getCircleOppositeAnchorAbsolutePosition = function getCircleOppositeAnchorAb
305
303
  return (0, _intersect.getPointPositionBeforeCanvasTransformation)([oppositeX, oppositeY], shape.translation, shape.rotation, center);
306
304
  };
307
305
 
308
- var resizePicture = function resizePicture(cursorPosition, canvasOffset, originalShape, selectionMode) {
309
- return resizeRect(cursorPosition, canvasOffset, originalShape, selectionMode, true);
306
+ var resizePicture = function resizePicture(cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding) {
307
+ return resizeRect(cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding, true);
310
308
  };
311
309
 
312
310
  exports.resizePicture = resizePicture;
313
311
 
314
- var resizeShape = function resizeShape(ctx, shape, cursorPosition, canvasOffset, originalShape, selectionMode) {
315
- if (shape.type === 'line' || shape.type === 'polygon' || shape.type === 'curve') return resizeLine(cursorPosition, canvasOffset, originalShape, selectionMode);else if (shape.type === 'brush') return resizeBrush(cursorPosition, canvasOffset, originalShape, selectionMode);else if (shape.type === 'circle') return resizeCircle(cursorPosition, canvasOffset, originalShape, selectionMode);else if (shape.type === 'ellipse') return resizeEllipse(cursorPosition, canvasOffset, originalShape, selectionMode);else if (shape.type === 'rect') return resizeRect(cursorPosition, canvasOffset, originalShape, selectionMode);else if (shape.type === 'square') return resizeRect(cursorPosition, canvasOffset, originalShape, selectionMode, true);else if (shape.type === 'text') return resizeText(ctx, cursorPosition, canvasOffset, originalShape, selectionMode);else if (shape.type === 'picture') return resizePicture(cursorPosition, canvasOffset, originalShape, selectionMode);
312
+ var resizeShape = function resizeShape(ctx, shape, cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding) {
313
+ 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);
316
314
  return shape;
317
315
  };
318
316
 
319
317
  exports.resizeShape = resizeShape;
320
318
 
321
- var transformShape = function transformShape(ctx, shape, cursorPosition, canvasOffset, selectionMode) {
319
+ var transformShape = function transformShape(ctx, shape, cursorPosition, canvasOffset, selectionMode, selectionPadding) {
322
320
  if (selectionMode.mode === _Mode.SelectionModeLib.brush) {
323
321
  return paintNewPointToShape(shape, cursorPosition);
324
322
  } else if (selectionMode.mode === _Mode.SelectionModeLib.translate) {
@@ -326,7 +324,7 @@ var transformShape = function transformShape(ctx, shape, cursorPosition, canvasO
326
324
  } else if (selectionMode.mode === _Mode.SelectionModeLib.rotate) {
327
325
  return rotateShape(shape, cursorPosition, selectionMode.originalShape, selectionMode.cursorStartPosition, selectionMode.center);
328
326
  } else if (selectionMode.mode === _Mode.SelectionModeLib.resize) {
329
- return resizeShape(ctx, shape, cursorPosition, canvasOffset, selectionMode.originalShape, selectionMode);
327
+ return resizeShape(ctx, shape, cursorPosition, canvasOffset, selectionMode.originalShape, selectionMode, selectionPadding);
330
328
  }
331
329
 
332
330
  return shape;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yorab/react-paint",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "keywords": [],
5
5
  "author": "Yohann Rabatel",
6
6
  "license": "MIT",