tldraw/lib/code/index.ts

59 lines
1.1 KiB
TypeScript
Raw Normal View History

2021-05-14 22:56:41 +00:00
import { Shape } from "types"
import * as vec from "utils/vec"
import { getShapeUtils } from "lib/shapes"
2021-05-15 13:02:13 +00:00
export const codeShapes = new Set<CodeShape<Shape>>([])
/**
* A base class for code shapes. Note that creating a shape adds it to the
* shape map, while deleting it removes it from the collected shapes set
*/
2021-05-14 22:56:41 +00:00
export default class CodeShape<T extends Shape> {
private _shape: T
constructor(props: T) {
this._shape = props
2021-05-15 13:02:13 +00:00
codeShapes.add(this)
2021-05-14 22:56:41 +00:00
}
destroy() {
2021-05-15 13:02:13 +00:00
codeShapes.delete(this)
2021-05-14 22:56:41 +00:00
}
moveTo(point: number[]) {
this.shape.point = point
}
translate(delta: number[]) {
this.shape.point = vec.add(this._shape.point, delta)
}
rotate(rotation: number) {
this.shape.rotation = rotation
}
scale(scale: number) {
return getShapeUtils(this.shape).scale(this.shape, scale)
}
getBounds() {
return getShapeUtils(this.shape).getBounds(this.shape)
}
hitTest(point: number[]) {
return getShapeUtils(this.shape).hitTest(this.shape, point)
}
get shape() {
return this._shape
}
get point() {
return [...this.shape.point]
}
get rotation() {
return this.shape.rotation
}
}