@gradio/imageeditor 0.12.14 → 0.14.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 (131) hide show
  1. package/CHANGELOG.md +36 -0
  2. package/Example.svelte +1 -1
  3. package/IMAGE_EDITOR_OVERVIEW.md +224 -0
  4. package/ImageEditor.stories.svelte +13 -5
  5. package/Index.svelte +46 -45
  6. package/{shared/InteractiveImageEditor.svelte → InteractiveImageEditor.svelte} +65 -103
  7. package/README.md +2 -0
  8. package/dist/Example.svelte.d.ts +1 -1
  9. package/dist/Index.svelte +35 -33
  10. package/dist/Index.svelte.d.ts +25 -16
  11. package/dist/{shared/InteractiveImageEditor.svelte → InteractiveImageEditor.svelte} +54 -88
  12. package/dist/{shared/InteractiveImageEditor.svelte.d.ts → InteractiveImageEditor.svelte.d.ts} +17 -15
  13. package/dist/shared/Anchor.svelte +311 -0
  14. package/dist/shared/Anchor.svelte.d.ts +18 -0
  15. package/dist/shared/Controls.svelte +169 -18
  16. package/dist/shared/Controls.svelte.d.ts +9 -2
  17. package/dist/shared/{tools/IconButton.svelte → IconButton.svelte} +19 -3
  18. package/dist/shared/{tools/IconButton.svelte.d.ts → IconButton.svelte.d.ts} +4 -1
  19. package/dist/shared/ImageEditor.svelte +572 -289
  20. package/dist/shared/ImageEditor.svelte.d.ts +54 -56
  21. package/dist/shared/Layers.svelte +260 -0
  22. package/dist/shared/Layers.svelte.d.ts +37 -0
  23. package/dist/shared/Resize.svelte +218 -0
  24. package/dist/shared/Resize.svelte.d.ts +29 -0
  25. package/dist/shared/SecondaryToolbar.svelte +55 -0
  26. package/dist/shared/SecondaryToolbar.svelte.d.ts +37 -0
  27. package/dist/shared/Toolbar.svelte +337 -0
  28. package/dist/shared/Toolbar.svelte.d.ts +42 -0
  29. package/dist/shared/{tools → brush}/BrushOptions.svelte +68 -64
  30. package/dist/shared/{tools → brush}/BrushOptions.svelte.d.ts +9 -9
  31. package/dist/shared/{tools → brush}/BrushSize.svelte +2 -27
  32. package/dist/shared/{tools → brush}/ColorField.svelte +2 -1
  33. package/dist/shared/{tools → brush}/ColorField.svelte.d.ts +1 -0
  34. package/dist/shared/{tools → brush}/ColorPicker.svelte +81 -9
  35. package/dist/shared/{tools → brush}/ColorPicker.svelte.d.ts +1 -0
  36. package/dist/shared/{tools → brush}/ColorSwatch.svelte +78 -38
  37. package/dist/shared/{tools → brush}/ColorSwatch.svelte.d.ts +6 -2
  38. package/dist/shared/brush/brush-cursor.d.ts +101 -0
  39. package/dist/shared/brush/brush-cursor.js +327 -0
  40. package/dist/shared/brush/brush-textures.d.ts +65 -0
  41. package/dist/shared/brush/brush-textures.js +385 -0
  42. package/dist/shared/brush/brush-utils.d.ts +32 -0
  43. package/dist/shared/brush/brush-utils.js +59 -0
  44. package/dist/shared/brush/brush.d.ts +152 -0
  45. package/dist/shared/brush/brush.js +397 -0
  46. package/dist/shared/brush/types.d.ts +37 -0
  47. package/dist/shared/brush/types.js +1 -0
  48. package/dist/shared/core/editor.d.ts +261 -0
  49. package/dist/shared/core/editor.js +1264 -0
  50. package/dist/shared/crop/crop.d.ts +201 -0
  51. package/dist/shared/crop/crop.js +727 -0
  52. package/dist/shared/image/image.d.ts +89 -0
  53. package/dist/shared/image/image.js +183 -0
  54. package/dist/shared/resize/resize.d.ts +270 -0
  55. package/dist/shared/resize/resize.js +1413 -0
  56. package/dist/shared/types.d.ts +16 -0
  57. package/dist/shared/utils/pixi.d.ts +16 -57
  58. package/dist/shared/utils/pixi.js +15 -113
  59. package/dist/shared/zoom/zoom.d.ts +136 -0
  60. package/dist/shared/zoom/zoom.js +548 -0
  61. package/package.json +8 -7
  62. package/shared/Anchor.svelte +384 -0
  63. package/shared/Controls.svelte +178 -37
  64. package/shared/{tools/IconButton.svelte → IconButton.svelte} +21 -4
  65. package/shared/ImageEditor.svelte +712 -350
  66. package/shared/Layers.svelte +282 -0
  67. package/shared/Resize.svelte +239 -0
  68. package/shared/SecondaryToolbar.svelte +67 -0
  69. package/shared/Toolbar.svelte +386 -0
  70. package/shared/brush/BRUSH_TOOL.md +134 -0
  71. package/shared/brush/BrushOptions.svelte +200 -0
  72. package/shared/{tools → brush}/BrushSize.svelte +2 -30
  73. package/shared/{tools → brush}/ColorField.svelte +3 -1
  74. package/shared/{tools → brush}/ColorPicker.svelte +86 -9
  75. package/shared/{tools → brush}/ColorSwatch.svelte +95 -42
  76. package/shared/brush/brush-cursor.ts +408 -0
  77. package/shared/brush/brush-textures.ts +485 -0
  78. package/shared/brush/brush-utils.ts +76 -0
  79. package/shared/brush/brush.ts +510 -0
  80. package/shared/brush/types.ts +41 -0
  81. package/shared/core/EDITOR.md +182 -0
  82. package/shared/core/editor.ts +1788 -0
  83. package/shared/crop/CROP.md +139 -0
  84. package/shared/crop/crop.ts +1027 -0
  85. package/shared/image/IMAGE.md +135 -0
  86. package/shared/image/image.ts +298 -0
  87. package/shared/resize/resize.ts +1894 -0
  88. package/shared/types.ts +19 -0
  89. package/shared/utils/pixi.ts +19 -246
  90. package/shared/zoom/zoom.ts +729 -0
  91. package/dist/shared/layers/Layers.svelte +0 -231
  92. package/dist/shared/layers/Layers.svelte.d.ts +0 -18
  93. package/dist/shared/layers/utils.d.ts +0 -73
  94. package/dist/shared/layers/utils.js +0 -156
  95. package/dist/shared/tools/Brush.svelte +0 -215
  96. package/dist/shared/tools/Brush.svelte.d.ts +0 -29
  97. package/dist/shared/tools/Crop.svelte +0 -155
  98. package/dist/shared/tools/Crop.svelte.d.ts +0 -16
  99. package/dist/shared/tools/Cropper.svelte +0 -271
  100. package/dist/shared/tools/Cropper.svelte.d.ts +0 -41
  101. package/dist/shared/tools/Handle.svelte +0 -260
  102. package/dist/shared/tools/Handle.svelte.d.ts +0 -29
  103. package/dist/shared/tools/Sources.svelte +0 -257
  104. package/dist/shared/tools/Sources.svelte.d.ts +0 -34
  105. package/dist/shared/tools/Tools.svelte +0 -102
  106. package/dist/shared/tools/Tools.svelte.d.ts +0 -48
  107. package/dist/shared/tools/brush.d.ts +0 -62
  108. package/dist/shared/tools/brush.js +0 -133
  109. package/dist/shared/tools/crop.d.ts +0 -15
  110. package/dist/shared/tools/crop.js +0 -154
  111. package/dist/shared/tools/index.d.ts +0 -5
  112. package/dist/shared/tools/index.js +0 -5
  113. package/dist/shared/tools/sources.d.ts +0 -46
  114. package/dist/shared/tools/sources.js +0 -121
  115. package/dist/shared/tools/types.d.ts +0 -1
  116. package/shared/layers/Layers.svelte +0 -250
  117. package/shared/layers/utils.ts +0 -294
  118. package/shared/tools/Brush.svelte +0 -280
  119. package/shared/tools/BrushOptions.svelte +0 -180
  120. package/shared/tools/Crop.svelte +0 -189
  121. package/shared/tools/Cropper.svelte +0 -346
  122. package/shared/tools/Handle.svelte +0 -289
  123. package/shared/tools/Sources.svelte +0 -283
  124. package/shared/tools/Tools.svelte +0 -162
  125. package/shared/tools/brush.ts +0 -247
  126. package/shared/tools/crop.ts +0 -213
  127. package/shared/tools/index.ts +0 -5
  128. package/shared/tools/sources.ts +0 -195
  129. package/shared/tools/types.ts +0 -1
  130. /package/dist/shared/{tools → brush}/BrushSize.svelte.d.ts +0 -0
  131. /package/dist/shared/{tools/types.js → types.js} +0 -0
@@ -0,0 +1,139 @@
1
+ # Crop Tool Documentation
2
+
3
+ ## Overview
4
+
5
+ The crop tool is a component of the image editor that allows users to select a portion of an image to keep while discarding the rest. It provides an interactive UI with handles that can be dragged to adjust the crop area.
6
+
7
+ ## File Structure
8
+
9
+ - `js/imageeditor/shared/crop/crop.ts` - Main implementation of the crop tool
10
+ - `js/imageeditor/shared/core/editor.ts` - Defines the `Tool` interface and `ImageEditorContext`
11
+ - `js/imageeditor/shared/Toolbar.svelte` - Defines the tool types and subtool types
12
+
13
+ ## Implementation Details
14
+
15
+ ### Class: `CropTool`
16
+
17
+ The `CropTool` class implements the `Tool` interface defined in `editor.ts`. It provides the following functionality:
18
+
19
+ - Interactive crop area with draggable corners and edges
20
+ - Visual feedback with a mask that shows only the selected area
21
+ - Ability to move the entire crop window
22
+ - Constraints to keep the crop area within the image bounds
23
+
24
+ ### Key Components
25
+
26
+ #### State Management
27
+
28
+ The crop tool maintains several state variables:
29
+
30
+ - `crop_bounds` - Stores the x, y, width, and height of the crop area
31
+ - `is_dragging` - Tracks if a handle is being dragged
32
+ - `is_dragging_window` - Tracks if the entire crop window is being dragged
33
+ - `selected_handle` - Reference to the currently selected handle
34
+ - `active_corner_index` - Index of the active corner (-1 if none)
35
+ - `active_edge_index` - Index of the active edge (-1 if none)
36
+
37
+ #### Visual Elements
38
+
39
+ The crop tool creates several visual elements:
40
+
41
+ - `crop_ui_container` - Container for all UI elements
42
+ - `crop_mask` - Graphics object used to mask the image
43
+ - Corner handles - L-shaped handles at each corner
44
+ - Edge handles - Bar-shaped handles at the middle of each edge
45
+
46
+ #### Event Handling
47
+
48
+ The tool sets up event listeners for:
49
+
50
+ - `pointerdown` - Start dragging a handle or the window
51
+ - `pointermove` - Update crop bounds during dragging
52
+ - `pointerup` - End dragging operations
53
+
54
+ ### Key Methods
55
+
56
+ #### Setup and Initialization
57
+
58
+ - `setup(context, tool, subtool)` - Initializes the tool with the editor context
59
+ - `init_crop_ui()` - Creates the UI elements
60
+ - `set_crop_mask()` - Sets up the mask for the image
61
+
62
+ #### UI Creation
63
+
64
+ - `make_crop_ui(width, height)` - Creates the crop UI container
65
+ - `create_handle(is_edge)` - Creates a handle (corner or edge)
66
+ - `create_corner_handles(container, width, height)` - Creates the corner handles
67
+ - `create_edge_handles(container, width, height)` - Creates the edge handles
68
+
69
+ #### Event Handlers
70
+
71
+ - `handle_pointer_down(event, handle, corner_index, edge_index)` - Handles pointer down events
72
+ - `handle_pointer_move(event)` - Handles pointer move events
73
+ - `handle_pointer_up()` - Handles pointer up events
74
+ - `handle_window_drag_start(event)` - Handles the start of window dragging
75
+
76
+ #### Update Methods
77
+
78
+ - `update_crop_bounds(delta)` - Updates crop bounds based on pointer movement
79
+ - `constrain_crop_bounds()` - Ensures crop bounds stay within image dimensions
80
+ - `update_crop_mask()` - Updates the mask graphics
81
+ - `update_crop_ui()` - Updates the crop UI position and dimensions
82
+ - `update_handle_positions(width, height)` - Updates handle positions
83
+
84
+ ### Integration with Editor
85
+
86
+ The crop tool integrates with the editor through the `ImageEditorContext` interface, which provides:
87
+
88
+ - `app` - The PIXI.js Application instance
89
+ - `image_container` - The container holding the image
90
+ - `dimensions` - A readable store with the image dimensions
91
+ - `position` - A readable store with the image position
92
+ - `scale` - A readable store with the image scale
93
+
94
+ ## Usage Flow
95
+
96
+ 1. The user selects the "crop" subtool from the toolbar
97
+ 2. The crop tool initializes with a crop area matching the full image
98
+ 3. The user can:
99
+ - Drag corners to resize from that corner
100
+ - Drag edges to resize from that edge
101
+ - Drag the center area to move the entire crop window
102
+ 4. The image is masked to show only the selected area
103
+ 5. When the user applies the crop, the image is cropped to the selected area
104
+
105
+ ## Implementation Notes
106
+
107
+ ### Masking Technique
108
+
109
+ The crop tool uses a PIXI.js mask with alpha=0 to make the mask invisible while still functioning as a mask. This prevents the white background from appearing in the masked area.
110
+
111
+ ### Scaling Considerations
112
+
113
+ The tool handles scaling by:
114
+ - Storing crop bounds in image coordinates (unscaled)
115
+ - Applying scale when positioning UI elements
116
+ - Converting between global and local coordinates for pointer events
117
+
118
+ ### Constraints
119
+
120
+ The tool enforces several constraints:
121
+ - Minimum crop size of 20x20 pixels
122
+ - Crop area cannot extend beyond image boundaries
123
+ - Handles cannot be dragged beyond valid positions
124
+
125
+ ## Maintenance Notes
126
+
127
+ When modifying the crop tool, consider:
128
+
129
+ 1. **Event Handling**: Ensure proper event propagation and stopping
130
+ 2. **Coordinate Systems**: Be careful with conversions between global and local coordinates
131
+ 3. **Scale Handling**: Account for image scaling in all calculations
132
+ 4. **Performance**: Minimize unnecessary updates to the mask and UI
133
+ 5. **Edge Cases**: Test with extreme crop sizes and positions
134
+
135
+ ## Related Components
136
+
137
+ - **Toolbar**: Controls tool selection
138
+ - **ImageEditor**: Provides the context and manages the overall editor state
139
+ - **LayerManager**: Manages image layers that the crop tool operates on