@widgetic/canvas 0.5.42 → 0.5.44
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.
- package/README.md +1 -0
- package/dist/canvas/DomCanvas.svelte +53 -20
- package/dist/canvas/DomCanvas.svelte.d.ts +19 -7
- package/dist/canvas/chromeLayout.d.ts +14 -3
- package/dist/canvas/chromeLayout.js +14 -3
- package/dist/canvas/host/CanvasHost.d.ts +66 -6
- package/dist/canvas/host/CanvasHost.js +316 -91
- package/dist/canvas/host/CanvasHostApi.d.ts +2 -2
- package/dist/canvas/host/CanvasHostApi.js +5 -1
- package/dist/canvas/host/nearCards.d.ts +33 -0
- package/dist/canvas/host/nearCards.js +78 -0
- package/dist/canvas/host/pageEvents.d.ts +16 -4
- package/dist/canvas/host/pageEvents.js +13 -4
- package/dist/canvas/host/pictureSaves.d.ts +33 -0
- package/dist/canvas/host/pictureSaves.js +42 -0
- package/dist/canvas/host/screenRects.d.ts +5 -0
- package/dist/canvas/host/storedPictures.d.ts +88 -0
- package/dist/canvas/host/storedPictures.js +288 -0
- package/dist/canvas/index.d.ts +2 -1
- package/dist/canvas/index.js +1 -1
- package/dist/canvas/shapes/shapeImageStore.d.ts +45 -2
- package/dist/canvas/shapes/shapeImageStore.js +7 -0
- package/dist/canvas/toolbarPosition.d.ts +2 -2
- package/dist/canvas/toolbarPosition.js +2 -2
- package/dist/canvas/types.d.ts +17 -0
- package/dist/canvas/ui/ColorField.svelte +7 -2
- package/dist/canvas/ui/SelectionPanel.svelte +56 -31
- package/dist/canvas/ui/SelectionPanel.svelte.d.ts +7 -0
- package/dist/canvas/ui/SelectorIndicator.svelte +81 -0
- package/dist/canvas/ui/SelectorIndicator.svelte.d.ts +11 -0
- package/dist/canvas/ui/ToolButton.svelte +11 -3
- package/dist/canvas/ui/floatingCard.d.ts +2 -2
- package/dist/canvas/ui/floatingCard.js +2 -2
- package/dist/canvas/ui/floatingLayer.d.ts +13 -2
- package/dist/canvas/ui/floatingLayer.js +14 -2
- package/dist/canvas/view/CanvasStage.svelte +101 -67
- package/dist/canvas/view/LeftToRightScope.svelte +21 -0
- package/dist/canvas/view/LeftToRightScope.svelte.d.ts +7 -0
- package/dist/canvas/view/ObjectView.svelte +15 -10
- package/dist/canvas/view/ObjectView.svelte.d.ts +1 -1
- package/dist/canvas/view/liftedScene.d.ts +31 -0
- package/dist/canvas/view/liftedScene.js +146 -0
- package/dist/canvas/view/mountedScene.d.ts +71 -0
- package/dist/canvas/view/mountedScene.js +363 -0
- package/dist/engine/EngineCore.d.ts +89 -1
- package/dist/engine/EngineCore.js +158 -4
- package/dist/engine/codec/canvasDocument.d.ts +5 -0
- package/dist/engine/codec/canvasDocument.js +31 -4
- package/dist/engine/core/cameraMove.d.ts +67 -0
- package/dist/engine/core/cameraMove.js +154 -0
- package/dist/engine/core/history.d.ts +6 -0
- package/dist/engine/core/history.js +16 -0
- package/dist/engine/core/scene.d.ts +10 -0
- package/dist/engine/core/scene.js +24 -0
- package/dist/engine/core/types.d.ts +8 -2
- package/dist/engine/input/PointerController.js +12 -0
- package/dist/engine/input/gestures.js +4 -2
- package/dist/engine/state.d.ts +20 -0
- package/dist/engine/state.js +1 -0
- package/dist/fonts/interFont.d.ts +10 -1
- package/dist/fonts/interFont.js +36 -13
- package/dist/i18n/language.d.ts +3 -3
- package/dist/i18n/messages.d.ts +8 -1
- package/dist/i18n/messages.js +9 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/package.json +1 -1
- package/dist/canvas/ui/SelectorBar.svelte +0 -84
- package/dist/canvas/ui/SelectorBar.svelte.d.ts +0 -12
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A big selection that is carried, drawn as one layer.
|
|
3
|
+
*
|
|
4
|
+
* A drag writes a new record for every shape it carries, at every step, and every object that gets a new record is told, works out
|
|
5
|
+
* its placement and has its element written: with the whole of a board of 1,000 objects selected that is 1,000 elements at every
|
|
6
|
+
* frame of the drag, which is the one thing a board of this size could not do within a frame (18.7 ms at 1,000 objects, 58 ms at 3,000).
|
|
7
|
+
*
|
|
8
|
+
* What is carried does not change except for where it stands, and the world layer already moves as a whole for a pan: so while the
|
|
9
|
+
* pointer is down the carried objects are drawn from the records they had when the drag began, which are the same records at every
|
|
10
|
+
* step (the stage's list holds the same objects, and an object that is the same record as before is not told anything), and the world
|
|
11
|
+
* layer is moved by how far the selection has been carried, once. The objects that are not carried would move with the layer, so each of
|
|
12
|
+
* them is drawn from its own record moved back by the same distance: it stands where it stood. Both come to the same place on
|
|
13
|
+
* the screen as the records the engine holds, which is the truth and is what is drawn again, with no distance, the moment the drag ends.
|
|
14
|
+
*
|
|
15
|
+
* carried: on the screen = view + zoom * (distance + record from the start) = where the engine's record is
|
|
16
|
+
* not carried: on the screen = view + zoom * (distance + (record - distance)) = where the engine's record is
|
|
17
|
+
*
|
|
18
|
+
* The cost of a step is then the work for the objects that are *not* carried, and none for the ones that are. That is why it pays when
|
|
19
|
+
* most of what has an element is carried (select all, then drag), and not when a few shapes are carried over a crowd: `show` looks at
|
|
20
|
+
* the numbers and keeps the plain way (the stage draws the engine's records) when the other way would not be cheaper.
|
|
21
|
+
*
|
|
22
|
+
* Nothing about the engine changes: it writes its records at every step (the frame, the guides, the ties of the arrows, the host's own
|
|
23
|
+
* layers and the undo step all read them), and its `carry` in the overlay only says what is carried, from which records and how far
|
|
24
|
+
* (`CarriedShapes`). Where there is nothing like it, or anything about it is not as expected, the objects are drawn the plain way.
|
|
25
|
+
*
|
|
26
|
+
* Plain TypeScript with no Svelte file in it, so that Node can test it.
|
|
27
|
+
*/
|
|
28
|
+
import { isBoxedObject } from '../../engine/core/types.js';
|
|
29
|
+
import { canvasLog } from '../canvasLogger.js';
|
|
30
|
+
/** A carried selection is drawn as one layer only when at least this many of its objects have an element: a few cost nothing to write. */
|
|
31
|
+
export const LIFT_MINIMUM_CARRIED = 40;
|
|
32
|
+
/**
|
|
33
|
+
* ...and only when the objects that are not carried (and have an element) are at most this share of the ones that are, when the drag
|
|
34
|
+
* begins: the work of a step is the work for those, so that is how much of the work is left (a third at the most, with one half).
|
|
35
|
+
*/
|
|
36
|
+
export const LIFT_UNCARRIED_SHARE = 0.5;
|
|
37
|
+
const NO_OBJECTS = Object.freeze([]);
|
|
38
|
+
/** An object moved by a distance (the same object when the distance is none): a group draws nothing and has no place to move. */
|
|
39
|
+
function movedBy(object, distanceX, distanceY) {
|
|
40
|
+
if ((distanceX === 0 && distanceY === 0) || !isBoxedObject(object))
|
|
41
|
+
return object;
|
|
42
|
+
return { ...object, x: object.x + distanceX, y: object.y + distanceY };
|
|
43
|
+
}
|
|
44
|
+
/** How many of the objects are carried. */
|
|
45
|
+
export function countCarried(objects, shapeIds) {
|
|
46
|
+
let carriedCount = 0;
|
|
47
|
+
for (const object of objects) {
|
|
48
|
+
if (shapeIds.has(object.id))
|
|
49
|
+
carriedCount += 1;
|
|
50
|
+
}
|
|
51
|
+
return carriedCount;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* The records to draw while the carried shapes are lifted: each carried object as it was when the drag began (the very same record at
|
|
55
|
+
* every step), each of the others as the engine holds it, moved back by how far the selection has been carried. Null when a carried
|
|
56
|
+
* object has no record from the start, which says that what the engine handed over is not what this drag is made of.
|
|
57
|
+
*/
|
|
58
|
+
export function recordsWhileLifted(mounted, carry) {
|
|
59
|
+
const records = new Array(mounted.length);
|
|
60
|
+
for (let position = 0; position < mounted.length; position += 1) {
|
|
61
|
+
const object = mounted[position];
|
|
62
|
+
if (carry.shapeIds.has(object.id)) {
|
|
63
|
+
const recordFromTheStart = carry.startRecords.get(object.id);
|
|
64
|
+
if (recordFromTheStart === undefined)
|
|
65
|
+
return null;
|
|
66
|
+
records[position] = recordFromTheStart;
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
records[position] = movedBy(object, -carry.deltaX, -carry.deltaY);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return records;
|
|
73
|
+
}
|
|
74
|
+
/** Whether two lists hold the very same records in the same order. */
|
|
75
|
+
function sameRecordsInOrder(first, second) {
|
|
76
|
+
if (first === second)
|
|
77
|
+
return true;
|
|
78
|
+
if (first.length !== second.length)
|
|
79
|
+
return false;
|
|
80
|
+
for (let position = 0; position < first.length; position += 1) {
|
|
81
|
+
if (first[position] !== second[position])
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
return true;
|
|
85
|
+
}
|
|
86
|
+
export function createLiftedView() {
|
|
87
|
+
// The drag that was seen last, told by the set of its shapes (one set for a drag, the same one at every step), and what was decided for it.
|
|
88
|
+
let seenDrag = null;
|
|
89
|
+
let decision = 'undecided';
|
|
90
|
+
// What was handed out last: the same records again are handed out as the very same list, so that the stage is told nothing.
|
|
91
|
+
let lastList = NO_OBJECTS;
|
|
92
|
+
// Once the work has failed the objects are drawn the plain way from then on (and the failure is said once).
|
|
93
|
+
let failed = false;
|
|
94
|
+
const drawnPlain = (mounted) => ({ objects: mounted, offsetX: 0, offsetY: 0 });
|
|
95
|
+
return {
|
|
96
|
+
show(mounted, carry) {
|
|
97
|
+
if (carry === null) {
|
|
98
|
+
seenDrag = null;
|
|
99
|
+
decision = 'undecided';
|
|
100
|
+
lastList = NO_OBJECTS;
|
|
101
|
+
return drawnPlain(mounted);
|
|
102
|
+
}
|
|
103
|
+
if (failed)
|
|
104
|
+
return drawnPlain(mounted);
|
|
105
|
+
try {
|
|
106
|
+
if (carry.shapeIds !== seenDrag) {
|
|
107
|
+
seenDrag = carry.shapeIds;
|
|
108
|
+
decision = 'undecided';
|
|
109
|
+
lastList = NO_OBJECTS;
|
|
110
|
+
}
|
|
111
|
+
if (decision === 'plain')
|
|
112
|
+
return drawnPlain(mounted);
|
|
113
|
+
const carriedCount = countCarried(mounted, carry.shapeIds);
|
|
114
|
+
const uncarriedCount = mounted.length - carriedCount;
|
|
115
|
+
if (decision === 'undecided') {
|
|
116
|
+
const worthIt = carriedCount >= LIFT_MINIMUM_CARRIED && uncarriedCount <= carriedCount * LIFT_UNCARRIED_SHARE;
|
|
117
|
+
decision = worthIt ? 'lifted' : 'plain';
|
|
118
|
+
// Said once for the drag, and only for the drags that are drawn another way: the plain way is every other drag.
|
|
119
|
+
if (worthIt)
|
|
120
|
+
canvasLog(`Canvas: a drag of ${carriedCount} objects (${uncarriedCount} others with an element) is drawn as one layer`);
|
|
121
|
+
}
|
|
122
|
+
else if (uncarriedCount > carriedCount) {
|
|
123
|
+
// The drag has come to a crowd of objects that are not carried: moving each of them back costs more than writing the carried ones.
|
|
124
|
+
decision = 'plain';
|
|
125
|
+
canvasLog(`Canvas: the drag came to ${uncarriedCount} objects that are not carried, so it is drawn object by object from here`);
|
|
126
|
+
}
|
|
127
|
+
if (decision === 'plain')
|
|
128
|
+
return drawnPlain(mounted);
|
|
129
|
+
const records = recordsWhileLifted(mounted, carry);
|
|
130
|
+
if (records === null) {
|
|
131
|
+
decision = 'plain';
|
|
132
|
+
canvasLog('Canvas: a carried object had no record from the start of the drag, so the drag is drawn object by object');
|
|
133
|
+
return drawnPlain(mounted);
|
|
134
|
+
}
|
|
135
|
+
const list = sameRecordsInOrder(records, lastList) ? lastList : records;
|
|
136
|
+
lastList = list;
|
|
137
|
+
return { objects: list, offsetX: carry.deltaX, offsetY: carry.deltaY };
|
|
138
|
+
}
|
|
139
|
+
catch (error) {
|
|
140
|
+
failed = true;
|
|
141
|
+
console.warn('Canvas: a carried selection could not be drawn as one layer, so every object is drawn by itself', error);
|
|
142
|
+
return drawnPlain(mounted);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { Readable } from 'svelte/store';
|
|
2
|
+
import type { Scene } from '../../engine/core/scene.js';
|
|
3
|
+
import type { ObjectId, SceneObject, Size, Viewport } from '../../engine/core/types.js';
|
|
4
|
+
/** A board with fewer objects than this keeps an element for every one of them. */
|
|
5
|
+
export declare const CULLING_MINIMUM_OBJECTS = 150;
|
|
6
|
+
/** How far past the view an object is given an element, as a share of the view's width and height, on every side. */
|
|
7
|
+
export declare const NEAR_MARGIN_RATIO = 0.5;
|
|
8
|
+
/**
|
|
9
|
+
* How far past the view an object that has an element keeps it, in the same measure. More than `NEAR_MARGIN_RATIO`: an object
|
|
10
|
+
* is not made at the edge of the near area and taken away the moment the view has moved a little, which would have a pan that
|
|
11
|
+
* goes out and back build the same objects twice (a few hundred of them, when the whole board is in view). The most that can have
|
|
12
|
+
* an element is what is in this area, which is nine views. It is not used by a view that has just zoomed in, nor by the first
|
|
13
|
+
* area of a board (see `nearAreaAround`).
|
|
14
|
+
*/
|
|
15
|
+
export declare const KEEP_MARGIN_RATIO = 1;
|
|
16
|
+
/**
|
|
17
|
+
* How far the view may go past where it was when the near area was made, as a share of the view, before the area is made again.
|
|
18
|
+
* What is left of the margin at that moment is the room that a quick pan has before it reaches an object without an element.
|
|
19
|
+
*/
|
|
20
|
+
export declare const REFRESH_DISTANCE_RATIO = 0.15;
|
|
21
|
+
/** The area is made again once the view has zoomed in by this factor: the objects that are not near the view now lose their element at once. */
|
|
22
|
+
export declare const ZOOM_IN_REFRESH_RATIO = 1.5;
|
|
23
|
+
/** How many objects that had no element get one in the first frame, when many are wanted at once (what follows adapts, see below). */
|
|
24
|
+
export declare const MOUNT_BATCH_SIZE = 30;
|
|
25
|
+
/** The batch never gets smaller than this, so that a very slow device (or a frame that is slow for another reason, a zoom with a thousand objects in view) still gets somewhere... */
|
|
26
|
+
export declare const MIN_MOUNT_BATCH_SIZE = 16;
|
|
27
|
+
/** ...and never larger than this, however quick the frames are: one frame must stay a frame's work. */
|
|
28
|
+
export declare const MAX_MOUNT_BATCH_SIZE = 120;
|
|
29
|
+
/** What the batch grows by after a frame that came on time. */
|
|
30
|
+
export declare const MOUNT_BATCH_GROWTH = 8;
|
|
31
|
+
/** A frame that comes later than this after the one before it (a frame at 60 Hz is 16.7 ms) says the last batch was too much. */
|
|
32
|
+
export declare const SLOW_FRAME_MS = 24;
|
|
33
|
+
/** A frame that comes sooner than this after the one before it says the last batch left room. */
|
|
34
|
+
export declare const QUICK_FRAME_MS = 20;
|
|
35
|
+
/** Frames further apart than this were not being drawn (a hidden tab): they say nothing of how much a batch costs. */
|
|
36
|
+
export declare const PAUSED_FRAME_MS = 500;
|
|
37
|
+
/** The list before anything was handed over (and the list of an empty board): one shared list. */
|
|
38
|
+
export declare const NOTHING_MOUNTED: readonly SceneObject[];
|
|
39
|
+
/** What the board knows when it asks: the records, where the view is, and the text that is being typed in. */
|
|
40
|
+
export interface MountedSceneInput {
|
|
41
|
+
scene: Scene;
|
|
42
|
+
viewport: Viewport;
|
|
43
|
+
/** The size of the board element in CSS pixels. A board with no size yet has every object drawn: nothing is known to be far. */
|
|
44
|
+
viewSize: Size;
|
|
45
|
+
/** The text that is being typed in, or null: it keeps its element wherever it is. */
|
|
46
|
+
editingTextId: ObjectId | null;
|
|
47
|
+
}
|
|
48
|
+
export interface MountedSceneOptions {
|
|
49
|
+
/** Boards with fewer objects than this are not culled (default `CULLING_MINIMUM_OBJECTS`). */
|
|
50
|
+
minimumObjects?: number;
|
|
51
|
+
/**
|
|
52
|
+
* How many objects get an element in one frame. Left out, the number starts at `MOUNT_BATCH_SIZE` and follows how the frames
|
|
53
|
+
* are going; given, it stays what it is (what a test asks for).
|
|
54
|
+
*/
|
|
55
|
+
batchSize?: number;
|
|
56
|
+
/**
|
|
57
|
+
* Calls `callback` at the next frame, with the time of that frame when it is known, and returns what cancels it (default:
|
|
58
|
+
* `requestAnimationFrame`). Where there is no frame to wait for (the server) every object that is wanted is made at once.
|
|
59
|
+
*/
|
|
60
|
+
scheduleFrame?: (callback: (frameTime?: number) => void) => () => void;
|
|
61
|
+
}
|
|
62
|
+
export interface MountedScene extends Readable<readonly SceneObject[]> {
|
|
63
|
+
/**
|
|
64
|
+
* Takes what the board knows now. Called at every frame of a pan or a drag, so it is quick when nothing that matters changed:
|
|
65
|
+
* the same records, the view still inside the near area, the same text being typed in.
|
|
66
|
+
*/
|
|
67
|
+
take(input: MountedSceneInput): void;
|
|
68
|
+
/** Stops what is waiting for a frame. The store keeps its last list. */
|
|
69
|
+
destroy(): void;
|
|
70
|
+
}
|
|
71
|
+
export declare function createMountedScene(options?: MountedSceneOptions): MountedScene;
|
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which objects of the board have an element: the ones near what the person sees.
|
|
3
|
+
*
|
|
4
|
+
* Every object of the board used to be an element of the world layer, seen or not, and the work of the browser grew with
|
|
5
|
+
* the number of them (with 3000 objects, the page spent its time on elements that were far outside the screen). Now an
|
|
6
|
+
* object has an element only while it is close to the view; the rest is only a record in the engine's scene, which costs
|
|
7
|
+
* nothing to the browser. What the person does with those records does not need an element (the pointer is found by the
|
|
8
|
+
* engine's own hit test, a selection is drawn from the records), so a far object appears the moment it is near, and the
|
|
9
|
+
* person never meets one that is missing: the margin below is the room an object has to be made in.
|
|
10
|
+
*
|
|
11
|
+
* It is a store of its own, and not something `CanvasStage` derives from the engine's state, for the reason that
|
|
12
|
+
* `erasingMarks.ts` gives: the state is a new object at every frame of a pan, a zoom or a drag, and the board must only be
|
|
13
|
+
* told when the list of objects with an element is a different one.
|
|
14
|
+
*
|
|
15
|
+
* What it decides, in short:
|
|
16
|
+
*
|
|
17
|
+
* - A board of fewer objects than `CULLING_MINIMUM_OBJECTS` keeps an element for every one of them, as it always did: on a
|
|
18
|
+
* small board there is nothing to save, and nothing that could go wrong.
|
|
19
|
+
* - Above that, an object has an element when the rectangle it paints (its box turned, with its stroke, see
|
|
20
|
+
* `shapeVisualBounds`) touches the *near area*: the view, with half a view of room on every side.
|
|
21
|
+
* - An object that has its element already keeps it a while longer: until it is out of the *keep area*, which is the view with
|
|
22
|
+
* a whole view of room on every side. A pan that goes out and comes back (the hand of a person on a tablet does that all the
|
|
23
|
+
* time) must not have the browser build the same objects twice. The keep area is for a view that pans or zooms out. A view
|
|
24
|
+
* that zooms in has no use for what it left behind (the person looks at a few objects of a big board now, which is what this
|
|
25
|
+
* is all for), so it lets go at once of everything that is not near it.
|
|
26
|
+
* - The near area is not worked out again at every frame. It is kept while the view stays within `REFRESH_DISTANCE_RATIO`
|
|
27
|
+
* of a view of where it was when the area was made (and has not zoomed in by more than `ZOOM_IN_REFRESH_RATIO`), so
|
|
28
|
+
* most frames of a pan cost nothing but that check, and the objects are made in steps instead of one by one.
|
|
29
|
+
* - A text that is being typed in always has its element, wherever the person scrolls to: its field is the element.
|
|
30
|
+
* - When many objects are wanted at once (the board was just opened, the view jumped to the whole board, an undo brought
|
|
31
|
+
* back a thousand shapes) they are not all made in the same frame: the ones the person can see come first, the nearest
|
|
32
|
+
* to the middle of the view first among them, then the ones of the margin. How many a frame takes follows how the frames
|
|
33
|
+
* are going: it starts at `MOUNT_BATCH_SIZE`, shrinks by half when a frame came late (the last batch was too much for this
|
|
34
|
+
* device) and grows a little when frames come on time, so that a fast desktop is done in a few frames and a slow tablet
|
|
35
|
+
* keeps drawing at its pace. Objects that are out of the keep area lose their element at once, which is cheap.
|
|
36
|
+
* - The order of the list is the paint order of the scene, and a group, which draws nothing, is not in it.
|
|
37
|
+
*
|
|
38
|
+
* Plain TypeScript with no Svelte file in it, so that Node can test it.
|
|
39
|
+
*/
|
|
40
|
+
import { writable } from 'svelte/store';
|
|
41
|
+
import { rectsIntersect } from '../../engine/core/geometry.js';
|
|
42
|
+
import { shapeVisualBounds } from '../../engine/core/scene.js';
|
|
43
|
+
import { isBoxedObject } from '../../engine/core/types.js';
|
|
44
|
+
import { visibleWorldRect } from '../../engine/core/viewport.js';
|
|
45
|
+
/** A board with fewer objects than this keeps an element for every one of them. */
|
|
46
|
+
export const CULLING_MINIMUM_OBJECTS = 150;
|
|
47
|
+
/** How far past the view an object is given an element, as a share of the view's width and height, on every side. */
|
|
48
|
+
export const NEAR_MARGIN_RATIO = 0.5;
|
|
49
|
+
/**
|
|
50
|
+
* How far past the view an object that has an element keeps it, in the same measure. More than `NEAR_MARGIN_RATIO`: an object
|
|
51
|
+
* is not made at the edge of the near area and taken away the moment the view has moved a little, which would have a pan that
|
|
52
|
+
* goes out and back build the same objects twice (a few hundred of them, when the whole board is in view). The most that can have
|
|
53
|
+
* an element is what is in this area, which is nine views. It is not used by a view that has just zoomed in, nor by the first
|
|
54
|
+
* area of a board (see `nearAreaAround`).
|
|
55
|
+
*/
|
|
56
|
+
export const KEEP_MARGIN_RATIO = 1;
|
|
57
|
+
/**
|
|
58
|
+
* How far the view may go past where it was when the near area was made, as a share of the view, before the area is made again.
|
|
59
|
+
* What is left of the margin at that moment is the room that a quick pan has before it reaches an object without an element.
|
|
60
|
+
*/
|
|
61
|
+
export const REFRESH_DISTANCE_RATIO = 0.15;
|
|
62
|
+
/** The area is made again once the view has zoomed in by this factor: the objects that are not near the view now lose their element at once. */
|
|
63
|
+
export const ZOOM_IN_REFRESH_RATIO = 1.5;
|
|
64
|
+
/** How many objects that had no element get one in the first frame, when many are wanted at once (what follows adapts, see below). */
|
|
65
|
+
export const MOUNT_BATCH_SIZE = 30;
|
|
66
|
+
/** The batch never gets smaller than this, so that a very slow device (or a frame that is slow for another reason, a zoom with a thousand objects in view) still gets somewhere... */
|
|
67
|
+
export const MIN_MOUNT_BATCH_SIZE = 16;
|
|
68
|
+
/** ...and never larger than this, however quick the frames are: one frame must stay a frame's work. */
|
|
69
|
+
export const MAX_MOUNT_BATCH_SIZE = 120;
|
|
70
|
+
/** What the batch grows by after a frame that came on time. */
|
|
71
|
+
export const MOUNT_BATCH_GROWTH = 8;
|
|
72
|
+
/** A frame that comes later than this after the one before it (a frame at 60 Hz is 16.7 ms) says the last batch was too much. */
|
|
73
|
+
export const SLOW_FRAME_MS = 24;
|
|
74
|
+
/** A frame that comes sooner than this after the one before it says the last batch left room. */
|
|
75
|
+
export const QUICK_FRAME_MS = 20;
|
|
76
|
+
/** Frames further apart than this were not being drawn (a hidden tab): they say nothing of how much a batch costs. */
|
|
77
|
+
export const PAUSED_FRAME_MS = 500;
|
|
78
|
+
/** The list before anything was handed over (and the list of an empty board): one shared list. */
|
|
79
|
+
export const NOTHING_MOUNTED = Object.freeze([]);
|
|
80
|
+
/**
|
|
81
|
+
* The rectangle an object paints, in the world: its box turned, with the stroke or the brush's overflow, never negative in size.
|
|
82
|
+
*
|
|
83
|
+
* It is not remembered for the record. A record is measured in about a tenth of a microsecond, and a `WeakMap` that is handed every
|
|
84
|
+
* new record of a drag (all of them, when the whole selection moves) costs ten times that at every frame: measured, a drag of the
|
|
85
|
+
* whole of a board of 1,000 objects spent most of this module's time on the map and not on the measuring.
|
|
86
|
+
*/
|
|
87
|
+
function paintedBoundsOf(shape) {
|
|
88
|
+
const measured = shapeVisualBounds(shape);
|
|
89
|
+
if (measured.width >= 0 && measured.height >= 0)
|
|
90
|
+
return measured;
|
|
91
|
+
// A box that was written with a negative size is the same box from the other corner.
|
|
92
|
+
return {
|
|
93
|
+
x: measured.width < 0 ? measured.x + measured.width : measured.x,
|
|
94
|
+
y: measured.height < 0 ? measured.y + measured.height : measured.y,
|
|
95
|
+
width: Math.abs(measured.width),
|
|
96
|
+
height: Math.abs(measured.height)
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
/** A view with `ratio` of its width and height added on every side. */
|
|
100
|
+
function viewWithMargin(view, ratio) {
|
|
101
|
+
const marginX = view.width * ratio;
|
|
102
|
+
const marginY = view.height * ratio;
|
|
103
|
+
return { x: view.x - marginX, y: view.y - marginY, width: view.width + marginX * 2, height: view.height + marginY * 2 };
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* The near area around a view (`NEAR_MARGIN_RATIO` on every side), and the keep area that goes with it.
|
|
107
|
+
*
|
|
108
|
+
* The keep area is wider (`KEEP_MARGIN_RATIO`) only for a view that comes after another one (`previous`) and has not zoomed in by
|
|
109
|
+
* more than `ZOOM_IN_REFRESH_RATIO` since: it panned, or zoomed out, and what it made stays until it is far. A view that has
|
|
110
|
+
* zoomed in, or the first view of a board (nothing was made around a view before it, or everything was, which a board that has no
|
|
111
|
+
* size yet does), has the keep area of the near area: what is not near it loses its element at once.
|
|
112
|
+
*/
|
|
113
|
+
function nearAreaAround(view, zoom, previous) {
|
|
114
|
+
const keepsWhatItHas = previous !== null && zoom <= previous.zoom * ZOOM_IN_REFRESH_RATIO;
|
|
115
|
+
return {
|
|
116
|
+
view,
|
|
117
|
+
zoom,
|
|
118
|
+
area: viewWithMargin(view, NEAR_MARGIN_RATIO),
|
|
119
|
+
keepArea: viewWithMargin(view, keepsWhatItHas ? KEEP_MARGIN_RATIO : NEAR_MARGIN_RATIO)
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
/** Whether the near area still serves a view: the view is where it was, give or take the slack, and has not zoomed in far. */
|
|
123
|
+
function isStillNear(near, view, zoom) {
|
|
124
|
+
if (zoom > near.zoom * ZOOM_IN_REFRESH_RATIO)
|
|
125
|
+
return false;
|
|
126
|
+
const slackX = near.view.width * REFRESH_DISTANCE_RATIO;
|
|
127
|
+
const slackY = near.view.height * REFRESH_DISTANCE_RATIO;
|
|
128
|
+
return (view.x >= near.view.x - slackX &&
|
|
129
|
+
view.y >= near.view.y - slackY &&
|
|
130
|
+
view.x + view.width <= near.view.x + near.view.width + slackX &&
|
|
131
|
+
view.y + view.height <= near.view.y + near.view.height + slackY);
|
|
132
|
+
}
|
|
133
|
+
/** A rectangle made of finite numbers, with some size: anything else says the board has no size (or no view) yet. */
|
|
134
|
+
function isUsableView(view) {
|
|
135
|
+
return Number.isFinite(view.x) && Number.isFinite(view.y) && Number.isFinite(view.width) && Number.isFinite(view.height) && view.width > 0 && view.height > 0;
|
|
136
|
+
}
|
|
137
|
+
/** Whether two lists hold the very same records in the same order (the very same list is the quick case). */
|
|
138
|
+
function sameRecords(first, second) {
|
|
139
|
+
if (first === second)
|
|
140
|
+
return true;
|
|
141
|
+
if (first.length !== second.length)
|
|
142
|
+
return false;
|
|
143
|
+
for (let position = 0; position < first.length; position += 1) {
|
|
144
|
+
if (first[position] !== second[position])
|
|
145
|
+
return false;
|
|
146
|
+
}
|
|
147
|
+
return true;
|
|
148
|
+
}
|
|
149
|
+
/** Whether two lists are of the very same objects (by id) in the same order, whatever their records are: a drag changes records and not objects. */
|
|
150
|
+
function sameObjects(first, second) {
|
|
151
|
+
if (first.length !== second.length)
|
|
152
|
+
return false;
|
|
153
|
+
for (let position = 0; position < first.length; position += 1) {
|
|
154
|
+
if (first[position].id !== second[position].id)
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
return true;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* The ids of the `count` objects to make first: the ones that touch the view before the others, and the nearest to the middle of the
|
|
161
|
+
* view first in each of the two kinds.
|
|
162
|
+
*/
|
|
163
|
+
function nearestIdsFirst(candidates, view, count) {
|
|
164
|
+
const middleX = view.x + view.width / 2;
|
|
165
|
+
const middleY = view.y + view.height / 2;
|
|
166
|
+
// Squared distances stay far below this, so adding it puts every object that is out of the view after every one that is in it.
|
|
167
|
+
const OUT_OF_VIEW = 1e18;
|
|
168
|
+
const ranked = candidates.map((object) => {
|
|
169
|
+
const bounds = paintedBoundsOf(object);
|
|
170
|
+
const offsetX = bounds.x + bounds.width / 2 - middleX;
|
|
171
|
+
const offsetY = bounds.y + bounds.height / 2 - middleY;
|
|
172
|
+
return { id: object.id, rank: (rectsIntersect(view, bounds) ? 0 : OUT_OF_VIEW) + offsetX * offsetX + offsetY * offsetY };
|
|
173
|
+
});
|
|
174
|
+
ranked.sort((first, second) => first.rank - second.rank);
|
|
175
|
+
return new Set(ranked.slice(0, count).map((entry) => entry.id));
|
|
176
|
+
}
|
|
177
|
+
/** The frame the page gives, or null where there is none (a server render). */
|
|
178
|
+
function pageFrameScheduler() {
|
|
179
|
+
if (typeof requestAnimationFrame !== 'function' || typeof cancelAnimationFrame !== 'function')
|
|
180
|
+
return null;
|
|
181
|
+
return (callback) => {
|
|
182
|
+
const handle = requestAnimationFrame((frameTime) => callback(frameTime));
|
|
183
|
+
return () => cancelAnimationFrame(handle);
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
export function createMountedScene(options = {}) {
|
|
187
|
+
const minimumObjects = options.minimumObjects ?? CULLING_MINIMUM_OBJECTS;
|
|
188
|
+
const scheduleFrame = options.scheduleFrame ?? pageFrameScheduler();
|
|
189
|
+
// A batch size that was given stays; otherwise it follows the frames (see `adaptBatchSizeTo`).
|
|
190
|
+
const batchSizeFollowsFrames = options.batchSize === undefined;
|
|
191
|
+
// With no frame to wait for, there is no later to leave the rest of the objects to.
|
|
192
|
+
let batchSize = scheduleFrame === null ? Infinity : Math.max(1, options.batchSize ?? MOUNT_BATCH_SIZE);
|
|
193
|
+
// The time of the frame in which the last batch was made: the next frame says how long that batch took to show.
|
|
194
|
+
let lastBatchFrameTime = null;
|
|
195
|
+
const { subscribe, set } = writable(NOTHING_MOUNTED);
|
|
196
|
+
/** The list the subscribers have. */
|
|
197
|
+
let published = NOTHING_MOUNTED;
|
|
198
|
+
// The ids of the published list, made when someone asks and kept for as long as the list is of the same objects (see `publish`).
|
|
199
|
+
let idsOfPublishedFor = null;
|
|
200
|
+
let idsOfPublished = new Set();
|
|
201
|
+
let near = null;
|
|
202
|
+
let latestInput = null;
|
|
203
|
+
let latestView = null;
|
|
204
|
+
// What the last work was made from: the same again is no news.
|
|
205
|
+
let workedScene = null;
|
|
206
|
+
let workedEditingTextId = null;
|
|
207
|
+
let workedNear = null;
|
|
208
|
+
let cancelFrame = null;
|
|
209
|
+
let destroyed = false;
|
|
210
|
+
// Once the work has failed, the board is drawn whole from then on (and the failure is said once).
|
|
211
|
+
let failed = false;
|
|
212
|
+
function idsOfPublishedList() {
|
|
213
|
+
if (idsOfPublishedFor !== published) {
|
|
214
|
+
idsOfPublished = new Set(published.map((object) => object.id));
|
|
215
|
+
idsOfPublishedFor = published;
|
|
216
|
+
}
|
|
217
|
+
return idsOfPublished;
|
|
218
|
+
}
|
|
219
|
+
function publish(next) {
|
|
220
|
+
// A store tells its subscribers of every `set` of an object, equal or not: the same records again is no news.
|
|
221
|
+
if (sameRecords(next, published))
|
|
222
|
+
return;
|
|
223
|
+
// A drag hands over other records for the same objects, at every frame: the ids that were gathered for the list that was
|
|
224
|
+
// published are the ids of this one too, and gathering them again (a set of every id) would cost a good part of the frame.
|
|
225
|
+
if (idsOfPublishedFor === published && sameObjects(next, published))
|
|
226
|
+
idsOfPublishedFor = next;
|
|
227
|
+
published = next;
|
|
228
|
+
set(next);
|
|
229
|
+
}
|
|
230
|
+
function stopWaitingForFrame() {
|
|
231
|
+
cancelFrame?.();
|
|
232
|
+
cancelFrame = null;
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Learns from the frames how many objects a frame can take. The frame that comes after a batch is late when the batch was too
|
|
236
|
+
* much (the page could not draw it in time), so the next batch is half of it; frames on time let the batch grow by a few.
|
|
237
|
+
* A frame time that is not known, or a pause between frames (a hidden tab), teaches nothing.
|
|
238
|
+
*/
|
|
239
|
+
function adaptBatchSizeTo(frameTime) {
|
|
240
|
+
if (!batchSizeFollowsFrames || batchSize === Infinity)
|
|
241
|
+
return;
|
|
242
|
+
if (typeof frameTime !== 'number' || !Number.isFinite(frameTime)) {
|
|
243
|
+
lastBatchFrameTime = null;
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
const previous = lastBatchFrameTime;
|
|
247
|
+
lastBatchFrameTime = frameTime;
|
|
248
|
+
if (previous === null)
|
|
249
|
+
return;
|
|
250
|
+
const interval = frameTime - previous;
|
|
251
|
+
if (interval > PAUSED_FRAME_MS || interval < 0)
|
|
252
|
+
return;
|
|
253
|
+
if (interval > SLOW_FRAME_MS)
|
|
254
|
+
batchSize = Math.max(MIN_MOUNT_BATCH_SIZE, Math.floor(batchSize / 2));
|
|
255
|
+
else if (interval < QUICK_FRAME_MS)
|
|
256
|
+
batchSize = Math.min(MAX_MOUNT_BATCH_SIZE, batchSize + MOUNT_BATCH_GROWTH);
|
|
257
|
+
}
|
|
258
|
+
/** Every object has its element: the board is small, or nothing is known of the view, or the work failed. */
|
|
259
|
+
function publishWholeScene(scene) {
|
|
260
|
+
stopWaitingForFrame();
|
|
261
|
+
near = null;
|
|
262
|
+
workedScene = null;
|
|
263
|
+
workedNear = null;
|
|
264
|
+
publish(scene);
|
|
265
|
+
}
|
|
266
|
+
function giveUpCulling(error, scene) {
|
|
267
|
+
if (!failed) {
|
|
268
|
+
failed = true;
|
|
269
|
+
console.warn('Canvas: the board could not tell which objects are near the view, so every object is drawn', error);
|
|
270
|
+
}
|
|
271
|
+
publishWholeScene(scene);
|
|
272
|
+
}
|
|
273
|
+
/** Works out the list from the records and the near area, and makes what is wanted in the frames that follow when it is a lot. */
|
|
274
|
+
function mountNearObjects(input, nearArea, view) {
|
|
275
|
+
const { scene, editingTextId } = input;
|
|
276
|
+
// The objects that have an element now: the ones that keep it while they are inside the keep area.
|
|
277
|
+
const alreadyMade = idsOfPublishedList();
|
|
278
|
+
const wanted = [];
|
|
279
|
+
// How many of the wanted ones have no element yet: counted on the way, so that a pass in which nothing is missing (the frames of
|
|
280
|
+
// a drag) makes no list of them.
|
|
281
|
+
let missingCount = 0;
|
|
282
|
+
for (const object of scene) {
|
|
283
|
+
// A group draws nothing: its members are objects of their own.
|
|
284
|
+
if (!isBoxedObject(object))
|
|
285
|
+
continue;
|
|
286
|
+
const hasElementAlready = alreadyMade.has(object.id);
|
|
287
|
+
if (object.id !== editingTextId) {
|
|
288
|
+
const painted = paintedBoundsOf(object);
|
|
289
|
+
// An object is given an element inside the near area, and keeps it (it is not made again later) inside the keep area.
|
|
290
|
+
const keepsItsElement = hasElementAlready && rectsIntersect(nearArea.keepArea, painted);
|
|
291
|
+
if (!keepsItsElement && !rectsIntersect(nearArea.area, painted))
|
|
292
|
+
continue;
|
|
293
|
+
}
|
|
294
|
+
wanted.push(object);
|
|
295
|
+
if (!hasElementAlready)
|
|
296
|
+
missingCount += 1;
|
|
297
|
+
}
|
|
298
|
+
let next = wanted;
|
|
299
|
+
let moreToMake = false;
|
|
300
|
+
if (batchSize !== Infinity && missingCount > batchSize) {
|
|
301
|
+
const missing = wanted.filter((object) => !alreadyMade.has(object.id));
|
|
302
|
+
const makeNow = nearestIdsFirst(missing, view, batchSize);
|
|
303
|
+
next = wanted.filter((object) => alreadyMade.has(object.id) || makeNow.has(object.id));
|
|
304
|
+
moreToMake = true;
|
|
305
|
+
}
|
|
306
|
+
workedScene = scene;
|
|
307
|
+
workedEditingTextId = editingTextId;
|
|
308
|
+
workedNear = nearArea;
|
|
309
|
+
publish(next);
|
|
310
|
+
if (!moreToMake) {
|
|
311
|
+
stopWaitingForFrame();
|
|
312
|
+
// The next burst starts from a fresh reading of the frames.
|
|
313
|
+
lastBatchFrameTime = null;
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
if (cancelFrame !== null || scheduleFrame === null)
|
|
317
|
+
return;
|
|
318
|
+
cancelFrame = scheduleFrame((frameTime) => {
|
|
319
|
+
cancelFrame = null;
|
|
320
|
+
if (destroyed || latestInput === null || near === null || latestView === null)
|
|
321
|
+
return;
|
|
322
|
+
adaptBatchSizeTo(frameTime);
|
|
323
|
+
try {
|
|
324
|
+
mountNearObjects(latestInput, near, latestView);
|
|
325
|
+
}
|
|
326
|
+
catch (error) {
|
|
327
|
+
giveUpCulling(error, latestInput.scene);
|
|
328
|
+
}
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
return {
|
|
332
|
+
subscribe,
|
|
333
|
+
take(input) {
|
|
334
|
+
if (destroyed)
|
|
335
|
+
return;
|
|
336
|
+
latestInput = input;
|
|
337
|
+
try {
|
|
338
|
+
if (failed || input.scene.length < minimumObjects) {
|
|
339
|
+
publishWholeScene(input.scene);
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
342
|
+
const view = visibleWorldRect(input.viewport, input.viewSize);
|
|
343
|
+
if (!isUsableView(view) || !Number.isFinite(input.viewport.zoom) || input.viewport.zoom <= 0) {
|
|
344
|
+
publishWholeScene(input.scene);
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
latestView = view;
|
|
348
|
+
if (near === null || !isStillNear(near, view, input.viewport.zoom))
|
|
349
|
+
near = nearAreaAround(view, input.viewport.zoom, near);
|
|
350
|
+
if (input.scene === workedScene && input.editingTextId === workedEditingTextId && near === workedNear)
|
|
351
|
+
return;
|
|
352
|
+
mountNearObjects(input, near, view);
|
|
353
|
+
}
|
|
354
|
+
catch (error) {
|
|
355
|
+
giveUpCulling(error, input.scene);
|
|
356
|
+
}
|
|
357
|
+
},
|
|
358
|
+
destroy() {
|
|
359
|
+
destroyed = true;
|
|
360
|
+
stopWaitingForFrame();
|
|
361
|
+
}
|
|
362
|
+
};
|
|
363
|
+
}
|