Skip to content
Snippets Groups Projects
Commit 2ab8bf28 authored by Jack Reed's avatar Jack Reed
Browse files

Enable resizing

parent 9c56f3d0
Branches
No related tags found
No related merge requests found
import React, { Component } from 'react'; import React, { Component } from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { Tool } from '@psychobolt/react-paperjs'; import { Tool } from '@psychobolt/react-paperjs';
import { Rectangle } from 'paper';
import flatten from 'lodash/flatten'; import flatten from 'lodash/flatten';
import { mapChildren } from './utils'; import { mapChildren } from './utils';
/** */ /** */
...@@ -26,6 +27,14 @@ class EditTool extends Component { ...@@ -26,6 +27,14 @@ class EditTool extends Component {
paths.forEach((path) => { paths.forEach((path) => {
if (path.contains(e.point)) { if (path.contains(e.point)) {
path.data.state = 'moving'; // eslint-disable-line no-param-reassign path.data.state = 'moving'; // eslint-disable-line no-param-reassign
return;
}
if (path.hitTest(e.point, { segments: true, tolerance: 8 })) {
path.data.state = 'resizing'; // eslint-disable-line no-param-reassign
path.data.bounds = path.bounds.clone(); // eslint-disable-line no-param-reassign
path.data.scaleBase = e.point.subtract( // eslint-disable-line no-param-reassign
path.bounds.center,
);
} }
}); });
} }
...@@ -46,6 +55,13 @@ class EditTool extends Component { ...@@ -46,6 +55,13 @@ class EditTool extends Component {
path.position = path.position.add( // eslint-disable-line no-param-reassign path.position = path.position.add( // eslint-disable-line no-param-reassign
e.point.subtract(e.lastPoint), e.point.subtract(e.lastPoint),
); );
} else if (path.data.state === 'resizing') {
const { bounds } = path.data;
const scale = e.point.subtract(bounds.center).length / path.data.scaleBase.length;
const tlVec = bounds.topLeft.subtract(bounds.center).multiply(scale);
const brVec = bounds.bottomRight.subtract(bounds.center).multiply(scale);
const newBounds = new Rectangle(tlVec.add(bounds.center), brVec.add(bounds.center));
path.bounds = newBounds; // eslint-disable-line no-param-reassign
} }
}); });
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment