@dxos/react-ui-canvas 0.7.5-feature-compute.4d9d99a
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/LICENSE +8 -0
- package/README.md +1 -0
- package/dist/lib/browser/index.mjs +605 -0
- package/dist/lib/browser/index.mjs.map +7 -0
- package/dist/lib/browser/meta.json +1 -0
- package/dist/lib/node/index.cjs +641 -0
- package/dist/lib/node/index.cjs.map +7 -0
- package/dist/lib/node/meta.json +1 -0
- package/dist/lib/node-esm/index.mjs +607 -0
- package/dist/lib/node-esm/index.mjs.map +7 -0
- package/dist/lib/node-esm/meta.json +1 -0
- package/dist/types/src/components/Canvas/Canvas.d.ts +15 -0
- package/dist/types/src/components/Canvas/Canvas.d.ts.map +1 -0
- package/dist/types/src/components/Canvas/Canvas.stories.d.ts +9 -0
- package/dist/types/src/components/Canvas/Canvas.stories.d.ts.map +1 -0
- package/dist/types/src/components/Canvas/index.d.ts +2 -0
- package/dist/types/src/components/Canvas/index.d.ts.map +1 -0
- package/dist/types/src/components/FPS.d.ts +9 -0
- package/dist/types/src/components/FPS.d.ts.map +1 -0
- package/dist/types/src/components/Grid/Grid.d.ts +19 -0
- package/dist/types/src/components/Grid/Grid.d.ts.map +1 -0
- package/dist/types/src/components/Grid/Grid.stories.d.ts +8 -0
- package/dist/types/src/components/Grid/Grid.stories.d.ts.map +1 -0
- package/dist/types/src/components/Grid/index.d.ts +2 -0
- package/dist/types/src/components/Grid/index.d.ts.map +1 -0
- package/dist/types/src/components/index.d.ts +4 -0
- package/dist/types/src/components/index.d.ts.map +1 -0
- package/dist/types/src/hooks/index.d.ts +4 -0
- package/dist/types/src/hooks/index.d.ts.map +1 -0
- package/dist/types/src/hooks/projection.d.ts +58 -0
- package/dist/types/src/hooks/projection.d.ts.map +1 -0
- package/dist/types/src/hooks/useCanvasContext.d.ts +13 -0
- package/dist/types/src/hooks/useCanvasContext.d.ts.map +1 -0
- package/dist/types/src/hooks/useWheel.d.ts +8 -0
- package/dist/types/src/hooks/useWheel.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +5 -0
- package/dist/types/src/index.d.ts.map +1 -0
- package/dist/types/src/types.d.ts +20 -0
- package/dist/types/src/types.d.ts.map +1 -0
- package/dist/types/src/util/index.d.ts +3 -0
- package/dist/types/src/util/index.d.ts.map +1 -0
- package/dist/types/src/util/svg.d.ts +33 -0
- package/dist/types/src/util/svg.d.ts.map +1 -0
- package/dist/types/src/util/svg.stories.d.ts +6 -0
- package/dist/types/src/util/svg.stories.d.ts.map +1 -0
- package/dist/types/src/util/util.d.ts +17 -0
- package/dist/types/src/util/util.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -0
- package/package.json +61 -0
- package/src/components/Canvas/Canvas.stories.tsx +109 -0
- package/src/components/Canvas/Canvas.tsx +89 -0
- package/src/components/Canvas/index.ts +5 -0
- package/src/components/FPS.tsx +98 -0
- package/src/components/Grid/Grid.stories.tsx +41 -0
- package/src/components/Grid/Grid.tsx +87 -0
- package/src/components/Grid/index.ts +5 -0
- package/src/components/index.ts +7 -0
- package/src/hooks/index.ts +7 -0
- package/src/hooks/projection.tsx +156 -0
- package/src/hooks/useCanvasContext.tsx +29 -0
- package/src/hooks/useWheel.tsx +107 -0
- package/src/index.ts +8 -0
- package/src/types.ts +13 -0
- package/src/util/index.ts +6 -0
- package/src/util/svg.stories.tsx +45 -0
- package/src/util/svg.tsx +131 -0
- package/src/util/util.ts +50 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
Copyright (c) 2022 DXOS
|
|
3
|
+
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
5
|
+
|
|
6
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
7
|
+
|
|
8
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# @dxos/react-ui-canvas-editor
|
|
@@ -0,0 +1,605 @@
|
|
|
1
|
+
// packages/ui/react-ui-canvas/src/components/Canvas/Canvas.tsx
|
|
2
|
+
import React2, { forwardRef, useEffect as useEffect2, useImperativeHandle, useMemo, useState } from "react";
|
|
3
|
+
import { useResizeDetector } from "react-resize-detector";
|
|
4
|
+
import { mx as mx2 } from "@dxos/react-ui-theme";
|
|
5
|
+
|
|
6
|
+
// packages/ui/react-ui-canvas/src/hooks/projection.tsx
|
|
7
|
+
import * as d3 from "d3";
|
|
8
|
+
import { applyToPoints, compose, identity, inverse, scale as scaleMatrix, translate as translateMatrix } from "transformation-matrix";
|
|
9
|
+
var defaultOrigin = {
|
|
10
|
+
x: 0,
|
|
11
|
+
y: 0
|
|
12
|
+
};
|
|
13
|
+
var ProjectionMapper = class {
|
|
14
|
+
constructor(bounds, scale, offset) {
|
|
15
|
+
this._bounds = {
|
|
16
|
+
width: 0,
|
|
17
|
+
height: 0
|
|
18
|
+
};
|
|
19
|
+
this._scale = 1;
|
|
20
|
+
this._offset = defaultOrigin;
|
|
21
|
+
this._toScreen = identity();
|
|
22
|
+
this._toModel = identity();
|
|
23
|
+
if (bounds && scale && offset) {
|
|
24
|
+
this.update(bounds, scale, offset);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
update(bounds, scale, offset) {
|
|
28
|
+
this._bounds = bounds;
|
|
29
|
+
this._scale = scale;
|
|
30
|
+
this._offset = offset;
|
|
31
|
+
this._toScreen = compose(
|
|
32
|
+
// NOTE: Order is important.
|
|
33
|
+
translateMatrix(this._offset.x, this._offset.y),
|
|
34
|
+
scaleMatrix(this._scale)
|
|
35
|
+
);
|
|
36
|
+
this._toModel = inverse(this._toScreen);
|
|
37
|
+
return this;
|
|
38
|
+
}
|
|
39
|
+
get bounds() {
|
|
40
|
+
return this._bounds;
|
|
41
|
+
}
|
|
42
|
+
get scale() {
|
|
43
|
+
return this._scale;
|
|
44
|
+
}
|
|
45
|
+
get offset() {
|
|
46
|
+
return this._offset;
|
|
47
|
+
}
|
|
48
|
+
toScreen(points) {
|
|
49
|
+
return applyToPoints(this._toScreen, points);
|
|
50
|
+
}
|
|
51
|
+
toModel(points) {
|
|
52
|
+
return applyToPoints(this._toModel, points);
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
var getZoomTransform = ({ scale, offset, pos, newScale }) => {
|
|
56
|
+
return {
|
|
57
|
+
scale: newScale,
|
|
58
|
+
offset: {
|
|
59
|
+
x: pos.x - (pos.x - offset.x) * (newScale / scale),
|
|
60
|
+
y: pos.y - (pos.y - offset.y) * (newScale / scale)
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
var zoomInPlace = (setTransform, pos, offset, current, next, delay = 200) => {
|
|
65
|
+
const is = d3.interpolate(current, next);
|
|
66
|
+
d3.transition().ease(d3.easeSinOut).duration(delay).tween("zoom", () => (t) => {
|
|
67
|
+
const newScale = is(t);
|
|
68
|
+
setTransform(getZoomTransform({
|
|
69
|
+
scale: current,
|
|
70
|
+
newScale,
|
|
71
|
+
offset,
|
|
72
|
+
pos
|
|
73
|
+
}));
|
|
74
|
+
});
|
|
75
|
+
};
|
|
76
|
+
var noop = () => {
|
|
77
|
+
};
|
|
78
|
+
var zoomTo = (setTransform, current, next, delay = 200, cb = noop) => {
|
|
79
|
+
const is = d3.interpolateObject({
|
|
80
|
+
scale: current.scale,
|
|
81
|
+
...current.offset
|
|
82
|
+
}, {
|
|
83
|
+
scale: next.scale,
|
|
84
|
+
...next.offset
|
|
85
|
+
});
|
|
86
|
+
d3.transition().ease(d3.easeSinOut).duration(delay).tween("zoom", () => (t) => {
|
|
87
|
+
const { scale, x, y } = is(t);
|
|
88
|
+
setTransform({
|
|
89
|
+
scale,
|
|
90
|
+
offset: {
|
|
91
|
+
x,
|
|
92
|
+
y
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
}).on("end", cb);
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
// packages/ui/react-ui-canvas/src/hooks/useCanvasContext.tsx
|
|
99
|
+
import { createContext, useContext } from "react";
|
|
100
|
+
import { raise } from "@dxos/debug";
|
|
101
|
+
var CanvasContext = /* @__PURE__ */ createContext(null);
|
|
102
|
+
var useCanvasContext = () => {
|
|
103
|
+
return useContext(CanvasContext) ?? raise(new Error("Missing CanvasContext"));
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
// packages/ui/react-ui-canvas/src/hooks/useWheel.tsx
|
|
107
|
+
import { bindAll } from "bind-event-listener";
|
|
108
|
+
import { useEffect } from "react";
|
|
109
|
+
|
|
110
|
+
// packages/ui/react-ui-canvas/src/util/svg.tsx
|
|
111
|
+
import React from "react";
|
|
112
|
+
import { mx } from "@dxos/react-ui-theme";
|
|
113
|
+
var createPath = (points, join = false) => {
|
|
114
|
+
return [
|
|
115
|
+
"M",
|
|
116
|
+
points.map(({ x, y }) => `${x},${y}`).join(" L "),
|
|
117
|
+
join ? "Z" : ""
|
|
118
|
+
].join(" ");
|
|
119
|
+
};
|
|
120
|
+
var Markers = ({ id = "dx-marker", classNames }) => {
|
|
121
|
+
return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(Arrow, {
|
|
122
|
+
id: `${id}-arrow-start`,
|
|
123
|
+
dir: "start",
|
|
124
|
+
classNames
|
|
125
|
+
}), /* @__PURE__ */ React.createElement(Arrow, {
|
|
126
|
+
id: `${id}-arrow-end`,
|
|
127
|
+
dir: "end",
|
|
128
|
+
classNames
|
|
129
|
+
}), /* @__PURE__ */ React.createElement(Arrow, {
|
|
130
|
+
id: `${id}-triangle-start`,
|
|
131
|
+
dir: "start",
|
|
132
|
+
closed: true,
|
|
133
|
+
classNames
|
|
134
|
+
}), /* @__PURE__ */ React.createElement(Arrow, {
|
|
135
|
+
id: `${id}-triangle-end`,
|
|
136
|
+
dir: "end",
|
|
137
|
+
closed: true,
|
|
138
|
+
classNames
|
|
139
|
+
}), /* @__PURE__ */ React.createElement(Marker, {
|
|
140
|
+
id: `${id}-circle`,
|
|
141
|
+
pos: {
|
|
142
|
+
x: 8,
|
|
143
|
+
y: 8
|
|
144
|
+
},
|
|
145
|
+
size: {
|
|
146
|
+
width: 16,
|
|
147
|
+
height: 16
|
|
148
|
+
}
|
|
149
|
+
}, /* @__PURE__ */ React.createElement("circle", {
|
|
150
|
+
cx: 8,
|
|
151
|
+
cy: 8,
|
|
152
|
+
r: 5,
|
|
153
|
+
stroke: "context-stroke",
|
|
154
|
+
className: mx(classNames)
|
|
155
|
+
})));
|
|
156
|
+
};
|
|
157
|
+
var Marker = ({ id, className, children, pos: { x: refX, y: refY }, size: { width: markerWidth, height: markerHeight }, fill, ...rest }) => /* @__PURE__ */ React.createElement("marker", {
|
|
158
|
+
id,
|
|
159
|
+
className,
|
|
160
|
+
refX,
|
|
161
|
+
refY,
|
|
162
|
+
markerWidth,
|
|
163
|
+
markerHeight,
|
|
164
|
+
markerUnits: "strokeWidth",
|
|
165
|
+
orient: "auto",
|
|
166
|
+
...rest
|
|
167
|
+
}, children);
|
|
168
|
+
var Arrow = ({ classNames, id, size = 16, dir = "end", closed = false }) => /* @__PURE__ */ React.createElement(Marker, {
|
|
169
|
+
id,
|
|
170
|
+
size: {
|
|
171
|
+
width: size,
|
|
172
|
+
height: size
|
|
173
|
+
},
|
|
174
|
+
pos: dir === "end" ? {
|
|
175
|
+
x: size,
|
|
176
|
+
y: size / 2
|
|
177
|
+
} : {
|
|
178
|
+
x: 0,
|
|
179
|
+
y: size / 2
|
|
180
|
+
}
|
|
181
|
+
}, /* @__PURE__ */ React.createElement("path", {
|
|
182
|
+
fill: closed ? void 0 : "none",
|
|
183
|
+
stroke: "context-stroke",
|
|
184
|
+
className: mx(classNames),
|
|
185
|
+
d: createPath(dir === "end" ? [
|
|
186
|
+
{
|
|
187
|
+
x: 1,
|
|
188
|
+
y: 1
|
|
189
|
+
},
|
|
190
|
+
{
|
|
191
|
+
x: size,
|
|
192
|
+
y: size / 2
|
|
193
|
+
},
|
|
194
|
+
{
|
|
195
|
+
x: 1,
|
|
196
|
+
y: size - 1
|
|
197
|
+
}
|
|
198
|
+
] : [
|
|
199
|
+
{
|
|
200
|
+
x: size - 1,
|
|
201
|
+
y: 1
|
|
202
|
+
},
|
|
203
|
+
{
|
|
204
|
+
x: 0,
|
|
205
|
+
y: size / 2
|
|
206
|
+
},
|
|
207
|
+
{
|
|
208
|
+
x: size - 1,
|
|
209
|
+
y: size - 1
|
|
210
|
+
}
|
|
211
|
+
], closed)
|
|
212
|
+
}));
|
|
213
|
+
var GridPattern = ({ classNames, id, size, offset }) => /* @__PURE__ */ React.createElement("pattern", {
|
|
214
|
+
id,
|
|
215
|
+
x: (size / 2 + offset.x) % size,
|
|
216
|
+
y: (size / 2 + offset.y) % size,
|
|
217
|
+
width: size,
|
|
218
|
+
height: size,
|
|
219
|
+
patternUnits: "userSpaceOnUse"
|
|
220
|
+
}, /* @__PURE__ */ React.createElement("g", {
|
|
221
|
+
className: mx(classNames)
|
|
222
|
+
}, /* @__PURE__ */ React.createElement("line", {
|
|
223
|
+
x1: 0,
|
|
224
|
+
y1: size / 2,
|
|
225
|
+
x2: size,
|
|
226
|
+
y2: size / 2
|
|
227
|
+
}), /* @__PURE__ */ React.createElement("line", {
|
|
228
|
+
x1: size / 2,
|
|
229
|
+
y1: 0,
|
|
230
|
+
x2: size / 2,
|
|
231
|
+
y2: size
|
|
232
|
+
})));
|
|
233
|
+
|
|
234
|
+
// packages/ui/react-ui-canvas/src/util/util.ts
|
|
235
|
+
var logged = false;
|
|
236
|
+
var getRelativePoint = (el, ev) => {
|
|
237
|
+
const rect = el.getBoundingClientRect();
|
|
238
|
+
return {
|
|
239
|
+
x: ev.clientX - rect.x,
|
|
240
|
+
y: ev.clientY - rect.top
|
|
241
|
+
};
|
|
242
|
+
};
|
|
243
|
+
var testId = (id, inspect = false) => {
|
|
244
|
+
if (inspect) {
|
|
245
|
+
if (!logged) {
|
|
246
|
+
console.log("Open storybook in expanded window;\nthen run INSPECT()");
|
|
247
|
+
logged = true;
|
|
248
|
+
}
|
|
249
|
+
window.INSPECT = () => {
|
|
250
|
+
const el = document.querySelector(`[data-test-id="${id}"]`);
|
|
251
|
+
window.inspect(el);
|
|
252
|
+
console.log(el);
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
return {
|
|
256
|
+
[DATA_TEST_ID]: id
|
|
257
|
+
};
|
|
258
|
+
};
|
|
259
|
+
var inspectElement = (el) => {
|
|
260
|
+
window.INSPECT = () => {
|
|
261
|
+
window.inspect(el);
|
|
262
|
+
window.element = el;
|
|
263
|
+
console.log("Open storybook in expanded window;\nthen run INSPECT()");
|
|
264
|
+
console.log(el);
|
|
265
|
+
};
|
|
266
|
+
};
|
|
267
|
+
var DATA_TEST_ID = "data-test-id";
|
|
268
|
+
|
|
269
|
+
// packages/ui/react-ui-canvas/src/hooks/useWheel.tsx
|
|
270
|
+
var defaultOptions = {
|
|
271
|
+
zoom: true
|
|
272
|
+
};
|
|
273
|
+
var useWheel = (options = defaultOptions) => {
|
|
274
|
+
const { root, setProjection } = useCanvasContext();
|
|
275
|
+
useEffect(() => {
|
|
276
|
+
if (!root) {
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
return bindAll(root, [
|
|
280
|
+
{
|
|
281
|
+
type: "wheel",
|
|
282
|
+
options: {
|
|
283
|
+
capture: true,
|
|
284
|
+
passive: false
|
|
285
|
+
},
|
|
286
|
+
listener: (ev) => {
|
|
287
|
+
const zooming = isWheelZooming(ev);
|
|
288
|
+
if (!hasFocus(root) && !zooming) {
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
ev.preventDefault();
|
|
292
|
+
if (zooming && !options.zoom) {
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
if (ev.ctrlKey) {
|
|
296
|
+
if (!root) {
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
setProjection(({ scale, offset }) => {
|
|
300
|
+
const pos = getRelativePoint(root, ev);
|
|
301
|
+
const scaleSensitivity = 0.01;
|
|
302
|
+
const newScale = scale * Math.exp(-ev.deltaY * scaleSensitivity);
|
|
303
|
+
return getZoomTransform({
|
|
304
|
+
scale,
|
|
305
|
+
offset,
|
|
306
|
+
newScale,
|
|
307
|
+
pos
|
|
308
|
+
});
|
|
309
|
+
});
|
|
310
|
+
} else {
|
|
311
|
+
setProjection(({ scale, offset: { x, y } }) => {
|
|
312
|
+
return {
|
|
313
|
+
scale,
|
|
314
|
+
offset: {
|
|
315
|
+
x: x - ev.deltaX,
|
|
316
|
+
y: y - ev.deltaY
|
|
317
|
+
}
|
|
318
|
+
};
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
]);
|
|
324
|
+
}, [
|
|
325
|
+
root
|
|
326
|
+
]);
|
|
327
|
+
};
|
|
328
|
+
var isWheelZooming = (ev) => {
|
|
329
|
+
if (ev.ctrlKey || ev.metaKey) {
|
|
330
|
+
return Math.abs(ev.deltaY) > 0 || Math.abs(ev.deltaZ) > 0;
|
|
331
|
+
}
|
|
332
|
+
return false;
|
|
333
|
+
};
|
|
334
|
+
var hasFocus = (element) => {
|
|
335
|
+
const activeElement = document.activeElement;
|
|
336
|
+
if (!activeElement) {
|
|
337
|
+
return false;
|
|
338
|
+
}
|
|
339
|
+
let shadowActive = activeElement;
|
|
340
|
+
while (shadowActive?.shadowRoot?.activeElement) {
|
|
341
|
+
shadowActive = shadowActive.shadowRoot.activeElement;
|
|
342
|
+
}
|
|
343
|
+
let current = element;
|
|
344
|
+
while (current) {
|
|
345
|
+
if (current === activeElement || current === shadowActive) {
|
|
346
|
+
return true;
|
|
347
|
+
}
|
|
348
|
+
current = current.parentElement;
|
|
349
|
+
}
|
|
350
|
+
return false;
|
|
351
|
+
};
|
|
352
|
+
|
|
353
|
+
// packages/ui/react-ui-canvas/src/components/Canvas/Canvas.tsx
|
|
354
|
+
var Canvas = /* @__PURE__ */ forwardRef(({ children, classNames, scale: _scale = 1, offset: _offset = defaultOrigin, ...props }, forwardedRef) => {
|
|
355
|
+
const { ref, width = 0, height = 0 } = useResizeDetector();
|
|
356
|
+
const [ready, setReady] = useState(false);
|
|
357
|
+
const [{ scale, offset }, setProjection] = useState({
|
|
358
|
+
scale: _scale,
|
|
359
|
+
offset: _offset
|
|
360
|
+
});
|
|
361
|
+
useEffect2(() => {
|
|
362
|
+
if (width && height && offset === defaultOrigin) {
|
|
363
|
+
setProjection({
|
|
364
|
+
scale,
|
|
365
|
+
offset: {
|
|
366
|
+
x: width / 2,
|
|
367
|
+
y: height / 2
|
|
368
|
+
}
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
}, [
|
|
372
|
+
width,
|
|
373
|
+
height,
|
|
374
|
+
scale,
|
|
375
|
+
offset
|
|
376
|
+
]);
|
|
377
|
+
const projection = useMemo(() => new ProjectionMapper(), []);
|
|
378
|
+
useEffect2(() => {
|
|
379
|
+
projection.update({
|
|
380
|
+
width,
|
|
381
|
+
height
|
|
382
|
+
}, scale, offset);
|
|
383
|
+
if (offset !== defaultOrigin) {
|
|
384
|
+
setReady(true);
|
|
385
|
+
}
|
|
386
|
+
}, [
|
|
387
|
+
projection,
|
|
388
|
+
scale,
|
|
389
|
+
offset,
|
|
390
|
+
width,
|
|
391
|
+
height
|
|
392
|
+
]);
|
|
393
|
+
const styles = useMemo(() => {
|
|
394
|
+
return {
|
|
395
|
+
// NOTE: Order is important.
|
|
396
|
+
transform: `translate(${offset.x}px, ${offset.y}px) scale(${scale})`,
|
|
397
|
+
visibility: width && height ? "visible" : "hidden"
|
|
398
|
+
};
|
|
399
|
+
}, [
|
|
400
|
+
scale,
|
|
401
|
+
offset
|
|
402
|
+
]);
|
|
403
|
+
useImperativeHandle(forwardedRef, () => {
|
|
404
|
+
return {
|
|
405
|
+
setProjection: async (projection2) => {
|
|
406
|
+
setProjection(projection2);
|
|
407
|
+
}
|
|
408
|
+
};
|
|
409
|
+
}, [
|
|
410
|
+
ref
|
|
411
|
+
]);
|
|
412
|
+
return /* @__PURE__ */ React2.createElement(CanvasContext.Provider, {
|
|
413
|
+
value: {
|
|
414
|
+
root: ref.current,
|
|
415
|
+
ready,
|
|
416
|
+
width,
|
|
417
|
+
height,
|
|
418
|
+
scale,
|
|
419
|
+
offset,
|
|
420
|
+
styles,
|
|
421
|
+
projection,
|
|
422
|
+
setProjection
|
|
423
|
+
}
|
|
424
|
+
}, /* @__PURE__ */ React2.createElement("div", {
|
|
425
|
+
role: "none",
|
|
426
|
+
...props,
|
|
427
|
+
className: mx2("absolute inset-0 overflow-hidden", classNames),
|
|
428
|
+
ref
|
|
429
|
+
}, ready ? children : null));
|
|
430
|
+
});
|
|
431
|
+
|
|
432
|
+
// packages/ui/react-ui-canvas/src/components/FPS.tsx
|
|
433
|
+
import React3, { useEffect as useEffect3, useReducer, useRef } from "react";
|
|
434
|
+
import { mx as mx3 } from "@dxos/react-ui-theme";
|
|
435
|
+
var SEC = 1e3;
|
|
436
|
+
var FPS = ({ classNames, width = 60, height = 30, bar = "bg-cyan-500" }) => {
|
|
437
|
+
const [{ fps, max, len }, dispatch] = useReducer((state) => {
|
|
438
|
+
const currentTime = Date.now();
|
|
439
|
+
if (currentTime > state.prevTime + SEC) {
|
|
440
|
+
const nextFPS = [
|
|
441
|
+
...new Array(Math.floor((currentTime - state.prevTime - SEC) / SEC)).fill(0),
|
|
442
|
+
Math.max(1, Math.round(state.frames * SEC / (currentTime - state.prevTime)))
|
|
443
|
+
];
|
|
444
|
+
return {
|
|
445
|
+
max: Math.max(state.max, ...nextFPS),
|
|
446
|
+
len: Math.min(state.len + nextFPS.length, width),
|
|
447
|
+
fps: [
|
|
448
|
+
...state.fps,
|
|
449
|
+
...nextFPS
|
|
450
|
+
].slice(-width),
|
|
451
|
+
frames: 1,
|
|
452
|
+
prevTime: currentTime
|
|
453
|
+
};
|
|
454
|
+
} else {
|
|
455
|
+
return {
|
|
456
|
+
...state,
|
|
457
|
+
frames: state.frames + 1
|
|
458
|
+
};
|
|
459
|
+
}
|
|
460
|
+
}, {
|
|
461
|
+
max: 0,
|
|
462
|
+
len: 0,
|
|
463
|
+
fps: [],
|
|
464
|
+
frames: 0,
|
|
465
|
+
prevTime: Date.now()
|
|
466
|
+
});
|
|
467
|
+
const requestRef = useRef();
|
|
468
|
+
const tick = () => {
|
|
469
|
+
dispatch();
|
|
470
|
+
requestRef.current = requestAnimationFrame(tick);
|
|
471
|
+
};
|
|
472
|
+
useEffect3(() => {
|
|
473
|
+
requestRef.current = requestAnimationFrame(tick);
|
|
474
|
+
return () => {
|
|
475
|
+
if (requestRef.current) {
|
|
476
|
+
cancelAnimationFrame(requestRef.current);
|
|
477
|
+
}
|
|
478
|
+
};
|
|
479
|
+
}, []);
|
|
480
|
+
return /* @__PURE__ */ React3.createElement("div", {
|
|
481
|
+
style: {
|
|
482
|
+
width: width + 6
|
|
483
|
+
},
|
|
484
|
+
className: mx3("relative flex flex-col p-0.5", "bg-base text-xs text-subdued font-thin pointer-events-none border border-separator", classNames)
|
|
485
|
+
}, /* @__PURE__ */ React3.createElement("div", null, fps[len - 1], " FPS"), /* @__PURE__ */ React3.createElement("div", {
|
|
486
|
+
className: "w-full relative",
|
|
487
|
+
style: {
|
|
488
|
+
height
|
|
489
|
+
}
|
|
490
|
+
}, fps.map((frame, i) => /* @__PURE__ */ React3.createElement("div", {
|
|
491
|
+
key: `fps-${i}`,
|
|
492
|
+
className: bar,
|
|
493
|
+
style: {
|
|
494
|
+
position: "absolute",
|
|
495
|
+
bottom: 0,
|
|
496
|
+
right: `${len - 1 - i}px`,
|
|
497
|
+
height: `${height * frame / max}px`,
|
|
498
|
+
width: 1
|
|
499
|
+
}
|
|
500
|
+
}))));
|
|
501
|
+
};
|
|
502
|
+
|
|
503
|
+
// packages/ui/react-ui-canvas/src/components/Grid/Grid.tsx
|
|
504
|
+
import React4, { forwardRef as forwardRef2, useMemo as useMemo2, useId } from "react";
|
|
505
|
+
import { useForwardedRef } from "@dxos/react-ui";
|
|
506
|
+
import { mx as mx4 } from "@dxos/react-ui-theme";
|
|
507
|
+
var gridRatios = [
|
|
508
|
+
1 / 4,
|
|
509
|
+
1,
|
|
510
|
+
4,
|
|
511
|
+
16
|
|
512
|
+
];
|
|
513
|
+
var defaultGridSize = 16;
|
|
514
|
+
var defaultOffset = {
|
|
515
|
+
x: 0,
|
|
516
|
+
y: 0
|
|
517
|
+
};
|
|
518
|
+
var createId = (parent, grid) => `dx-canvas-grid-${parent}-${grid}`;
|
|
519
|
+
var GridComponent = /* @__PURE__ */ forwardRef2(({ size: gridSize = defaultGridSize, scale = 1, offset = defaultOffset, showAxes = true, classNames }, forwardedRef) => {
|
|
520
|
+
const svgRef = useForwardedRef(forwardedRef);
|
|
521
|
+
const instanceId = useId();
|
|
522
|
+
const grids = useMemo2(() => gridRatios.map((ratio) => ({
|
|
523
|
+
id: ratio,
|
|
524
|
+
size: ratio * gridSize * scale
|
|
525
|
+
})).filter(({ size }) => size >= gridSize && size <= 256), [
|
|
526
|
+
gridSize,
|
|
527
|
+
scale
|
|
528
|
+
]);
|
|
529
|
+
const { width = 0, height = 0 } = svgRef.current?.getBoundingClientRect() ?? {};
|
|
530
|
+
return /* @__PURE__ */ React4.createElement("svg", {
|
|
531
|
+
...testId("dx-canvas-grid"),
|
|
532
|
+
ref: svgRef,
|
|
533
|
+
className: mx4("absolute inset-0 w-full h-full pointer-events-none touch-none select-none", "stroke-neutral-500", classNames)
|
|
534
|
+
}, /* @__PURE__ */ React4.createElement("defs", null, grids.map(({ id, size }) => /* @__PURE__ */ React4.createElement(GridPattern, {
|
|
535
|
+
key: id,
|
|
536
|
+
id: createId(instanceId, id),
|
|
537
|
+
offset,
|
|
538
|
+
size
|
|
539
|
+
}))), showAxes && /* @__PURE__ */ React4.createElement(React4.Fragment, null, /* @__PURE__ */ React4.createElement("line", {
|
|
540
|
+
x1: 0,
|
|
541
|
+
y1: offset.y,
|
|
542
|
+
x2: width,
|
|
543
|
+
y2: offset.y,
|
|
544
|
+
className: "stroke-neutral-500 opacity-40"
|
|
545
|
+
}), /* @__PURE__ */ React4.createElement("line", {
|
|
546
|
+
x1: offset.x,
|
|
547
|
+
y1: 0,
|
|
548
|
+
x2: offset.x,
|
|
549
|
+
y2: height,
|
|
550
|
+
className: "stroke-neutral-500 opacity-40"
|
|
551
|
+
})), /* @__PURE__ */ React4.createElement("g", null, grids.map(({ id }, i) => /* @__PURE__ */ React4.createElement("rect", {
|
|
552
|
+
key: id,
|
|
553
|
+
opacity: 0.1 + i * 0.05,
|
|
554
|
+
fill: `url(#${createId(instanceId, id)})`,
|
|
555
|
+
width: "100%",
|
|
556
|
+
height: "100%"
|
|
557
|
+
}))));
|
|
558
|
+
});
|
|
559
|
+
var Grid = (props) => {
|
|
560
|
+
const { scale, offset } = useCanvasContext();
|
|
561
|
+
return /* @__PURE__ */ React4.createElement(GridComponent, {
|
|
562
|
+
...props,
|
|
563
|
+
scale,
|
|
564
|
+
offset
|
|
565
|
+
});
|
|
566
|
+
};
|
|
567
|
+
|
|
568
|
+
// packages/ui/react-ui-canvas/src/types.ts
|
|
569
|
+
import { S } from "@dxos/effect";
|
|
570
|
+
var Point = S.Struct({
|
|
571
|
+
x: S.Number,
|
|
572
|
+
y: S.Number
|
|
573
|
+
});
|
|
574
|
+
var Dimension = S.Struct({
|
|
575
|
+
width: S.Number,
|
|
576
|
+
height: S.Number
|
|
577
|
+
});
|
|
578
|
+
var Rect = S.extend(Point, Dimension);
|
|
579
|
+
export {
|
|
580
|
+
Arrow,
|
|
581
|
+
Canvas,
|
|
582
|
+
CanvasContext,
|
|
583
|
+
DATA_TEST_ID,
|
|
584
|
+
Dimension,
|
|
585
|
+
FPS,
|
|
586
|
+
Grid,
|
|
587
|
+
GridComponent,
|
|
588
|
+
GridPattern,
|
|
589
|
+
Marker,
|
|
590
|
+
Markers,
|
|
591
|
+
Point,
|
|
592
|
+
ProjectionMapper,
|
|
593
|
+
Rect,
|
|
594
|
+
createPath,
|
|
595
|
+
defaultOrigin,
|
|
596
|
+
getRelativePoint,
|
|
597
|
+
getZoomTransform,
|
|
598
|
+
inspectElement,
|
|
599
|
+
testId,
|
|
600
|
+
useCanvasContext,
|
|
601
|
+
useWheel,
|
|
602
|
+
zoomInPlace,
|
|
603
|
+
zoomTo
|
|
604
|
+
};
|
|
605
|
+
//# sourceMappingURL=index.mjs.map
|