tldraw/lib/shapes/rectangle.tsx

71 lines
1.2 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"
import { BaseLibShape, RectangleShape, ShapeType } from "types"
2021-05-12 22:08:53 +00:00
import { boundsCache } from "./index"
2021-05-12 21:11:17 +00:00
const Rectangle: BaseLibShape<ShapeType.Rectangle> = {
create(props): RectangleShape {
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
},
rotate(shape) {
return shape
},
translate(shape) {
return shape
},
scale(shape, scale: number) {
return shape
},
stretch(shape, scaleX: number, scaleY: number) {
return shape
},
}
export default Rectangle