tldraw/state/shape-utils/rectangle.tsx

226 lines
5.9 KiB
TypeScript
Raw Normal View History

2021-07-08 09:59:47 +00:00
import { uniqueId, getPerfectDashProps, getFromCache } from 'utils/utils'
import vec from 'utils/vec'
import { DashStyle, RectangleShape, ShapeType } from 'types'
2021-06-24 12:34:43 +00:00
import { getSvgPathFromStroke, translateBounds, rng, shuffleArr } from 'utils'
2021-06-22 18:13:16 +00:00
import { defaultStyle, getShapeStyle } from 'state/shape-styles'
2021-06-07 21:12:14 +00:00
import getStroke from 'perfect-freehand'
2021-06-21 21:35:28 +00:00
import { registerShapeUtils } from './register'
2021-06-07 21:12:14 +00:00
const pathCache = new WeakMap<number[], string>([])
2021-05-12 21:11:17 +00:00
2021-05-20 09:49:40 +00:00
const rectangle = registerShapeUtils<RectangleShape>({
2021-05-14 12:44:23 +00:00
boundsCache: new WeakMap([]),
defaultProps: {
id: uniqueId(),
type: ShapeType.Rectangle,
name: 'Rectangle',
parentId: 'page1',
childIndex: 0,
point: [0, 0],
size: [1, 1],
radius: 2,
rotation: 0,
style: defaultStyle,
2021-05-12 21:11:17 +00:00
},
shouldRender(shape, prev) {
return shape.size !== prev.size || shape.style !== prev.style
},
2021-07-10 20:39:29 +00:00
render(shape, { isHovered, isDarkMode }) {
2021-06-08 10:32:20 +00:00
const { id, size, radius, style } = shape
2021-07-10 20:39:29 +00:00
const styles = getShapeStyle(style, isDarkMode)
2021-06-22 18:13:16 +00:00
const strokeWidth = +styles.strokeWidth
2021-06-07 21:12:14 +00:00
2021-07-01 22:11:09 +00:00
if (style.dash === DashStyle.Draw) {
2021-07-08 09:59:47 +00:00
const pathData = getFromCache(pathCache, shape.size, (cache) => {
cache.set(shape.size, renderPath(shape))
})
2021-06-07 21:12:14 +00:00
return (
2021-07-09 19:43:18 +00:00
<>
{style.isFilled && (
<rect
rx={radius}
ry={radius}
x={+styles.strokeWidth / 2}
y={+styles.strokeWidth / 2}
width={Math.max(0, size[0] - strokeWidth)}
height={Math.max(0, size[1] - strokeWidth)}
strokeWidth={0}
fill={styles.fill}
stroke={styles.stroke}
/>
)}
2021-07-03 16:30:06 +00:00
<path
2021-07-08 09:59:47 +00:00
d={pathData}
2021-07-03 16:30:06 +00:00
fill={styles.stroke}
stroke={styles.stroke}
strokeWidth={styles.strokeWidth}
2021-07-09 16:15:27 +00:00
filter={isHovered ? 'url(#expand)' : 'none'}
2021-07-09 19:43:18 +00:00
pointerEvents={style.isFilled ? 'all' : 'stroke'}
/>
2021-07-09 19:43:18 +00:00
</>
)
}
2021-06-07 21:12:14 +00:00
2021-06-22 18:13:16 +00:00
const sw = strokeWidth * 1.618
const w = Math.max(0, size[0] - sw / 2)
const h = Math.max(0, size[1] - sw / 2)
2021-06-22 18:13:16 +00:00
const strokes: [number[], number[], number][] = [
[[sw / 2, sw / 2], [w, sw / 2], w - sw / 2],
[[w, sw / 2], [w, h], h - sw / 2],
[[w, h], [sw / 2, h], w - sw / 2],
[[sw / 2, h], [sw / 2, sw / 2], h - sw / 2],
2021-06-22 18:13:16 +00:00
]
const paths = strokes.map(([start, end, length], i) => {
const { strokeDasharray, strokeDashoffset } = getPerfectDashProps(
length,
sw,
2021-07-01 22:11:09 +00:00
shape.style.dash
2021-06-22 18:13:16 +00:00
)
return (
<line
key={id + '_' + i}
x1={start[0]}
y1={start[1]}
x2={end[0]}
y2={end[1]}
stroke={styles.stroke}
strokeWidth={sw}
strokeLinecap="round"
strokeDasharray={strokeDasharray}
strokeDashoffset={strokeDashoffset}
/>
)
})
2021-05-23 13:46:04 +00:00
return (
2021-07-09 19:43:18 +00:00
<>
2021-06-22 18:13:16 +00:00
<rect
x={sw / 2}
y={sw / 2}
width={w}
height={h}
2021-06-22 18:13:16 +00:00
fill={styles.fill}
2021-07-09 19:43:18 +00:00
stroke="transparent"
strokeWidth={sw}
pointerEvents={style.isFilled ? 'all' : 'stroke'}
2021-06-22 18:13:16 +00:00
/>
2021-07-09 19:43:18 +00:00
<g filter={isHovered ? 'url(#expand)' : 'none'} pointerEvents="stroke">
{paths}
</g>
</>
2021-05-23 13:46:04 +00:00
)
2021-05-12 21:11:17 +00:00
},
getBounds(shape) {
2021-07-08 09:59:47 +00:00
const bounds = getFromCache(this.boundsCache, shape, (cache) => {
2021-05-18 08:32:20 +00:00
const [width, height] = shape.size
2021-07-08 09:59:47 +00:00
cache.set(shape, {
2021-05-18 08:32:20 +00:00
minX: 0,
maxX: width,
minY: 0,
maxY: height,
width,
height,
2021-07-08 09:59:47 +00:00
})
})
2021-05-12 22:08:53 +00:00
2021-07-08 09:59:47 +00:00
return translateBounds(bounds, shape.point)
2021-05-18 08:32:20 +00:00
},
2021-06-02 15:58:51 +00:00
hitTest() {
2021-05-12 21:11:17 +00:00
return true
},
transform(shape, bounds, { initialShape, transformOrigin, scaleX, scaleY }) {
2021-05-29 13:59:11 +00:00
if (shape.rotation === 0 && !shape.isAspectRatioLocked) {
shape.size = vec.round([bounds.width, bounds.height])
shape.point = vec.round([bounds.minX, bounds.minY])
2021-05-18 10:45:29 +00:00
} else {
shape.size = vec.round(
vec.mul(initialShape.size, Math.min(Math.abs(scaleX), Math.abs(scaleY)))
2021-05-22 20:35:53 +00:00
)
shape.point = vec.round([
bounds.minX +
(bounds.width - shape.size[0]) *
(scaleX < 0 ? 1 - transformOrigin[0] : transformOrigin[0]),
bounds.minY +
(bounds.height - shape.size[1]) *
(scaleY < 0 ? 1 - transformOrigin[1] : transformOrigin[1]),
])
shape.rotation =
(scaleX < 0 && scaleY >= 0) || (scaleY < 0 && scaleX >= 0)
? -initialShape.rotation
: initialShape.rotation
2021-05-18 10:45:29 +00:00
}
return this
2021-05-12 21:11:17 +00:00
},
2021-05-15 13:02:13 +00:00
transformSingle(shape, bounds) {
shape.size = vec.round([bounds.width, bounds.height])
shape.point = vec.round([bounds.minX, bounds.minY])
return this
},
2021-05-13 18:22:16 +00:00
})
2021-05-12 21:11:17 +00:00
2021-05-13 18:22:16 +00:00
export default rectangle
2021-06-07 21:12:14 +00:00
function renderPath(shape: RectangleShape) {
const styles = getShapeStyle(shape.style)
2021-06-08 10:32:20 +00:00
const getRandom = rng(shape.id)
const strokeWidth = +styles.strokeWidth
const baseOffset = strokeWidth / 2
2021-06-07 21:12:14 +00:00
2021-06-21 21:35:28 +00:00
const offsets = Array.from(Array(4)).map(() => [
2021-06-08 10:32:20 +00:00
getRandom() * baseOffset,
getRandom() * baseOffset,
2021-06-07 21:12:14 +00:00
])
const sw = strokeWidth
const w = Math.max(0, shape.size[0] - sw / 2)
const h = Math.max(0, shape.size[1] - sw / 2)
const tl = vec.add([sw / 2, sw / 2], offsets[0])
const tr = vec.add([w, sw / 2], offsets[1])
2021-06-07 21:12:14 +00:00
const br = vec.add([w, h], offsets[2])
const bl = vec.add([sw / 2, h], offsets[3])
2021-06-07 21:12:14 +00:00
const lines = shuffleArr(
[
2021-06-24 12:34:43 +00:00
vec.pointsBetween(tr, br),
vec.pointsBetween(br, bl),
vec.pointsBetween(bl, tl),
vec.pointsBetween(tl, tr),
2021-06-07 21:12:14 +00:00
],
2021-06-08 10:32:20 +00:00
Math.floor(5 + getRandom() * 4)
2021-06-07 21:12:14 +00:00
)
const stroke = getStroke(
2021-06-08 10:32:20 +00:00
[...lines.flat().slice(2), ...lines[0], ...lines[0].slice(4)],
2021-06-07 21:12:14 +00:00
{
2021-06-08 11:06:24 +00:00
size: 1 + +styles.strokeWidth,
2021-06-07 21:12:14 +00:00
thinning: 0.6,
easing: (t) => t * t * t * t,
end: { taper: +styles.strokeWidth * 20 },
start: { taper: +styles.strokeWidth * 20 },
simulatePressure: false,
}
)
2021-07-08 09:59:47 +00:00
return getSvgPathFromStroke(stroke)
2021-06-07 21:12:14 +00:00
}