tldraw/lib/shape-utils/draw.tsx

185 lines
4.5 KiB
TypeScript
Raw Normal View History

2021-05-28 14:37:23 +00:00
import { v4 as uuid } from 'uuid'
import * as vec from 'utils/vec'
import { DrawShape, ShapeType } from 'types'
import { registerShapeUtils } from './index'
import { intersectPolylineBounds } from 'utils/intersections'
import { boundsContainPolygon } from 'utils/bounds'
import getStroke from 'perfect-freehand'
2021-05-27 22:07:27 +00:00
import {
getBoundsFromPoints,
2021-05-28 16:25:43 +00:00
getRotatedCorners,
2021-05-27 22:07:27 +00:00
getSvgPathFromStroke,
translateBounds,
2021-05-28 14:37:23 +00:00
} from 'utils/utils'
2021-05-27 18:51:25 +00:00
2021-05-28 21:05:40 +00:00
const pathCache = new WeakMap<DrawShape, string>([])
2021-05-27 17:59:40 +00:00
const draw = registerShapeUtils<DrawShape>({
boundsCache: new WeakMap([]),
create(props) {
return {
id: uuid(),
type: ShapeType.Draw,
isGenerated: false,
2021-05-28 14:37:23 +00:00
name: 'Draw',
parentId: 'page0',
2021-05-27 17:59:40 +00:00
childIndex: 0,
point: [0, 0],
points: [[0, 0]],
rotation: 0,
isAspectRatioLocked: false,
isLocked: false,
isHidden: false,
2021-05-27 17:59:40 +00:00
...props,
style: {
strokeWidth: 2,
2021-05-28 14:37:23 +00:00
strokeLinecap: 'round',
strokeLinejoin: 'round',
2021-05-27 17:59:40 +00:00
...props.style,
2021-05-28 20:30:27 +00:00
fill: props.style.stroke,
2021-05-27 17:59:40 +00:00
},
}
},
2021-05-27 18:51:25 +00:00
render(shape) {
2021-05-28 21:05:40 +00:00
const { id, points, style } = shape
2021-05-27 18:51:25 +00:00
2021-05-28 21:05:40 +00:00
if (!pathCache.has(shape)) {
2021-05-28 16:25:43 +00:00
if (points.length < 2) {
2021-05-28 21:05:40 +00:00
const left = [+style.strokeWidth, 0]
2021-05-28 16:25:43 +00:00
let d: number[][] = []
for (let i = 0; i < 10; i++) {
2021-05-28 21:05:40 +00:00
d.push(vec.rotWith(left, [0, 0], i * ((Math.PI * 2) / 8)))
2021-05-28 16:25:43 +00:00
}
2021-05-28 21:05:40 +00:00
pathCache.set(shape, getSvgPathFromStroke(d))
2021-05-28 16:25:43 +00:00
} else {
2021-05-28 20:30:27 +00:00
pathCache.set(
2021-05-28 21:05:40 +00:00
shape,
2021-05-28 20:30:27 +00:00
getSvgPathFromStroke(
2021-05-28 21:05:40 +00:00
getStroke(points, { size: +style.strokeWidth * 2 })
2021-05-28 20:30:27 +00:00
)
)
2021-05-28 16:25:43 +00:00
}
2021-05-27 18:51:25 +00:00
}
2021-05-28 21:05:40 +00:00
return <path id={id} d={pathCache.get(shape)} />
2021-05-27 17:59:40 +00:00
},
applyStyles(shape, style) {
Object.assign(shape.style, style)
2021-05-28 20:30:27 +00:00
shape.style.fill = shape.style.stroke
2021-05-27 17:59:40 +00:00
return this
},
getBounds(shape) {
if (!this.boundsCache.has(shape)) {
const bounds = getBoundsFromPoints(shape.points)
this.boundsCache.set(shape, bounds)
}
return translateBounds(this.boundsCache.get(shape), shape.point)
},
getRotatedBounds(shape) {
2021-05-28 16:25:43 +00:00
return getBoundsFromPoints(
getRotatedCorners(this.getBounds(shape), shape.rotation)
)
2021-05-27 17:59:40 +00:00
},
getCenter(shape) {
2021-05-28 16:25:43 +00:00
const bounds = this.getRotatedBounds(shape)
2021-05-27 17:59:40 +00:00
return [bounds.minX + bounds.width / 2, bounds.minY + bounds.height / 2]
},
hitTest(shape, point) {
let pt = vec.sub(point, shape.point)
let prev = shape.points[0]
for (let i = 1; i < shape.points.length; i++) {
let curr = shape.points[i]
2021-05-28 21:05:40 +00:00
if (
vec.distanceToLineSegment(prev, curr, pt) < +shape.style.strokeWidth
) {
2021-05-27 17:59:40 +00:00
return true
}
prev = curr
}
return false
},
hitTestBounds(this, shape, brushBounds) {
const b = this.getBounds(shape)
const center = [b.minX + b.width / 2, b.minY + b.height / 2]
const rotatedCorners = [
[b.minX, b.minY],
[b.maxX, b.minY],
[b.maxX, b.maxY],
[b.minX, b.maxY],
].map((point) => vec.rotWith(point, center, shape.rotation))
return (
boundsContainPolygon(brushBounds, rotatedCorners) ||
intersectPolylineBounds(
shape.points.map((point) => vec.add(point, shape.point)),
brushBounds
).length > 0
)
},
rotateTo(shape, rotation) {
shape.rotation = rotation
2021-05-28 16:25:43 +00:00
// console.log(shape.points.map(([x, y]) => [x, y]))
// const bounds = this.getBounds(shape)
// const center = [bounds.width / 2, bounds.height / 2]
// shape.points = shape.points.map((pt) => vec.rotWith(pt, center, rotation))
2021-05-27 17:59:40 +00:00
return this
},
translateTo(shape, point) {
2021-05-28 20:30:27 +00:00
shape.point = vec.toPrecision(point)
2021-05-27 17:59:40 +00:00
return this
},
transform(shape, bounds, { initialShape, scaleX, scaleY }) {
const initialShapeBounds = this.boundsCache.get(initialShape)
shape.points = initialShape.points.map(([x, y]) => {
return [
bounds.width *
(scaleX < 0
? 1 - x / initialShapeBounds.width
: x / initialShapeBounds.width),
bounds.height *
(scaleY < 0
? 1 - y / initialShapeBounds.height
: y / initialShapeBounds.height),
]
})
const newBounds = getBoundsFromPoints(shape.points)
shape.point = vec.sub(
[bounds.minX, bounds.minY],
[newBounds.minX, newBounds.minY]
)
return this
},
transformSingle(shape, bounds, info) {
this.transform(shape, bounds, info)
return this
},
setProperty(shape, prop, value) {
shape[prop] = value
2021-05-27 17:59:40 +00:00
return this
},
canTransform: true,
canChangeAspectRatio: true,
})
export default draw