2021-07-01 14:03:02 +00:00
|
|
|
import { uniqueId } from 'utils/utils'
|
2021-06-16 12:09:45 +00:00
|
|
|
import vec from 'utils/vec'
|
2021-07-03 15:06:27 +00:00
|
|
|
import { DashStyle, DrawShape, ShapeType } from 'types'
|
2021-05-28 14:37:23 +00:00
|
|
|
import { intersectPolylineBounds } from 'utils/intersections'
|
2021-06-20 10:22:42 +00:00
|
|
|
import getStroke, { getStrokePoints } 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,
|
|
|
|
getSvgPathFromStroke,
|
|
|
|
translateBounds,
|
2021-06-24 12:34:43 +00:00
|
|
|
boundsContain,
|
2021-06-24 08:18:14 +00:00
|
|
|
} from 'utils'
|
2021-06-21 21:35:28 +00:00
|
|
|
import { defaultStyle, getShapeStyle } from 'state/shape-styles'
|
|
|
|
import { registerShapeUtils } from './register'
|
2021-05-27 18:51:25 +00:00
|
|
|
|
2021-06-06 19:34:27 +00:00
|
|
|
const rotatedCache = new WeakMap<DrawShape, number[][]>([])
|
2021-07-03 16:30:06 +00:00
|
|
|
const drawPathCache = new WeakMap<DrawShape['points'], string>([])
|
2021-07-03 13:28:43 +00:00
|
|
|
const simplePathCache = new WeakMap<DrawShape['points'], string>([])
|
2021-06-20 10:22:42 +00:00
|
|
|
const polygonCache = new WeakMap<DrawShape['points'], string>([])
|
2021-05-27 17:59:40 +00:00
|
|
|
|
|
|
|
const draw = registerShapeUtils<DrawShape>({
|
|
|
|
boundsCache: new WeakMap([]),
|
|
|
|
|
2021-06-26 11:52:36 +00:00
|
|
|
canStyleFill: true,
|
|
|
|
|
2021-07-01 14:03:02 +00:00
|
|
|
defaultProps: {
|
|
|
|
id: uniqueId(),
|
|
|
|
type: ShapeType.Draw,
|
|
|
|
name: 'Draw',
|
|
|
|
parentId: 'page1',
|
|
|
|
childIndex: 0,
|
|
|
|
point: [0, 0],
|
|
|
|
points: [],
|
|
|
|
rotation: 0,
|
2021-07-04 08:47:37 +00:00
|
|
|
|
2021-07-01 14:03:02 +00:00
|
|
|
style: defaultStyle,
|
2021-05-27 17:59:40 +00:00
|
|
|
},
|
|
|
|
|
2021-06-26 11:52:36 +00:00
|
|
|
shouldRender(shape, prev) {
|
|
|
|
return shape.points !== prev.points || shape.style !== prev.style
|
|
|
|
},
|
|
|
|
|
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-07-03 13:28:43 +00:00
|
|
|
const strokeWidth = +styles.strokeWidth
|
|
|
|
|
2021-07-03 16:30:06 +00:00
|
|
|
const shouldFill =
|
|
|
|
points.length > 3 &&
|
|
|
|
vec.dist(points[0], points[points.length - 1]) < +styles.strokeWidth * 2
|
|
|
|
|
|
|
|
// For very short lines, draw a point instead of a line
|
2021-05-27 18:51:25 +00:00
|
|
|
|
2021-06-12 08:10:12 +00:00
|
|
|
if (points.length > 0 && points.length < 3) {
|
2021-06-02 11:50:34 +00:00
|
|
|
return (
|
2021-06-27 09:07:20 +00:00
|
|
|
<g id={id}>
|
2021-07-03 16:30:06 +00:00
|
|
|
<circle
|
|
|
|
r={strokeWidth * 0.618}
|
|
|
|
fill={styles.stroke}
|
|
|
|
stroke={styles.stroke}
|
|
|
|
strokeWidth={styles.strokeWidth}
|
|
|
|
/>
|
2021-06-27 09:07:20 +00:00
|
|
|
</g>
|
2021-06-02 11:50:34 +00:00
|
|
|
)
|
2021-05-29 13:59:11 +00:00
|
|
|
}
|
|
|
|
|
2021-07-03 16:30:06 +00:00
|
|
|
// For drawn lines, draw a line from the path cache
|
2021-06-20 10:22:42 +00:00
|
|
|
|
2021-07-03 13:28:43 +00:00
|
|
|
if (shape.style.dash === DashStyle.Draw) {
|
2021-07-03 16:30:06 +00:00
|
|
|
if (!drawPathCache.has(points)) {
|
|
|
|
drawPathCache.set(shape.points, getDrawStrokePath(shape))
|
|
|
|
}
|
|
|
|
|
2021-07-03 13:28:43 +00:00
|
|
|
if (shouldFill && !polygonCache.has(points)) {
|
2021-07-03 15:06:27 +00:00
|
|
|
polygonCache.set(shape.points, getFillPath(shape))
|
2021-07-03 13:28:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<g id={id}>
|
|
|
|
{shouldFill && (
|
|
|
|
<path
|
|
|
|
d={polygonCache.get(points)}
|
|
|
|
strokeWidth="0"
|
|
|
|
stroke="none"
|
2021-07-03 16:30:06 +00:00
|
|
|
fill={styles.fill}
|
2021-07-03 13:28:43 +00:00
|
|
|
/>
|
|
|
|
)}
|
2021-07-03 16:30:06 +00:00
|
|
|
<path
|
|
|
|
d={drawPathCache.get(points)}
|
|
|
|
fill={styles.stroke}
|
|
|
|
stroke={styles.stroke}
|
2021-07-04 08:24:45 +00:00
|
|
|
strokeWidth={strokeWidth}
|
2021-07-03 16:30:06 +00:00
|
|
|
/>
|
2021-07-03 13:28:43 +00:00
|
|
|
</g>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// For solid, dash and dotted lines, draw a regular stroke path
|
|
|
|
|
|
|
|
const strokeDasharray = {
|
|
|
|
[DashStyle.Dotted]: `${strokeWidth / 10} ${strokeWidth * 3}`,
|
|
|
|
[DashStyle.Dashed]: `${strokeWidth * 3} ${strokeWidth * 3}`,
|
|
|
|
[DashStyle.Solid]: `none`,
|
|
|
|
}[style.dash]
|
|
|
|
|
|
|
|
const strokeDashoffset = {
|
|
|
|
[DashStyle.Dotted]: `-${strokeWidth / 20}`,
|
|
|
|
[DashStyle.Dashed]: `-${strokeWidth}`,
|
|
|
|
[DashStyle.Solid]: `none`,
|
|
|
|
}[style.dash]
|
|
|
|
|
|
|
|
if (!simplePathCache.has(points)) {
|
2021-07-03 15:06:27 +00:00
|
|
|
simplePathCache.set(points, getSolidStrokePath(shape))
|
2021-06-20 10:22:42 +00:00
|
|
|
}
|
|
|
|
|
2021-07-03 13:28:43 +00:00
|
|
|
const path = simplePathCache.get(points)
|
|
|
|
|
2021-06-20 08:29:46 +00:00
|
|
|
return (
|
|
|
|
<g id={id}>
|
2021-07-03 13:28:43 +00:00
|
|
|
{style.dash !== DashStyle.Solid && (
|
2021-06-20 10:22:42 +00:00
|
|
|
<path
|
2021-07-03 13:28:43 +00:00
|
|
|
d={path}
|
|
|
|
fill="transparent"
|
|
|
|
stroke="transparent"
|
2021-07-03 16:30:06 +00:00
|
|
|
strokeWidth={strokeWidth * 2}
|
2021-06-20 10:22:42 +00:00
|
|
|
/>
|
|
|
|
)}
|
2021-07-03 13:28:43 +00:00
|
|
|
<path
|
|
|
|
d={path}
|
|
|
|
fill={shouldFill ? styles.fill : 'none'}
|
2021-07-03 16:30:06 +00:00
|
|
|
stroke={styles.stroke}
|
|
|
|
strokeWidth={strokeWidth * 1.618}
|
2021-07-03 13:28:43 +00:00
|
|
|
strokeDasharray={strokeDasharray}
|
|
|
|
strokeDashoffset={strokeDashoffset}
|
|
|
|
/>
|
2021-06-20 08:29:46 +00:00
|
|
|
</g>
|
|
|
|
)
|
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) {
|
2021-06-21 21:35:28 +00:00
|
|
|
const 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)
|
2021-06-18 09:32:07 +00:00
|
|
|
shape.points = initialShape.points.map(([x, y, r]) => {
|
2021-05-27 17:59:40 +00:00
|
|
|
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),
|
2021-06-18 09:32:07 +00:00
|
|
|
r,
|
2021-05-27 17:59:40 +00:00
|
|
|
]
|
|
|
|
})
|
|
|
|
|
|
|
|
const newBounds = getBoundsFromPoints(shape.points)
|
|
|
|
|
|
|
|
shape.point = vec.sub(
|
|
|
|
[bounds.minX, bounds.minY],
|
|
|
|
[newBounds.minX, newBounds.minY]
|
|
|
|
)
|
|
|
|
return this
|
|
|
|
},
|
|
|
|
|
2021-07-03 13:28:43 +00:00
|
|
|
// applyStyles(shape, style) {
|
|
|
|
// const styles = { ...shape.style, ...style }
|
|
|
|
// styles.dash = DashStyle.Solid
|
|
|
|
// shape.style = styles
|
|
|
|
// return this
|
|
|
|
// },
|
2021-05-27 17:59:40 +00:00
|
|
|
|
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-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-07-03 15:06:27 +00:00
|
|
|
/**
|
|
|
|
* Get the fill path for a closed draw shape.
|
|
|
|
*
|
|
|
|
* ### Example
|
|
|
|
*
|
|
|
|
*```ts
|
|
|
|
* someCache.set(getFillPath(shape))
|
|
|
|
*```
|
|
|
|
*/
|
|
|
|
function getFillPath(shape: DrawShape) {
|
|
|
|
const styles = getShapeStyle(shape.style)
|
2021-06-02 21:17:38 +00:00
|
|
|
|
2021-06-06 07:33:30 +00:00
|
|
|
if (shape.points.length < 2) {
|
2021-07-03 15:06:27 +00:00
|
|
|
return ''
|
|
|
|
}
|
|
|
|
|
|
|
|
return getSvgPathFromStroke(
|
|
|
|
getStrokePoints(shape.points, {
|
|
|
|
size: 1 + +styles.strokeWidth * 2,
|
|
|
|
thinning: 0.85,
|
|
|
|
end: { taper: +styles.strokeWidth * 20 },
|
|
|
|
start: { taper: +styles.strokeWidth * 20 },
|
|
|
|
}).map((pt) => pt.point)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the path data for a draw stroke.
|
|
|
|
*
|
|
|
|
* ### Example
|
|
|
|
*
|
|
|
|
*```ts
|
|
|
|
* someCache.set(getDrawStrokePath(shape))
|
|
|
|
*```
|
|
|
|
*/
|
|
|
|
function getDrawStrokePath(shape: DrawShape) {
|
|
|
|
const styles = getShapeStyle(shape.style)
|
|
|
|
|
|
|
|
if (shape.points.length < 2) {
|
|
|
|
return ''
|
2021-06-06 07:33:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const options =
|
|
|
|
shape.points[1][2] === 0.5 ? simulatePressureSettings : realPressureSettings
|
|
|
|
|
|
|
|
const stroke = getStroke(shape.points, {
|
|
|
|
size: 1 + +styles.strokeWidth * 2,
|
|
|
|
thinning: 0.85,
|
2021-07-04 08:24:45 +00:00
|
|
|
end: { taper: +styles.strokeWidth * 10 },
|
|
|
|
start: { taper: +styles.strokeWidth * 10 },
|
2021-06-06 07:33:30 +00:00
|
|
|
...options,
|
|
|
|
})
|
|
|
|
|
2021-07-03 15:06:27 +00:00
|
|
|
return getSvgPathFromStroke(stroke)
|
2021-06-02 21:17:38 +00:00
|
|
|
}
|
2021-06-20 10:22:42 +00:00
|
|
|
|
2021-07-03 15:06:27 +00:00
|
|
|
/**
|
|
|
|
* Get the path data for a solid draw stroke.
|
|
|
|
*
|
|
|
|
* ### Example
|
|
|
|
*
|
|
|
|
*```ts
|
|
|
|
* getSolidStrokePath(shape)
|
|
|
|
*```
|
|
|
|
*/
|
|
|
|
function getSolidStrokePath(shape: DrawShape) {
|
|
|
|
let { points } = shape
|
2021-07-03 13:28:43 +00:00
|
|
|
|
2021-07-03 15:06:27 +00:00
|
|
|
let len = points.length
|
2021-07-03 13:28:43 +00:00
|
|
|
|
|
|
|
if (len === 0) return 'M 0 0 L 0 0'
|
2021-07-03 15:06:27 +00:00
|
|
|
if (len < 3) return `M ${points[0][0]} ${points[0][1]}`
|
2021-07-03 13:28:43 +00:00
|
|
|
|
|
|
|
// Remove duplicates from points
|
2021-07-03 15:06:27 +00:00
|
|
|
points = points.reduce<number[][]>((acc, pt, i) => {
|
2021-07-03 13:28:43 +00:00
|
|
|
if (i === 0 || !vec.isEqual(pt, acc[i - 1])) {
|
|
|
|
acc.push(pt)
|
|
|
|
}
|
|
|
|
return acc
|
|
|
|
}, [])
|
|
|
|
|
2021-07-03 15:06:27 +00:00
|
|
|
len = points.length
|
2021-07-03 13:28:43 +00:00
|
|
|
|
2021-07-03 15:06:27 +00:00
|
|
|
const d = points.reduce(
|
2021-07-03 13:28:43 +00:00
|
|
|
(acc, [x0, y0], i, arr) => {
|
2021-07-03 15:06:27 +00:00
|
|
|
if (i === len - 1) {
|
2021-07-03 13:28:43 +00:00
|
|
|
acc.push('L', x0, y0)
|
|
|
|
return acc
|
|
|
|
}
|
|
|
|
|
|
|
|
const [x1, y1] = arr[i + 1]
|
|
|
|
acc.push(
|
|
|
|
x0.toFixed(2),
|
|
|
|
y0.toFixed(2),
|
|
|
|
((x0 + x1) / 2).toFixed(2),
|
|
|
|
((y0 + y1) / 2).toFixed(2)
|
|
|
|
)
|
|
|
|
return acc
|
|
|
|
},
|
2021-07-03 15:06:27 +00:00
|
|
|
['M', points[0][0], points[0][1], 'Q']
|
2021-07-03 13:28:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const path = d.join(' ').replaceAll(/(\s[0-9]*\.[0-9]{2})([0-9]*)\b/g, '$1')
|
|
|
|
|
|
|
|
return path
|
|
|
|
}
|