| import { |
| Container, |
| Graphics, |
| Point, |
| Rectangle, |
| FederatedPointerEvent, |
| Sprite |
| } from "pixi.js"; |
| import { type ImageEditorContext, type Tool } from "../core/editor"; |
|
|
| import { type Tool as ToolbarTool, type Subtool } from "../Toolbar.svelte"; |
| import { get_canvas_blob } from "../utils/pixi"; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| interface CropBounds { |
| x: number; |
| y: number; |
| width: number; |
| height: number; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| interface EditorState { |
| current_tool: ToolbarTool; |
| current_subtool: Subtool; |
| } |
|
|
| |
| |
| |
| |
| |
| export class CropTool implements Tool { |
| name = "crop" as const; |
| private image_editor_context!: ImageEditorContext; |
| current_tool: ToolbarTool = "image"; |
| current_subtool: Subtool = "crop"; |
|
|
| |
| private readonly CORNER_SIZE = 25; |
| private readonly LINE_THICKNESS = 5; |
| private readonly HANDLE_COLOR = 0x000000; |
| private readonly HIT_AREA_SIZE = 40; |
|
|
| |
| private is_dragging = false; |
| private selected_handle: Container | null = null; |
| private active_corner_index = -1; |
| private active_edge_index = -1; |
| private last_pointer_position: Point | null = null; |
| crop_bounds: CropBounds = { x: 0, y: 0, width: 0, height: 0 }; |
| private crop_ui_container: Container | null = null; |
| private crop_mask: Graphics | null = null; |
| private dimensions: { width: number; height: number } = { |
| width: 0, |
| height: 0 |
| }; |
| private position: { x: number; y: number } = { x: 0, y: 0 }; |
| private scale = 1; |
| private is_dragging_window = false; |
| private drag_start_position: Point | null = null; |
| private drag_start_bounds: CropBounds | null = null; |
| private background_image_watcher: (() => void) | null = null; |
| private has_been_manually_changed = false; |
| private event_callbacks: Map<string, (() => void)[]> = new Map(); |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| async setup( |
| context: ImageEditorContext, |
| tool: ToolbarTool, |
| subtool: Subtool |
| ): Promise<void> { |
| this.image_editor_context = context; |
| this.current_tool = tool; |
| this.current_subtool = subtool; |
|
|
| context.dimensions.subscribe((dimensions) => { |
| this.dimensions = dimensions; |
|
|
| |
| this.crop_bounds = { |
| x: 0, |
| y: 0, |
| width: dimensions.width, |
| height: dimensions.height |
| }; |
|
|
| |
| if (this.crop_ui_container) { |
| this.update_crop_ui(); |
| } |
|
|
| |
| this.set_crop_mask(); |
| this.update_crop_mask(); |
| }); |
|
|
| context.position.subscribe((position) => { |
| this.position = position; |
| }); |
|
|
| context.scale.subscribe((scale) => { |
| this.scale = scale; |
| }); |
|
|
| |
| this.background_image_watcher = () => { |
| |
| if ( |
| this.crop_mask && |
| this.current_tool === "image" && |
| this.current_subtool === "crop" |
| ) { |
| |
| if (this.image_editor_context.background_image) { |
| this.image_editor_context.background_image.setMask(this.crop_mask); |
| } |
| } |
| }; |
|
|
| |
| this.image_editor_context.app.ticker.add(this.background_image_watcher); |
|
|
| |
| await this.init_crop_ui(); |
|
|
| |
| this.setup_event_listeners(); |
|
|
| |
| const isCropActive = tool === "image" && subtool === "crop"; |
| if (this.crop_ui_container) { |
| this.crop_ui_container.visible = isCropActive; |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| cleanup(): void { |
| |
| if (this.crop_ui_container) { |
| this.image_editor_context.app.stage.removeChild(this.crop_ui_container); |
| } |
|
|
| |
| if (this.crop_mask) { |
| if (this.crop_mask.parent) { |
| this.crop_mask.parent.removeChild(this.crop_mask); |
| } |
|
|
| |
| if ( |
| this.image_editor_context.background_image && |
| this.image_editor_context.background_image.mask === this.crop_mask |
| ) { |
| this.image_editor_context.background_image.mask = null; |
| } |
|
|
| |
| if (this.image_editor_context.image_container.mask === this.crop_mask) { |
| this.image_editor_context.image_container.mask = null; |
| } |
| } |
|
|
| |
| this.crop_mask = null; |
| this.crop_ui_container = null; |
|
|
| |
| if (this.background_image_watcher) { |
| this.image_editor_context.app.ticker.remove( |
| this.background_image_watcher |
| ); |
| this.background_image_watcher = null; |
| } |
|
|
| |
| this.cleanup_event_listeners(); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| set_tool(tool: ToolbarTool, subtool: Subtool): void { |
| this.current_tool = tool; |
| this.current_subtool = subtool; |
|
|
| |
| const isCropActive = tool === "image" && subtool === "crop"; |
|
|
| |
| if (this.crop_ui_container) { |
| this.crop_ui_container.visible = isCropActive; |
| } |
|
|
| |
| if (isCropActive && this.crop_mask) { |
| this.update_crop_mask(); |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| private set_crop_mask(): void { |
| |
| if (this.crop_mask) { |
| if (this.crop_mask.parent) { |
| this.crop_mask.parent.removeChild(this.crop_mask); |
| } |
|
|
| |
| if (this.image_editor_context.image_container.mask === this.crop_mask) { |
| this.image_editor_context.image_container.mask = null; |
| } |
|
|
| |
| if ( |
| this.image_editor_context.background_image && |
| this.image_editor_context.background_image.mask === this.crop_mask |
| ) { |
| this.image_editor_context.background_image.mask = null; |
| } |
| } |
|
|
| |
| this.crop_mask = new Graphics(); |
|
|
| |
| |
| this.image_editor_context.image_container.addChild(this.crop_mask); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| private async init_crop_ui(): Promise<void> { |
| |
| const { width, height } = |
| this.image_editor_context.background_image?.getLocalBounds() || { |
| width: false, |
| height: false |
| }; |
|
|
| this.crop_ui_container = this.make_crop_ui( |
| this.dimensions.width * this.scale, |
| this.dimensions.height * this.scale |
| ); |
|
|
| |
| this.crop_ui_container.position.set(this.position.x, this.position.y); |
|
|
| |
| this.crop_ui_container.visible = false; |
|
|
| this.image_editor_context.app.stage.addChild(this.crop_ui_container); |
|
|
| |
| this.set_crop_mask(); |
|
|
| |
| this.image_editor_context.app.ticker.add(this.update_crop_ui.bind(this)); |
|
|
| |
| this.update_crop_mask(); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| private make_crop_ui(width: number, height: number): Container { |
| const crop_container = new Container(); |
| crop_container.eventMode = "static"; |
| crop_container.interactiveChildren = true; |
|
|
| |
| const crop_outline = new Graphics() |
| .rect(0, 0, width, height) |
| .stroke({ width: 1, color: 0x000000, alignment: 0, alpha: 0.5 }); |
|
|
| |
| crop_outline.eventMode = "static"; |
| crop_outline.cursor = "move"; |
|
|
| |
| crop_outline.hitArea = new Rectangle(0, 0, width, height); |
|
|
| |
| crop_outline.on("pointerdown", this.handle_window_drag_start.bind(this)); |
|
|
| crop_container.addChild(crop_outline); |
|
|
| |
| this.create_corner_handles(crop_container, width, height); |
|
|
| |
| this.create_edge_handles(crop_container, width, height); |
|
|
| return crop_container; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| private create_handle(is_edge = false): Container { |
| const handle = new Container(); |
| handle.eventMode = "static"; |
|
|
| const handle_graphics = new Graphics(); |
|
|
| if (is_edge) { |
| |
| handle_graphics |
| .rect(0, 0, this.CORNER_SIZE * 1.5, this.LINE_THICKNESS) |
| .fill(this.HANDLE_COLOR); |
| } else { |
| |
| handle_graphics |
| .rect(0, 0, this.CORNER_SIZE, this.LINE_THICKNESS) |
| .rect(0, 0, this.LINE_THICKNESS, this.CORNER_SIZE) |
| .fill(this.HANDLE_COLOR); |
| } |
|
|
| handle.addChild(handle_graphics); |
|
|
| |
| const hit_size = is_edge ? this.HIT_AREA_SIZE * 1.5 : this.HIT_AREA_SIZE; |
| handle.hitArea = new Rectangle( |
| -hit_size / 2 + this.LINE_THICKNESS, |
| -hit_size / 2 + this.LINE_THICKNESS, |
| hit_size, |
| hit_size |
| ); |
|
|
| return handle; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| private create_edge_handles( |
| container: Container, |
| width: number, |
| height: number |
| ): void { |
| |
| [-1, 1].forEach((i, index) => { |
| const handle = this.create_handle(true); |
| handle.rotation = 0; |
|
|
| handle.on("pointerdown", (event: FederatedPointerEvent) => { |
| this.handle_pointer_down(event, handle, -1, index); |
| }); |
|
|
| |
| handle.x = width / 2 - (this.CORNER_SIZE * 3) / 2; |
| handle.y = i < 0 ? -this.LINE_THICKNESS : height; |
| handle.cursor = "ns-resize"; |
|
|
| container.addChild(handle); |
| }); |
|
|
| |
| [-1, 1].forEach((i, index) => { |
| const handle = this.create_handle(true); |
| handle.rotation = Math.PI / 2; |
|
|
| handle.on("pointerdown", (event: FederatedPointerEvent) => { |
| this.handle_pointer_down(event, handle, -1, index + 2); |
| }); |
|
|
| |
| handle.x = i < 0 ? -this.LINE_THICKNESS : width; |
| handle.y = height / 2 - (this.CORNER_SIZE * 3) / 2; |
| handle.cursor = "ew-resize"; |
|
|
| container.addChild(handle); |
| }); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| private create_corner_handles( |
| container: Container, |
| width: number, |
| height: number |
| ): void { |
| const corners = [ |
| { x: 0, y: 0, xScale: 1, yScale: 1, cursor: "nwse-resize" }, |
| { x: width, y: 0, xScale: -1, yScale: 1, cursor: "nesw-resize" }, |
| { x: 0, y: height, xScale: 1, yScale: -1, cursor: "nesw-resize" }, |
| { x: width, y: height, xScale: -1, yScale: -1, cursor: "nwse-resize" } |
| ]; |
|
|
| corners.forEach(({ x, y, xScale, yScale, cursor }, i) => { |
| const corner = this.create_handle(false); |
| corner.x = x - (xScale < 0 ? -this.LINE_THICKNESS : this.LINE_THICKNESS); |
| corner.y = y - (yScale < 0 ? -this.LINE_THICKNESS : this.LINE_THICKNESS); |
| corner.scale.set(xScale, yScale); |
|
|
| corner.on("pointerdown", (event: FederatedPointerEvent) => { |
| this.handle_pointer_down(event, corner, i, -1); |
| }); |
|
|
| corner.cursor = cursor; |
|
|
| container.addChild(corner); |
| }); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| private handle_pointer_down( |
| event: FederatedPointerEvent, |
| handle: Container, |
| corner_index: number, |
| edge_index: number |
| ): void { |
| |
| if (this.current_subtool !== "crop") return; |
|
|
| event.stopPropagation(); |
|
|
| this.is_dragging = true; |
| this.selected_handle = handle; |
| this.active_corner_index = corner_index; |
| this.active_edge_index = edge_index; |
|
|
| |
| const local_pos = this.image_editor_context.image_container.toLocal( |
| event.global |
| ); |
|
|
| this.last_pointer_position = new Point(local_pos.x, local_pos.y); |
| } |
|
|
| |
| |
| |
| |
| private update_crop_bounds(delta: Point): void { |
| |
| this.has_been_manually_changed = true; |
|
|
| |
| const scaled_delta = new Point(delta.x, delta.y); |
|
|
| |
| const original = { |
| x: this.crop_bounds.x, |
| y: this.crop_bounds.y, |
| width: this.crop_bounds.width, |
| height: this.crop_bounds.height |
| }; |
|
|
| if (this.active_corner_index !== -1) { |
| |
| switch (this.active_corner_index) { |
| case 0: |
| |
| const newWidth0 = Math.max(20, original.width - scaled_delta.x); |
| const widthDiff0 = original.width - newWidth0; |
| this.crop_bounds.width = newWidth0; |
| this.crop_bounds.x = original.x + widthDiff0; |
|
|
| |
| const newHeight0 = Math.max(20, original.height - scaled_delta.y); |
| const heightDiff0 = original.height - newHeight0; |
| this.crop_bounds.height = newHeight0; |
| this.crop_bounds.y = original.y + heightDiff0; |
| break; |
|
|
| case 1: |
| |
| this.crop_bounds.width = Math.max( |
| 20, |
| original.width + scaled_delta.x |
| ); |
|
|
| |
| const newHeight1 = Math.max(20, original.height - scaled_delta.y); |
| const heightDiff1 = original.height - newHeight1; |
| this.crop_bounds.height = newHeight1; |
| this.crop_bounds.y = original.y + heightDiff1; |
| break; |
|
|
| case 2: |
| |
| const newWidth2 = Math.max(20, original.width - scaled_delta.x); |
| const widthDiff2 = original.width - newWidth2; |
| this.crop_bounds.width = newWidth2; |
| this.crop_bounds.x = original.x + widthDiff2; |
|
|
| |
| this.crop_bounds.height = Math.max( |
| 20, |
| original.height + scaled_delta.y |
| ); |
| break; |
|
|
| case 3: |
| |
| this.crop_bounds.width = Math.max( |
| 20, |
| original.width + scaled_delta.x |
| ); |
|
|
| |
| this.crop_bounds.height = Math.max( |
| 20, |
| original.height + scaled_delta.y |
| ); |
| break; |
| } |
| } else if (this.active_edge_index !== -1) { |
| |
| switch (this.active_edge_index) { |
| case 0: |
| |
| const newHeight = Math.max(20, original.height - scaled_delta.y); |
| const heightDiff = original.height - newHeight; |
| this.crop_bounds.height = newHeight; |
| this.crop_bounds.y = original.y + heightDiff; |
| break; |
|
|
| case 1: |
| |
| this.crop_bounds.height = Math.max( |
| 20, |
| original.height + scaled_delta.y |
| ); |
| break; |
|
|
| case 2: |
| |
| const newWidth = Math.max(20, original.width - scaled_delta.x); |
| const widthDiff = original.width - newWidth; |
| this.crop_bounds.width = newWidth; |
| this.crop_bounds.x = original.x + widthDiff; |
| break; |
|
|
| case 3: |
| |
| this.crop_bounds.width = Math.max( |
| 20, |
| original.width + scaled_delta.x |
| ); |
| break; |
| } |
| } |
|
|
| |
| this.constrain_crop_bounds(); |
|
|
| |
| this.update_crop_ui(); |
| this.update_crop_mask(); |
|
|
| |
| if (this.crop_mask && this.image_editor_context.background_image) { |
| this.image_editor_context.background_image.setMask(this.crop_mask); |
| } |
| } |
|
|
| |
| |
| |
| private constrain_crop_bounds(): void { |
| |
| const original = { |
| x: this.crop_bounds.x, |
| y: this.crop_bounds.y, |
| width: this.crop_bounds.width, |
| height: this.crop_bounds.height |
| }; |
|
|
| |
| |
| this.crop_bounds.width = Math.max( |
| 20, |
| Math.min(this.crop_bounds.width, this.dimensions.width) |
| ); |
|
|
| this.crop_bounds.height = Math.max( |
| 20, |
| Math.min(this.crop_bounds.height, this.dimensions.height) |
| ); |
|
|
| |
| const oldX = this.crop_bounds.x; |
| this.crop_bounds.x = Math.max( |
| 0, |
| Math.min( |
| this.crop_bounds.x, |
| this.dimensions.width - this.crop_bounds.width |
| ) |
| ); |
|
|
| |
| if ( |
| (this.crop_bounds.x !== oldX && this.active_corner_index === 0) || |
| this.active_edge_index === 2 |
| ) { |
| |
| const xDiff = this.crop_bounds.x - oldX; |
| this.crop_bounds.width -= xDiff; |
| } |
|
|
| const oldY = this.crop_bounds.y; |
| this.crop_bounds.y = Math.max( |
| 0, |
| Math.min( |
| this.crop_bounds.y, |
| this.dimensions.height - this.crop_bounds.height |
| ) |
| ); |
|
|
| |
| if ( |
| this.crop_bounds.y !== oldY && |
| (this.active_corner_index === 0 || |
| this.active_corner_index === 1 || |
| this.active_edge_index === 0) |
| ) { |
| |
| const yDiff = this.crop_bounds.y - oldY; |
| this.crop_bounds.height -= yDiff; |
| } |
|
|
| |
| this.crop_bounds.width = Math.max( |
| 20, |
| Math.min( |
| this.crop_bounds.width, |
| this.dimensions.width - this.crop_bounds.x |
| ) |
| ); |
|
|
| this.crop_bounds.height = Math.max( |
| 20, |
| Math.min( |
| this.crop_bounds.height, |
| this.dimensions.height - this.crop_bounds.y |
| ) |
| ); |
| } |
|
|
| |
| |
| |
| private update_crop_mask(): void { |
| if (!this.crop_mask) return; |
|
|
| |
| this.crop_mask.clear(); |
|
|
| const { width, height } = |
| this.image_editor_context.background_image?.getLocalBounds() ?? { |
| width: 0, |
| height: 0 |
| }; |
| this.crop_mask |
| .rect(0, 0, width, height) |
| .fill({ color: 0x000000, alpha: 0.4 }) |
| .rect( |
| this.crop_bounds.x, |
| this.crop_bounds.y, |
| this.crop_bounds.width, |
| this.crop_bounds.height |
| ) |
| .cut(); |
|
|
| |
| |
|
|
| |
| |
|
|
| |
| |
| if (this.image_editor_context.background_image) { |
| this.image_editor_context.background_image.mask = null; |
| } |
|
|
| |
| if (this.image_editor_context.image_container.mask === this.crop_mask) { |
| this.image_editor_context.image_container.mask = null; |
| } |
| } |
|
|
| |
| |
| |
| private update_crop_ui(): void { |
| if (!this.crop_mask || !this.crop_ui_container) return; |
|
|
| |
| this.crop_ui_container.position.set( |
| this.position.x + this.crop_bounds.x * this.scale, |
| this.position.y + this.crop_bounds.y * this.scale |
| ); |
|
|
| |
| const scaled_width = this.crop_bounds.width * this.scale; |
| const scaled_height = this.crop_bounds.height * this.scale; |
|
|
| |
| const outline = this.crop_ui_container.getChildAt(0) as Graphics; |
| outline |
| .clear() |
| .rect(0, 0, scaled_width, scaled_height) |
| .stroke({ width: 1, color: 0x000000, alignment: 0, alpha: 0.5 }); |
|
|
| |
| outline.hitArea = new Rectangle(0, 0, scaled_width, scaled_height); |
|
|
| |
| this.update_handle_positions(scaled_width, scaled_height); |
| } |
|
|
| |
| |
| |
| |
| |
| private update_handle_positions(width: number, height: number): void { |
| if (!this.crop_ui_container) return; |
|
|
| |
| const handles = this.crop_ui_container.children.slice(1); |
|
|
| |
| const corners = handles.slice(0, 4); |
| const cornerPositions = [ |
| { x: 0, y: 0 }, |
| { x: width, y: 0 }, |
| { x: 0, y: height }, |
| { x: width, y: height } |
| ]; |
|
|
| corners.forEach((handle, i) => { |
| const xScale = i % 2 === 0 ? 1 : -1; |
| const yScale = i < 2 ? 1 : -1; |
| handle.position.set( |
| cornerPositions[i].x - |
| (xScale < 0 ? -this.LINE_THICKNESS : this.LINE_THICKNESS), |
| cornerPositions[i].y - |
| (yScale < 0 ? -this.LINE_THICKNESS : this.LINE_THICKNESS) |
| ); |
| }); |
|
|
| |
| const edges = handles.slice(4); |
| edges.forEach((handle, i) => { |
| if (i < 2) { |
| |
| handle.position.set( |
| width / 2 - (this.CORNER_SIZE * 1.5) / 2, |
| i === 0 ? -this.LINE_THICKNESS : height |
| ); |
| } else { |
| |
| handle.position.set( |
| i === 2 ? 0 : width + this.LINE_THICKNESS, |
| height / 2 - (this.CORNER_SIZE * 1.5) / 2 |
| ); |
| } |
| }); |
| } |
|
|
| |
| |
| |
| private setup_event_listeners(): void { |
| const stage = this.image_editor_context.app.stage; |
| stage.eventMode = "static"; |
| stage.on("pointermove", this.handle_pointer_move.bind(this)); |
| stage.on("pointerup", this.handle_pointer_up.bind(this)); |
| stage.on("pointerupoutside", this.handle_pointer_up.bind(this)); |
| } |
|
|
| |
| |
| |
| private cleanup_event_listeners(): void { |
| const stage = this.image_editor_context.app.stage; |
|
|
| stage.off("pointermove", this.handle_pointer_move.bind(this)); |
| stage.off("pointerup", this.handle_pointer_up.bind(this)); |
| stage.off("pointerupoutside", this.handle_pointer_up.bind(this)); |
| } |
|
|
| |
| |
| |
| |
| private handle_pointer_move(event: FederatedPointerEvent): void { |
| if (this.current_subtool !== "crop") return; |
|
|
| if ( |
| this.is_dragging_window && |
| this.drag_start_position && |
| this.drag_start_bounds |
| ) { |
| |
| const local_pos = this.image_editor_context.image_container.toLocal( |
| event.global |
| ); |
| const delta = new Point( |
| local_pos.x - this.drag_start_position.x, |
| local_pos.y - this.drag_start_position.y |
| ); |
|
|
| |
| this.crop_bounds = { |
| x: this.drag_start_bounds.x + delta.x, |
| y: this.drag_start_bounds.y + delta.y, |
| width: this.drag_start_bounds.width, |
| height: this.drag_start_bounds.height |
| }; |
|
|
| |
| this.constrain_crop_bounds(); |
|
|
| |
| this.update_crop_mask(); |
|
|
| |
| if (this.crop_mask && this.image_editor_context.background_image) { |
| this.image_editor_context.background_image.setMask(this.crop_mask); |
| } |
|
|
| return; |
| } |
|
|
| |
| if ( |
| this.is_dragging && |
| this.selected_handle && |
| this.last_pointer_position |
| ) { |
| |
| const local_pos = this.image_editor_context.image_container.toLocal( |
| event.global |
| ); |
| const current_position = new Point(local_pos.x, local_pos.y); |
|
|
| const delta = new Point( |
| current_position.x - this.last_pointer_position.x, |
| current_position.y - this.last_pointer_position.y |
| ); |
|
|
| this.update_crop_bounds(delta); |
| this.last_pointer_position = current_position; |
| this.update_crop_mask(); |
| } |
| } |
|
|
| |
| |
| |
| private handle_pointer_up(): void { |
| if (this.current_subtool !== "crop") return; |
|
|
| this.is_dragging = false; |
| this.is_dragging_window = false; |
| this.selected_handle = null; |
| this.last_pointer_position = null; |
| this.drag_start_position = null; |
| this.drag_start_bounds = null; |
| this.active_corner_index = -1; |
| this.active_edge_index = -1; |
|
|
| this.notify("change"); |
| } |
|
|
| |
| |
| |
| |
| private handle_window_drag_start(event: FederatedPointerEvent): void { |
| if (this.current_subtool !== "crop") return; |
| event.stopPropagation(); |
|
|
| this.is_dragging_window = true; |
| |
| this.has_been_manually_changed = true; |
|
|
| |
| const local_pos = this.image_editor_context.image_container.toLocal( |
| event.global |
| ); |
| this.drag_start_position = new Point(local_pos.x, local_pos.y); |
| this.drag_start_bounds = { ...this.crop_bounds }; |
| } |
|
|
| |
| |
| |
| public get crop_manually_changed(): boolean { |
| return this.has_been_manually_changed; |
| } |
|
|
| on<T extends string>(event: T, callback: () => void): void { |
| this.event_callbacks.set(event, [ |
| ...(this.event_callbacks.get(event) || []), |
| callback |
| ]); |
| } |
|
|
| off<T extends string>(event: T, callback: () => void): void { |
| this.event_callbacks.set( |
| event, |
| this.event_callbacks.get(event)?.filter((cb) => cb !== callback) || [] |
| ); |
| } |
|
|
| private notify<T extends string>(event: T): void { |
| for (const callback of this.event_callbacks.get(event) || []) { |
| callback(); |
| } |
| } |
|
|
| public get_crop_bounds(): { |
| x: number; |
| y: number; |
| crop_dimensions: { width: number; height: number }; |
| image_dimensions: { width: number; height: number }; |
| } { |
| const { width, height } = |
| this.image_editor_context.background_image?.getLocalBounds() ?? { |
| width: 0, |
| height: 0 |
| }; |
| return { |
| x: this.crop_bounds.x, |
| y: this.crop_bounds.y, |
| crop_dimensions: { |
| width: this.crop_bounds.width, |
| height: this.crop_bounds.height |
| }, |
| image_dimensions: { |
| width, |
| height |
| } |
| }; |
| } |
|
|
| public get_image(): Promise<Blob | null> { |
| if (!this.image_editor_context.background_image) |
| return Promise.resolve(null); |
| const container = new Container(); |
| const sprite = new Sprite( |
| this.image_editor_context.background_image.texture |
| ); |
| container.addChild(sprite); |
|
|
| return get_canvas_blob( |
| this.image_editor_context.app.renderer, |
| container, |
| this.crop_bounds |
| ); |
| } |
| } |
|
|