tldraw/lib/shapes/rectangle.tsx

82 lines
1.5 KiB
TypeScript
Raw Normal View History

2021-05-12 21:11:17 +00:00
import { v4 as uuid } from "uuid"
import * as vec from "utils/vec"
2021-05-13 18:22:16 +00:00
import { RectangleShape, ShapeType } from "types"
2021-05-12 22:08:53 +00:00
import { boundsCache } from "./index"
2021-05-13 18:22:16 +00:00
import { boundsContained, boundsCollide } from "utils/bounds"
import { createShape } from "./base-shape"
2021-05-12 21:11:17 +00:00
2021-05-13 18:22:16 +00:00
const rectangle = createShape<RectangleShape>({
create(props) {
2021-05-12 21:11:17 +00:00
return {
id: uuid(),
type: ShapeType.Rectangle,
name: "Rectangle",
parentId: "page0",
childIndex: 0,
point: [0, 0],
size: [1, 1],
rotation: 0,
style: {},
...props,
}
},
render({ id, size }) {
return <rect id={id} width={size[0]} height={size[1]} />
},
getBounds(shape) {
2021-05-12 22:08:53 +00:00
if (boundsCache.has(shape)) {
return boundsCache.get(shape)
}
2021-05-12 21:11:17 +00:00
const {
point: [x, y],
size: [width, height],
} = shape
2021-05-12 22:08:53 +00:00
const bounds = {
2021-05-12 21:11:17 +00:00
minX: x,
maxX: x + width,
minY: y,
maxY: y + height,
width,
height,
}
2021-05-12 22:08:53 +00:00
boundsCache.set(shape, bounds)
return bounds
2021-05-12 21:11:17 +00:00
},
hitTest(shape) {
return true
},
2021-05-13 18:22:16 +00:00
hitTestBounds(shape, brushBounds) {
const shapeBounds = this.getBounds(shape)
return (
boundsContained(shapeBounds, brushBounds) ||
boundsCollide(shapeBounds, brushBounds)
)
},
2021-05-12 21:11:17 +00:00
rotate(shape) {
return shape
},
2021-05-13 06:44:52 +00:00
translate(shape, delta) {
shape.point = vec.add(shape.point, delta)
2021-05-12 21:11:17 +00:00
return shape
},
2021-05-13 18:22:16 +00:00
scale(shape, scale) {
2021-05-12 21:11:17 +00:00
return shape
},
2021-05-13 18:22:16 +00:00
stretch(shape, scaleX, scaleY) {
2021-05-12 21:11:17 +00:00
return shape
},
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