js-draw 0.18.2 → 0.20.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 (269) hide show
  1. package/.eslintrc.js +1 -0
  2. package/CHANGELOG.md +10 -0
  3. package/dist/bundle.js +2 -2
  4. package/dist/bundledStyles.js +1 -0
  5. package/dist/cjs/src/Color4.d.ts +8 -0
  6. package/dist/cjs/src/Color4.js +67 -0
  7. package/dist/cjs/src/Editor.d.ts +2 -2
  8. package/dist/cjs/src/Editor.js +7 -7
  9. package/dist/cjs/src/SVGLoader.js +77 -12
  10. package/dist/cjs/src/Viewport.d.ts +2 -0
  11. package/dist/cjs/src/Viewport.js +6 -2
  12. package/dist/cjs/src/components/AbstractComponent.d.ts +2 -2
  13. package/dist/cjs/src/components/AbstractComponent.js +3 -3
  14. package/dist/cjs/src/components/{ImageBackground.d.ts → BackgroundComponent.d.ts} +23 -3
  15. package/dist/cjs/src/components/BackgroundComponent.js +309 -0
  16. package/dist/cjs/src/components/RestylableComponent.d.ts +21 -2
  17. package/dist/cjs/src/components/Stroke.d.ts +35 -0
  18. package/dist/cjs/src/components/Stroke.js +37 -3
  19. package/dist/cjs/src/components/TextComponent.d.ts +27 -17
  20. package/dist/cjs/src/components/TextComponent.js +23 -1
  21. package/dist/cjs/src/components/lib.d.ts +4 -3
  22. package/dist/cjs/src/components/lib.js +2 -2
  23. package/dist/cjs/src/components/util/StrokeSmoother.js +25 -15
  24. package/dist/cjs/src/lib.d.ts +30 -0
  25. package/dist/cjs/src/lib.js +30 -0
  26. package/dist/cjs/src/localizations/de.js +1 -1
  27. package/dist/cjs/src/localizations/es.js +1 -1
  28. package/dist/cjs/src/math/Path.js +1 -1
  29. package/dist/cjs/src/math/polynomial/QuadraticBezier.d.ts +28 -0
  30. package/dist/cjs/src/math/polynomial/QuadraticBezier.js +115 -0
  31. package/dist/cjs/src/math/polynomial/solveQuadratic.d.ts +6 -0
  32. package/dist/cjs/src/math/polynomial/solveQuadratic.js +36 -0
  33. package/dist/cjs/src/rendering/RenderingStyle.d.ts +4 -4
  34. package/dist/cjs/src/rendering/TextRenderingStyle.d.ts +10 -10
  35. package/dist/cjs/src/rendering/lib.d.ts +2 -0
  36. package/dist/cjs/src/rendering/renderers/AbstractRenderer.d.ts +2 -2
  37. package/dist/cjs/src/rendering/renderers/CanvasRenderer.d.ts +2 -2
  38. package/dist/cjs/src/rendering/renderers/CanvasRenderer.js +5 -3
  39. package/dist/cjs/src/rendering/renderers/DummyRenderer.d.ts +2 -2
  40. package/dist/cjs/src/rendering/renderers/SVGRenderer.d.ts +2 -2
  41. package/dist/cjs/src/rendering/renderers/SVGRenderer.js +15 -6
  42. package/dist/cjs/src/rendering/renderers/TextOnlyRenderer.d.ts +2 -2
  43. package/dist/cjs/src/toolbar/IconProvider.d.ts +2 -2
  44. package/dist/cjs/src/toolbar/localization.d.ts +2 -1
  45. package/dist/cjs/src/toolbar/localization.js +3 -2
  46. package/dist/cjs/src/toolbar/widgets/BaseWidget.js +1 -1
  47. package/dist/cjs/src/toolbar/widgets/DocumentPropertiesWidget.d.ts +5 -0
  48. package/dist/cjs/src/toolbar/widgets/DocumentPropertiesWidget.js +77 -2
  49. package/dist/cjs/src/toolbar/widgets/PenToolWidget.js +1 -1
  50. package/dist/cjs/src/tools/FindTool.js +1 -1
  51. package/dist/cjs/src/tools/SoundUITool.d.ts +24 -0
  52. package/dist/cjs/src/tools/SoundUITool.js +164 -0
  53. package/dist/cjs/src/tools/TextTool.d.ts +2 -2
  54. package/dist/cjs/src/tools/ToolController.js +6 -1
  55. package/dist/cjs/src/tools/lib.d.ts +1 -0
  56. package/dist/cjs/src/tools/lib.js +3 -1
  57. package/dist/cjs/src/tools/localization.d.ts +3 -0
  58. package/dist/cjs/src/tools/localization.js +3 -0
  59. package/dist/mjs/src/Color4.d.ts +8 -0
  60. package/dist/mjs/src/Color4.mjs +64 -0
  61. package/dist/mjs/src/Editor.d.ts +2 -2
  62. package/dist/mjs/src/Editor.mjs +6 -6
  63. package/dist/mjs/src/SVGLoader.mjs +76 -11
  64. package/dist/mjs/src/Viewport.d.ts +2 -0
  65. package/dist/mjs/src/Viewport.mjs +6 -2
  66. package/dist/mjs/src/components/AbstractComponent.d.ts +2 -2
  67. package/dist/mjs/src/components/AbstractComponent.mjs +3 -3
  68. package/dist/mjs/src/components/{ImageBackground.d.ts → BackgroundComponent.d.ts} +23 -3
  69. package/dist/mjs/src/components/BackgroundComponent.mjs +279 -0
  70. package/dist/mjs/src/components/RestylableComponent.d.ts +21 -2
  71. package/dist/mjs/src/components/Stroke.d.ts +35 -0
  72. package/dist/mjs/src/components/Stroke.mjs +37 -3
  73. package/dist/mjs/src/components/TextComponent.d.ts +27 -17
  74. package/dist/mjs/src/components/TextComponent.mjs +23 -1
  75. package/dist/mjs/src/components/lib.d.ts +4 -3
  76. package/dist/mjs/src/components/lib.mjs +2 -2
  77. package/dist/mjs/src/components/util/StrokeSmoother.mjs +25 -15
  78. package/dist/mjs/src/lib.d.ts +30 -0
  79. package/dist/mjs/src/lib.mjs +30 -0
  80. package/dist/mjs/src/localizations/de.mjs +1 -1
  81. package/dist/mjs/src/localizations/es.mjs +1 -1
  82. package/dist/mjs/src/math/Path.mjs +1 -1
  83. package/dist/mjs/src/math/polynomial/QuadraticBezier.d.ts +28 -0
  84. package/dist/mjs/src/math/polynomial/QuadraticBezier.mjs +109 -0
  85. package/dist/mjs/src/math/polynomial/solveQuadratic.d.ts +6 -0
  86. package/dist/mjs/src/math/polynomial/solveQuadratic.mjs +34 -0
  87. package/dist/mjs/src/rendering/RenderingStyle.d.ts +4 -4
  88. package/dist/mjs/src/rendering/TextRenderingStyle.d.ts +10 -10
  89. package/dist/mjs/src/rendering/lib.d.ts +2 -0
  90. package/dist/mjs/src/rendering/renderers/AbstractRenderer.d.ts +2 -2
  91. package/dist/mjs/src/rendering/renderers/CanvasRenderer.d.ts +2 -2
  92. package/dist/mjs/src/rendering/renderers/CanvasRenderer.mjs +5 -3
  93. package/dist/mjs/src/rendering/renderers/DummyRenderer.d.ts +2 -2
  94. package/dist/mjs/src/rendering/renderers/SVGRenderer.d.ts +2 -2
  95. package/dist/mjs/src/rendering/renderers/SVGRenderer.mjs +15 -6
  96. package/dist/mjs/src/rendering/renderers/TextOnlyRenderer.d.ts +2 -2
  97. package/dist/mjs/src/toolbar/IconProvider.d.ts +2 -2
  98. package/dist/mjs/src/toolbar/localization.d.ts +2 -1
  99. package/dist/mjs/src/toolbar/localization.mjs +3 -2
  100. package/dist/mjs/src/toolbar/widgets/BaseWidget.mjs +1 -1
  101. package/dist/mjs/src/toolbar/widgets/DocumentPropertiesWidget.d.ts +5 -0
  102. package/dist/mjs/src/toolbar/widgets/DocumentPropertiesWidget.mjs +54 -2
  103. package/dist/mjs/src/toolbar/widgets/PenToolWidget.mjs +1 -1
  104. package/dist/mjs/src/tools/FindTool.mjs +1 -1
  105. package/dist/mjs/src/tools/SoundUITool.d.ts +24 -0
  106. package/dist/mjs/src/tools/SoundUITool.mjs +158 -0
  107. package/dist/mjs/src/tools/TextTool.d.ts +2 -2
  108. package/dist/mjs/src/tools/ToolController.mjs +6 -1
  109. package/dist/mjs/src/tools/lib.d.ts +1 -0
  110. package/dist/mjs/src/tools/lib.mjs +1 -0
  111. package/dist/mjs/src/tools/localization.d.ts +3 -0
  112. package/dist/mjs/src/tools/localization.mjs +3 -0
  113. package/jest.config.js +1 -1
  114. package/package.json +19 -17
  115. package/src/Editor.css +2 -2
  116. package/src/tools/SoundUITool.css +15 -0
  117. package/src/tools/tools.css +4 -0
  118. package/dist/cjs/src/components/ImageBackground.js +0 -146
  119. package/dist/mjs/src/components/ImageBackground.mjs +0 -139
  120. package/src/Color4.test.ts +0 -40
  121. package/src/Color4.ts +0 -236
  122. package/src/Editor.loadFrom.test.ts +0 -24
  123. package/src/Editor.toSVG.test.ts +0 -111
  124. package/src/Editor.ts +0 -1122
  125. package/src/EditorImage.test.ts +0 -120
  126. package/src/EditorImage.ts +0 -603
  127. package/src/EventDispatcher.test.ts +0 -123
  128. package/src/EventDispatcher.ts +0 -71
  129. package/src/Pointer.ts +0 -127
  130. package/src/SVGLoader.test.ts +0 -114
  131. package/src/SVGLoader.ts +0 -511
  132. package/src/UndoRedoHistory.test.ts +0 -33
  133. package/src/UndoRedoHistory.ts +0 -102
  134. package/src/Viewport.ts +0 -319
  135. package/src/bundle/bundled.ts +0 -7
  136. package/src/commands/Command.ts +0 -45
  137. package/src/commands/Duplicate.ts +0 -48
  138. package/src/commands/Erase.ts +0 -74
  139. package/src/commands/SerializableCommand.ts +0 -49
  140. package/src/commands/UnresolvedCommand.ts +0 -37
  141. package/src/commands/invertCommand.ts +0 -51
  142. package/src/commands/lib.ts +0 -16
  143. package/src/commands/localization.ts +0 -47
  144. package/src/commands/uniteCommands.test.ts +0 -23
  145. package/src/commands/uniteCommands.ts +0 -135
  146. package/src/components/AbstractComponent.transformBy.test.ts +0 -22
  147. package/src/components/AbstractComponent.ts +0 -364
  148. package/src/components/ImageBackground.test.ts +0 -35
  149. package/src/components/ImageBackground.ts +0 -176
  150. package/src/components/ImageComponent.ts +0 -171
  151. package/src/components/RestylableComponent.ts +0 -142
  152. package/src/components/SVGGlobalAttributesObject.ts +0 -81
  153. package/src/components/Stroke.test.ts +0 -139
  154. package/src/components/Stroke.ts +0 -245
  155. package/src/components/TextComponent.test.ts +0 -99
  156. package/src/components/TextComponent.ts +0 -315
  157. package/src/components/UnknownSVGObject.test.ts +0 -10
  158. package/src/components/UnknownSVGObject.ts +0 -60
  159. package/src/components/builders/ArrowBuilder.ts +0 -107
  160. package/src/components/builders/FreehandLineBuilder.ts +0 -212
  161. package/src/components/builders/LineBuilder.ts +0 -77
  162. package/src/components/builders/PressureSensitiveFreehandLineBuilder.ts +0 -454
  163. package/src/components/builders/RectangleBuilder.ts +0 -74
  164. package/src/components/builders/types.ts +0 -15
  165. package/src/components/lib.ts +0 -25
  166. package/src/components/localization.ts +0 -22
  167. package/src/components/util/StrokeSmoother.ts +0 -293
  168. package/src/components/util/describeComponentList.ts +0 -18
  169. package/src/lib.ts +0 -37
  170. package/src/localization.ts +0 -34
  171. package/src/localizations/de.ts +0 -98
  172. package/src/localizations/en.ts +0 -8
  173. package/src/localizations/es.ts +0 -74
  174. package/src/localizations/getLocalizationTable.test.ts +0 -27
  175. package/src/localizations/getLocalizationTable.ts +0 -55
  176. package/src/math/LineSegment2.test.ts +0 -99
  177. package/src/math/LineSegment2.ts +0 -160
  178. package/src/math/Mat33.test.ts +0 -244
  179. package/src/math/Mat33.ts +0 -437
  180. package/src/math/Path.fromString.test.ts +0 -223
  181. package/src/math/Path.test.ts +0 -198
  182. package/src/math/Path.toString.test.ts +0 -77
  183. package/src/math/Path.ts +0 -790
  184. package/src/math/Rect2.test.ts +0 -204
  185. package/src/math/Rect2.ts +0 -315
  186. package/src/math/Triangle.ts +0 -29
  187. package/src/math/Vec2.test.ts +0 -30
  188. package/src/math/Vec2.ts +0 -18
  189. package/src/math/Vec3.test.ts +0 -44
  190. package/src/math/Vec3.ts +0 -218
  191. package/src/math/lib.ts +0 -15
  192. package/src/math/rounding.test.ts +0 -65
  193. package/src/math/rounding.ts +0 -156
  194. package/src/rendering/Display.ts +0 -249
  195. package/src/rendering/RenderingStyle.test.ts +0 -68
  196. package/src/rendering/RenderingStyle.ts +0 -55
  197. package/src/rendering/TextRenderingStyle.ts +0 -45
  198. package/src/rendering/caching/CacheRecord.test.ts +0 -49
  199. package/src/rendering/caching/CacheRecord.ts +0 -77
  200. package/src/rendering/caching/CacheRecordManager.ts +0 -71
  201. package/src/rendering/caching/RenderingCache.test.ts +0 -44
  202. package/src/rendering/caching/RenderingCache.ts +0 -66
  203. package/src/rendering/caching/RenderingCacheNode.ts +0 -405
  204. package/src/rendering/caching/testUtils.ts +0 -35
  205. package/src/rendering/caching/types.ts +0 -34
  206. package/src/rendering/lib.ts +0 -6
  207. package/src/rendering/localization.ts +0 -20
  208. package/src/rendering/renderers/AbstractRenderer.ts +0 -222
  209. package/src/rendering/renderers/CanvasRenderer.ts +0 -296
  210. package/src/rendering/renderers/DummyRenderer.test.ts +0 -42
  211. package/src/rendering/renderers/DummyRenderer.ts +0 -136
  212. package/src/rendering/renderers/SVGRenderer.ts +0 -354
  213. package/src/rendering/renderers/TextOnlyRenderer.ts +0 -70
  214. package/src/testing/beforeEachFile.ts +0 -8
  215. package/src/testing/createEditor.ts +0 -11
  216. package/src/testing/global.d.ts +0 -17
  217. package/src/testing/lib.ts +0 -3
  218. package/src/testing/loadExpectExtensions.ts +0 -25
  219. package/src/testing/sendPenEvent.ts +0 -31
  220. package/src/testing/sendTouchEvent.ts +0 -78
  221. package/src/toolbar/HTMLToolbar.ts +0 -492
  222. package/src/toolbar/IconProvider.ts +0 -736
  223. package/src/toolbar/lib.ts +0 -4
  224. package/src/toolbar/localization.ts +0 -106
  225. package/src/toolbar/makeColorInput.ts +0 -145
  226. package/src/toolbar/types.ts +0 -5
  227. package/src/toolbar/widgets/ActionButtonWidget.ts +0 -39
  228. package/src/toolbar/widgets/BaseToolWidget.ts +0 -56
  229. package/src/toolbar/widgets/BaseWidget.ts +0 -377
  230. package/src/toolbar/widgets/DocumentPropertiesWidget.ts +0 -167
  231. package/src/toolbar/widgets/EraserToolWidget.ts +0 -85
  232. package/src/toolbar/widgets/HandToolWidget.ts +0 -250
  233. package/src/toolbar/widgets/InsertImageWidget.ts +0 -223
  234. package/src/toolbar/widgets/OverflowWidget.ts +0 -92
  235. package/src/toolbar/widgets/PenToolWidget.ts +0 -288
  236. package/src/toolbar/widgets/SelectionToolWidget.ts +0 -190
  237. package/src/toolbar/widgets/TextToolWidget.ts +0 -145
  238. package/src/toolbar/widgets/lib.ts +0 -13
  239. package/src/tools/BaseTool.ts +0 -76
  240. package/src/tools/Eraser.test.ts +0 -103
  241. package/src/tools/Eraser.ts +0 -139
  242. package/src/tools/FindTool.ts +0 -152
  243. package/src/tools/PanZoom.test.ts +0 -310
  244. package/src/tools/PanZoom.ts +0 -520
  245. package/src/tools/PasteHandler.ts +0 -95
  246. package/src/tools/Pen.test.ts +0 -194
  247. package/src/tools/Pen.ts +0 -226
  248. package/src/tools/PipetteTool.ts +0 -55
  249. package/src/tools/SelectionTool/SelectAllShortcutHandler.ts +0 -28
  250. package/src/tools/SelectionTool/Selection.ts +0 -607
  251. package/src/tools/SelectionTool/SelectionHandle.ts +0 -108
  252. package/src/tools/SelectionTool/SelectionTool.test.ts +0 -261
  253. package/src/tools/SelectionTool/SelectionTool.ts +0 -480
  254. package/src/tools/SelectionTool/TransformMode.ts +0 -114
  255. package/src/tools/SelectionTool/types.ts +0 -11
  256. package/src/tools/TextTool.ts +0 -326
  257. package/src/tools/ToolController.ts +0 -178
  258. package/src/tools/ToolEnabledGroup.ts +0 -14
  259. package/src/tools/ToolSwitcherShortcut.ts +0 -39
  260. package/src/tools/ToolbarShortcutHandler.ts +0 -34
  261. package/src/tools/UndoRedoShortcut.test.ts +0 -56
  262. package/src/tools/UndoRedoShortcut.ts +0 -25
  263. package/src/tools/lib.ts +0 -21
  264. package/src/tools/localization.ts +0 -66
  265. package/src/types.ts +0 -234
  266. package/src/util/assertions.ts +0 -55
  267. package/src/util/fileToBase64.ts +0 -18
  268. package/src/util/untilNextAnimationFrame.ts +0 -9
  269. package/src/util/waitForTimeout.ts +0 -9
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "js-draw",
3
- "version": "0.18.2",
3
+ "version": "0.20.0",
4
4
  "description": "Draw pictures using a pen, touchscreen, or mouse! JS-draw is a drawing library for JavaScript and TypeScript. ",
5
- "types": "./dist/cjs/src/lib.d.ts",
5
+ "types": "./dist/mjs/src/lib.d.ts",
6
6
  "main": "./dist/cjs/src/lib.js",
7
7
  "exports": {
8
8
  ".": {
9
- "types": "./dist/cjs/src/lib.d.ts",
9
+ "types": "./dist/mjs/src/lib.d.ts",
10
10
  "import": "./dist/mjs/src/lib.mjs",
11
11
  "require": "./dist/cjs/src/lib.js"
12
12
  },
@@ -20,6 +20,9 @@
20
20
  "types": "./dist/mjs/src/bundle/bundled.d.ts",
21
21
  "default": "./dist/bundle.js"
22
22
  },
23
+ "./bundledStyles": {
24
+ "default": "./dist/bundledStyles.js"
25
+ },
23
26
  "./Editor": {
24
27
  "types": "./dist/mjs/src/Editor.d.ts",
25
28
  "default": "./dist/mjs/src/Editor.mjs"
@@ -79,33 +82,32 @@
79
82
  "postpack": "pinst --enable"
80
83
  },
81
84
  "dependencies": {
82
- "@babel/runtime": "^7.21.0",
83
85
  "@melloware/coloris": "^0.18.0",
84
86
  "bezier-js": "^6.1.3"
85
87
  },
86
88
  "devDependencies": {
87
89
  "@types/bezier-js": "^4.1.0",
88
- "@types/jest": "^29.4.0",
90
+ "@types/jest": "^29.4.2",
89
91
  "@types/jsdom": "^21.1.0",
90
- "@types/node": "^18.14.0",
91
- "@typescript-eslint/eslint-plugin": "^5.53.0",
92
- "@typescript-eslint/parser": "^5.53.0",
92
+ "@types/node": "^18.15.3",
93
+ "@typescript-eslint/eslint-plugin": "^5.55.0",
94
+ "@typescript-eslint/parser": "^5.55.0",
93
95
  "css-loader": "^6.7.3",
94
- "eslint": "^8.34.0",
96
+ "eslint": "^8.36.0",
95
97
  "husky": "^8.0.3",
96
- "jest": "^29.4.3",
97
- "jest-environment-jsdom": "^29.4.3",
98
- "jsdom": "^21.1.0",
99
- "lint-staged": "^13.1.2",
98
+ "jest": "^29.5.0",
99
+ "jest-environment-jsdom": "^29.5.0",
100
+ "jsdom": "^21.1.1",
101
+ "lint-staged": "^13.2.0",
100
102
  "pinst": "^3.0.0",
101
- "style-loader": "^3.3.1",
102
- "terser-webpack-plugin": "^5.3.6",
103
+ "style-loader": "^3.3.2",
104
+ "terser-webpack-plugin": "^5.3.7",
103
105
  "ts-jest": "^29.0.5",
104
106
  "ts-loader": "^9.4.2",
105
107
  "ts-node": "^10.9.1",
106
- "typedoc": "^0.23.25",
108
+ "typedoc": "^0.23.28",
107
109
  "typescript": "^4.9.5",
108
- "webpack": "^5.75.0"
110
+ "webpack": "^5.76.1"
109
111
  },
110
112
  "bugs": {
111
113
  "url": "https://github.com/personalizedrefrigerator/js-draw/issues"
package/src/Editor.css CHANGED
@@ -1,7 +1,6 @@
1
1
 
2
2
  @import url('./toolbar/toolbar.css');
3
- @import url('./tools/SelectionTool/SelectionTool.css');
4
- @import url('./tools/FindTool.css');
3
+ @import url('./tools/tools.css');
5
4
 
6
5
  .imageEditorContainer {
7
6
  /* Deafult colors for the editor */
@@ -83,4 +82,5 @@
83
82
 
84
83
  .imageEditorContainer .textRendererOutputContainer:focus-within {
85
84
  overflow: visible;
85
+ z-index: 5;
86
86
  }
@@ -0,0 +1,15 @@
1
+
2
+ .js-draw-sound-ui-toggle {
3
+ width: 0px;
4
+ height: 0px;
5
+ overflow: hidden;
6
+ }
7
+
8
+ .js-draw-sound-ui-toggle:focus-within, .js-draw-sound-ui-toggle.sound-ui-tool-enabled {
9
+ overflow: visible;
10
+ z-index: 5;
11
+ }
12
+
13
+ .js-draw-sound-ui-toggle:not(:focus-within):not(:hover).sound-ui-tool-enabled {
14
+ opacity: 0.5;
15
+ }
@@ -0,0 +1,4 @@
1
+
2
+ @import url(./SelectionTool/SelectionTool.css);
3
+ @import url(./FindTool.css);
4
+ @import url(./SoundUITool.css);
@@ -1,146 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.imageBackgroundCSSClassName = exports.BackgroundType = void 0;
7
- const Color4_1 = __importDefault(require("../Color4"));
8
- const EditorImage_1 = require("../EditorImage");
9
- const Rect2_1 = __importDefault(require("../math/Rect2"));
10
- const AbstractComponent_1 = __importDefault(require("./AbstractComponent"));
11
- const RestylableComponent_1 = require("./RestylableComponent");
12
- var BackgroundType;
13
- (function (BackgroundType) {
14
- BackgroundType[BackgroundType["SolidColor"] = 0] = "SolidColor";
15
- BackgroundType[BackgroundType["None"] = 1] = "None";
16
- })(BackgroundType = exports.BackgroundType || (exports.BackgroundType = {}));
17
- exports.imageBackgroundCSSClassName = 'js-draw-image-background';
18
- // Represents the background of an image in the editor.
19
- class ImageBackground extends AbstractComponent_1.default {
20
- constructor(backgroundType, mainColor) {
21
- super('image-background', 0);
22
- this.backgroundType = backgroundType;
23
- this.mainColor = mainColor;
24
- this.viewportSizeChangeListener = null;
25
- // eslint-disable-next-line @typescript-eslint/prefer-as-const
26
- this.isRestylableComponent = true;
27
- this.contentBBox = Rect2_1.default.empty;
28
- }
29
- getStyle() {
30
- let color = this.mainColor;
31
- if (this.backgroundType === BackgroundType.None) {
32
- color = undefined;
33
- }
34
- return {
35
- color,
36
- };
37
- }
38
- updateStyle(style) {
39
- return (0, RestylableComponent_1.createRestyleComponentCommand)(this.getStyle(), style, this);
40
- }
41
- // @internal
42
- forceStyle(style, editor) {
43
- const fill = style.color;
44
- if (!fill) {
45
- return;
46
- }
47
- this.mainColor = fill;
48
- if (fill.eq(Color4_1.default.transparent)) {
49
- this.backgroundType = BackgroundType.None;
50
- }
51
- else {
52
- this.backgroundType = BackgroundType.SolidColor;
53
- }
54
- if (editor) {
55
- editor.image.queueRerenderOf(this);
56
- editor.queueRerender();
57
- }
58
- }
59
- onAddToImage(image) {
60
- if (this.viewportSizeChangeListener) {
61
- console.warn('onAddToImage called when background is already in an image');
62
- this.onRemoveFromImage();
63
- }
64
- this.viewportSizeChangeListener = image.notifier.on(EditorImage_1.EditorImageEventType.ExportViewportChanged, () => {
65
- this.recomputeBBox(image);
66
- });
67
- this.recomputeBBox(image);
68
- }
69
- onRemoveFromImage() {
70
- var _a;
71
- (_a = this.viewportSizeChangeListener) === null || _a === void 0 ? void 0 : _a.remove();
72
- this.viewportSizeChangeListener = null;
73
- }
74
- recomputeBBox(image) {
75
- const importExportRect = image.getImportExportViewport().visibleRect;
76
- if (!this.contentBBox.eq(importExportRect)) {
77
- this.contentBBox = importExportRect;
78
- // Re-render this if already added to the EditorImage.
79
- image.queueRerenderOf(this);
80
- }
81
- }
82
- render(canvas, visibleRect) {
83
- if (this.backgroundType === BackgroundType.None) {
84
- return;
85
- }
86
- canvas.startObject(this.contentBBox);
87
- if (this.backgroundType === BackgroundType.SolidColor) {
88
- // If the rectangle for this region contains the visible rect,
89
- // we can fill the entire visible rectangle (which may be more efficient than
90
- // filling the entire region for this.)
91
- if (visibleRect) {
92
- const intersection = visibleRect.intersection(this.contentBBox);
93
- if (intersection) {
94
- canvas.fillRect(intersection, this.mainColor);
95
- }
96
- }
97
- else {
98
- canvas.fillRect(this.contentBBox, this.mainColor);
99
- }
100
- }
101
- canvas.endObject(this.getLoadSaveData(), [exports.imageBackgroundCSSClassName]);
102
- }
103
- intersects(lineSegment) {
104
- return this.contentBBox.getEdges().some(edge => edge.intersects(lineSegment));
105
- }
106
- isSelectable() {
107
- return false;
108
- }
109
- isBackground() {
110
- return true;
111
- }
112
- serializeToJSON() {
113
- return {
114
- mainColor: this.mainColor.toHexString(),
115
- backgroundType: this.backgroundType,
116
- };
117
- }
118
- applyTransformation(_affineTransfm) {
119
- // Do nothing — it doesn't make sense to transform the background.
120
- }
121
- description(localizationTable) {
122
- if (this.backgroundType === BackgroundType.SolidColor) {
123
- return localizationTable.filledBackgroundWithColor(this.mainColor.toString());
124
- }
125
- else {
126
- return localizationTable.emptyBackground;
127
- }
128
- }
129
- createClone() {
130
- return new ImageBackground(this.backgroundType, this.mainColor);
131
- }
132
- // @internal
133
- static deserializeFromJSON(json) {
134
- if (typeof json === 'string') {
135
- json = JSON.parse(json);
136
- }
137
- if (typeof json.mainColor !== 'string') {
138
- throw new Error('Error deserializing — mainColor must be of type string.');
139
- }
140
- const backgroundType = json.backgroundType === BackgroundType.SolidColor ? BackgroundType.SolidColor : BackgroundType.None;
141
- const mainColor = Color4_1.default.fromHex(json.mainColor);
142
- return new ImageBackground(backgroundType, mainColor);
143
- }
144
- }
145
- exports.default = ImageBackground;
146
- AbstractComponent_1.default.registerComponent('image-background', ImageBackground.deserializeFromJSON);
@@ -1,139 +0,0 @@
1
- import Color4 from '../Color4.mjs';
2
- import { EditorImageEventType } from '../EditorImage.mjs';
3
- import Rect2 from '../math/Rect2.mjs';
4
- import AbstractComponent from './AbstractComponent.mjs';
5
- import { createRestyleComponentCommand } from './RestylableComponent.mjs';
6
- export var BackgroundType;
7
- (function (BackgroundType) {
8
- BackgroundType[BackgroundType["SolidColor"] = 0] = "SolidColor";
9
- BackgroundType[BackgroundType["None"] = 1] = "None";
10
- })(BackgroundType || (BackgroundType = {}));
11
- export const imageBackgroundCSSClassName = 'js-draw-image-background';
12
- // Represents the background of an image in the editor.
13
- export default class ImageBackground extends AbstractComponent {
14
- constructor(backgroundType, mainColor) {
15
- super('image-background', 0);
16
- this.backgroundType = backgroundType;
17
- this.mainColor = mainColor;
18
- this.viewportSizeChangeListener = null;
19
- // eslint-disable-next-line @typescript-eslint/prefer-as-const
20
- this.isRestylableComponent = true;
21
- this.contentBBox = Rect2.empty;
22
- }
23
- getStyle() {
24
- let color = this.mainColor;
25
- if (this.backgroundType === BackgroundType.None) {
26
- color = undefined;
27
- }
28
- return {
29
- color,
30
- };
31
- }
32
- updateStyle(style) {
33
- return createRestyleComponentCommand(this.getStyle(), style, this);
34
- }
35
- // @internal
36
- forceStyle(style, editor) {
37
- const fill = style.color;
38
- if (!fill) {
39
- return;
40
- }
41
- this.mainColor = fill;
42
- if (fill.eq(Color4.transparent)) {
43
- this.backgroundType = BackgroundType.None;
44
- }
45
- else {
46
- this.backgroundType = BackgroundType.SolidColor;
47
- }
48
- if (editor) {
49
- editor.image.queueRerenderOf(this);
50
- editor.queueRerender();
51
- }
52
- }
53
- onAddToImage(image) {
54
- if (this.viewportSizeChangeListener) {
55
- console.warn('onAddToImage called when background is already in an image');
56
- this.onRemoveFromImage();
57
- }
58
- this.viewportSizeChangeListener = image.notifier.on(EditorImageEventType.ExportViewportChanged, () => {
59
- this.recomputeBBox(image);
60
- });
61
- this.recomputeBBox(image);
62
- }
63
- onRemoveFromImage() {
64
- var _a;
65
- (_a = this.viewportSizeChangeListener) === null || _a === void 0 ? void 0 : _a.remove();
66
- this.viewportSizeChangeListener = null;
67
- }
68
- recomputeBBox(image) {
69
- const importExportRect = image.getImportExportViewport().visibleRect;
70
- if (!this.contentBBox.eq(importExportRect)) {
71
- this.contentBBox = importExportRect;
72
- // Re-render this if already added to the EditorImage.
73
- image.queueRerenderOf(this);
74
- }
75
- }
76
- render(canvas, visibleRect) {
77
- if (this.backgroundType === BackgroundType.None) {
78
- return;
79
- }
80
- canvas.startObject(this.contentBBox);
81
- if (this.backgroundType === BackgroundType.SolidColor) {
82
- // If the rectangle for this region contains the visible rect,
83
- // we can fill the entire visible rectangle (which may be more efficient than
84
- // filling the entire region for this.)
85
- if (visibleRect) {
86
- const intersection = visibleRect.intersection(this.contentBBox);
87
- if (intersection) {
88
- canvas.fillRect(intersection, this.mainColor);
89
- }
90
- }
91
- else {
92
- canvas.fillRect(this.contentBBox, this.mainColor);
93
- }
94
- }
95
- canvas.endObject(this.getLoadSaveData(), [imageBackgroundCSSClassName]);
96
- }
97
- intersects(lineSegment) {
98
- return this.contentBBox.getEdges().some(edge => edge.intersects(lineSegment));
99
- }
100
- isSelectable() {
101
- return false;
102
- }
103
- isBackground() {
104
- return true;
105
- }
106
- serializeToJSON() {
107
- return {
108
- mainColor: this.mainColor.toHexString(),
109
- backgroundType: this.backgroundType,
110
- };
111
- }
112
- applyTransformation(_affineTransfm) {
113
- // Do nothing — it doesn't make sense to transform the background.
114
- }
115
- description(localizationTable) {
116
- if (this.backgroundType === BackgroundType.SolidColor) {
117
- return localizationTable.filledBackgroundWithColor(this.mainColor.toString());
118
- }
119
- else {
120
- return localizationTable.emptyBackground;
121
- }
122
- }
123
- createClone() {
124
- return new ImageBackground(this.backgroundType, this.mainColor);
125
- }
126
- // @internal
127
- static deserializeFromJSON(json) {
128
- if (typeof json === 'string') {
129
- json = JSON.parse(json);
130
- }
131
- if (typeof json.mainColor !== 'string') {
132
- throw new Error('Error deserializing — mainColor must be of type string.');
133
- }
134
- const backgroundType = json.backgroundType === BackgroundType.SolidColor ? BackgroundType.SolidColor : BackgroundType.None;
135
- const mainColor = Color4.fromHex(json.mainColor);
136
- return new ImageBackground(backgroundType, mainColor);
137
- }
138
- }
139
- AbstractComponent.registerComponent('image-background', ImageBackground.deserializeFromJSON);
@@ -1,40 +0,0 @@
1
- import Color4 from './Color4';
2
-
3
- describe('Color4', () => {
4
- it('should convert to #RRGGBB-format hex strings (when no alpha)', () => {
5
- expect(Color4.black.toHexString()).toBe('#000000');
6
- expect(Color4.fromHex('#f0f').toHexString()).toBe('#f000f0');
7
- });
8
-
9
- it('should create #RRGGBBAA-format hex strings when there is an alpha component', () => {
10
- expect(Color4.ofRGBA(1, 1, 1, 0.5).toHexString()).toBe('#ffffff80');
11
- });
12
-
13
- it('should parse rgb and rgba-format strings', () => {
14
- expect(Color4.fromString('rgb(0, 0, 0)')).objEq(Color4.black);
15
- expect(Color4.fromString('rgb ( 255, 0,\t 0)')).objEq(Color4.ofRGBA(1, 0, 0, 1));
16
- expect(Color4.fromString('rgba ( 255, 0,\t 0, 0.5)')).objEq(Color4.ofRGBA(1, 0, 0, 0.5));
17
- expect(Color4.fromString('rgba( 0, 0, 128, 0)')).objEq(Color4.ofRGBA(0, 0, 128/255, 0));
18
- });
19
-
20
- it('should mix blue and red to get dark purple', () => {
21
- expect(Color4.ofRGB(1, 0, 0).mix(Color4.ofRGB(0, 0, 1), 0.5)).objEq(Color4.ofRGB(0.5, 0, 0.5));
22
- expect(Color4.ofRGB(1, 0, 0).mix(Color4.ofRGB(0, 0, 1), 0.1)).objEq(Color4.ofRGB(0.9, 0, 0.1));
23
- });
24
-
25
- it('should mix red and green to get yellow', () => {
26
- expect(Color4.ofRGB(1, 0, 0).mix(Color4.ofRGB(0, 1, 0), 0.3)).objEq(
27
- Color4.ofRGB(0.7, 0.3, 0)
28
- );
29
- });
30
-
31
- it('should mix red with nothing and get red', () => {
32
- expect(Color4.average([ Color4.red ])).objEq(Color4.red);
33
- });
34
-
35
- it('different colors should be different', () => {
36
- expect(Color4.red.eq(Color4.red)).toBe(true);
37
- expect(Color4.red.eq(Color4.green)).toBe(false);
38
- expect(Color4.fromString('#ff000000').eq(Color4.transparent)).toBe(true);
39
- });
40
- });
package/src/Color4.ts DELETED
@@ -1,236 +0,0 @@
1
-
2
- export default class Color4 {
3
- private constructor(
4
- /** Red component. Should be in the range [0, 1]. */
5
- public readonly r: number,
6
-
7
- /** Green component. `g` ∈ [0, 1] */
8
- public readonly g: number,
9
-
10
- /** Blue component. `b` ∈ [0, 1] */
11
- public readonly b: number,
12
-
13
- /** Alpha/transparent component. `a` ∈ [0, 1]. 0 = transparent */
14
- public readonly a: number
15
- ) {
16
- }
17
-
18
- /**
19
- * Create a color from red, green, blue components. The color is fully opaque (`a = 1.0`).
20
- *
21
- * Each component should be in the range [0, 1].
22
- */
23
- public static ofRGB(red: number, green: number, blue: number): Color4 {
24
- return Color4.ofRGBA(red, green, blue, 1.0);
25
- }
26
-
27
- public static ofRGBA(red: number, green: number, blue: number, alpha: number): Color4 {
28
- red = Math.max(0, Math.min(red, 1));
29
- green = Math.max(0, Math.min(green, 1));
30
- blue = Math.max(0, Math.min(blue, 1));
31
- alpha = Math.max(0, Math.min(alpha, 1));
32
-
33
- return new Color4(red, green, blue, alpha);
34
- }
35
-
36
- public static fromHex(hexString: string): Color4 {
37
- // Remove starting '#' (if present)
38
- hexString = (hexString.match(/^[#]?(.*)$/) ?? [])[1];
39
- hexString = hexString.toUpperCase();
40
-
41
- if (!hexString.match(/^[0-9A-F]+$/)) {
42
- throw new Error(`${hexString} is not in a valid format.`);
43
- }
44
-
45
- // RGBA or RGB
46
- if (hexString.length === 3 || hexString.length === 4) {
47
- // Each character is a component
48
- const components = hexString.split('');
49
-
50
- // Convert to RRGGBBAA or RRGGBB format
51
- hexString = components.map(component => `${component}0`).join('');
52
- }
53
-
54
- if (hexString.length === 6) {
55
- // Alpha component
56
- hexString += 'FF';
57
- }
58
-
59
- const components: number[] = [];
60
- for (let i = 2; i <= hexString.length; i += 2) {
61
- const chunk = hexString.substring(i - 2, i);
62
- components.push(parseInt(chunk, 16) / 255);
63
- }
64
-
65
- if (components.length !== 4) {
66
- throw new Error(`Unable to parse ${hexString}: Wrong number of components.`);
67
- }
68
-
69
- return Color4.ofRGBA(components[0], components[1], components[2], components[3]);
70
- }
71
-
72
- /** Like fromHex, but can handle additional colors if an `HTMLCanvasElement` is available. */
73
- public static fromString(text: string): Color4 {
74
- if (text.startsWith('#')) {
75
- return Color4.fromHex(text);
76
- }
77
-
78
- if (text === 'none' || text === 'transparent') {
79
- return Color4.transparent;
80
- }
81
-
82
- // rgba?: Match both rgb and rgba strings.
83
- // ([,0-9.]+): Match any string of only numeric, '.' and ',' characters.
84
- const rgbRegex = /^rgba?\(([,0-9.]+)\)$/i;
85
- const rgbMatch = text.replace(/\s*/g, '').match(rgbRegex);
86
-
87
- if (rgbMatch) {
88
- const componentsListStr = rgbMatch[1];
89
- const componentsList = JSON.parse(`[ ${componentsListStr} ]`);
90
-
91
- if (componentsList.length === 3) {
92
- return Color4.ofRGB(
93
- componentsList[0] / 255, componentsList[1] / 255, componentsList[2] / 255
94
- );
95
- } else if (componentsList.length === 4) {
96
- return Color4.ofRGBA(
97
- componentsList[0] / 255, componentsList[1] / 255, componentsList[2] / 255, componentsList[3]
98
- );
99
- } else {
100
- throw new Error(`RGB string, ${text}, has wrong number of components: ${componentsList.length}`);
101
- }
102
- }
103
-
104
- // Otherwise, try to use an HTMLCanvasElement to determine the color.
105
- // Note: We may be unable to create an HTMLCanvasElement if running as a unit test.
106
- const canvas = document.createElement('canvas');
107
- canvas.width = 1;
108
- canvas.height = 1;
109
-
110
- const ctx = canvas.getContext('2d')!;
111
- ctx.fillStyle = text;
112
- ctx.fillRect(0, 0, 1, 1);
113
-
114
- const data = ctx.getImageData(0, 0, 1, 1);
115
- const red = data.data[0] / 255;
116
- const green = data.data[1] / 255;
117
- const blue = data.data[2] / 255;
118
- const alpha = data.data[3] / 255;
119
-
120
- return Color4.ofRGBA(red, green, blue, alpha);
121
- }
122
-
123
- /** @returns true if `this` and `other` are approximately equal. */
124
- public eq(other: Color4|null|undefined): boolean {
125
- if (other == null) {
126
- return false;
127
- }
128
-
129
- // If both completely transparent,
130
- if (this.a === 0 && other.a === 0) {
131
- return true;
132
- }
133
-
134
- return this.toHexString() === other.toHexString();
135
- }
136
-
137
- /**
138
- * If `fractionTo` is not in the range [0, 1], it will be clamped to the nearest number
139
- * in that range. For example, `a.mix(b, -1)` is equivalent to `a.mix(b, 0)`.
140
- *
141
- * @returns a color `fractionTo` of the way from this color to `other`.
142
- *
143
- * @example
144
- * ```ts
145
- * Color4.ofRGB(1, 0, 0).mix(Color4.ofRGB(0, 1, 0), 0.1) // -> Color4(0.9, 0.1, 0)
146
- * ```
147
- */
148
- public mix(other: Color4, fractionTo: number): Color4 {
149
- fractionTo = Math.min(Math.max(fractionTo, 0), 1);
150
- const fractionOfThis = 1 - fractionTo;
151
- return new Color4(
152
- this.r * fractionOfThis + other.r * fractionTo,
153
- this.g * fractionOfThis + other.g * fractionTo,
154
- this.b * fractionOfThis + other.b * fractionTo,
155
- this.a * fractionOfThis + other.a * fractionTo,
156
- );
157
- }
158
-
159
- /**
160
- * @returns the component-wise average of `colors`, or `Color4.transparent` if `colors` is empty.
161
- */
162
- public static average(colors: Color4[]) {
163
- let averageA = 0;
164
- let averageR = 0;
165
- let averageG = 0;
166
- let averageB = 0;
167
-
168
- for (const color of colors) {
169
- averageA += color.a;
170
- averageR += color.r;
171
- averageG += color.g;
172
- averageB += color.b;
173
- }
174
-
175
- if (colors.length > 0) {
176
- averageA /= colors.length;
177
- averageR /= colors.length;
178
- averageG /= colors.length;
179
- averageB /= colors.length;
180
- }
181
-
182
- return new Color4(averageR, averageG, averageB, averageA);
183
- }
184
-
185
- private hexString: string|null = null;
186
-
187
- /**
188
- * @returns a hexadecimal color string representation of `this`, in the form `#rrggbbaa`.
189
- *
190
- * @example
191
- * ```
192
- * Color4.red.toHexString(); // -> #ff0000ff
193
- * ```
194
- */
195
- public toHexString(): string {
196
- if (this.hexString) {
197
- return this.hexString;
198
- }
199
-
200
- const componentToHex = (component: number): string => {
201
- const res = Math.round(255 * component).toString(16);
202
-
203
- if (res.length === 1) {
204
- return `0${res}`;
205
- }
206
- return res;
207
- };
208
-
209
- const alpha = componentToHex(this.a);
210
- const red = componentToHex(this.r);
211
- const green = componentToHex(this.g);
212
- const blue = componentToHex(this.b);
213
- if (alpha === 'ff') {
214
- return `#${red}${green}${blue}`;
215
- }
216
- this.hexString = `#${red}${green}${blue}${alpha}`;
217
- return this.hexString;
218
- }
219
-
220
- public toString() {
221
- return this.toHexString();
222
- }
223
-
224
- public static transparent = Color4.ofRGBA(0, 0, 0, 0);
225
- public static red = Color4.ofRGB(1.0, 0.0, 0.0);
226
- public static green = Color4.ofRGB(0.0, 1.0, 0.0);
227
- public static blue = Color4.ofRGB(0.0, 0.0, 1.0);
228
- public static purple = Color4.ofRGB(0.5, 0.2, 0.5);
229
- public static yellow = Color4.ofRGB(1, 1, 0.1);
230
- public static clay = Color4.ofRGB(0.8, 0.4, 0.2);
231
- public static black = Color4.ofRGB(0, 0, 0);
232
- public static gray = Color4.ofRGB(0.5, 0.5, 0.5);
233
- public static white = Color4.ofRGB(1, 1, 1);
234
- }
235
-
236
- export { Color4 };