2021-05-28 14:37:23 +00:00
|
|
|
import { v4 as uuid } from 'uuid'
|
|
|
|
import * as vec from 'utils/vec'
|
2021-06-02 21:17:38 +00:00
|
|
|
import { DashStyle, DrawShape, ShapeStyles, ShapeType } from 'types'
|
2021-05-28 14:37:23 +00:00
|
|
|
import { registerShapeUtils } from './index'
|
|
|
|
import { intersectPolylineBounds } from 'utils/intersections'
|
2021-06-06 19:34:27 +00:00
|
|
|
import { boundsContain, boundsContainPolygon } from 'utils/bounds'
|
2021-05-28 14:37:23 +00:00
|
|
|
import getStroke from 'perfect-freehand'
|
2021-05-27 22:07:27 +00:00
|
|
|
import {
|
2021-05-29 22:27:19 +00:00
|
|
|
getBoundsCenter,
|
2021-05-27 22:07:27 +00:00
|
|
|
getBoundsFromPoints,
|
2021-06-06 19:34:27 +00:00
|
|
|
getRotatedCorners,
|
2021-05-27 22:07:27 +00:00
|
|
|
getSvgPathFromStroke,
|
2021-06-06 19:34:27 +00:00
|
|
|
rotateBounds,
|
2021-05-27 22:07:27 +00:00
|
|
|
translateBounds,
|
2021-05-28 14:37:23 +00:00
|
|
|
} from 'utils/utils'
|
2021-06-02 11:50:34 +00:00
|
|
|
import { defaultStyle, getShapeStyle } from 'lib/shape-styles'
|
2021-05-27 18:51:25 +00:00
|
|
|
|
2021-06-06 19:34:27 +00:00
|
|
|
const rotatedCache = new WeakMap<DrawShape, number[][]>([])
|
2021-05-29 13:59:11 +00:00
|
|
|
const pathCache = new WeakMap<DrawShape['points'], string>([])
|
2021-05-27 17:59:40 +00:00
|
|
|
|
|
|
|
const draw = registerShapeUtils<DrawShape>({
|
|
|
|
boundsCache: new WeakMap([]),
|
|
|
|
|
|
|
|
create(props) {
|
|
|
|
return {
|
|
|
|
id: uuid(),
|
2021-06-07 21:12:14 +00:00
|
|
|
seed: Math.random(),
|
2021-05-27 17:59:40 +00:00
|
|
|
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],
|
2021-05-30 13:20:25 +00:00
|
|
|
points: [],
|
2021-05-27 17:59:40 +00:00
|
|
|
rotation: 0,
|
2021-05-29 10:12:28 +00:00
|
|
|
isAspectRatioLocked: false,
|
|
|
|
isLocked: false,
|
|
|
|
isHidden: false,
|
2021-05-27 17:59:40 +00:00
|
|
|
...props,
|
|
|
|
style: {
|
2021-06-02 11:50:34 +00:00
|
|
|
...defaultStyle,
|
2021-05-27 17:59:40 +00:00
|
|
|
...props.style,
|
2021-06-02 11:50:34 +00:00
|
|
|
isFilled: false,
|
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-06-02 11:50:34 +00:00
|
|
|
const styles = getShapeStyle(style)
|
|
|
|
|
2021-05-29 13:59:11 +00:00
|
|
|
if (!pathCache.has(points)) {
|
2021-06-02 21:17:38 +00:00
|
|
|
renderPath(shape, style)
|
2021-05-27 18:51:25 +00:00
|
|
|
}
|
|
|
|
|
2021-05-29 13:59:11 +00:00
|
|
|
if (points.length < 2) {
|
2021-06-02 11:50:34 +00:00
|
|
|
return (
|
|
|
|
<circle id={id} r={+styles.strokeWidth * 0.618} fill={styles.stroke} />
|
|
|
|
)
|
2021-05-29 13:59:11 +00:00
|
|
|
}
|
|
|
|
|
2021-06-02 11:50:34 +00:00
|
|
|
return <path id={id} d={pathCache.get(points)} fill={styles.stroke} />
|
2021-05-27 17:59:40 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
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-06-06 20:49:15 +00:00
|
|
|
return translateBounds(
|
2021-06-06 19:34:27 +00:00
|
|
|
getBoundsFromPoints(shape.points, shape.rotation),
|
2021-05-29 22:27:19 +00:00
|
|
|
shape.point
|
|
|
|
)
|
2021-05-27 17:59:40 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
getCenter(shape) {
|
2021-06-06 20:49:15 +00:00
|
|
|
return getBoundsCenter(this.getBounds(shape))
|
2021-05-27 17:59:40 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
hitTest(shape, point) {
|
|
|
|
let pt = vec.sub(point, shape.point)
|
2021-06-02 11:50:34 +00:00
|
|
|
const min = +getShapeStyle(shape.style).strokeWidth
|
|
|
|
return shape.points.some(
|
|
|
|
(curr, i) =>
|
|
|
|
i > 0 && vec.distanceToLineSegment(shape.points[i - 1], curr, pt) < min
|
|
|
|
)
|
2021-05-27 17:59:40 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
hitTestBounds(this, shape, brushBounds) {
|
2021-06-06 19:34:27 +00:00
|
|
|
// Test axis-aligned shape
|
|
|
|
if (shape.rotation === 0) {
|
|
|
|
return (
|
|
|
|
boundsContain(brushBounds, this.getBounds(shape)) ||
|
2021-06-07 11:18:50 +00:00
|
|
|
intersectPolylineBounds(
|
|
|
|
shape.points,
|
|
|
|
translateBounds(brushBounds, vec.neg(shape.point))
|
|
|
|
).length > 0
|
2021-06-06 19:34:27 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test rotated shape
|
|
|
|
const rBounds = this.getRotatedBounds(shape)
|
2021-05-27 17:59:40 +00:00
|
|
|
|
2021-06-06 19:34:27 +00:00
|
|
|
if (!rotatedCache.has(shape)) {
|
2021-06-07 11:18:50 +00:00
|
|
|
const c = getBoundsCenter(getBoundsFromPoints(shape.points))
|
2021-06-06 19:34:27 +00:00
|
|
|
rotatedCache.set(
|
|
|
|
shape,
|
2021-06-07 11:18:50 +00:00
|
|
|
shape.points.map((pt) => vec.rotWith(pt, c, shape.rotation))
|
2021-06-06 19:34:27 +00:00
|
|
|
)
|
|
|
|
}
|
2021-05-27 17:59:40 +00:00
|
|
|
|
|
|
|
return (
|
2021-06-06 19:34:27 +00:00
|
|
|
boundsContain(brushBounds, rBounds) ||
|
2021-06-07 11:18:50 +00:00
|
|
|
intersectPolylineBounds(
|
|
|
|
rotatedCache.get(shape),
|
|
|
|
translateBounds(brushBounds, vec.neg(shape.point))
|
|
|
|
).length > 0
|
2021-05-27 17:59:40 +00:00
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
transform(shape, bounds, { initialShape, scaleX, scaleY }) {
|
|
|
|
const initialShapeBounds = this.boundsCache.get(initialShape)
|
|
|
|
shape.points = initialShape.points.map(([x, y]) => {
|
|
|
|
return [
|
|
|
|
bounds.width *
|
2021-06-06 20:49:15 +00:00
|
|
|
(scaleX < 0 // * sin?
|
2021-05-27 17:59:40 +00:00
|
|
|
? 1 - x / initialShapeBounds.width
|
|
|
|
: x / initialShapeBounds.width),
|
|
|
|
bounds.height *
|
2021-06-06 20:49:15 +00:00
|
|
|
(scaleY < 0 // * cos?
|
2021-05-27 17:59:40 +00:00
|
|
|
? 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
|
|
|
|
},
|
|
|
|
|
2021-06-02 15:58:51 +00:00
|
|
|
applyStyles(shape, style) {
|
2021-06-02 21:17:38 +00:00
|
|
|
const styles = { ...shape.style, ...style }
|
|
|
|
styles.isFilled = false
|
|
|
|
styles.dash = DashStyle.Solid
|
|
|
|
shape.style = styles
|
|
|
|
shape.points = [...shape.points]
|
2021-05-27 17:59:40 +00:00
|
|
|
return this
|
|
|
|
},
|
|
|
|
|
2021-06-07 11:18:50 +00:00
|
|
|
onSessionComplete(shape) {
|
|
|
|
const bounds = this.getBounds(shape)
|
|
|
|
|
|
|
|
const [x1, y1] = vec.sub([bounds.minX, bounds.minY], shape.point)
|
|
|
|
|
|
|
|
shape.points = shape.points.map(([x0, y0, p]) => [x0 - x1, y0 - y1, p])
|
|
|
|
|
|
|
|
this.translateTo(shape, vec.add(shape.point, [x1, y1]))
|
|
|
|
|
|
|
|
return this
|
|
|
|
},
|
|
|
|
|
2021-06-01 21:49:32 +00:00
|
|
|
canStyleFill: false,
|
2021-05-27 17:59:40 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
export default draw
|
2021-06-01 21:49:32 +00:00
|
|
|
|
2021-06-06 07:33:30 +00:00
|
|
|
const simulatePressureSettings = {
|
|
|
|
simulatePressure: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
const realPressureSettings = {
|
|
|
|
easing: (t: number) => t * t,
|
|
|
|
simulatePressure: false,
|
2021-06-07 11:18:50 +00:00
|
|
|
start: { taper: 1 },
|
|
|
|
end: { taper: 1 },
|
2021-06-06 07:33:30 +00:00
|
|
|
}
|
|
|
|
|
2021-06-02 21:17:38 +00:00
|
|
|
function renderPath(shape: DrawShape, style: ShapeStyles) {
|
|
|
|
const styles = getShapeStyle(style)
|
|
|
|
|
2021-06-06 07:33:30 +00:00
|
|
|
if (shape.points.length < 2) {
|
|
|
|
pathCache.set(shape.points, '')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const options =
|
|
|
|
shape.points[1][2] === 0.5 ? simulatePressureSettings : realPressureSettings
|
|
|
|
|
|
|
|
const stroke = getStroke(shape.points, {
|
|
|
|
size: 1 + +styles.strokeWidth * 2,
|
|
|
|
thinning: 0.85,
|
|
|
|
end: { taper: +styles.strokeWidth * 20 },
|
|
|
|
start: { taper: +styles.strokeWidth * 20 },
|
|
|
|
...options,
|
|
|
|
})
|
|
|
|
|
|
|
|
pathCache.set(shape.points, getSvgPathFromStroke(stroke))
|
2021-06-02 21:17:38 +00:00
|
|
|
}
|