2021-06-16 12:09:45 +00:00
|
|
|
import vec from 'utils/vec'
|
2021-06-08 16:43:33 +00:00
|
|
|
import {
|
2021-07-01 14:03:02 +00:00
|
|
|
getArcLength,
|
|
|
|
uniqueId,
|
2021-06-08 16:43:33 +00:00
|
|
|
getSvgPathFromStroke,
|
|
|
|
rng,
|
|
|
|
getBoundsFromPoints,
|
|
|
|
translateBounds,
|
2021-06-24 12:34:43 +00:00
|
|
|
pointInBounds,
|
2021-07-01 14:03:02 +00:00
|
|
|
circleFromThreePoints,
|
|
|
|
isAngleBetween,
|
|
|
|
getPerfectDashProps,
|
2021-07-08 10:05:25 +00:00
|
|
|
clampToRotationToSegments,
|
2021-07-08 12:15:23 +00:00
|
|
|
lerpAngles,
|
|
|
|
clamp,
|
2021-07-08 09:59:47 +00:00
|
|
|
getFromCache,
|
2021-06-24 08:18:14 +00:00
|
|
|
} from 'utils'
|
2021-06-22 21:06:51 +00:00
|
|
|
import {
|
|
|
|
ArrowShape,
|
|
|
|
DashStyle,
|
|
|
|
Decoration,
|
|
|
|
ShapeHandle,
|
|
|
|
ShapeType,
|
|
|
|
} from 'types'
|
2021-06-01 21:49:32 +00:00
|
|
|
import {
|
|
|
|
intersectArcBounds,
|
|
|
|
intersectLineSegmentBounds,
|
|
|
|
} from 'utils/intersections'
|
2021-06-21 21:35:28 +00:00
|
|
|
import { defaultStyle, getShapeStyle } from 'state/shape-styles'
|
2021-06-08 16:43:33 +00:00
|
|
|
import getStroke from 'perfect-freehand'
|
2021-06-21 13:13:16 +00:00
|
|
|
import React from 'react'
|
2021-06-21 21:35:28 +00:00
|
|
|
import { registerShapeUtils } from './register'
|
2021-05-31 19:13:43 +00:00
|
|
|
|
2021-06-21 13:13:16 +00:00
|
|
|
// A cache for semi-expensive circles calculated from three points
|
2021-06-01 21:49:32 +00:00
|
|
|
function getCtp(shape: ArrowShape) {
|
2021-06-21 13:13:16 +00:00
|
|
|
const { start, end, bend } = shape.handles
|
|
|
|
return circleFromThreePoints(start.point, end.point, bend.point)
|
2021-06-01 21:49:32 +00:00
|
|
|
}
|
|
|
|
|
2021-05-31 19:13:43 +00:00
|
|
|
const arrow = registerShapeUtils<ArrowShape>({
|
|
|
|
boundsCache: new WeakMap([]),
|
|
|
|
|
2021-07-01 14:03:02 +00:00
|
|
|
defaultProps: {
|
|
|
|
id: uniqueId(),
|
|
|
|
type: ShapeType.Arrow,
|
2021-07-04 08:47:37 +00:00
|
|
|
|
2021-07-01 14:03:02 +00:00
|
|
|
name: 'Arrow',
|
|
|
|
parentId: 'page1',
|
|
|
|
childIndex: 0,
|
|
|
|
point: [0, 0],
|
|
|
|
rotation: 0,
|
2021-07-04 08:47:37 +00:00
|
|
|
|
2021-07-01 14:03:02 +00:00
|
|
|
bend: 0,
|
|
|
|
handles: {
|
|
|
|
start: {
|
|
|
|
id: 'start',
|
|
|
|
index: 0,
|
|
|
|
point: [0, 0],
|
2021-05-31 19:13:43 +00:00
|
|
|
},
|
2021-07-01 14:03:02 +00:00
|
|
|
end: {
|
|
|
|
id: 'end',
|
|
|
|
index: 1,
|
|
|
|
point: [1, 1],
|
|
|
|
},
|
|
|
|
bend: {
|
|
|
|
id: 'bend',
|
|
|
|
index: 2,
|
|
|
|
point: [0.5, 0.5],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
decorations: {
|
|
|
|
start: null,
|
|
|
|
middle: null,
|
|
|
|
end: Decoration.Arrow,
|
|
|
|
},
|
|
|
|
style: {
|
|
|
|
...defaultStyle,
|
|
|
|
isFilled: false,
|
|
|
|
},
|
|
|
|
},
|
2021-06-25 11:01:22 +00:00
|
|
|
|
2021-07-01 14:03:02 +00:00
|
|
|
create(props) {
|
|
|
|
const shape = {
|
|
|
|
...this.defaultProps,
|
|
|
|
...props,
|
2021-05-31 19:13:43 +00:00
|
|
|
decorations: {
|
2021-07-01 14:03:02 +00:00
|
|
|
...this.defaultProps.decorations,
|
|
|
|
...props.decorations,
|
2021-05-31 19:13:43 +00:00
|
|
|
},
|
|
|
|
style: {
|
2021-07-01 14:03:02 +00:00
|
|
|
...this.defaultProps.style,
|
2021-05-31 19:13:43 +00:00
|
|
|
...props.style,
|
2021-06-02 11:50:34 +00:00
|
|
|
isFilled: false,
|
2021-05-31 19:13:43 +00:00
|
|
|
},
|
|
|
|
}
|
2021-07-01 14:03:02 +00:00
|
|
|
|
|
|
|
return shape
|
2021-05-31 19:13:43 +00:00
|
|
|
},
|
|
|
|
|
2021-06-27 12:28:54 +00:00
|
|
|
shouldRender(shape, prev) {
|
|
|
|
return shape.handles !== prev.handles || shape.style !== prev.style
|
|
|
|
},
|
|
|
|
|
2021-07-09 19:43:18 +00:00
|
|
|
render(shape) {
|
|
|
|
const { bend, handles, style } = shape
|
2021-05-31 19:13:43 +00:00
|
|
|
const { start, end, bend: _bend } = handles
|
|
|
|
|
2021-07-08 11:01:33 +00:00
|
|
|
const isStraightLine =
|
|
|
|
vec.dist(_bend.point, vec.round(vec.med(start.point, end.point))) < 1
|
2021-05-31 19:13:43 +00:00
|
|
|
|
2021-07-08 12:15:23 +00:00
|
|
|
const isDraw = shape.style.dash === DashStyle.Draw
|
|
|
|
|
2021-06-21 13:13:16 +00:00
|
|
|
const styles = getShapeStyle(style)
|
2021-06-02 11:50:34 +00:00
|
|
|
|
2021-07-08 12:26:55 +00:00
|
|
|
const { strokeWidth } = styles
|
2021-06-02 21:17:38 +00:00
|
|
|
|
2021-06-22 18:13:16 +00:00
|
|
|
const arrowDist = vec.dist(start.point, end.point)
|
|
|
|
|
2021-07-08 12:15:23 +00:00
|
|
|
const arrowHeadlength = Math.min(arrowDist / 3, strokeWidth * 8)
|
|
|
|
|
2021-06-22 21:06:51 +00:00
|
|
|
let shaftPath: JSX.Element
|
2021-07-08 12:15:23 +00:00
|
|
|
let insetStart: number[]
|
|
|
|
let insetEnd: number[]
|
2021-06-22 21:06:51 +00:00
|
|
|
|
2021-06-21 13:13:16 +00:00
|
|
|
if (isStraightLine) {
|
2021-07-08 12:15:23 +00:00
|
|
|
const sw = strokeWidth * (isDraw ? 0.618 : 1.618)
|
2021-06-02 21:17:38 +00:00
|
|
|
|
2021-07-08 12:15:23 +00:00
|
|
|
const path = isDraw
|
2021-07-08 18:09:37 +00:00
|
|
|
? renderFreehandArrowShaft(shape)
|
2021-07-08 12:15:23 +00:00
|
|
|
: 'M' + vec.round(start.point) + 'L' + vec.round(end.point)
|
2021-06-02 21:17:38 +00:00
|
|
|
|
2021-07-01 22:11:09 +00:00
|
|
|
const { strokeDasharray, strokeDashoffset } = getPerfectDashProps(
|
|
|
|
arrowDist,
|
|
|
|
sw,
|
|
|
|
shape.style.dash,
|
|
|
|
2
|
|
|
|
)
|
2021-06-22 18:13:16 +00:00
|
|
|
|
2021-07-08 12:15:23 +00:00
|
|
|
insetStart = vec.nudge(start.point, end.point, arrowHeadlength)
|
|
|
|
insetEnd = vec.nudge(end.point, start.point, arrowHeadlength)
|
2021-06-22 21:06:51 +00:00
|
|
|
|
2021-06-30 20:30:48 +00:00
|
|
|
// Straight arrow path
|
2021-06-22 21:06:51 +00:00
|
|
|
shaftPath = (
|
|
|
|
<>
|
2021-06-08 16:43:33 +00:00
|
|
|
<path
|
2021-06-21 13:13:16 +00:00
|
|
|
d={path}
|
2021-06-08 16:43:33 +00:00
|
|
|
fill="none"
|
2021-06-21 13:13:16 +00:00
|
|
|
strokeWidth={Math.max(8, strokeWidth * 2)}
|
|
|
|
strokeDasharray="none"
|
2021-06-22 18:13:16 +00:00
|
|
|
strokeDashoffset="none"
|
|
|
|
strokeLinecap="round"
|
2021-07-09 19:43:18 +00:00
|
|
|
strokeLinejoin="round"
|
2021-06-08 16:43:33 +00:00
|
|
|
/>
|
2021-06-21 13:13:16 +00:00
|
|
|
<path
|
|
|
|
d={path}
|
2021-06-30 20:30:48 +00:00
|
|
|
fill={styles.stroke}
|
2021-07-03 16:30:06 +00:00
|
|
|
stroke={styles.stroke}
|
2021-07-08 12:15:23 +00:00
|
|
|
strokeWidth={sw}
|
2021-06-22 18:13:16 +00:00
|
|
|
strokeDasharray={strokeDasharray}
|
|
|
|
strokeDashoffset={strokeDashoffset}
|
2021-06-21 13:13:16 +00:00
|
|
|
strokeLinecap="round"
|
2021-07-09 19:43:18 +00:00
|
|
|
strokeLinejoin="round"
|
2021-06-30 20:30:48 +00:00
|
|
|
/>
|
2021-06-22 21:06:51 +00:00
|
|
|
</>
|
2021-06-02 21:17:38 +00:00
|
|
|
)
|
2021-06-22 21:06:51 +00:00
|
|
|
} else {
|
|
|
|
const circle = getCtp(shape)
|
2021-06-02 21:17:38 +00:00
|
|
|
|
2021-07-08 12:15:23 +00:00
|
|
|
const sw = strokeWidth * (isDraw ? 0.618 : 1.618)
|
|
|
|
|
|
|
|
const path = isDraw
|
2021-07-08 18:09:37 +00:00
|
|
|
? renderCurvedFreehandArrowShaft(shape, circle)
|
2021-07-08 12:15:23 +00:00
|
|
|
: getArrowArcPath(start, end, circle, bend)
|
|
|
|
|
|
|
|
const arcLength = getArcLength(
|
|
|
|
[circle[0], circle[1]],
|
|
|
|
circle[2],
|
|
|
|
start.point,
|
|
|
|
end.point
|
|
|
|
)
|
2021-06-08 16:43:33 +00:00
|
|
|
|
2021-07-01 22:11:09 +00:00
|
|
|
const { strokeDasharray, strokeDashoffset } = getPerfectDashProps(
|
2021-07-08 12:15:23 +00:00
|
|
|
arcLength - 1,
|
2021-07-01 22:11:09 +00:00
|
|
|
sw,
|
|
|
|
shape.style.dash,
|
|
|
|
2
|
|
|
|
)
|
2021-06-22 21:06:51 +00:00
|
|
|
|
2021-07-08 12:15:23 +00:00
|
|
|
const center = [circle[0], circle[1]]
|
|
|
|
const radius = circle[2]
|
|
|
|
const sa = vec.angle(center, start.point)
|
|
|
|
const ea = vec.angle(center, end.point)
|
|
|
|
const t = arrowHeadlength / Math.abs(arcLength)
|
2021-06-22 21:06:51 +00:00
|
|
|
|
2021-07-08 12:15:23 +00:00
|
|
|
insetStart = vec.nudgeAtAngle(center, lerpAngles(sa, ea, t), radius)
|
|
|
|
insetEnd = vec.nudgeAtAngle(center, lerpAngles(ea, sa, t), radius)
|
2021-06-22 21:06:51 +00:00
|
|
|
|
2021-06-30 20:30:48 +00:00
|
|
|
// Curved arrow path
|
2021-06-22 21:06:51 +00:00
|
|
|
shaftPath = (
|
|
|
|
<>
|
|
|
|
<path
|
|
|
|
d={path}
|
|
|
|
fill="none"
|
2021-07-03 16:30:06 +00:00
|
|
|
stroke="transparent"
|
2021-06-22 21:06:51 +00:00
|
|
|
strokeWidth={Math.max(8, strokeWidth * 2)}
|
|
|
|
strokeDasharray="none"
|
|
|
|
strokeDashoffset="none"
|
|
|
|
strokeLinecap="round"
|
2021-07-09 19:43:18 +00:00
|
|
|
strokeLinejoin="round"
|
2021-06-22 21:06:51 +00:00
|
|
|
/>
|
|
|
|
<path
|
|
|
|
d={path}
|
2021-07-08 12:15:23 +00:00
|
|
|
fill={isDraw ? styles.stroke : 'none'}
|
2021-07-03 16:30:06 +00:00
|
|
|
stroke={styles.stroke}
|
2021-06-23 15:00:23 +00:00
|
|
|
strokeWidth={sw}
|
2021-06-22 21:06:51 +00:00
|
|
|
strokeDasharray={strokeDasharray}
|
|
|
|
strokeDashoffset={strokeDashoffset}
|
|
|
|
strokeLinecap="round"
|
2021-07-09 19:43:18 +00:00
|
|
|
strokeLinejoin="round"
|
2021-07-09 16:15:27 +00:00
|
|
|
/>
|
2021-06-22 21:06:51 +00:00
|
|
|
</>
|
2021-06-21 13:13:16 +00:00
|
|
|
)
|
2021-06-08 16:43:33 +00:00
|
|
|
}
|
2021-05-31 19:13:43 +00:00
|
|
|
|
2021-07-09 16:15:27 +00:00
|
|
|
const sw = strokeWidth * 1.618
|
|
|
|
|
2021-06-21 13:13:16 +00:00
|
|
|
return (
|
2021-07-09 19:43:18 +00:00
|
|
|
<g pointerEvents="all">
|
2021-06-22 21:06:51 +00:00
|
|
|
{shaftPath}
|
|
|
|
{shape.decorations.start === Decoration.Arrow && (
|
|
|
|
<path
|
2021-07-08 12:15:23 +00:00
|
|
|
d={getArrowHeadPath(shape, start.point, insetStart)}
|
2021-06-22 21:06:51 +00:00
|
|
|
fill="none"
|
2021-07-03 16:30:06 +00:00
|
|
|
stroke={styles.stroke}
|
2021-07-09 16:15:27 +00:00
|
|
|
strokeWidth={sw}
|
2021-06-22 21:06:51 +00:00
|
|
|
strokeDashoffset="none"
|
|
|
|
strokeDasharray="none"
|
2021-07-09 16:15:27 +00:00
|
|
|
strokeLinecap="round"
|
|
|
|
strokeLinejoin="round"
|
2021-07-09 19:43:18 +00:00
|
|
|
pointerEvents="stroke"
|
2021-06-22 21:06:51 +00:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{shape.decorations.end === Decoration.Arrow && (
|
|
|
|
<path
|
2021-07-08 12:15:23 +00:00
|
|
|
d={getArrowHeadPath(shape, end.point, insetEnd)}
|
2021-06-22 21:06:51 +00:00
|
|
|
fill="none"
|
2021-07-03 16:30:06 +00:00
|
|
|
stroke={styles.stroke}
|
2021-07-09 16:15:27 +00:00
|
|
|
strokeWidth={sw}
|
2021-06-22 21:06:51 +00:00
|
|
|
strokeDashoffset="none"
|
|
|
|
strokeDasharray="none"
|
2021-07-09 16:15:27 +00:00
|
|
|
strokeLinecap="round"
|
|
|
|
strokeLinejoin="round"
|
2021-07-09 19:43:18 +00:00
|
|
|
pointerEvents="stroke"
|
2021-06-22 21:06:51 +00:00
|
|
|
/>
|
|
|
|
)}
|
2021-06-21 13:13:16 +00:00
|
|
|
</g>
|
|
|
|
)
|
2021-05-31 19:13:43 +00:00
|
|
|
},
|
|
|
|
|
2021-06-05 14:29:49 +00:00
|
|
|
rotateBy(shape, delta) {
|
2021-06-05 07:42:17 +00:00
|
|
|
const { start, end, bend } = shape.handles
|
2021-06-05 14:29:49 +00:00
|
|
|
const mp = vec.med(start.point, end.point)
|
|
|
|
start.point = vec.rotWith(start.point, mp, delta)
|
|
|
|
end.point = vec.rotWith(end.point, mp, delta)
|
|
|
|
bend.point = vec.rotWith(bend.point, mp, delta)
|
2021-06-05 07:42:17 +00:00
|
|
|
|
2021-07-08 10:05:25 +00:00
|
|
|
this.onHandleChange(shape, shape.handles, {
|
|
|
|
delta: [0, 0],
|
|
|
|
shiftKey: false,
|
|
|
|
})
|
2021-06-05 07:42:17 +00:00
|
|
|
|
2021-06-05 14:29:49 +00:00
|
|
|
return this
|
|
|
|
},
|
2021-06-05 07:42:17 +00:00
|
|
|
|
2021-06-05 14:29:49 +00:00
|
|
|
rotateTo(shape, rotation, delta) {
|
|
|
|
const { start, end, bend } = shape.handles
|
|
|
|
const mp = vec.med(start.point, end.point)
|
|
|
|
start.point = vec.rotWith(start.point, mp, delta)
|
|
|
|
end.point = vec.rotWith(end.point, mp, delta)
|
|
|
|
bend.point = vec.rotWith(bend.point, mp, delta)
|
2021-06-05 07:42:17 +00:00
|
|
|
|
2021-07-08 10:05:25 +00:00
|
|
|
this.onHandleChange(shape, shape.handles, {
|
|
|
|
delta: [0, 0],
|
|
|
|
shiftKey: false,
|
|
|
|
})
|
2021-06-05 07:42:17 +00:00
|
|
|
|
|
|
|
return this
|
|
|
|
},
|
|
|
|
|
2021-05-31 19:13:43 +00:00
|
|
|
getBounds(shape) {
|
2021-07-08 09:59:47 +00:00
|
|
|
const bounds = getFromCache(this.boundsCache, shape, (cache) => {
|
2021-06-21 13:13:16 +00:00
|
|
|
const { start, bend, end } = shape.handles
|
2021-07-08 09:59:47 +00:00
|
|
|
cache.set(
|
2021-06-21 13:13:16 +00:00
|
|
|
shape,
|
|
|
|
getBoundsFromPoints([start.point, bend.point, end.point])
|
|
|
|
)
|
2021-07-08 09:59:47 +00:00
|
|
|
})
|
2021-06-05 06:36:39 +00:00
|
|
|
|
2021-07-08 09:59:47 +00:00
|
|
|
return translateBounds(bounds, shape.point)
|
2021-06-05 06:36:39 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
getRotatedBounds(shape) {
|
2021-06-21 13:13:16 +00:00
|
|
|
const { start, bend, end } = shape.handles
|
2021-06-05 14:29:49 +00:00
|
|
|
return translateBounds(
|
2021-06-21 13:13:16 +00:00
|
|
|
getBoundsFromPoints([start.point, bend.point, end.point], shape.rotation),
|
2021-06-05 14:29:49 +00:00
|
|
|
shape.point
|
|
|
|
)
|
|
|
|
},
|
2021-05-31 19:13:43 +00:00
|
|
|
|
2021-06-05 14:29:49 +00:00
|
|
|
getCenter(shape) {
|
|
|
|
const { start, end } = shape.handles
|
|
|
|
return vec.add(shape.point, vec.med(start.point, end.point))
|
2021-05-31 19:13:43 +00:00
|
|
|
},
|
|
|
|
|
2021-07-09 19:43:18 +00:00
|
|
|
hitTest() {
|
|
|
|
return true
|
2021-05-31 19:13:43 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
hitTestBounds(this, shape, brushBounds) {
|
2021-06-01 21:49:32 +00:00
|
|
|
const { start, end, bend } = shape.handles
|
|
|
|
|
|
|
|
const sp = vec.add(shape.point, start.point)
|
|
|
|
const ep = vec.add(shape.point, end.point)
|
|
|
|
|
|
|
|
if (pointInBounds(sp, brushBounds) || pointInBounds(ep, brushBounds)) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vec.isEqual(vec.med(start.point, end.point), bend.point)) {
|
|
|
|
return intersectLineSegmentBounds(sp, ep, brushBounds).length > 0
|
|
|
|
} else {
|
|
|
|
const [cx, cy, r] = getCtp(shape)
|
|
|
|
const cp = vec.add(shape.point, [cx, cy])
|
|
|
|
|
|
|
|
return intersectArcBounds(sp, ep, cp, r, brushBounds).length > 0
|
|
|
|
}
|
2021-05-31 19:13:43 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
transform(shape, bounds, { initialShape, scaleX, scaleY }) {
|
|
|
|
const initialShapeBounds = this.getBounds(initialShape)
|
|
|
|
|
2021-06-21 13:13:16 +00:00
|
|
|
// let nw = initialShape.point[0] / initialShapeBounds.width
|
|
|
|
// let nh = initialShape.point[1] / initialShapeBounds.height
|
|
|
|
|
|
|
|
// shape.point = [
|
|
|
|
// bounds.width * (scaleX < 0 ? 1 - nw : nw),
|
|
|
|
// bounds.height * (scaleY < 0 ? 1 - nh : nh),
|
|
|
|
// ]
|
|
|
|
|
2021-05-31 19:13:43 +00:00
|
|
|
shape.point = [bounds.minX, bounds.minY]
|
|
|
|
|
2021-06-21 13:13:16 +00:00
|
|
|
const handles = ['start', 'end']
|
|
|
|
|
|
|
|
handles.forEach((handle) => {
|
|
|
|
const [x, y] = initialShape.handles[handle].point
|
2021-06-21 21:35:28 +00:00
|
|
|
const nw = x / initialShapeBounds.width
|
|
|
|
const nh = y / initialShapeBounds.height
|
2021-05-31 19:13:43 +00:00
|
|
|
|
2021-06-21 13:13:16 +00:00
|
|
|
shape.handles[handle].point = [
|
2021-05-31 19:13:43 +00:00
|
|
|
bounds.width * (scaleX < 0 ? 1 - nw : nw),
|
|
|
|
bounds.height * (scaleY < 0 ? 1 - nh : nh),
|
|
|
|
]
|
|
|
|
})
|
|
|
|
|
2021-06-21 13:13:16 +00:00
|
|
|
const { start, bend, end } = shape.handles
|
|
|
|
|
|
|
|
const dist = vec.dist(start.point, end.point)
|
|
|
|
|
|
|
|
const midPoint = vec.med(start.point, end.point)
|
|
|
|
|
|
|
|
const bendDist = (dist / 2) * initialShape.bend
|
2021-05-31 20:44:21 +00:00
|
|
|
|
2021-06-21 13:13:16 +00:00
|
|
|
const u = vec.uni(vec.vec(start.point, end.point))
|
2021-05-31 20:44:21 +00:00
|
|
|
|
2021-06-21 13:13:16 +00:00
|
|
|
const point = vec.add(midPoint, vec.mul(vec.per(u), bendDist))
|
2021-05-31 20:44:21 +00:00
|
|
|
|
2021-06-21 13:13:16 +00:00
|
|
|
bend.point = Math.abs(bendDist) < 10 ? midPoint : point
|
2021-05-31 20:44:21 +00:00
|
|
|
|
2021-05-31 19:13:43 +00:00
|
|
|
return this
|
|
|
|
},
|
|
|
|
|
2021-06-22 21:06:51 +00:00
|
|
|
onDoublePointHandle(shape, handle) {
|
|
|
|
switch (handle) {
|
|
|
|
case 'bend': {
|
|
|
|
shape.bend = 0
|
|
|
|
shape.handles.bend.point = getBendPoint(shape)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
case 'start': {
|
|
|
|
shape.decorations.start = shape.decorations.start
|
|
|
|
? null
|
|
|
|
: Decoration.Arrow
|
|
|
|
break
|
|
|
|
}
|
|
|
|
case 'end': {
|
|
|
|
shape.decorations.end = shape.decorations.end ? null : Decoration.Arrow
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-06-05 14:29:49 +00:00
|
|
|
|
2021-06-22 21:06:51 +00:00
|
|
|
return this
|
|
|
|
},
|
|
|
|
|
2021-07-08 10:05:25 +00:00
|
|
|
onHandleChange(shape, handles, { shiftKey }) {
|
|
|
|
// Apple changes to the handles
|
2021-06-21 21:35:28 +00:00
|
|
|
for (const id in handles) {
|
2021-05-31 19:13:43 +00:00
|
|
|
const handle = handles[id]
|
|
|
|
shape.handles[handle.id] = handle
|
2021-06-23 15:00:23 +00:00
|
|
|
}
|
2021-05-31 19:13:43 +00:00
|
|
|
|
2021-07-08 10:05:25 +00:00
|
|
|
// If the user is holding shift, we want to snap the handles to angles
|
|
|
|
for (const id in handles) {
|
|
|
|
if ((id === 'start' || id === 'end') && shiftKey) {
|
|
|
|
const point = handles[id].point
|
|
|
|
const other = id === 'start' ? shape.handles.end : shape.handles.start
|
|
|
|
const angle = vec.angle(other.point, point)
|
|
|
|
const distance = vec.dist(other.point, point)
|
|
|
|
const newAngle = clampToRotationToSegments(angle, 24)
|
|
|
|
|
|
|
|
shape.handles[id].point = vec.nudgeAtAngle(
|
|
|
|
other.point,
|
|
|
|
newAngle,
|
|
|
|
distance
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the user is moving the bend handle, we want to move the bend point
|
2021-06-23 15:00:23 +00:00
|
|
|
if ('bend' in handles) {
|
2021-06-01 21:49:32 +00:00
|
|
|
const { start, end, bend } = shape.handles
|
|
|
|
|
2021-07-08 10:05:25 +00:00
|
|
|
const distance = vec.dist(start.point, end.point)
|
2021-06-23 15:00:23 +00:00
|
|
|
const midPoint = vec.med(start.point, end.point)
|
2021-07-08 10:05:25 +00:00
|
|
|
const angle = vec.angle(start.point, end.point)
|
2021-06-23 15:00:23 +00:00
|
|
|
const u = vec.uni(vec.vec(start.point, end.point))
|
2021-06-01 21:49:32 +00:00
|
|
|
|
2021-07-08 10:05:25 +00:00
|
|
|
// Create a line segment perendicular to the line between the start and end points
|
|
|
|
const ap = vec.add(midPoint, vec.mul(vec.per(u), distance / 2))
|
|
|
|
const bp = vec.sub(midPoint, vec.mul(vec.per(u), distance / 2))
|
2021-06-01 21:49:32 +00:00
|
|
|
|
2021-07-08 12:15:23 +00:00
|
|
|
const bendPoint = vec.nearestPointOnLineSegment(ap, bp, bend.point, true)
|
|
|
|
|
2021-07-08 11:01:33 +00:00
|
|
|
// Find the distance between the midpoint and the nearest point on the
|
|
|
|
// line segment to the bend handle's dragged point
|
2021-07-08 12:15:23 +00:00
|
|
|
const bendDist = vec.dist(midPoint, bendPoint)
|
2021-07-08 10:05:25 +00:00
|
|
|
|
2021-07-08 11:01:33 +00:00
|
|
|
// The shape's "bend" is the ratio of the bend to the distance between
|
|
|
|
// the start and end points. If the bend is below a certain amount, the
|
|
|
|
// bend should be zero.
|
2021-07-08 12:15:23 +00:00
|
|
|
shape.bend = clamp(bendDist / (distance / 2), -0.99, 0.99)
|
2021-06-23 15:00:23 +00:00
|
|
|
|
2021-07-08 10:05:25 +00:00
|
|
|
// If the point is to the left of the line segment, we make the bend
|
|
|
|
// negative, otherwise it's positive.
|
2021-07-08 12:15:23 +00:00
|
|
|
const angleToBend = vec.angle(start.point, bendPoint)
|
2021-07-08 11:01:33 +00:00
|
|
|
|
2021-07-08 12:15:23 +00:00
|
|
|
if (isAngleBetween(angle, angle + Math.PI, angleToBend)) {
|
2021-06-23 15:00:23 +00:00
|
|
|
shape.bend *= -1
|
2021-05-31 19:13:43 +00:00
|
|
|
}
|
2021-05-31 20:44:21 +00:00
|
|
|
}
|
2021-05-31 19:13:43 +00:00
|
|
|
|
2021-07-08 11:01:33 +00:00
|
|
|
shape.handles.start.point = vec.round(shape.handles.start.point)
|
|
|
|
shape.handles.end.point = vec.round(shape.handles.end.point)
|
|
|
|
shape.handles.bend.point = getBendPoint(shape)
|
2021-06-30 20:30:48 +00:00
|
|
|
|
2021-06-05 14:29:49 +00:00
|
|
|
return this
|
|
|
|
},
|
|
|
|
|
|
|
|
onSessionComplete(shape) {
|
|
|
|
const bounds = this.getBounds(shape)
|
|
|
|
|
|
|
|
const offset = vec.sub([bounds.minX, bounds.minY], shape.point)
|
|
|
|
|
|
|
|
this.translateTo(shape, vec.add(shape.point, offset))
|
|
|
|
|
|
|
|
const { start, end, bend } = shape.handles
|
|
|
|
|
2021-07-08 11:01:33 +00:00
|
|
|
start.point = vec.round(vec.sub(start.point, offset))
|
|
|
|
end.point = vec.round(vec.sub(end.point, offset))
|
|
|
|
bend.point = vec.round(vec.sub(bend.point, offset))
|
2021-06-05 14:29:49 +00:00
|
|
|
|
2021-06-21 13:13:16 +00:00
|
|
|
shape.handles = { ...shape.handles }
|
|
|
|
|
2021-05-31 19:13:43 +00:00
|
|
|
return this
|
|
|
|
},
|
|
|
|
|
2021-06-02 15:58:51 +00:00
|
|
|
applyStyles(shape, style) {
|
|
|
|
Object.assign(shape.style, style)
|
|
|
|
shape.style.isFilled = false
|
|
|
|
return this
|
|
|
|
},
|
|
|
|
|
2021-06-01 21:49:32 +00:00
|
|
|
canStyleFill: false,
|
2021-05-31 19:13:43 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
export default arrow
|
|
|
|
|
|
|
|
function getArrowArcPath(
|
2021-06-01 08:56:41 +00:00
|
|
|
start: ShapeHandle,
|
|
|
|
end: ShapeHandle,
|
|
|
|
circle: number[],
|
|
|
|
bend: number
|
2021-05-31 19:13:43 +00:00
|
|
|
) {
|
2021-06-01 08:56:41 +00:00
|
|
|
return [
|
|
|
|
'M',
|
|
|
|
start.point[0],
|
|
|
|
start.point[1],
|
|
|
|
'A',
|
|
|
|
circle[2],
|
|
|
|
circle[2],
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
bend < 0 ? 0 : 1,
|
|
|
|
end.point[0],
|
|
|
|
end.point[1],
|
|
|
|
].join(' ')
|
2021-05-31 19:13:43 +00:00
|
|
|
}
|
2021-06-01 21:49:32 +00:00
|
|
|
|
|
|
|
function getBendPoint(shape: ArrowShape) {
|
2021-06-02 21:17:38 +00:00
|
|
|
const { start, end } = shape.handles
|
2021-06-01 21:49:32 +00:00
|
|
|
|
|
|
|
const dist = vec.dist(start.point, end.point)
|
|
|
|
const midPoint = vec.med(start.point, end.point)
|
2021-06-18 15:31:46 +00:00
|
|
|
const bendDist = (dist / 2) * shape.bend
|
2021-06-01 21:49:32 +00:00
|
|
|
const u = vec.uni(vec.vec(start.point, end.point))
|
|
|
|
|
2021-07-08 12:15:23 +00:00
|
|
|
const point = vec.round(
|
2021-07-08 10:05:25 +00:00
|
|
|
Math.abs(bendDist) < 10
|
|
|
|
? midPoint
|
|
|
|
: vec.add(midPoint, vec.mul(vec.per(u), bendDist))
|
|
|
|
)
|
2021-07-08 12:15:23 +00:00
|
|
|
|
|
|
|
return point
|
2021-06-01 21:49:32 +00:00
|
|
|
}
|
2021-06-05 14:29:49 +00:00
|
|
|
|
2021-06-22 21:06:51 +00:00
|
|
|
function renderFreehandArrowShaft(shape: ArrowShape) {
|
2021-06-08 16:43:33 +00:00
|
|
|
const { style, id } = shape
|
2021-06-18 15:31:46 +00:00
|
|
|
const { start, end } = shape.handles
|
2021-06-08 16:43:33 +00:00
|
|
|
|
|
|
|
const getRandom = rng(id)
|
|
|
|
|
|
|
|
const strokeWidth = +getShapeStyle(style).strokeWidth * 2
|
|
|
|
|
2021-07-01 22:11:09 +00:00
|
|
|
const st = Math.abs(getRandom())
|
2021-06-08 16:43:33 +00:00
|
|
|
|
2021-07-08 11:01:33 +00:00
|
|
|
const stroke = getStroke(
|
|
|
|
[
|
|
|
|
...vec.pointsBetween(start.point, end.point),
|
|
|
|
end.point,
|
|
|
|
end.point,
|
|
|
|
end.point,
|
|
|
|
end.point,
|
|
|
|
],
|
|
|
|
{
|
|
|
|
size: strokeWidth / 2,
|
|
|
|
thinning: 0.5 + getRandom() * 0.3,
|
|
|
|
easing: (t) => t * t,
|
|
|
|
end: { taper: 1 },
|
|
|
|
start: { taper: 1 + 32 * (st * st * st) },
|
|
|
|
simulatePressure: true,
|
|
|
|
last: true,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
const path = getSvgPathFromStroke(stroke)
|
2021-06-08 16:43:33 +00:00
|
|
|
|
2021-07-08 11:01:33 +00:00
|
|
|
return path
|
2021-06-08 16:43:33 +00:00
|
|
|
}
|
2021-06-21 13:13:16 +00:00
|
|
|
|
2021-07-08 12:15:23 +00:00
|
|
|
function renderCurvedFreehandArrowShaft(shape: ArrowShape, circle: number[]) {
|
|
|
|
const { style, id } = shape
|
2021-06-21 13:13:16 +00:00
|
|
|
const { start, end } = shape.handles
|
|
|
|
|
2021-07-08 12:15:23 +00:00
|
|
|
const getRandom = rng(id)
|
|
|
|
|
|
|
|
const strokeWidth = +getShapeStyle(style).strokeWidth * 2
|
2021-06-21 13:13:16 +00:00
|
|
|
|
2021-07-08 12:15:23 +00:00
|
|
|
const st = Math.abs(getRandom())
|
2021-06-21 13:13:16 +00:00
|
|
|
|
2021-07-08 12:15:23 +00:00
|
|
|
const center = [circle[0], circle[1]]
|
|
|
|
const radius = circle[2]
|
2021-06-21 13:13:16 +00:00
|
|
|
|
2021-07-08 12:15:23 +00:00
|
|
|
const startAngle = vec.angle(center, start.point)
|
|
|
|
|
|
|
|
const endAngle = vec.angle(center, end.point)
|
2021-06-21 13:13:16 +00:00
|
|
|
|
2021-07-08 12:15:23 +00:00
|
|
|
const points: number[][] = []
|
2021-06-21 13:13:16 +00:00
|
|
|
|
2021-07-08 12:26:55 +00:00
|
|
|
for (let i = 0; i < 21; i++) {
|
|
|
|
const t = i / 20
|
2021-07-08 12:15:23 +00:00
|
|
|
const angle = lerpAngles(startAngle, endAngle, t)
|
|
|
|
points.push(vec.round(vec.nudgeAtAngle(center, angle, radius)))
|
|
|
|
}
|
|
|
|
|
2021-07-08 12:26:55 +00:00
|
|
|
const stroke = getStroke([...points, end.point, end.point], {
|
|
|
|
size: strokeWidth / 2,
|
|
|
|
thinning: 0.5 + getRandom() * 0.3,
|
|
|
|
easing: (t) => t * t,
|
2021-07-08 12:46:34 +00:00
|
|
|
end: {
|
|
|
|
taper: shape.decorations.end ? 1 : 1 + strokeWidth * 5 * (st * st * st),
|
|
|
|
},
|
|
|
|
start: {
|
|
|
|
taper: shape.decorations.start ? 1 : 1 + strokeWidth * 5 * (st * st * st),
|
|
|
|
},
|
2021-07-08 12:26:55 +00:00
|
|
|
simulatePressure: true,
|
|
|
|
streamline: 0.01,
|
|
|
|
last: true,
|
|
|
|
})
|
2021-07-08 12:15:23 +00:00
|
|
|
|
|
|
|
const path = getSvgPathFromStroke(stroke)
|
|
|
|
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
|
|
|
function getArrowHeadPath(shape: ArrowShape, point: number[], inset: number[]) {
|
|
|
|
const { left, right } = getArrowHeadPoints(shape, point, inset)
|
|
|
|
return ['M', left, 'L', point, right].join(' ')
|
|
|
|
}
|
|
|
|
|
|
|
|
function getArrowHeadPoints(
|
|
|
|
shape: ArrowShape,
|
|
|
|
point: number[],
|
|
|
|
inset: number[]
|
|
|
|
) {
|
2021-06-21 13:13:16 +00:00
|
|
|
// Use the shape's random seed to create minor offsets for the angles
|
|
|
|
const getRandom = rng(shape.id)
|
|
|
|
|
|
|
|
return {
|
2021-07-08 12:15:23 +00:00
|
|
|
left: vec.rotWith(inset, point, Math.PI / 6 + (Math.PI / 12) * getRandom()),
|
|
|
|
right: vec.rotWith(
|
|
|
|
inset,
|
2021-06-22 21:06:51 +00:00
|
|
|
point,
|
2021-07-08 12:15:23 +00:00
|
|
|
-Math.PI / 6 + (Math.PI / 12) * getRandom()
|
2021-06-21 13:13:16 +00:00
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|