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