Merge pull request #1000 from adroitwhiz/addpoint-divideat

Use divideAt in PointTool.addPoint so that adding new points doesn't affect curves' shape
This commit is contained in:
adroitwhiz 2020-06-04 15:22:29 -04:00 committed by GitHub
commit 32e80364c1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,4 +1,3 @@
import paper from '@scratch/paper';
import {HANDLE_RATIO, snapDeltaToAngle} from '../math'; import {HANDLE_RATIO, snapDeltaToAngle} from '../math';
import {getActionBounds} from '../view'; import {getActionBounds} from '../view';
import {clearSelection, getSelectedLeafItems, getSelectedSegments} from '../selection'; import {clearSelection, getSelectedLeafItems, getSelectedSegments} from '../selection';
@ -68,51 +67,16 @@ class PointTool {
* @param {?boolean} hitProperties.multiselect Whether to multiselect on mouse down (e.g. shift key held) * @param {?boolean} hitProperties.multiselect Whether to multiselect on mouse down (e.g. shift key held)
*/ */
addPoint (hitProperties) { addPoint (hitProperties) {
// Length of curve from previous point to new point const newSegment = hitProperties.hitResult.item.divideAt(hitProperties.hitResult.location);
const beforeCurveLength = hitProperties.hitResult.location.curveOffset;
const afterCurveLength =
hitProperties.hitResult.location.curve.length - hitProperties.hitResult.location.curveOffset;
// Handle length based on curve length until next point // If we're adding a point in the middle of a straight line, it won't be smooth by default, so smooth it
let handleIn = hitProperties.hitResult.location.tangent.multiply(-beforeCurveLength * HANDLE_RATIO); if (!newSegment.hasHandles()) newSegment.smooth();
let handleOut = hitProperties.hitResult.location.tangent.multiply(afterCurveLength * HANDLE_RATIO);
// Don't let one handle overwhelm the other (results in path doubling back on itself weirdly)
if (handleIn.length > 3 * handleOut.length) {
handleIn = handleIn.multiply(3 * handleOut.length / handleIn.length);
}
if (handleOut.length > 3 * handleIn.length) {
handleOut = handleOut.multiply(3 * handleIn.length / handleOut.length);
}
const beforeSegment = hitProperties.hitResult.item.segments[hitProperties.hitResult.location.index];
const afterSegment = hitProperties.hitResult.item.segments[hitProperties.hitResult.location.index + 1];
// Add segment
const newSegment = new paper.Segment(hitProperties.hitResult.location.point, handleIn, handleOut);
hitProperties.hitResult.item.insert(hitProperties.hitResult.location.index + 1, newSegment);
hitProperties.hitResult.segment = newSegment; hitProperties.hitResult.segment = newSegment;
if (!hitProperties.multiselect) { if (!hitProperties.multiselect) {
clearSelection(this.clearSelectedItems); clearSelection(this.clearSelectedItems);
} }
newSegment.selected = true; newSegment.selected = true;
// Adjust handles of curve before and curve after to account for new curve length
if (beforeSegment && beforeSegment.handleOut) {
if (afterSegment) {
beforeSegment.handleOut =
beforeSegment.handleOut.multiply(beforeCurveLength * HANDLE_RATIO / beforeSegment.handleOut.length);
} else {
beforeSegment.handleOut = null;
}
}
if (afterSegment && afterSegment.handleIn) {
if (beforeSegment) {
afterSegment.handleIn =
afterSegment.handleIn.multiply(afterCurveLength * HANDLE_RATIO / afterSegment.handleIn.length);
} else {
afterSegment.handleIn = null;
}
}
} }
removePoint (hitResult) { removePoint (hitResult) {
const index = hitResult.segment.index; const index = hitResult.segment.index;