adds hit testing for hovers
This commit is contained in:
parent
b8d3b35b07
commit
afa8f53dff
25 changed files with 1034 additions and 83 deletions
24
lib/code/circle.ts
Normal file
24
lib/code/circle.ts
Normal file
|
@ -0,0 +1,24 @@
|
|||
import CodeShape from "./index"
|
||||
import { v4 as uuid } from "uuid"
|
||||
import { CircleShape, ShapeType } from "types"
|
||||
|
||||
export default class Circle extends CodeShape<CircleShape> {
|
||||
constructor(props = {} as Partial<CircleShape>) {
|
||||
super({
|
||||
id: uuid(),
|
||||
type: ShapeType.Circle,
|
||||
name: "Circle",
|
||||
parentId: "page0",
|
||||
childIndex: 0,
|
||||
point: [0, 0],
|
||||
rotation: 0,
|
||||
radius: 20,
|
||||
style: {},
|
||||
...props,
|
||||
})
|
||||
}
|
||||
|
||||
get radius() {
|
||||
return this.shape.radius
|
||||
}
|
||||
}
|
54
lib/code/index.ts
Normal file
54
lib/code/index.ts
Normal file
|
@ -0,0 +1,54 @@
|
|||
import { Shape } from "types"
|
||||
import * as vec from "utils/vec"
|
||||
import { getShapeUtils } from "lib/shapes"
|
||||
|
||||
export default class CodeShape<T extends Shape> {
|
||||
private _shape: T
|
||||
|
||||
constructor(props: T) {
|
||||
this._shape = props
|
||||
shapeMap.add(this)
|
||||
}
|
||||
|
||||
destroy() {
|
||||
shapeMap.delete(this)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
export const shapeMap = new Set<CodeShape<Shape>>([])
|
24
lib/code/rectangle.ts
Normal file
24
lib/code/rectangle.ts
Normal file
|
@ -0,0 +1,24 @@
|
|||
import CodeShape from "./index"
|
||||
import { v4 as uuid } from "uuid"
|
||||
import { RectangleShape, ShapeType } from "types"
|
||||
|
||||
export default class Rectangle extends CodeShape<RectangleShape> {
|
||||
constructor(props = {} as Partial<RectangleShape>) {
|
||||
super({
|
||||
id: uuid(),
|
||||
type: ShapeType.Rectangle,
|
||||
name: "Rectangle",
|
||||
parentId: "page0",
|
||||
childIndex: 0,
|
||||
point: [0, 0],
|
||||
size: [1, 1],
|
||||
rotation: 0,
|
||||
style: {},
|
||||
...props,
|
||||
})
|
||||
}
|
||||
|
||||
get size() {
|
||||
return this.shape.size
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue