js-draw 1.0.1 → 1.1.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 (197) hide show
  1. package/LICENSE +21 -0
  2. package/dist/Editor.css +1 -0
  3. package/dist/bundle.js +1 -1
  4. package/dist/bundledStyles.js +1 -1
  5. package/dist/cjs/toolbar/AbstractToolbar.d.ts +9 -13
  6. package/dist/cjs/toolbar/AbstractToolbar.js +14 -19
  7. package/dist/cjs/toolbar/widgets/SaveActionWidget.d.ts +10 -0
  8. package/dist/cjs/toolbar/widgets/SaveActionWidget.js +26 -0
  9. package/dist/cjs/toolbar/widgets/keybindings.d.ts +1 -0
  10. package/dist/cjs/toolbar/widgets/keybindings.js +4 -1
  11. package/dist/cjs/version.js +1 -1
  12. package/dist/mjs/toolbar/AbstractToolbar.d.ts +9 -13
  13. package/dist/mjs/toolbar/AbstractToolbar.mjs +14 -19
  14. package/dist/mjs/toolbar/widgets/SaveActionWidget.d.ts +10 -0
  15. package/dist/mjs/toolbar/widgets/SaveActionWidget.mjs +21 -0
  16. package/dist/mjs/toolbar/widgets/keybindings.d.ts +1 -0
  17. package/dist/mjs/toolbar/widgets/keybindings.mjs +3 -0
  18. package/dist/mjs/version.mjs +1 -1
  19. package/docs/img/readme-images/js-draw.jpg +0 -0
  20. package/docs/img/readme-images/unsupported-elements--in-editor.png +0 -0
  21. package/package.json +5 -4
  22. package/src/toolbar/EdgeToolbar.scss +1 -0
  23. package/dist-test/test_imports/package-lock.json +0 -13
  24. package/dist-test/test_imports/package.json +0 -12
  25. package/dist-test/test_imports/test-imports.js +0 -11
  26. package/dist-test/test_imports/test-require.cjs +0 -14
  27. package/src/Editor.loadFrom.test.ts +0 -24
  28. package/src/Editor.test.ts +0 -107
  29. package/src/Editor.toSVG.test.ts +0 -294
  30. package/src/Editor.ts +0 -1443
  31. package/src/EditorImage.test.ts +0 -117
  32. package/src/EditorImage.ts +0 -609
  33. package/src/EventDispatcher.test.ts +0 -123
  34. package/src/EventDispatcher.ts +0 -72
  35. package/src/Pointer.ts +0 -183
  36. package/src/SVGLoader.test.ts +0 -114
  37. package/src/SVGLoader.ts +0 -672
  38. package/src/UndoRedoHistory.test.ts +0 -34
  39. package/src/UndoRedoHistory.ts +0 -102
  40. package/src/Viewport.ts +0 -322
  41. package/src/bundle/bundled.ts +0 -7
  42. package/src/commands/Command.ts +0 -45
  43. package/src/commands/Duplicate.ts +0 -75
  44. package/src/commands/Erase.ts +0 -95
  45. package/src/commands/SerializableCommand.ts +0 -49
  46. package/src/commands/UnresolvedCommand.ts +0 -37
  47. package/src/commands/invertCommand.ts +0 -58
  48. package/src/commands/lib.ts +0 -16
  49. package/src/commands/localization.ts +0 -47
  50. package/src/commands/uniteCommands.test.ts +0 -23
  51. package/src/commands/uniteCommands.ts +0 -140
  52. package/src/components/AbstractComponent.transformBy.test.ts +0 -23
  53. package/src/components/AbstractComponent.ts +0 -383
  54. package/src/components/BackgroundComponent.test.ts +0 -44
  55. package/src/components/BackgroundComponent.ts +0 -348
  56. package/src/components/ImageComponent.ts +0 -176
  57. package/src/components/RestylableComponent.ts +0 -161
  58. package/src/components/SVGGlobalAttributesObject.ts +0 -79
  59. package/src/components/Stroke.test.ts +0 -137
  60. package/src/components/Stroke.ts +0 -294
  61. package/src/components/TextComponent.test.ts +0 -202
  62. package/src/components/TextComponent.ts +0 -429
  63. package/src/components/UnknownSVGObject.test.ts +0 -10
  64. package/src/components/UnknownSVGObject.ts +0 -60
  65. package/src/components/builders/ArrowBuilder.ts +0 -106
  66. package/src/components/builders/CircleBuilder.ts +0 -100
  67. package/src/components/builders/FreehandLineBuilder.test.ts +0 -24
  68. package/src/components/builders/FreehandLineBuilder.ts +0 -210
  69. package/src/components/builders/LineBuilder.ts +0 -77
  70. package/src/components/builders/PressureSensitiveFreehandLineBuilder.ts +0 -453
  71. package/src/components/builders/RectangleBuilder.ts +0 -73
  72. package/src/components/builders/types.ts +0 -15
  73. package/src/components/lib.ts +0 -31
  74. package/src/components/localization.ts +0 -24
  75. package/src/components/util/StrokeSmoother.ts +0 -302
  76. package/src/components/util/describeComponentList.ts +0 -18
  77. package/src/dialogs/makeAboutDialog.ts +0 -82
  78. package/src/inputEvents.ts +0 -143
  79. package/src/lib.ts +0 -91
  80. package/src/localization.ts +0 -34
  81. package/src/localizations/de.ts +0 -146
  82. package/src/localizations/en.ts +0 -8
  83. package/src/localizations/es.ts +0 -74
  84. package/src/localizations/getLocalizationTable.test.ts +0 -27
  85. package/src/localizations/getLocalizationTable.ts +0 -74
  86. package/src/rendering/Display.ts +0 -247
  87. package/src/rendering/RenderablePathSpec.ts +0 -88
  88. package/src/rendering/RenderingStyle.test.ts +0 -68
  89. package/src/rendering/RenderingStyle.ts +0 -55
  90. package/src/rendering/TextRenderingStyle.ts +0 -55
  91. package/src/rendering/caching/CacheRecord.test.ts +0 -48
  92. package/src/rendering/caching/CacheRecord.ts +0 -76
  93. package/src/rendering/caching/CacheRecordManager.ts +0 -71
  94. package/src/rendering/caching/RenderingCache.test.ts +0 -43
  95. package/src/rendering/caching/RenderingCache.ts +0 -66
  96. package/src/rendering/caching/RenderingCacheNode.ts +0 -404
  97. package/src/rendering/caching/testUtils.ts +0 -35
  98. package/src/rendering/caching/types.ts +0 -34
  99. package/src/rendering/lib.ts +0 -8
  100. package/src/rendering/localization.ts +0 -20
  101. package/src/rendering/renderers/AbstractRenderer.ts +0 -232
  102. package/src/rendering/renderers/CanvasRenderer.ts +0 -312
  103. package/src/rendering/renderers/DummyRenderer.test.ts +0 -41
  104. package/src/rendering/renderers/DummyRenderer.ts +0 -142
  105. package/src/rendering/renderers/SVGRenderer.ts +0 -434
  106. package/src/rendering/renderers/TextOnlyRenderer.test.ts +0 -34
  107. package/src/rendering/renderers/TextOnlyRenderer.ts +0 -68
  108. package/src/shortcuts/KeyBinding.test.ts +0 -61
  109. package/src/shortcuts/KeyBinding.ts +0 -257
  110. package/src/shortcuts/KeyboardShortcutManager.test.ts +0 -95
  111. package/src/shortcuts/KeyboardShortcutManager.ts +0 -163
  112. package/src/shortcuts/lib.ts +0 -3
  113. package/src/testing/createEditor.ts +0 -11
  114. package/src/testing/getUniquePointerId.ts +0 -18
  115. package/src/testing/lib.ts +0 -3
  116. package/src/testing/sendPenEvent.ts +0 -36
  117. package/src/testing/sendTouchEvent.ts +0 -71
  118. package/src/toolbar/AbstractToolbar.ts +0 -542
  119. package/src/toolbar/DropdownToolbar.ts +0 -220
  120. package/src/toolbar/EdgeToolbar.test.ts +0 -54
  121. package/src/toolbar/EdgeToolbar.ts +0 -543
  122. package/src/toolbar/IconProvider.ts +0 -861
  123. package/src/toolbar/constants.ts +0 -1
  124. package/src/toolbar/lib.ts +0 -6
  125. package/src/toolbar/localization.ts +0 -136
  126. package/src/toolbar/types.ts +0 -13
  127. package/src/toolbar/widgets/ActionButtonWidget.ts +0 -39
  128. package/src/toolbar/widgets/BaseToolWidget.ts +0 -81
  129. package/src/toolbar/widgets/BaseWidget.ts +0 -495
  130. package/src/toolbar/widgets/DocumentPropertiesWidget.ts +0 -250
  131. package/src/toolbar/widgets/EraserToolWidget.ts +0 -84
  132. package/src/toolbar/widgets/HandToolWidget.ts +0 -239
  133. package/src/toolbar/widgets/InsertImageWidget.ts +0 -248
  134. package/src/toolbar/widgets/OverflowWidget.ts +0 -92
  135. package/src/toolbar/widgets/PenToolWidget.ts +0 -369
  136. package/src/toolbar/widgets/SelectionToolWidget.ts +0 -195
  137. package/src/toolbar/widgets/TextToolWidget.ts +0 -149
  138. package/src/toolbar/widgets/components/makeColorInput.ts +0 -184
  139. package/src/toolbar/widgets/components/makeFileInput.ts +0 -128
  140. package/src/toolbar/widgets/components/makeGridSelector.ts +0 -179
  141. package/src/toolbar/widgets/components/makeSeparator.ts +0 -17
  142. package/src/toolbar/widgets/components/makeThicknessSlider.ts +0 -62
  143. package/src/toolbar/widgets/keybindings.ts +0 -19
  144. package/src/toolbar/widgets/layout/DropdownLayoutManager.ts +0 -262
  145. package/src/toolbar/widgets/layout/EdgeToolbarLayoutManager.ts +0 -71
  146. package/src/toolbar/widgets/layout/types.ts +0 -74
  147. package/src/toolbar/widgets/lib.ts +0 -13
  148. package/src/tools/BaseTool.ts +0 -169
  149. package/src/tools/Eraser.test.ts +0 -103
  150. package/src/tools/Eraser.ts +0 -173
  151. package/src/tools/FindTool.test.ts +0 -67
  152. package/src/tools/FindTool.ts +0 -153
  153. package/src/tools/InputFilter/FunctionMapper.ts +0 -17
  154. package/src/tools/InputFilter/InputMapper.ts +0 -41
  155. package/src/tools/InputFilter/InputPipeline.test.ts +0 -41
  156. package/src/tools/InputFilter/InputPipeline.ts +0 -34
  157. package/src/tools/InputFilter/InputStabilizer.ts +0 -254
  158. package/src/tools/InputFilter/StrokeKeyboardControl.ts +0 -104
  159. package/src/tools/PanZoom.test.ts +0 -339
  160. package/src/tools/PanZoom.ts +0 -525
  161. package/src/tools/PasteHandler.ts +0 -94
  162. package/src/tools/Pen.test.ts +0 -260
  163. package/src/tools/Pen.ts +0 -284
  164. package/src/tools/PipetteTool.ts +0 -84
  165. package/src/tools/SelectionTool/SelectAllShortcutHandler.ts +0 -29
  166. package/src/tools/SelectionTool/Selection.ts +0 -647
  167. package/src/tools/SelectionTool/SelectionHandle.ts +0 -142
  168. package/src/tools/SelectionTool/SelectionTool.test.ts +0 -370
  169. package/src/tools/SelectionTool/SelectionTool.ts +0 -510
  170. package/src/tools/SelectionTool/TransformMode.ts +0 -112
  171. package/src/tools/SelectionTool/types.ts +0 -11
  172. package/src/tools/SoundUITool.ts +0 -221
  173. package/src/tools/TextTool.ts +0 -339
  174. package/src/tools/ToolController.ts +0 -224
  175. package/src/tools/ToolEnabledGroup.ts +0 -14
  176. package/src/tools/ToolSwitcherShortcut.ts +0 -39
  177. package/src/tools/ToolbarShortcutHandler.ts +0 -39
  178. package/src/tools/UndoRedoShortcut.test.ts +0 -62
  179. package/src/tools/UndoRedoShortcut.ts +0 -24
  180. package/src/tools/keybindings.ts +0 -85
  181. package/src/tools/lib.ts +0 -22
  182. package/src/tools/localization.ts +0 -76
  183. package/src/types.ts +0 -151
  184. package/src/util/ReactiveValue.test.ts +0 -168
  185. package/src/util/ReactiveValue.ts +0 -241
  186. package/src/util/assertions.ts +0 -55
  187. package/src/util/fileToBase64.ts +0 -18
  188. package/src/util/guessKeyCodeFromKey.ts +0 -36
  189. package/src/util/listPrefixMatch.ts +0 -19
  190. package/src/util/stopPropagationOfScrollingWheelEvents.ts +0 -20
  191. package/src/util/untilNextAnimationFrame.ts +0 -9
  192. package/src/util/waitForAll.ts +0 -18
  193. package/src/util/waitForTimeout.ts +0 -9
  194. package/src/version.test.ts +0 -12
  195. package/src/version.ts +0 -3
  196. package/tools/allLocales.js +0 -4
  197. package/tools/copyREADME.ts +0 -62
@@ -1,68 +0,0 @@
1
-
2
- import { Color4 } from '@js-draw/math';
3
- import RenderingStyle, { styleFromJSON, stylesEqual, styleToJSON } from './RenderingStyle';
4
-
5
-
6
- describe('RenderingStyle', () => {
7
- it('identical styles should be equal', () => {
8
- const redFill: RenderingStyle = {
9
- fill: Color4.red,
10
- };
11
- expect(stylesEqual(redFill, redFill)).toBe(true);
12
- expect(stylesEqual(
13
- { fill: Color4.ofRGB(1, 0, 0.3), },
14
- { fill: Color4.ofRGB(1, 0, 0.3), },
15
- )).toBe(true);
16
- expect(stylesEqual(
17
- { fill: Color4.red },
18
- { fill: Color4.blue },
19
- )).toBe(false);
20
-
21
- expect(stylesEqual(
22
- { fill: Color4.red, stroke: { width: 1, color: Color4.red }},
23
- { fill: Color4.red },
24
- )).toBe(false);
25
- expect(stylesEqual(
26
- { fill: Color4.red, stroke: { width: 1, color: Color4.red }},
27
- { fill: Color4.red, stroke: { width: 1, color: Color4.blue }},
28
- )).toBe(false);
29
- expect(stylesEqual(
30
- { fill: Color4.red, stroke: { width: 1, color: Color4.red }},
31
- { fill: Color4.red, stroke: { width: 1, color: Color4.red }},
32
- )).toBe(true);
33
- expect(stylesEqual(
34
- { fill: Color4.red, stroke: { width: 1, color: Color4.red }},
35
- { fill: Color4.red, stroke: { width: 2, color: Color4.red }},
36
- )).toBe(false);
37
- });
38
-
39
- it('styles should be convertable to JSON', () => {
40
- expect(styleToJSON({
41
- fill: Color4.red,
42
- })).toMatchObject({
43
- fill: '#ff0000',
44
- stroke: undefined,
45
- });
46
-
47
- expect(styleToJSON({
48
- fill: Color4.blue,
49
- stroke: {
50
- width: 4,
51
- color: Color4.red,
52
- }
53
- })).toMatchObject({
54
- fill: '#0000ff',
55
- stroke: {
56
- width: 4,
57
- color: '#ff0000'
58
- },
59
- });
60
- });
61
-
62
- it('JSON should be convertable into styles', () => {
63
- const redFillJSON = { fill: '#ff0000', };
64
- const redFillBlueStrokeJSON = { fill: '#ff0000', stroke: { width: 4, color: '#0000ff' }};
65
- expect(styleToJSON(styleFromJSON(redFillJSON))).toMatchObject(redFillJSON);
66
- expect(styleToJSON(styleFromJSON(redFillBlueStrokeJSON))).toMatchObject(redFillBlueStrokeJSON);
67
- });
68
- });
@@ -1,55 +0,0 @@
1
- import { Color4 } from '@js-draw/math';
2
-
3
- interface RenderingStyle {
4
- readonly fill: Color4;
5
- readonly stroke?: {
6
- readonly color: Color4;
7
- readonly width: number;
8
- };
9
- }
10
-
11
- export default RenderingStyle;
12
-
13
- export const cloneStyle = (style: RenderingStyle) => {
14
- return {
15
- fill: style.fill,
16
- stroke: style.stroke ? {
17
- ...style.stroke
18
- } : undefined,
19
- };
20
- };
21
-
22
- export const stylesEqual = (a: RenderingStyle, b: RenderingStyle): boolean => {
23
- const result = a === b || (a.fill.eq(b.fill)
24
- && (a.stroke == undefined) === (b.stroke == undefined)
25
- && (a.stroke?.color?.eq(b.stroke?.color) ?? true)
26
- && a.stroke?.width === b.stroke?.width);
27
-
28
- // Map undefined/null -> false
29
- return result ?? false;
30
- };
31
-
32
- // Returns an object that can be converted to a JSON string with
33
- // JSON.stringify.
34
- export const styleToJSON = (style: RenderingStyle) => {
35
- const stroke = !style.stroke ? undefined : {
36
- color: style.stroke.color.toHexString(),
37
- width: style.stroke.width,
38
- };
39
-
40
- return {
41
- fill: style.fill.toHexString(),
42
- stroke,
43
- };
44
- };
45
-
46
- export const styleFromJSON = (json: Record<string, any>) => {
47
- const stroke = json.stroke ? {
48
- color: Color4.fromHex(json.stroke.color),
49
- width: json.stroke.width,
50
- } : undefined;
51
- return {
52
- fill: Color4.fromHex(json.fill),
53
- stroke,
54
- };
55
- };
@@ -1,55 +0,0 @@
1
- import RenderingStyle, { cloneStyle, styleFromJSON, styleToJSON } from './RenderingStyle';
2
-
3
- export interface TextRenderingStyle {
4
- readonly size: number;
5
-
6
- /** Name of the font. */
7
- readonly fontFamily: string;
8
-
9
- /** For example, `bold`. Like CSS `font-weight`. */
10
- readonly fontWeight?: string;
11
-
12
- /** For example, `italic`. Like CSS `font-style`. */
13
- readonly fontStyle?: string;
14
-
15
- readonly fontVariant?: string;
16
-
17
- /** Fill and stroke of the text. */
18
- readonly renderingStyle: RenderingStyle;
19
- }
20
-
21
- export default TextRenderingStyle;
22
-
23
- export const cloneTextStyle = (style: TextRenderingStyle) => {
24
- return {
25
- ...style,
26
- renderingStyle: cloneStyle(style.renderingStyle),
27
- };
28
- };
29
-
30
- export const textStyleFromJSON = (json: any) => {
31
- if (typeof json === 'string') {
32
- json = JSON.parse(json);
33
- }
34
-
35
- if (typeof(json.fontFamily) !== 'string') {
36
- throw new Error('Serialized textStyle missing string fontFamily attribute!');
37
- }
38
-
39
- const style: TextRenderingStyle = {
40
- renderingStyle: styleFromJSON(json.renderingStyle),
41
- size: json.size,
42
- fontWeight: json.fontWeight,
43
- fontVariant: json.fontVariant,
44
- fontFamily: json.fontFamily,
45
- };
46
-
47
- return style;
48
- };
49
-
50
- export const textStyleToJSON = (style: TextRenderingStyle) => {
51
- return {
52
- ...style,
53
- renderingStyle: styleToJSON(style.renderingStyle),
54
- };
55
- };
@@ -1,48 +0,0 @@
1
- /* @jest-environment jsdom */
2
-
3
- import { Rect2, Vec2 } from '@js-draw/math';
4
- import CacheRecord from './CacheRecord';
5
- import { createCache } from './testUtils';
6
-
7
- describe('CacheRecord', () => {
8
- describe('should map points in the cache renderer\'s region to the cache renderer', () => {
9
- const blockResolution = Vec2.of(500, 500);
10
- const { cache } = createCache(undefined, {
11
- blockResolution,
12
- });
13
- const state = cache['sharedState'];
14
-
15
- const record = new CacheRecord(() => {}, state);
16
-
17
- describe('region has top left at (0, 0)', () => {
18
- it('region as big as the cache block', () => {
19
- const transform = record.getTransform(
20
- new Rect2(0, 0, 500, 500)
21
- );
22
- expect(transform.transformVec2(Vec2.unitX)).toMatchObject(Vec2.unitX);
23
- expect(transform.transformVec2(Vec2.unitY)).toMatchObject(Vec2.unitY);
24
- });
25
-
26
- it('region twice as big as cache block', () => {
27
- const transform = record.getTransform(
28
- new Rect2(0, 0, 1000, 1000)
29
- );
30
-
31
- // A size-one vector on the screen corresponds to a size 1/2 vector on the
32
- // cached surface.
33
- expect(transform.transformVec2(Vec2.unitX)).toMatchObject(Vec2.of(0.5, 0));
34
- expect(transform.transformVec2(Vec2.unitY)).toMatchObject(Vec2.of(0, 0.5));
35
- });
36
- });
37
-
38
- describe('region has top left at (100, 100)', () => {
39
- it('region as big as the cache block', () => {
40
- const transform = record.getTransform(
41
- new Rect2(100, 100, 500, 500)
42
- );
43
- expect(transform.transformVec2(Vec2.of(100, 100))).toMatchObject(Vec2.zero);
44
- expect(transform.transformVec2(Vec2.of(500, 500))).toMatchObject(Vec2.of(400, 400));
45
- });
46
- });
47
- });
48
- });
@@ -1,76 +0,0 @@
1
- import { Mat33, Rect2 } from '@js-draw/math';
2
- import AbstractRenderer from '../renderers/AbstractRenderer';
3
- import { BeforeDeallocCallback, CacheState } from './types';
4
-
5
- // Represents a cached renderer/canvas
6
- // This is not a [CacheNode] -- it handles cached renderers and does not have sub-renderers.
7
-
8
- export default class CacheRecord {
9
- private renderer: AbstractRenderer;
10
- private lastUsedCycle: number;
11
- private allocd: boolean = false;
12
-
13
- // For debugging
14
- public allocCount: number = 0;
15
-
16
- public constructor(
17
- private onBeforeDeallocCallback: BeforeDeallocCallback|null,
18
- private cacheState: CacheState,
19
- ) {
20
- this.renderer = cacheState.props.createRenderer();
21
- this.lastUsedCycle = -1;
22
- this.allocd = true;
23
- }
24
-
25
- public startRender(): AbstractRenderer {
26
- this.lastUsedCycle = this.cacheState.currentRenderingCycle;
27
- if (!this.allocd) {
28
- throw new Error('Only alloc\'d canvases can be rendered to');
29
- }
30
- return this.renderer;
31
- }
32
-
33
- public dealloc() {
34
- this.onBeforeDeallocCallback?.();
35
- this.allocd = false;
36
- this.onBeforeDeallocCallback = null;
37
- this.lastUsedCycle = 0;
38
- }
39
-
40
- public isAllocd() {
41
- return this.allocd;
42
- }
43
-
44
- public realloc(newDeallocCallback: BeforeDeallocCallback) {
45
- if (this.allocd) {
46
- this.dealloc();
47
- }
48
- this.allocd = true;
49
- this.onBeforeDeallocCallback = newDeallocCallback;
50
- this.lastUsedCycle = this.cacheState.currentRenderingCycle;
51
- this.allocCount ++;
52
- }
53
-
54
- public getLastUsedCycle(): number {
55
- return this.lastUsedCycle;
56
- }
57
-
58
- // Returns the transformation that maps [drawTo] to this' renderable region
59
- // (i.e. a [cacheProps.blockResolution]-sized rectangle with top left at (0, 0))
60
- public getTransform(drawTo: Rect2): Mat33 {
61
- const transform = Mat33.scaling2D(
62
- this.cacheState.props.blockResolution.x / drawTo.size.x
63
- ).rightMul(
64
- Mat33.translation(drawTo.topLeft.times(-1))
65
- );
66
-
67
- return transform;
68
- }
69
-
70
- public setRenderingRegion(drawTo: Rect2) {
71
- this.renderer.setTransform(
72
- // Invert to map objects instead of the viewport
73
- this.getTransform(drawTo)
74
- );
75
- }
76
- }
@@ -1,71 +0,0 @@
1
- import { BeforeDeallocCallback, CacheProps, CacheState } from './types';
2
- import CacheRecord from './CacheRecord';
3
- import { Rect2 } from '@js-draw/math';
4
-
5
- const debugMode = false;
6
-
7
- export class CacheRecordManager {
8
- // Fixed-size array: Cache blocks are assigned indicies into [cachedCanvases].
9
- private cacheRecords: CacheRecord[] = [];
10
- private maxCanvases: number;
11
- private cacheState: CacheState;
12
-
13
- public constructor(cacheProps: CacheProps) {
14
- this.maxCanvases = Math.ceil(
15
- // Assuming four components per pixel:
16
- cacheProps.cacheSize / 4 / cacheProps.blockResolution.x / cacheProps.blockResolution.y
17
- );
18
- }
19
-
20
- public setSharedState(state: CacheState) {
21
- this.cacheState = state;
22
- }
23
-
24
- public allocCanvas(drawTo: Rect2, onDealloc: BeforeDeallocCallback): CacheRecord {
25
- if (this.cacheRecords.length < this.maxCanvases) {
26
- const record: CacheRecord = new CacheRecord(
27
- onDealloc,
28
- this.cacheState,
29
- );
30
- record.setRenderingRegion(drawTo);
31
- this.cacheRecords.push(record);
32
-
33
- if (debugMode) {
34
- console.log('[Cache] Cache spaces used: ', this.cacheRecords.length, ' of ', this.maxCanvases);
35
- }
36
-
37
- return record;
38
- } else {
39
- const lru = this.getLeastRecentlyUsedRecord()!;
40
-
41
- if (debugMode) {
42
- console.log(
43
- '[Cache] Re-alloc. Times allocated: ', lru.allocCount,
44
- '\nLast used cycle: ', lru.getLastUsedCycle(),
45
- '\nCurrent cycle: ', this.cacheState.currentRenderingCycle
46
- );
47
- }
48
-
49
- lru.realloc(onDealloc);
50
- lru.setRenderingRegion(drawTo);
51
-
52
- if (debugMode) {
53
- console.log(
54
- '[Cache] Now re-alloc\'d. Last used cycle: ', lru.getLastUsedCycle()
55
- );
56
- console.assert(
57
- lru['cacheState'] === this.cacheState,
58
- '[Cache] Unequal cache states! cacheState should be a shared object!'
59
- );
60
- }
61
-
62
- return lru;
63
- }
64
- }
65
-
66
- // Returns null if there are no cache records. Returns an unalloc'd record if one exists.
67
- private getLeastRecentlyUsedRecord(): CacheRecord|null {
68
- this.cacheRecords.sort((a, b) => a.getLastUsedCycle() - b.getLastUsedCycle());
69
- return this.cacheRecords[0];
70
- }
71
- }
@@ -1,43 +0,0 @@
1
- /* @jest-environment jsdom */
2
-
3
- import DummyRenderer from '../renderers/DummyRenderer';
4
- import { createCache } from './testUtils';
5
- import Stroke from '../../components/Stroke';
6
- import { Path, Mat33, Color4 } from '@js-draw/math';
7
- import EditorImage from '../../EditorImage';
8
- import Viewport from '../../Viewport';
9
- import { pathToRenderable } from '../RenderablePathSpec';
10
-
11
- describe('RenderingCache', () => {
12
- const testPath = Path.fromString('M0,0 l100,500 l-20,20 L-100,-100');
13
- const testStroke = new Stroke([ pathToRenderable(testPath, { fill: Color4.purple }) ]);
14
-
15
- it('should create a root node large enough to contain the viewport', () => {
16
- let lastRenderer: DummyRenderer|null = null;
17
- let allocdRenderers: number = 0;
18
-
19
- const { editor, cache } = createCache((renderer) => {
20
- allocdRenderers ++;
21
- lastRenderer = renderer;
22
- });
23
- const screenRenderer = editor.display.getDryInkRenderer() as DummyRenderer;
24
-
25
- // No objects: Should not create a renderer.
26
- expect(lastRenderer).toBeNull();
27
- editor.image.renderWithCache(screenRenderer, cache, editor.viewport);
28
- expect(lastRenderer).toBeNull();
29
-
30
- editor.dispatch(EditorImage.addElement(testStroke));
31
- editor.image.renderWithCache(screenRenderer, cache, editor.viewport);
32
-
33
- expect(allocdRenderers).toBeGreaterThanOrEqual(1);
34
- expect(lastRenderer).not.toBeNull();
35
- expect(lastRenderer!.renderedPathCount).toBe(1);
36
-
37
- editor.dispatch(Viewport.transformBy(Mat33.scaling2D(0.1)));
38
- editor.image.renderWithCache(screenRenderer, cache, editor.viewport);
39
- expect(allocdRenderers).toBe(1);
40
- expect(lastRenderer!.renderedPathCount).toBe(1);
41
- expect(screenRenderer.renderedPathCount).toBeGreaterThanOrEqual(1);
42
- });
43
- });
@@ -1,66 +0,0 @@
1
- import { ImageNode } from '../../EditorImage';
2
- import { Rect2 } from '@js-draw/math';
3
- import Viewport from '../../Viewport';
4
- import AbstractRenderer from '../renderers/AbstractRenderer';
5
- import RenderingCacheNode from './RenderingCacheNode';
6
- import { CacheRecordManager } from './CacheRecordManager';
7
- import { CacheProps, CacheState } from './types';
8
-
9
- export default class RenderingCache {
10
- private sharedState: CacheState;
11
- private recordManager: CacheRecordManager;
12
- private rootNode: RenderingCacheNode|null;
13
-
14
- public constructor(cacheProps: CacheProps) {
15
- this.recordManager = new CacheRecordManager(cacheProps);
16
- this.sharedState = {
17
- props: cacheProps,
18
- currentRenderingCycle: 0,
19
- recordManager: this.recordManager,
20
- };
21
- this.recordManager.setSharedState(this.sharedState);
22
- }
23
-
24
- public render(screenRenderer: AbstractRenderer, image: ImageNode, viewport: Viewport) {
25
- const visibleRect = viewport.visibleRect;
26
- this.sharedState.currentRenderingCycle ++;
27
-
28
- // If we can't use the cache,
29
- if (!this.sharedState.props.isOfCorrectType(screenRenderer)) {
30
- image.render(screenRenderer, visibleRect);
31
- return;
32
- }
33
-
34
- if (!this.rootNode) {
35
- // Adjust the node so that it has the correct aspect ratio
36
- const res = this.sharedState.props.blockResolution;
37
-
38
- const topLeft = visibleRect.topLeft;
39
- this.rootNode = new RenderingCacheNode(
40
- new Rect2(topLeft.x, topLeft.y, res.x, res.y),
41
- this.sharedState
42
- );
43
- }
44
-
45
- while (!this.rootNode!.region.containsRect(visibleRect)) {
46
- this.rootNode = this.rootNode!.generateParent();
47
- }
48
-
49
- this.rootNode = this.rootNode!.smallestChildContaining(visibleRect) ?? this.rootNode;
50
-
51
- const visibleLeaves = image.getLeavesIntersectingRegion(
52
- viewport.visibleRect, rect => screenRenderer.isTooSmallToRender(rect)
53
- );
54
-
55
- let approxVisibleRenderTime = 0;
56
- for (const leaf of visibleLeaves) {
57
- approxVisibleRenderTime += leaf.getContent()!.getProportionalRenderingTime();
58
- }
59
-
60
- if (approxVisibleRenderTime > this.sharedState.props.minProportionalRenderTimeToUseCache) {
61
- this.rootNode!.renderItems(screenRenderer, [ image ], viewport);
62
- } else {
63
- image.render(screenRenderer, visibleRect);
64
- }
65
- }
66
- }