tldraw/state/shape-utils/arrow.tsx

561 lines
14 KiB
TypeScript
Raw Normal View History

2021-06-21 21:35:28 +00:00
import { getArcLength, uniqueId } from 'utils/utils'
import vec from 'utils/vec'
2021-06-08 16:43:33 +00:00
import {
getSvgPathFromStroke,
rng,
getBoundsFromPoints,
translateBounds,
pointsBetween,
} from 'utils/utils'
2021-06-21 21:35:28 +00:00
import { ArrowShape, DashStyle, ShapeHandle, ShapeType } from 'types'
2021-06-07 21:12:14 +00:00
import { circleFromThreePoints, isAngleBetween } from 'utils/utils'
2021-06-21 21:35:28 +00:00
import { pointInBounds } from 'utils/hitTests'
2021-06-01 21:49:32 +00:00
import {
intersectArcBounds,
intersectLineSegmentBounds,
} from 'utils/intersections'
2021-05-31 19:13:43 +00:00
import { pointInCircle } from 'utils/hitTests'
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-08 16:43:33 +00:00
const pathCache = new WeakMap<ArrowShape, string>([])
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([]),
create(props) {
const {
point = [0, 0],
handles = {
start: {
id: 'start',
index: 0,
point: [0, 0],
},
end: {
id: 'end',
index: 1,
point: [1, 1],
},
bend: {
id: 'bend',
index: 2,
point: [0.5, 0.5],
},
},
} = props
return {
id: uniqueId(),
2021-06-07 21:12:14 +00:00
seed: Math.random(),
2021-05-31 19:13:43 +00:00
type: ShapeType.Arrow,
isGenerated: false,
name: 'Arrow',
parentId: 'page1',
2021-05-31 19:13:43 +00:00
childIndex: 0,
point,
rotation: 0,
isAspectRatioLocked: false,
isLocked: false,
isHidden: false,
bend: 0,
handles,
decorations: {
start: null,
end: null,
middle: null,
},
...props,
style: {
...defaultStyle,
2021-05-31 19:13:43 +00:00
...props.style,
isFilled: false,
2021-05-31 19:13:43 +00:00
},
}
},
2021-06-01 21:49:32 +00:00
render(shape) {
2021-06-21 13:13:16 +00:00
const { id, bend, handles, style } = shape
2021-05-31 19:13:43 +00:00
const { start, end, bend: _bend } = handles
2021-06-21 13:13:16 +00:00
const isStraightLine = vec.isEqual(
2021-06-02 21:17:38 +00:00
_bend.point,
vec.med(start.point, end.point)
)
2021-05-31 19:13:43 +00:00
2021-06-21 13:13:16 +00:00
const styles = getShapeStyle(style)
2021-06-21 13:13:16 +00:00
const strokeWidth = +styles.strokeWidth
2021-06-02 21:17:38 +00:00
2021-06-21 13:13:16 +00:00
if (isStraightLine) {
// Render a straight arrow as a freehand path.
2021-06-08 16:43:33 +00:00
if (!pathCache.has(shape)) {
2021-06-21 13:13:16 +00:00
renderPath(shape)
2021-06-08 16:43:33 +00:00
}
2021-06-02 21:17:38 +00:00
2021-06-21 13:13:16 +00:00
const offset = -vec.dist(start.point, end.point) + strokeWidth
2021-06-08 16:43:33 +00:00
const path = pathCache.get(shape)
2021-06-02 21:17:38 +00:00
2021-06-21 13:13:16 +00:00
return (
<g id={id}>
{/* Improves hit testing */}
2021-06-08 16:43:33 +00:00
<path
2021-06-21 13:13:16 +00:00
d={path}
stroke="transparent"
2021-06-08 16:43:33 +00:00
fill="none"
2021-06-21 13:13:16 +00:00
strokeWidth={Math.max(8, strokeWidth * 2)}
2021-06-08 16:43:33 +00:00
strokeLinecap="round"
2021-06-21 13:13:16 +00:00
strokeDasharray="none"
2021-06-08 16:43:33 +00:00
/>
2021-06-21 13:13:16 +00:00
{/* Arrowshaft */}
<circle
cx={start.point[0]}
cy={start.point[1]}
r={strokeWidth}
fill={styles.stroke}
stroke="none"
/>
<path
d={path}
fill="none"
strokeWidth={
strokeWidth * (style.dash === DashStyle.Solid ? 1 : 1.618)
}
strokeDashoffset={offset}
strokeLinecap="round"
/>
{/* Arrowhead */}
{style.dash !== DashStyle.Solid && (
<path
d={getArrowHeadPath(shape, 0)}
strokeWidth={strokeWidth * 1.618}
strokeDasharray="none"
fill="none"
/>
)}
</g>
2021-06-02 21:17:38 +00:00
)
2021-06-21 13:13:16 +00:00
}
2021-06-02 21:17:38 +00:00
2021-06-21 13:13:16 +00:00
const circle = getCtp(shape)
2021-06-08 16:43:33 +00:00
2021-06-21 13:13:16 +00:00
if (!pathCache.has(shape)) {
renderPath(
shape,
vec.angle([circle[0], circle[1]], end.point) -
vec.angle(start.point, end.point) +
(Math.PI / 2) * (bend > 0 ? 0.98 : -0.98)
)
2021-06-08 16:43:33 +00:00
}
2021-05-31 19:13:43 +00:00
2021-06-21 13:13:16 +00:00
const path = getArrowArcPath(start, end, circle, bend)
const strokeDashOffset = getStrokeDashOffsetForArc(
shape,
circle,
strokeWidth
)
return (
<g id={id}>
{/* Improves hit testing */}
<path
d={path}
stroke="transparent"
fill="none"
strokeWidth={Math.max(8, strokeWidth * 2)}
strokeLinecap="round"
strokeDasharray="none"
/>
{/* Arrow Shaft */}
<circle
cx={start.point[0]}
cy={start.point[1]}
r={strokeWidth}
fill={styles.stroke}
stroke="none"
/>
<path
d={path}
fill="none"
strokeWidth={strokeWidth * 1.618}
strokeLinecap="round"
strokeDashoffset={strokeDashOffset}
/>
{/* Arrowhead */}
<path
d={pathCache.get(shape)}
strokeWidth={strokeWidth * 1.618}
strokeDasharray="none"
fill="none"
/>
</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-06-05 14:29:49 +00:00
this.onHandleChange(shape, shape.handles)
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-06-05 14:29:49 +00:00
this.onHandleChange(shape, shape.handles)
2021-06-05 07:42:17 +00:00
return this
},
2021-05-31 19:13:43 +00:00
getBounds(shape) {
2021-06-05 06:36:39 +00:00
if (!this.boundsCache.has(shape)) {
2021-06-21 13:13:16 +00:00
const { start, bend, end } = shape.handles
this.boundsCache.set(
shape,
getBoundsFromPoints([start.point, bend.point, end.point])
)
2021-06-05 06:36:39 +00:00
}
return translateBounds(this.boundsCache.get(shape), shape.point)
},
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
},
hitTest(shape, point) {
2021-06-21 13:13:16 +00:00
const { start, end } = shape.handles
2021-05-31 19:13:43 +00:00
if (shape.bend === 0) {
return (
vec.distanceToLineSegment(
start.point,
end.point,
vec.sub(point, shape.point)
) < 4
)
}
2021-06-01 21:49:32 +00:00
const [cx, cy, r] = getCtp(shape)
2021-05-31 19:13:43 +00:00
return !pointInCircle(point, vec.add(shape.point, [cx, cy]), r - 4)
},
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-04 16:08:43 +00:00
onHandleChange(shape, handles) {
2021-06-05 14:29:49 +00:00
// const oldBounds = this.getRotatedBounds(shape)
// const prevCenter = getBoundsCenter(oldBounds)
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-01 21:49:32 +00:00
const { start, end, bend } = shape.handles
2021-05-31 19:13:43 +00:00
const dist = vec.dist(start.point, end.point)
if (handle.id === 'bend') {
2021-06-01 21:49:32 +00:00
const midPoint = vec.med(start.point, end.point)
const u = vec.uni(vec.vec(start.point, end.point))
const ap = vec.add(midPoint, vec.mul(vec.per(u), dist / 2))
const bp = vec.sub(midPoint, vec.mul(vec.per(u), dist / 2))
bend.point = vec.nearestPointOnLineSegment(ap, bp, bend.point, true)
shape.bend = vec.dist(bend.point, midPoint) / (dist / 2)
const sa = vec.angle(end.point, start.point)
const la = sa - Math.PI / 2
if (isAngleBetween(sa, la, vec.angle(end.point, bend.point))) {
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-06-01 21:49:32 +00:00
shape.handles.bend.point = getBendPoint(shape)
2021-05-31 19:13:43 +00:00
2021-06-05 14:29:49 +00:00
// const newBounds = this.getRotatedBounds(shape)
// const newCenter = getBoundsCenter(newBounds)
// shape.point = vec.add(shape.point, vec.neg(vec.sub(newCenter, prevCenter)))
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
start.point = vec.sub(start.point, offset)
end.point = vec.sub(end.point, offset)
bend.point = vec.sub(bend.point, offset)
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))
return Math.abs(bendDist) < 10
? midPoint
: vec.add(midPoint, vec.mul(vec.per(u), bendDist))
}
2021-06-05 14:29:49 +00:00
2021-06-08 16:43:33 +00:00
function renderPath(shape: ArrowShape, endAngle = 0) {
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-06-21 13:13:16 +00:00
const sw = strokeWidth
2021-06-08 16:43:33 +00:00
// Start
const a = start.point
2021-06-21 13:13:16 +00:00
// End
const b = end.point
2021-06-08 16:43:33 +00:00
// Middle
const m = vec.add(
vec.lrp(start.point, end.point, 0.25 + Math.abs(getRandom()) / 2),
[getRandom() * sw, getRandom() * sw]
)
2021-06-21 13:13:16 +00:00
// Left and right sides of the arrowhead
let { left: c, right: d } = getArrowHeadPoints(shape, endAngle)
2021-06-08 16:43:33 +00:00
2021-06-21 13:13:16 +00:00
// Switch which side of the arrow is drawn first
if (getRandom() > 0) [c, d] = [d, c]
2021-06-08 16:43:33 +00:00
2021-06-21 13:13:16 +00:00
if (style.dash !== DashStyle.Solid) {
pathCache.set(
shape,
(endAngle ? ['M', c, 'L', b, d] : ['M', a, 'L', b]).join(' ')
)
return
2021-06-08 16:43:33 +00:00
}
const points = endAngle
? [
// Just the arrowhead
...pointsBetween(b, c),
...pointsBetween(c, b),
...pointsBetween(b, d),
...pointsBetween(d, b),
]
: [
2021-06-21 13:13:16 +00:00
// The arrow shaft
2021-06-08 16:43:33 +00:00
b,
a,
...pointsBetween(a, m),
...pointsBetween(m, b),
...pointsBetween(b, c),
...pointsBetween(c, b),
...pointsBetween(b, d),
...pointsBetween(d, b),
]
const stroke = getStroke(points, {
size: 1 + strokeWidth,
thinning: 0.6,
easing: (t) => t * t * t * t,
end: { taper: strokeWidth * 20 },
start: { taper: strokeWidth * 20 },
simulatePressure: false,
})
pathCache.set(shape, getSvgPathFromStroke(stroke))
}
2021-06-21 13:13:16 +00:00
function getArrowHeadPath(shape: ArrowShape, endAngle = 0) {
const { end } = shape.handles
const { left, right } = getArrowHeadPoints(shape, endAngle)
return ['M', left, 'L', end.point, right].join(' ')
}
function getArrowHeadPoints(shape: ArrowShape, endAngle = 0) {
const { start, end } = shape.handles
const stroke = +getShapeStyle(shape.style).strokeWidth * 2
const arrowDist = vec.dist(start.point, end.point)
const arrowHeadlength = Math.min(arrowDist / 3, stroke * 4)
// Unit vector from start to end
const u = vec.uni(vec.vec(start.point, end.point))
// The end of the arrowhead wings
const v = vec.rot(vec.mul(vec.neg(u), arrowHeadlength), endAngle)
// Use the shape's random seed to create minor offsets for the angles
const getRandom = rng(shape.id)
return {
left: vec.add(
end.point,
vec.rot(v, Math.PI / 6 + (Math.PI / 8) * getRandom())
),
right: vec.add(
end.point,
vec.rot(v, -(Math.PI / 6) + (Math.PI / 8) * getRandom())
),
}
}
function getStrokeDashOffsetForArc(
shape: ArrowShape,
circle: number[],
strokeWidth: number
) {
const { start, end } = shape.handles
const sweep = getArcLength(
[circle[0], circle[1]],
circle[2],
start.point,
end.point
)
return Math.abs(shape.bend) === 1
? -strokeWidth / 2
: shape.bend < 0
? sweep + strokeWidth
: -sweep + strokeWidth
}