import { AbstractCanvasObject, CanvasObjectEvents } from '../../../CanvasInterface/Objects/CanvasObject'; import { transformPoint } from '../../../Utils/Calc2D'; export abstract class CanvasObject extends AbstractCanvasObject { constructor(options?: any) { super(options); } getOptionsFromWrappedObject() { const o = this.wrappedObject; return { x: o.position.x, y: o.position.y, rotation: o.rotation, scaling: o.scaling.x, }; } setPosition(x: number, y: number) { const o = this.wrappedObject; o.position.x = x; o.position.y = y; } setRotation(rotation: number) { const o = this.wrappedObject; o.applyMatrix = false; o.rotation = rotation; } setScaling(scaling: number) { const o = this.wrappedObject; o.applyMatrix = false; o.scaling.x = scaling; o.scaling.y = scaling; } setSelected(selected: boolean) { const o = this.wrappedObject; o.selected = selected; } initObjectEvents() { //do nothing } getTransformedRect() { const o = this.wrappedObject; const { internalBounds, bounds, rotation, scaling } = o; const { width, height } = internalBounds; const { center } = bounds; const zoom = o.getView().getZoom(); const scaledX = center.x * zoom; const scaledY = center.y * zoom; const scaledWidth = width * scaling.x * zoom; const scaledHeight = height * scaling.y * zoom; return { left: scaledX - scaledWidth / 2, top: scaledY - scaledHeight / 2, width: scaledWidth, height: scaledHeight, rotation, center: { x: scaledX, y: scaledY, }, get topRight() { const x = this.left + this.width; const y = this.top; const { x: cx, y: cy } = this.center; return transformPoint(x, y, cx, cy, this.rotation); }, get bottomRight() { const x = this.left + this.width; const y = this.top + this.height; const { x: cx, y: cy } = this.center; return transformPoint(x, y, cx, cy, this.rotation); }, get bottomLeft() { const x = this.left; const y = this.top + this.height; const { x: cx, y: cy } = this.center; return transformPoint(x, y, cx, cy, this.rotation); }, get topLeft() { const x = this.left; const y = this.top; const { x: cx, y: cy } = this.center; return transformPoint(x, y, cx, cy, this.rotation); }, }; } getCenter() { const o = this.wrappedObject; const zoom = o.getView().getZoom(); const { x, y } = o.bounds.center.multiply(zoom); return { x, y, }; } remove() { super.remove(); this.wrappedObject.remove(); } wrappedObjectToObject(): any { const obj = this.wrappedObject.exportJSON({ asString: false }); delete obj.selected; return obj; } getBoundSize(): any { const { height, width } = this.wrappedObject.bounds; return { height, width, }; } getSize() { const { height, width } = this.wrappedObject.internalBounds; return { height, width, }; } triggerChangeEvent = prevState => { this._observable.triggerEvent(CanvasObjectEvents.OnChange, { prevState }); }; }