@gradio/imageeditor 0.12.13 → 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (130) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/Example.svelte +1 -1
  3. package/IMAGE_EDITOR_OVERVIEW.md +224 -0
  4. package/ImageEditor.stories.svelte +4 -4
  5. package/Index.svelte +35 -46
  6. package/{shared/InteractiveImageEditor.svelte → InteractiveImageEditor.svelte} +56 -103
  7. package/dist/Example.svelte.d.ts +1 -1
  8. package/dist/Index.svelte +29 -33
  9. package/dist/Index.svelte.d.ts +17 -16
  10. package/dist/{shared/InteractiveImageEditor.svelte → InteractiveImageEditor.svelte} +47 -88
  11. package/dist/{shared/InteractiveImageEditor.svelte.d.ts → InteractiveImageEditor.svelte.d.ts} +13 -15
  12. package/dist/shared/Anchor.svelte +311 -0
  13. package/dist/shared/Anchor.svelte.d.ts +18 -0
  14. package/dist/shared/Controls.svelte +162 -23
  15. package/dist/shared/Controls.svelte.d.ts +7 -2
  16. package/dist/shared/{tools/IconButton.svelte → IconButton.svelte} +19 -3
  17. package/dist/shared/{tools/IconButton.svelte.d.ts → IconButton.svelte.d.ts} +4 -1
  18. package/dist/shared/ImageEditor.svelte +546 -289
  19. package/dist/shared/ImageEditor.svelte.d.ts +51 -56
  20. package/dist/shared/Layers.svelte +233 -0
  21. package/dist/shared/Layers.svelte.d.ts +35 -0
  22. package/dist/shared/Resize.svelte +218 -0
  23. package/dist/shared/Resize.svelte.d.ts +29 -0
  24. package/dist/shared/SecondaryToolbar.svelte +54 -0
  25. package/dist/shared/SecondaryToolbar.svelte.d.ts +35 -0
  26. package/dist/shared/Toolbar.svelte +337 -0
  27. package/dist/shared/Toolbar.svelte.d.ts +42 -0
  28. package/dist/shared/{tools → brush}/BrushOptions.svelte +68 -64
  29. package/dist/shared/{tools → brush}/BrushOptions.svelte.d.ts +9 -9
  30. package/dist/shared/{tools → brush}/BrushSize.svelte +2 -27
  31. package/dist/shared/{tools → brush}/ColorField.svelte +2 -1
  32. package/dist/shared/{tools → brush}/ColorField.svelte.d.ts +1 -0
  33. package/dist/shared/{tools → brush}/ColorPicker.svelte +81 -9
  34. package/dist/shared/{tools → brush}/ColorPicker.svelte.d.ts +1 -0
  35. package/dist/shared/{tools → brush}/ColorSwatch.svelte +78 -38
  36. package/dist/shared/{tools → brush}/ColorSwatch.svelte.d.ts +6 -2
  37. package/dist/shared/brush/brush-cursor.d.ts +101 -0
  38. package/dist/shared/brush/brush-cursor.js +327 -0
  39. package/dist/shared/brush/brush-textures.d.ts +71 -0
  40. package/dist/shared/brush/brush-textures.js +404 -0
  41. package/dist/shared/brush/brush-utils.d.ts +32 -0
  42. package/dist/shared/brush/brush-utils.js +59 -0
  43. package/dist/shared/brush/brush.d.ts +152 -0
  44. package/dist/shared/brush/brush.js +396 -0
  45. package/dist/shared/brush/types.d.ts +37 -0
  46. package/dist/shared/brush/types.js +1 -0
  47. package/dist/shared/core/editor.d.ts +252 -0
  48. package/dist/shared/core/editor.js +1221 -0
  49. package/dist/shared/crop/crop.d.ts +201 -0
  50. package/dist/shared/crop/crop.js +727 -0
  51. package/dist/shared/image/image.d.ts +89 -0
  52. package/dist/shared/image/image.js +185 -0
  53. package/dist/shared/resize/resize.d.ts +270 -0
  54. package/dist/shared/resize/resize.js +1413 -0
  55. package/dist/shared/types.d.ts +12 -0
  56. package/dist/shared/utils/pixi.d.ts +16 -57
  57. package/dist/shared/utils/pixi.js +15 -113
  58. package/dist/shared/zoom/zoom.d.ts +136 -0
  59. package/dist/shared/zoom/zoom.js +548 -0
  60. package/package.json +11 -10
  61. package/shared/Anchor.svelte +384 -0
  62. package/shared/Controls.svelte +194 -27
  63. package/shared/{tools/IconButton.svelte → IconButton.svelte} +21 -4
  64. package/shared/ImageEditor.svelte +682 -349
  65. package/shared/Layers.svelte +249 -0
  66. package/shared/Resize.svelte +239 -0
  67. package/shared/SecondaryToolbar.svelte +61 -0
  68. package/shared/Toolbar.svelte +386 -0
  69. package/shared/brush/BRUSH_TOOL.md +134 -0
  70. package/shared/brush/BrushOptions.svelte +200 -0
  71. package/shared/{tools → brush}/BrushSize.svelte +2 -30
  72. package/shared/{tools → brush}/ColorField.svelte +3 -1
  73. package/shared/{tools → brush}/ColorPicker.svelte +86 -9
  74. package/shared/{tools → brush}/ColorSwatch.svelte +95 -42
  75. package/shared/brush/brush-cursor.ts +408 -0
  76. package/shared/brush/brush-textures.ts +508 -0
  77. package/shared/brush/brush-utils.ts +76 -0
  78. package/shared/brush/brush.ts +506 -0
  79. package/shared/brush/types.ts +41 -0
  80. package/shared/core/EDITOR.md +182 -0
  81. package/shared/core/editor.ts +1730 -0
  82. package/shared/crop/CROP.md +139 -0
  83. package/shared/crop/crop.ts +1027 -0
  84. package/shared/image/IMAGE.md +135 -0
  85. package/shared/image/image.ts +300 -0
  86. package/shared/resize/resize.ts +1894 -0
  87. package/shared/types.ts +14 -0
  88. package/shared/utils/pixi.ts +19 -246
  89. package/shared/zoom/zoom.ts +729 -0
  90. package/dist/shared/layers/Layers.svelte +0 -231
  91. package/dist/shared/layers/Layers.svelte.d.ts +0 -18
  92. package/dist/shared/layers/utils.d.ts +0 -73
  93. package/dist/shared/layers/utils.js +0 -156
  94. package/dist/shared/tools/Brush.svelte +0 -215
  95. package/dist/shared/tools/Brush.svelte.d.ts +0 -29
  96. package/dist/shared/tools/Crop.svelte +0 -155
  97. package/dist/shared/tools/Crop.svelte.d.ts +0 -16
  98. package/dist/shared/tools/Cropper.svelte +0 -271
  99. package/dist/shared/tools/Cropper.svelte.d.ts +0 -41
  100. package/dist/shared/tools/Handle.svelte +0 -260
  101. package/dist/shared/tools/Handle.svelte.d.ts +0 -29
  102. package/dist/shared/tools/Sources.svelte +0 -257
  103. package/dist/shared/tools/Sources.svelte.d.ts +0 -34
  104. package/dist/shared/tools/Tools.svelte +0 -102
  105. package/dist/shared/tools/Tools.svelte.d.ts +0 -48
  106. package/dist/shared/tools/brush.d.ts +0 -62
  107. package/dist/shared/tools/brush.js +0 -133
  108. package/dist/shared/tools/crop.d.ts +0 -15
  109. package/dist/shared/tools/crop.js +0 -154
  110. package/dist/shared/tools/index.d.ts +0 -5
  111. package/dist/shared/tools/index.js +0 -5
  112. package/dist/shared/tools/sources.d.ts +0 -46
  113. package/dist/shared/tools/sources.js +0 -121
  114. package/dist/shared/tools/types.d.ts +0 -1
  115. package/shared/layers/Layers.svelte +0 -250
  116. package/shared/layers/utils.ts +0 -294
  117. package/shared/tools/Brush.svelte +0 -280
  118. package/shared/tools/BrushOptions.svelte +0 -180
  119. package/shared/tools/Crop.svelte +0 -189
  120. package/shared/tools/Cropper.svelte +0 -346
  121. package/shared/tools/Handle.svelte +0 -289
  122. package/shared/tools/Sources.svelte +0 -283
  123. package/shared/tools/Tools.svelte +0 -162
  124. package/shared/tools/brush.ts +0 -247
  125. package/shared/tools/crop.ts +0 -213
  126. package/shared/tools/index.ts +0 -5
  127. package/shared/tools/sources.ts +0 -195
  128. package/shared/tools/types.ts +0 -1
  129. /package/dist/shared/{tools → brush}/BrushSize.svelte.d.ts +0 -0
  130. /package/dist/shared/{tools/types.js → types.js} +0 -0
@@ -1,48 +0,0 @@
1
- import { SvelteComponent } from "svelte";
2
- import type { Subscriber, Invalidator, Unsubscriber, Writable } from "svelte/store";
3
- import type { tool } from "../tools/";
4
- export declare const TOOL_KEY: unique symbol;
5
- type upload_tool = "bg_webcam" | "bg_upload" | "bg_clipboard" | "bg_color";
6
- type transform_tool = "crop" | "rotate";
7
- type brush_tool = "brush_color" | "brush_size";
8
- type eraser_tool = "eraser_size";
9
- export interface ToolOptions {
10
- order: number;
11
- label: string;
12
- icon: typeof Image;
13
- cb: (...args: any[]) => void;
14
- id: upload_tool | transform_tool | brush_tool | eraser_tool;
15
- }
16
- export interface ToolMeta {
17
- default: upload_tool | transform_tool | brush_tool | eraser_tool | null;
18
- options: ToolOptions[];
19
- }
20
- export interface ToolContext {
21
- register_tool: (type: tool, opts?: {
22
- cb: () => void;
23
- }) => () => void;
24
- active_tool: {
25
- set: (tool: tool) => void;
26
- subscribe(this: void, run: Subscriber<tool | null>, invalidate?: Invalidator<tool | null>): Unsubscriber;
27
- };
28
- current_color: Writable<string>;
29
- }
30
- import { Image } from "@gradio/icons";
31
- import { type I18nFormatter } from "@gradio/utils";
32
- declare const __propDef: {
33
- props: {
34
- i18n: I18nFormatter;
35
- };
36
- events: {
37
- [evt: string]: CustomEvent<any>;
38
- };
39
- slots: {
40
- default: {};
41
- };
42
- };
43
- export type ToolsProps = typeof __propDef.props;
44
- export type ToolsEvents = typeof __propDef.events;
45
- export type ToolsSlots = typeof __propDef.slots;
46
- export default class Tools extends SvelteComponent<ToolsProps, ToolsEvents, ToolsSlots> {
47
- }
48
- export {};
@@ -1,62 +0,0 @@
1
- import { Container, type ColorSource, type IRenderer } from "pixi.js";
2
- import type { LayerScene } from "../layers/utils";
3
- import { type Command } from "../utils/commands";
4
- export interface DrawCommand extends Command {
5
- /**
6
- * sets the initial state to begin drawing
7
- * @param options the options for drawing
8
- * @returns
9
- */
10
- start: (options: DrawOptions) => void;
11
- /**
12
- * continues drawing, smoothly interpolating between points where necessary
13
- * @param options the options for drawing
14
- * @returns
15
- */
16
- continue: (options: Points) => void;
17
- /**
18
- * stops drawing
19
- * @returns
20
- */
21
- stop: () => void;
22
- /**
23
- * Whether or not the user is currently drawing.
24
- */
25
- drawing: boolean;
26
- }
27
- /**
28
- * Coordinates for a draw or erase path.
29
- */
30
- interface Points {
31
- /**
32
- * The x coordinate of the path.
33
- */
34
- x: number;
35
- /**
36
- * The y coordinate of the path.
37
- */
38
- y: number;
39
- }
40
- /**
41
- * Options for drawing a path.
42
- */
43
- interface DrawOptions extends Points {
44
- /**
45
- * The size of the brush.
46
- */
47
- size: number;
48
- /**
49
- * The color of the brush.
50
- */
51
- color?: ColorSource;
52
- /**
53
- * The opacity of the brush.
54
- */
55
- opacity: number;
56
- /**
57
- * Whether or not to set the initial texture.
58
- */
59
- set_initial_texture?: boolean;
60
- }
61
- export declare function draw_path(renderer: IRenderer, stage: Container, layer: LayerScene, mode: "draw" | "erase"): DrawCommand;
62
- export {};
@@ -1,133 +0,0 @@
1
- import { Container, Graphics, Color, RenderTexture, Sprite } from "pixi.js";
2
- import {} from "../utils/commands";
3
- import { make_graphics } from "../utils/pixi";
4
- /**
5
- * Draws a circle on the given graphics object.
6
- * @param graphics the graphics object to draw on
7
- * @param x the x coordinate of the circle
8
- * @param y the y coordinate of the circle
9
- * @param brush_color the color of the circle
10
- * @param brush_size the radius of the circle
11
- */
12
- function drawCircle(graphics, x, y, brush_color = new Color("black"), brush_size) {
13
- const color = new Color(brush_color);
14
- graphics.beginFill(color);
15
- graphics.drawCircle(x, y, brush_size);
16
- graphics.endFill();
17
- }
18
- /**
19
- * Interpolates between two points.
20
- * @param point1 the first point
21
- * @param point2 the second point
22
- * @returns an array of points between the two points
23
- */
24
- function interpolate(point1, point2) {
25
- let points = [];
26
- const dx = point2.x - point1.x;
27
- const dy = point2.y - point1.y;
28
- const distance = Math.sqrt(dx * dx + dy * dy);
29
- const steps = Math.ceil(distance / 2);
30
- const stepX = dx / steps;
31
- const stepY = dy / steps;
32
- for (let j = 0; j < steps; j++) {
33
- const x = point1.x + j * stepX;
34
- const y = point1.y + j * stepY;
35
- points.push({ x, y });
36
- }
37
- return points;
38
- }
39
- export function draw_path(renderer, stage, layer, mode) {
40
- const paths = [];
41
- let initial_path;
42
- let graphics;
43
- let InitialTexture;
44
- let has_drawn = false;
45
- let id = 0;
46
- return {
47
- drawing: false,
48
- start: function ({ x, y, size, color = new Color("black"), opacity, set_initial_texture = true }) {
49
- if (set_initial_texture) {
50
- InitialTexture = RenderTexture.create({
51
- width: layer.draw_texture.width,
52
- height: layer.draw_texture.height
53
- });
54
- renderer.render(layer.composite, {
55
- renderTexture: InitialTexture
56
- });
57
- }
58
- initial_path = { x, y, size, color, opacity };
59
- paths.push({ x, y });
60
- graphics = make_graphics(id++);
61
- drawCircle(graphics, x, y, color, size);
62
- renderer.render(graphics, {
63
- renderTexture: mode === "draw" ? layer.draw_texture : layer.erase_texture,
64
- clear: false
65
- });
66
- this.drawing = true;
67
- },
68
- continue: function ({ x, y }) {
69
- const last_point = paths[paths.length - 1];
70
- const new_points = interpolate(last_point, { x, y });
71
- for (let i = 0; i < new_points.length; i++) {
72
- const { x, y } = new_points[i];
73
- drawCircle(graphics, x, y, initial_path.color, initial_path.size);
74
- paths.push({ x, y });
75
- }
76
- renderer.render(graphics, {
77
- renderTexture: mode === "draw" ? layer.draw_texture : layer.erase_texture,
78
- clear: false
79
- });
80
- graphics.clear();
81
- },
82
- stop: function () {
83
- const current_sketch = RenderTexture.create({
84
- width: layer.draw_texture.width,
85
- height: layer.draw_texture.height
86
- });
87
- renderer.render(layer.composite, {
88
- renderTexture: current_sketch
89
- });
90
- renderer.render(new Sprite(current_sketch), {
91
- renderTexture: layer.draw_texture
92
- });
93
- const clear_graphics = new Graphics()
94
- .beginFill(0x000000, 0) // Use a fill color with 0 alpha for transparency
95
- .drawRect(0, 0, layer.erase_texture.width, layer.erase_texture.height)
96
- .endFill();
97
- renderer.render(clear_graphics, {
98
- renderTexture: layer.erase_texture,
99
- clear: true
100
- });
101
- has_drawn = true;
102
- this.drawing = false;
103
- },
104
- execute: function () {
105
- if (!has_drawn) {
106
- for (let i = 1; i < paths.length; i++) {
107
- const { x, y } = paths[i];
108
- drawCircle(graphics, x, y, initial_path.color, initial_path.size);
109
- }
110
- renderer.render(graphics, {
111
- renderTexture: mode === "draw" ? layer.draw_texture : layer.erase_texture,
112
- clear: false
113
- });
114
- this.stop();
115
- }
116
- },
117
- undo: function () {
118
- const clear_graphics = new Graphics()
119
- .beginFill(0x000000, 0)
120
- .drawRect(0, 0, layer.erase_texture.width, layer.erase_texture.height)
121
- .endFill();
122
- renderer.render(new Sprite(InitialTexture), {
123
- renderTexture: layer.draw_texture
124
- });
125
- renderer.render(clear_graphics, {
126
- renderTexture: layer.erase_texture,
127
- clear: true
128
- });
129
- this.stop();
130
- has_drawn = false;
131
- }
132
- };
133
- }
@@ -1,15 +0,0 @@
1
- import { type IRenderer, type Container } from "pixi.js";
2
- import { type Command } from "../utils/commands";
3
- import type { Writable } from "svelte/store";
4
- export interface CropCommand extends Command {
5
- start: (width: number, height: number, previous_crop: [number, number, number, number], preview?: boolean, set_previous?: boolean) => void;
6
- stop: () => number;
7
- continue: (crop_size: [number, number, number, number], preview?: boolean) => void;
8
- }
9
- export declare function crop_canvas(renderer: IRenderer, background_container: Container, crop: Writable<[number, number, number, number]>, current_opacity?: number): CropCommand;
10
- export declare function resize_and_reposition(original_width: number, original_height: number, anchor: "t" | "r" | "l" | "b" | "tl" | "tr" | "bl" | "br" | "c", aspect_ratio: number, max_width: number, max_height: number): {
11
- new_width: number;
12
- new_height: number;
13
- x_offset: number;
14
- y_offset: number;
15
- };
@@ -1,154 +0,0 @@
1
- import { Graphics, Sprite, RenderTexture } from "pixi.js";
2
- import {} from "../utils/commands";
3
- import { spring } from "svelte/motion";
4
- export function crop_canvas(renderer, background_container, crop, current_opacity = 0) {
5
- let text;
6
- let sprite;
7
- const mask_graphics = new Graphics();
8
- let previous_crop;
9
- let final_crop;
10
- let width;
11
- let height;
12
- let alpha_spring = spring(current_opacity, {
13
- stiffness: 0.1,
14
- damping: 0.5
15
- });
16
- let spring_value = current_opacity;
17
- alpha_spring.subscribe((value) => {
18
- if (!final_crop)
19
- return;
20
- spring_value = value;
21
- crop_mask(width, height, final_crop, true);
22
- });
23
- function crop_mask(width, height, crop_size, preview) {
24
- mask_graphics.clear();
25
- if (preview) {
26
- mask_graphics.beginFill(0xffffff, spring_value);
27
- mask_graphics.drawRect(0, 0, width, height);
28
- mask_graphics.endFill();
29
- }
30
- mask_graphics.beginFill(0xffffff, 1);
31
- mask_graphics.drawRect(...crop_size);
32
- mask_graphics.endFill();
33
- renderer.render(mask_graphics, {
34
- renderTexture: text
35
- });
36
- }
37
- let clean = true;
38
- let stopped = false;
39
- return {
40
- start: (_width, _height, _previous_crop, _preview = true, set_previous = true) => {
41
- clean = false;
42
- text = RenderTexture.create({
43
- width: _width,
44
- height: _height
45
- });
46
- crop_mask(_width, _height, _previous_crop, _preview);
47
- sprite = new Sprite(text);
48
- background_container.mask = sprite;
49
- width = _width;
50
- height = _height;
51
- if (set_previous)
52
- previous_crop = JSON.parse(JSON.stringify(_previous_crop));
53
- },
54
- continue: (crop_size, preview = true) => {
55
- final_crop = JSON.parse(JSON.stringify(crop_size));
56
- if (spring_value === 0.2) {
57
- crop_mask(width, height, final_crop, preview);
58
- }
59
- else {
60
- alpha_spring.set(0.2);
61
- }
62
- },
63
- undo() {
64
- this.start(width, height, previous_crop, false);
65
- crop.set([
66
- previous_crop[0] / width,
67
- previous_crop[1] / height,
68
- previous_crop[2] / width,
69
- previous_crop[3] / height
70
- ]);
71
- clean = true;
72
- },
73
- stop() {
74
- stopped = true;
75
- return spring_value;
76
- },
77
- execute() {
78
- if (clean) {
79
- this.start(width, height, final_crop, false);
80
- crop.set([
81
- final_crop[0] / width,
82
- final_crop[1] / height,
83
- final_crop[2] / width,
84
- final_crop[3] / height
85
- ]);
86
- clean = true;
87
- }
88
- else {
89
- if (!stopped) {
90
- alpha_spring.set(0, { hard: true });
91
- }
92
- crop.set([
93
- final_crop[0] / width,
94
- final_crop[1] / height,
95
- final_crop[2] / width,
96
- final_crop[3] / height
97
- ]);
98
- clean = true;
99
- }
100
- }
101
- };
102
- }
103
- export function resize_and_reposition(original_width, original_height, anchor, aspect_ratio, max_width, max_height) {
104
- let new_width = original_width;
105
- let new_height = original_height;
106
- // Calculate new dimensions based on aspect ratio
107
- if (anchor.includes("t") || anchor.includes("b") || anchor == "c") {
108
- new_width = original_height * aspect_ratio;
109
- }
110
- if (anchor.includes("l") || anchor.includes("r") || anchor == "c") {
111
- new_height = original_width / aspect_ratio;
112
- }
113
- // Ensure aspect ratio is maintained
114
- new_height = new_height || new_width / aspect_ratio;
115
- new_width = new_width || new_height * aspect_ratio;
116
- // Apply max width/height constraints and adjust dimensions
117
- if (new_width > max_width) {
118
- new_width = max_width;
119
- new_height = new_width / aspect_ratio;
120
- }
121
- if (new_height > max_height) {
122
- new_height = max_height;
123
- new_width = new_height * aspect_ratio;
124
- }
125
- // Calculate offsets to ensure anchor position is maintained
126
- let x_offset = 0;
127
- let y_offset = 0;
128
- if (anchor.includes("r")) {
129
- x_offset = original_width - new_width;
130
- }
131
- else if (anchor.includes("l")) {
132
- x_offset = 0;
133
- }
134
- else {
135
- // Center or top/bottom center
136
- x_offset = (original_width - new_width) / 2;
137
- }
138
- if (anchor.includes("b")) {
139
- y_offset = original_height - new_height;
140
- }
141
- else if (anchor.includes("t")) {
142
- y_offset = 0;
143
- }
144
- else {
145
- // Center or left/right center
146
- y_offset = (original_height - new_height) / 2;
147
- }
148
- return {
149
- new_width: new_width,
150
- new_height: new_height,
151
- x_offset: x_offset,
152
- y_offset: y_offset
153
- };
154
- }
@@ -1,5 +0,0 @@
1
- export { default as Tools } from "./Tools.svelte";
2
- export { default as Brush } from "./Brush.svelte";
3
- export { default as Crop } from "./Crop.svelte";
4
- export { default as Sources } from "./Sources.svelte";
5
- export * from "./types";
@@ -1,5 +0,0 @@
1
- export { default as Tools } from "./Tools.svelte";
2
- export { default as Brush } from "./Brush.svelte";
3
- export { default as Crop } from "./Crop.svelte";
4
- export { default as Sources } from "./Sources.svelte";
5
- export * from "./types";
@@ -1,46 +0,0 @@
1
- import { type Container, type IRenderer, type ColorSource } from "pixi.js";
2
- import { type Command } from "../utils/commands";
3
- interface BgImageCommand extends Command {
4
- /**
5
- * Initial setup for the bg command
6
- * @returns
7
- */
8
- start: () => Promise<[number, number]>;
9
- }
10
- /**
11
- * Calculates new dimensions and position for an image to fit within a canvas while maintaining aspect ratio
12
- * @param image_width Original width of the image
13
- * @param image_height Original height of the image
14
- * @param canvas_width Width of the canvas
15
- * @param canvas_height Height of the canvas
16
- * @returns Object containing new dimensions and position
17
- */
18
- export declare function fit_image_to_canvas(image_width: number, image_height: number, canvas_width: number, canvas_height: number): {
19
- width: number;
20
- height: number;
21
- x: number;
22
- y: number;
23
- };
24
- export declare function add_bg_image(container: Container, renderer: IRenderer, background: Blob | File, resize: (width: number, height: number) => void, canvas_size: [number, number], fixed_canvas: boolean): BgImageCommand;
25
- /**
26
- * Command that sets a background
27
- */
28
- interface BgColorCommand extends Command {
29
- /**
30
- * Initial setup for the bg command
31
- * @returns
32
- */
33
- start: () => [number, number];
34
- }
35
- /**
36
- * Adds a background color to the canvas.
37
- * @param container The container to add the image to.
38
- * @param renderer The renderer to use for the image.
39
- * @param color The background color to add.
40
- * @param width The width of the background.
41
- * @param height The height of the background.
42
- * @param resize The function to resize the canvas.
43
- * @returns A command that can be used to undo the action.
44
- */
45
- export declare function add_bg_color(container: Container, renderer: IRenderer, color: ColorSource, width: number, height: number, resize: (width: number, height: number) => void): BgColorCommand;
46
- export {};
@@ -1,121 +0,0 @@
1
- import { Sprite, Texture, DisplayObject, Color, RenderTexture } from "pixi.js";
2
- import {} from "../utils/commands";
3
- import { make_graphics } from "../utils/pixi";
4
- /**
5
- * Calculates new dimensions and position for an image to fit within a canvas while maintaining aspect ratio
6
- * @param image_width Original width of the image
7
- * @param image_height Original height of the image
8
- * @param canvas_width Width of the canvas
9
- * @param canvas_height Height of the canvas
10
- * @returns Object containing new dimensions and position
11
- */
12
- export function fit_image_to_canvas(image_width, image_height, canvas_width, canvas_height) {
13
- const image_aspect_ratio = image_width / image_height;
14
- const canvas_aspect_ratio = canvas_width / canvas_height;
15
- let new_width;
16
- let new_height;
17
- if (image_width <= canvas_width && image_height <= canvas_height) {
18
- new_width = image_width;
19
- new_height = image_height;
20
- }
21
- else {
22
- if (image_aspect_ratio > canvas_aspect_ratio) {
23
- // Width is the limiting factor
24
- new_width = canvas_width;
25
- new_height = canvas_width / image_aspect_ratio;
26
- }
27
- else {
28
- // Height is the limiting factor
29
- new_height = canvas_height;
30
- new_width = canvas_height * image_aspect_ratio;
31
- }
32
- }
33
- // Calculate position to center the image
34
- const x = Math.round((canvas_width - new_width) / 2);
35
- const y = Math.round((canvas_height - new_height) / 2);
36
- return {
37
- width: Math.round(new_width),
38
- height: Math.round(new_height),
39
- x,
40
- y
41
- };
42
- }
43
- export function add_bg_image(container, renderer, background, resize, canvas_size, fixed_canvas) {
44
- let sprite;
45
- return {
46
- async start() {
47
- container.removeChildren();
48
- const img = await createImageBitmap(background);
49
- const bitmap_texture = Texture.from(img);
50
- sprite = new Sprite(bitmap_texture);
51
- if (!fixed_canvas) {
52
- canvas_size = [sprite.width, sprite.height];
53
- }
54
- if (fixed_canvas) {
55
- const [canvas_width, canvas_height] = canvas_size;
56
- const { width, height, x, y } = fit_image_to_canvas(sprite.width, sprite.height, canvas_width, canvas_height);
57
- sprite.width = width;
58
- sprite.height = height;
59
- sprite.x = x;
60
- sprite.y = y;
61
- return canvas_size;
62
- }
63
- // Use existing max_height based scaling if no canvas_size
64
- const x = fit_image_to_canvas(sprite.width, sprite.height, canvas_size[0], canvas_size[1]);
65
- sprite.width = x.width;
66
- sprite.height = x.height;
67
- sprite.x = 0;
68
- sprite.y = 0;
69
- return [x.width, x.height];
70
- },
71
- async execute() {
72
- resize(
73
- // @ts-ignore
74
- fixed_canvas ? canvas_size[0] : sprite.width,
75
- // @ts-ignore
76
- fixed_canvas ? canvas_size[1] : sprite.height);
77
- sprite.zIndex = 0;
78
- container.addChild(sprite);
79
- },
80
- undo() {
81
- container.removeChildAt(0);
82
- }
83
- };
84
- }
85
- /**
86
- * Adds a background color to the canvas.
87
- * @param container The container to add the image to.
88
- * @param renderer The renderer to use for the image.
89
- * @param color The background color to add.
90
- * @param width The width of the background.
91
- * @param height The height of the background.
92
- * @param resize The function to resize the canvas.
93
- * @returns A command that can be used to undo the action.
94
- */
95
- export function add_bg_color(container, renderer, color, width, height, resize) {
96
- let sprite;
97
- return {
98
- start() {
99
- container.removeChildren();
100
- const graphics = make_graphics(1);
101
- const texture = RenderTexture.create({
102
- width,
103
- height
104
- });
105
- graphics.beginFill(new Color(color));
106
- graphics.drawRect(0, 0, width, height);
107
- graphics.endFill();
108
- renderer.render(graphics, { renderTexture: texture });
109
- sprite = new Sprite(texture);
110
- return [sprite.width, sprite.height];
111
- },
112
- async execute() {
113
- resize(sprite.width, sprite.height);
114
- sprite.zIndex = 1;
115
- container.addChild(sprite);
116
- },
117
- undo() {
118
- container.removeChildren();
119
- }
120
- };
121
- }
@@ -1 +0,0 @@
1
- export type tool = "draw" | "erase" | "crop" | "bg";