react-two.js 0.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.
@@ -0,0 +1,508 @@
1
+ import { jsx as v, Fragment as y } from "react/jsx-runtime";
2
+ import h, { createContext as I, useContext as L, useRef as m, useEffect as l, useState as T, useImperativeHandle as R, useLayoutEffect as G } from "react";
3
+ import w from "two.js";
4
+ const E = I({ two: null, parent: null, width: 0, height: 0 }), g = () => L(E), j = (o, a = []) => {
5
+ const { two: u } = g(), r = m(o);
6
+ l(() => {
7
+ r.current = o;
8
+ }, [o, ...a]), l(() => {
9
+ if (!u)
10
+ return;
11
+ let i = 0;
12
+ return u.bind("update", f), () => u.unbind("update", f);
13
+ function f(t, n) {
14
+ var e;
15
+ i += n / 1e3, (e = r.current) == null || e.call(r, i, n);
16
+ }
17
+ }, [u, ...a]);
18
+ }, H = (o) => {
19
+ const { two: a, parent: u } = g(), r = m(null), [i, f] = T({
20
+ two: a,
21
+ parent: u,
22
+ width: 0,
23
+ height: 0
24
+ });
25
+ l(t, [o]);
26
+ function t() {
27
+ let n = () => {
28
+ };
29
+ if (!a) {
30
+ let c = function() {
31
+ const b = s.width !== k, C = !1;
32
+ b && (k = s.width), (b || C) && f((q) => ({ ...q, width: k, height: S }));
33
+ };
34
+ const d = { ...o };
35
+ delete d.children;
36
+ const s = new w(d).appendTo(r.current);
37
+ let k = s.width, S = s.height;
38
+ f({ two: s, parent: s.scene, width: k, height: S }), s.bind("update", c), n = () => {
39
+ var C;
40
+ (C = s.renderer.domElement.parentElement) == null || C.removeChild(
41
+ s.renderer.domElement
42
+ ), s.unbind("update", c);
43
+ const b = w.Instances.indexOf(s);
44
+ w.Instances.splice(b, 1), s.pause();
45
+ };
46
+ }
47
+ return n;
48
+ }
49
+ return /* @__PURE__ */ v(E.Provider, { value: i, children: /* @__PURE__ */ v("div", { ref: r, children: o.children }) });
50
+ }, O = h.forwardRef(
51
+ ({ x: o, y: a, ...u }, r) => {
52
+ const { two: i, parent: f, width: t, height: n } = g(), [e, c] = T(null);
53
+ l(() => {
54
+ if (i) {
55
+ const s = new w.Group();
56
+ return typeof o == "number" && (s.position.x = o), typeof a == "number" && (s.position.y = a), c(s), () => {
57
+ c(null);
58
+ };
59
+ }
60
+ }, [o, a, i]), l(() => {
61
+ const s = e;
62
+ if (f && s)
63
+ return f.add(s), d(), () => {
64
+ f.remove(s);
65
+ };
66
+ }, [e, f]), l(d, [u]);
67
+ function d() {
68
+ c((s) => {
69
+ if (s) {
70
+ const k = { ...u };
71
+ delete k.children;
72
+ for (const S in k)
73
+ S in s && (s[S] = k[S]);
74
+ }
75
+ return s;
76
+ });
77
+ }
78
+ return R(r, () => e, [e]), /* @__PURE__ */ v(E.Provider, { value: { two: i, parent: e, width: t, height: n }, children: u.children });
79
+ }
80
+ ), _ = h.forwardRef(
81
+ ({ manual: o, ...a }, u) => {
82
+ const { two: r, parent: i } = g(), f = m(null);
83
+ l(() => {
84
+ const n = new w.Path();
85
+ return f.current = n, o && (f.current.automatic = !1), () => {
86
+ f.current = null;
87
+ };
88
+ }, [o, r]), l(() => {
89
+ const n = f.current;
90
+ if (i && n)
91
+ return i.add(n), t(), () => {
92
+ i.remove(n);
93
+ };
94
+ }, [i]), l(t, [a]);
95
+ function t() {
96
+ if (f.current) {
97
+ const n = f.current;
98
+ for (const e in a)
99
+ e in n && (n[e] = a[e]);
100
+ }
101
+ }
102
+ return R(u, () => f.current), /* @__PURE__ */ v(y, {});
103
+ }
104
+ ), z = h.forwardRef(
105
+ (o, a) => {
106
+ const { two: u, parent: r } = g(), i = m(null);
107
+ l(() => {
108
+ const t = new w.Points();
109
+ return i.current = t, () => {
110
+ i.current = null;
111
+ };
112
+ }, [u]), l(() => {
113
+ const t = i.current;
114
+ if (r && t)
115
+ return r.add(t), f(), () => {
116
+ r.remove(t);
117
+ };
118
+ }, [r]), l(f, [o]);
119
+ function f() {
120
+ if (i.current) {
121
+ const t = i.current;
122
+ for (const n in o)
123
+ n in t && (t[n] = o[n]);
124
+ }
125
+ }
126
+ return R(a, () => i.current), /* @__PURE__ */ v(y, {});
127
+ }
128
+ ), B = h.forwardRef(
129
+ ({ x: o, y: a, ...u }, r) => {
130
+ const { two: i, parent: f } = g(), t = m(null);
131
+ l(() => {
132
+ const e = new w.Text(u.value, o, a);
133
+ return t.current = e, () => {
134
+ t.current = null;
135
+ };
136
+ }, [o, a, i]), l(() => {
137
+ const e = t.current;
138
+ if (f && e)
139
+ return f.add(e), n(), () => {
140
+ f.remove(e);
141
+ };
142
+ }, [f]), l(n, [u]);
143
+ function n() {
144
+ if (t.current) {
145
+ const e = t.current;
146
+ for (const c in u)
147
+ c in e && (e[c] = u[c]);
148
+ }
149
+ }
150
+ return R(r, () => t.current), /* @__PURE__ */ v(y, {});
151
+ }
152
+ ), J = h.forwardRef(
153
+ ({ x: o, y: a, resolution: u, ...r }, i) => {
154
+ const { two: f, parent: t } = g(), n = m(null);
155
+ l(() => {
156
+ const c = new w.ArcSegment(
157
+ o,
158
+ a,
159
+ r.innerRadius,
160
+ r.outerRadius,
161
+ r.startAngle,
162
+ r.endAngle,
163
+ u
164
+ );
165
+ return n.current = c, () => {
166
+ n.current = null;
167
+ };
168
+ }, [o, a, u, f]), l(() => {
169
+ const c = n.current;
170
+ if (t && c)
171
+ return t.add(c), e(), () => {
172
+ t.remove(c);
173
+ };
174
+ }, [t]), l(e, [r]);
175
+ function e() {
176
+ if (n.current) {
177
+ const c = n.current;
178
+ for (const d in r)
179
+ d in c && (c[d] = r[d]);
180
+ }
181
+ }
182
+ return R(i, () => n.current), /* @__PURE__ */ v(y, {});
183
+ }
184
+ ), K = h.forwardRef(
185
+ ({ x: o, y: a, resolution: u, ...r }, i) => {
186
+ const { two: f, parent: t } = g(), n = m(null);
187
+ l(() => {
188
+ const c = new w.Circle(o, a, r.radius, u);
189
+ return n.current = c, () => {
190
+ n.current = null;
191
+ };
192
+ }, [o, a, u, f]), l(() => {
193
+ const c = n.current;
194
+ if (t && c)
195
+ return t.add(c), e(), () => {
196
+ t.remove(c);
197
+ };
198
+ }, [t]), l(e, [r]);
199
+ function e() {
200
+ if (n.current) {
201
+ const c = n.current;
202
+ for (const d in r)
203
+ d in c && (c[d] = r[d]);
204
+ }
205
+ }
206
+ return R(i, () => n.current), /* @__PURE__ */ v(y, {});
207
+ }
208
+ ), M = h.forwardRef(
209
+ ({ x: o, y: a, resolution: u, ...r }, i) => {
210
+ const { two: f, parent: t } = g(), n = m(null);
211
+ l(() => {
212
+ const c = new w.Ellipse(
213
+ o,
214
+ a,
215
+ typeof r.width == "number" ? r.width / 2 : void 0,
216
+ typeof r.height == "number" ? r.height / 2 : void 0,
217
+ u
218
+ );
219
+ return n.current = c, () => {
220
+ n.current = null;
221
+ };
222
+ }, [o, a, u, f]), l(() => {
223
+ const c = n.current;
224
+ if (t && c)
225
+ return t.add(c), e(), () => {
226
+ t.remove(c);
227
+ };
228
+ }, [t]), l(e, [r]);
229
+ function e() {
230
+ if (n.current) {
231
+ const c = n.current;
232
+ for (const d in r)
233
+ d in c && (c[d] = r[d]);
234
+ }
235
+ }
236
+ return R(i, () => n.current), /* @__PURE__ */ v(y, {});
237
+ }
238
+ ), N = h.forwardRef(
239
+ ({ paths: o, x: a, y: u, autoPlay: r, ...i }, f) => {
240
+ const { two: t, parent: n } = g(), e = m(null);
241
+ G(() => {
242
+ const d = new w.ImageSequence(o, a, u, i.frameRate);
243
+ return e.current = d, r && e.current.play(), () => {
244
+ e.current = null;
245
+ };
246
+ }, [o, a, u, t]), l(() => {
247
+ const d = e.current;
248
+ if (n && d)
249
+ return n.add(d), c(), () => {
250
+ n.remove(d);
251
+ };
252
+ }, [n]), l(() => {
253
+ var d, s;
254
+ r ? (d = e.current) == null || d.play() : (s = e.current) == null || s.pause();
255
+ }, [r]), l(c, [i]);
256
+ function c() {
257
+ if (e.current) {
258
+ const d = e.current;
259
+ for (const s in i)
260
+ s in d && (d[s] = i[s]);
261
+ }
262
+ }
263
+ return R(f, () => e.current), /* @__PURE__ */ v(y, {});
264
+ }
265
+ ), Q = h.forwardRef(
266
+ ({ x1: o, y1: a, x2: u, y2: r, ...i }, f) => {
267
+ const { two: t, parent: n } = g(), e = m(null);
268
+ l(() => {
269
+ const d = new w.Line(o, a, u, r);
270
+ return e.current = d, () => {
271
+ e.current = null;
272
+ };
273
+ }, [o, a, u, r, t]), l(() => {
274
+ const d = e.current;
275
+ if (n && d)
276
+ return n.add(d), c(), () => {
277
+ n.remove(d);
278
+ };
279
+ }, [n]), l(c, [i]);
280
+ function c() {
281
+ if (e.current) {
282
+ const d = e.current;
283
+ for (const s in i)
284
+ s in d && (d[s] = i[s]);
285
+ }
286
+ }
287
+ return R(f, () => e.current), /* @__PURE__ */ v(y, {});
288
+ }
289
+ ), U = h.forwardRef(
290
+ ({ x: o, y: a, radius: u, ...r }, i) => {
291
+ const { two: f, parent: t } = g(), n = m(null);
292
+ l(() => {
293
+ const c = new w.Polygon(o, a, u, r.sides);
294
+ return n.current = c, () => {
295
+ n.current = null;
296
+ };
297
+ }, [o, a, u, f]), l(() => {
298
+ const c = n.current;
299
+ if (t && c)
300
+ return t.add(c), e(), () => {
301
+ t.remove(c);
302
+ };
303
+ }, [t]), l(e, [r]);
304
+ function e() {
305
+ if (n.current) {
306
+ const c = n.current;
307
+ for (const d in r)
308
+ d in c && (c[d] = r[d]);
309
+ }
310
+ }
311
+ return R(i, () => n.current), /* @__PURE__ */ v(y, {});
312
+ }
313
+ ), V = h.forwardRef(
314
+ ({ x: o, y: a, ...u }, r) => {
315
+ const { two: i, parent: f } = g(), t = m(null);
316
+ l(() => {
317
+ const e = new w.Rectangle(o, a, u.width, u.height);
318
+ return t.current = e, () => {
319
+ t.current = null;
320
+ };
321
+ }, [o, a, i]), l(() => {
322
+ const e = t.current;
323
+ if (f && e)
324
+ return f.add(e), n(), () => {
325
+ f.remove(e);
326
+ };
327
+ }, [f]), l(n, [u]);
328
+ function n() {
329
+ if (t.current) {
330
+ const e = t.current;
331
+ for (const c in u)
332
+ c in e && (e[c] = u[c]);
333
+ }
334
+ }
335
+ return R(r, () => t.current), /* @__PURE__ */ v(y, {});
336
+ }
337
+ ), W = h.forwardRef(({ x: o, y: a, ...u }, r) => {
338
+ const { two: i, parent: f } = g(), t = m(null);
339
+ l(() => {
340
+ const e = new w.RoundedRectangle(
341
+ o,
342
+ a,
343
+ u.width,
344
+ u.height,
345
+ u.radius
346
+ );
347
+ return t.current = e, () => {
348
+ t.current = null;
349
+ };
350
+ }, [o, a, i]), l(() => {
351
+ const e = t.current;
352
+ if (f && e)
353
+ return f.add(e), n(), () => {
354
+ f.remove(e);
355
+ };
356
+ }, [f]), l(n, [u]);
357
+ function n() {
358
+ if (t.current) {
359
+ const e = t.current;
360
+ for (const c in u)
361
+ c in e && (e[c] = u[c]);
362
+ }
363
+ }
364
+ return R(r, () => t.current), /* @__PURE__ */ v(y, {});
365
+ }), Z = h.forwardRef(
366
+ ({ path: o, x: a, y: u, autoPlay: r, ...i }, f) => {
367
+ const { two: t, parent: n } = g(), e = m(null);
368
+ G(() => {
369
+ const d = new w.Sprite(
370
+ o,
371
+ a,
372
+ u,
373
+ i.columns,
374
+ i.rows,
375
+ i.frameRate
376
+ );
377
+ return e.current = d, r && e.current.play(), () => {
378
+ e.current = null;
379
+ };
380
+ }, [o, a, u, t]), l(() => {
381
+ const d = e.current;
382
+ if (n && d)
383
+ return n.add(d), c(), () => {
384
+ n.remove(d);
385
+ };
386
+ }, [n]), l(() => {
387
+ var d, s;
388
+ r ? (d = e.current) == null || d.play() : (s = e.current) == null || s.pause();
389
+ }, [r]), l(c, [i]);
390
+ function c() {
391
+ if (e.current) {
392
+ const d = e.current;
393
+ for (const s in i)
394
+ s in d && (d[s] = i[s]);
395
+ }
396
+ }
397
+ return R(f, () => e.current), /* @__PURE__ */ v(y, {});
398
+ }
399
+ ), $ = h.forwardRef(
400
+ ({ x: o, y: a, ...u }, r) => {
401
+ const { two: i, parent: f } = g(), t = m(null);
402
+ l(() => {
403
+ const e = new w.Star(
404
+ o,
405
+ a,
406
+ u.innerRadius,
407
+ u.outerRadius,
408
+ u.sides
409
+ );
410
+ return t.current = e, () => {
411
+ t.current = null;
412
+ };
413
+ }, [o, a, i]), l(() => {
414
+ const e = t.current;
415
+ if (f && e)
416
+ return f.add(e), n(), () => {
417
+ f.remove(e);
418
+ };
419
+ }, [f]), l(n, [u]);
420
+ function n() {
421
+ if (t.current) {
422
+ const e = t.current;
423
+ for (const c in u)
424
+ c in e && (e[c] = u[c]);
425
+ }
426
+ }
427
+ return R(r, () => t.current), /* @__PURE__ */ v(y, {});
428
+ }
429
+ ), D = h.forwardRef(
430
+ ({ x1: o, y1: a, x2: u, y2: r, ...i }, f) => {
431
+ const { two: t } = g(), n = m(null);
432
+ !n.current && t && (n.current = new w.LinearGradient(o, a, u, r, i.stops)), l(() => {
433
+ n.current && (typeof o == "number" && (n.current.left.x = o), typeof a == "number" && (n.current.left.y = a), typeof u == "number" && (n.current.right.x = u), typeof r == "number" && (n.current.right.y = r));
434
+ }, [o, a, u, r]), l(e, [i]);
435
+ function e() {
436
+ if (n.current) {
437
+ const c = n.current;
438
+ for (const d in i)
439
+ d in c && (c[d] = i[d]);
440
+ }
441
+ }
442
+ return R(f, () => n.current), null;
443
+ }
444
+ ), x = h.forwardRef(
445
+ ({ x: o, y: a, focalX: u, focalY: r, ...i }, f) => {
446
+ const { two: t } = g(), n = m(null);
447
+ !n.current && t && (n.current = new w.RadialGradient(
448
+ o,
449
+ a,
450
+ i.radius,
451
+ i.stops,
452
+ u,
453
+ r
454
+ )), l(() => {
455
+ n.current && (typeof o == "number" && (n.current.center.x = o), typeof a == "number" && (n.current.center.y = a));
456
+ }, [o, a]), l(() => {
457
+ n.current && (typeof u == "number" && (n.current.focal.x = u), typeof r == "number" && (n.current.focal.y = r));
458
+ }, [u, r]), l(e, [i]);
459
+ function e() {
460
+ if (n.current) {
461
+ const c = n.current;
462
+ for (const d in i)
463
+ d in c && (c[d] = i[d]);
464
+ }
465
+ }
466
+ return R(f, () => n.current), null;
467
+ }
468
+ ), X = h.forwardRef(
469
+ ({ source: o, ...a }, u) => {
470
+ const r = m(null);
471
+ l(() => {
472
+ const f = new w.Texture(o);
473
+ return r.current = f, () => {
474
+ };
475
+ }, [o]), l(i, [a]);
476
+ function i() {
477
+ if (r.current) {
478
+ const f = r.current;
479
+ for (const t in a)
480
+ t in f && (f[t] = a[t]);
481
+ }
482
+ }
483
+ return R(u, () => r.current), null;
484
+ }
485
+ );
486
+ export {
487
+ J as ArcSegment,
488
+ H as Canvas,
489
+ K as Circle,
490
+ E as Context,
491
+ M as Ellipse,
492
+ O as Group,
493
+ N as ImageSequence,
494
+ Q as Line,
495
+ D as LinearGradient,
496
+ _ as Path,
497
+ z as Points,
498
+ U as Polygon,
499
+ x as RadialGradient,
500
+ V as Rectangle,
501
+ W as RoundedRectangle,
502
+ Z as Sprite,
503
+ $ as Star,
504
+ B as Text,
505
+ X as Texture,
506
+ j as useFrame,
507
+ g as useTwo
508
+ };
package/package.json ADDED
@@ -0,0 +1,75 @@
1
+ {
2
+ "name": "react-two.js",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "description": "A React virtual DOM for Two.js — a renderer agnostic two-dimensional drawing API for the web",
6
+ "main": "./dist/react-two-main.es.js",
7
+ "module": "./dist/react-two-main.es.js",
8
+ "types": "./dist/main.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "import": "./dist/react-two-main.es.js",
12
+ "types": "./dist/main.d.ts"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/jonobr1/react-two.js.git"
21
+ },
22
+ "keywords": [
23
+ "react",
24
+ "two.js",
25
+ "canvas",
26
+ "svg",
27
+ "webgl",
28
+ "graphics",
29
+ "animation",
30
+ "2d",
31
+ "drawing"
32
+ ],
33
+ "author": "Jono Brandel",
34
+ "license": "MIT",
35
+ "bugs": {
36
+ "url": "https://github.com/jonobr1/react-two.js/issues"
37
+ },
38
+ "homepage": "https://github.com/jonobr1/react-two.js#readme",
39
+ "scripts": {
40
+ "dev": "vite",
41
+ "build": "vite build && tsc --p ./tsconfig.build.json",
42
+ "lint": "eslint .",
43
+ "preview": "vite preview",
44
+ "test": "vitest",
45
+ "test:watch": "vitest --watch",
46
+ "test:ui": "vitest --ui",
47
+ "test:coverage": "vitest --coverage"
48
+ },
49
+ "devDependencies": {
50
+ "@eslint/js": "^9.13.0",
51
+ "@testing-library/jest-dom": "^6.6.3",
52
+ "@testing-library/react": "^16.3.0",
53
+ "@testing-library/user-event": "^14.6.1",
54
+ "@types/node": "^22.10.7",
55
+ "@types/react": "^18.3.12",
56
+ "@types/react-dom": "^18.3.1",
57
+ "@vitejs/plugin-react": "^4.3.3",
58
+ "@vitest/ui": "^3.2.4",
59
+ "eslint": "^9.13.0",
60
+ "eslint-plugin-react-hooks": "^5.0.0",
61
+ "eslint-plugin-react-refresh": "^0.4.14",
62
+ "globals": "^15.11.0",
63
+ "jsdom": "^26.1.0",
64
+ "typescript": "~5.6.2",
65
+ "typescript-eslint": "^8.11.0",
66
+ "vite": "^5.4.10",
67
+ "vite-plugin-dts": "^4.5.0",
68
+ "vitest": "^3.2.4"
69
+ },
70
+ "peerDependencies": {
71
+ "react": "^18.3.1",
72
+ "react-dom": "^18.3.1",
73
+ "two.js": "^v0.8.19"
74
+ }
75
+ }