camosaic 0.1.0 → 0.1.1

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 CHANGED
@@ -32,7 +32,7 @@ host.heartbeat();
32
32
 
33
33
  `detectTags(frame)` accepts `ImageData`-compatible data and returns every Camosaic AprilTag it finds, each with its display slot, corner (0 = top-left, 1 = top-right, 2 = bottom-right) and center. Tag ids 0-14 encode `slot * 3 + corner`, so displays are identified by the tag itself, not by color. Defaults are smoothing `0.6`, a `500ms` loss hold, and `keepPose: false`; set `host.options.keepPose = true` to keep showing a display's last known position after tracking is lost. A host assigns up to five slots.
34
34
 
35
- `createHost()` exposes the stage, peers, scene, and mutable `options` object. `track(frame, now?)` returns the detected tags and updates poses (pass `{ mirror: true }` when the preview is mirrored). Call `dispose()` when finished. Affine helpers use `[a, b, c, d, e, f]` canvas-transform order for inverse poses.
35
+ `createHost()` exposes the stage, peers, scene, and mutable `options` object. `track(frame, now?)` returns the detected tags and updates poses (pass `{ mirror: true }` when the preview is mirrored). `setLayout({ fit, scale, x, y })` resizes and moves the shared image or video (`fit` is `cover` or `contain`, `scale` 0.1-4, `x`/`y` from -1 to 1) without restarting video sync. `mediaRect(sourceW, sourceH, stageW, stageH, layout)` returns the rectangle the media lands on, which is what the web app's placement preview draws. Call `dispose()` when finished. Affine helpers use `[a, b, c, d, e, f]` canvas-transform order for inverse poses.
36
36
 
37
37
  ## React integration
38
38
 
@@ -8,11 +8,24 @@ interface Size {
8
8
  }
9
9
  type Pose = [number, number, number, number, number, number];
10
10
  type SceneType = "grid" | "gradient" | "plasma" | "clock" | "image" | "video";
11
+ /** How an image or video is placed on the shared stage. */
12
+ interface MediaLayout {
13
+ /** `cover` fills the stage (cropping), `contain` shows the whole media. */
14
+ fit: "cover" | "contain";
15
+ /** Zoom on top of the fit, 1 = as fitted. */
16
+ scale: number;
17
+ /** Center offset as a fraction of half the stage (-1 = left/top edge, 1 = right/bottom edge). */
18
+ x: number;
19
+ y: number;
20
+ }
21
+ declare const DEFAULT_MEDIA_LAYOUT: MediaLayout;
22
+ declare const MEDIA_SCALE_RANGE: readonly [0.1, 4];
11
23
  interface SceneState {
12
24
  scene: SceneType;
13
25
  videoUrl: string;
14
26
  image: string | null;
15
27
  epoch: number;
28
+ layout: MediaLayout;
16
29
  }
17
30
  interface TrackingOptions {
18
31
  smooth: number;
@@ -76,6 +89,10 @@ type WireMessage = {
76
89
  videoUrl: string;
77
90
  image: string | null;
78
91
  epoch: number;
92
+ layout: MediaLayout;
93
+ } | {
94
+ t: "layout";
95
+ layout: MediaLayout;
79
96
  } | {
80
97
  t: "pose";
81
98
  m: Pose | null;
@@ -122,6 +139,8 @@ declare class CamosaicHost {
122
139
  handle(slot: number, message: WireMessage): void;
123
140
  setStage(w: number, h: number): void;
124
141
  setScene(patch: Partial<SceneState>): void;
142
+ /** Moves or resizes the image/video on every display. Unlike `setScene` it does not restart video sync. */
143
+ setLayout(patch: Partial<MediaLayout>): void;
125
144
  heartbeat(now?: number): void;
126
145
  /**
127
146
  * Finds the AprilTags in a camera frame and updates every display's pose.
@@ -143,12 +162,20 @@ declare class SceneView {
143
162
  private imageKey;
144
163
  video: HTMLVideoElement | null;
145
164
  videoUrl: string;
165
+ layout: MediaLayout;
146
166
  private lastSync;
147
167
  apply(state: SceneState): void;
148
168
  resume(): void;
149
169
  sync(time: number): void;
150
170
  dispose(): void;
151
171
  }
172
+ /** Where an image or video of the given size lands on the stage for a layout. */
173
+ declare function mediaRect(sourceW: number, sourceH: number, stageW: number, stageH: number, layout: MediaLayout): {
174
+ x: number;
175
+ y: number;
176
+ w: number;
177
+ h: number;
178
+ };
152
179
  declare function drawScene(ctx: CanvasRenderingContext2D, w: number, h: number, view: SceneView, time: number, plasmaBuffer: HTMLCanvasElement): void;
153
180
 
154
181
  declare class CamosaicChild {
@@ -187,4 +214,4 @@ declare class CamosaicChild {
187
214
  }
188
215
  declare function createChild(send: (message: WireMessage) => void): CamosaicChild;
189
216
 
190
- export { CamosaicChild as C, DEFAULT_TRACKING_OPTIONS as D, type ImageFrame as I, type Link as L, MARKER_HUES as M, type Peer as P, STAGE_WIDTH as S, type TagHit as T, type WireMessage as W, CamosaicHost as a, DETECT_WIDTH as b, MAX_PEERS as c, type Point as d, type Pose as e, type SceneState as f, type SceneType as g, SceneView as h, type Size as i, type TrackingOptions as j, applyPose as k, createChild as l, createHost as m, drawScene as n, invertPose as o, lerpPose as p, markerGeom as q, poseFrom as r };
217
+ export { CamosaicChild as C, DEFAULT_MEDIA_LAYOUT as D, type ImageFrame as I, type Link as L, MARKER_HUES as M, type Peer as P, STAGE_WIDTH as S, type TagHit as T, type WireMessage as W, CamosaicHost as a, DEFAULT_TRACKING_OPTIONS as b, DETECT_WIDTH as c, MAX_PEERS as d, MEDIA_SCALE_RANGE as e, type MediaLayout as f, type Point as g, type Pose as h, type SceneState as i, type SceneType as j, SceneView as k, type Size as l, type TrackingOptions as m, applyPose as n, createChild as o, createHost as p, drawScene as q, invertPose as r, lerpPose as s, markerGeom as t, mediaRect as u, poseFrom as v };
@@ -3,6 +3,8 @@ var MARKER_HUES = [330, 50, 0, 90, 290];
3
3
  var MAX_PEERS = MARKER_HUES.length;
4
4
  var STAGE_WIDTH = 1280;
5
5
  var DETECT_WIDTH = 1280;
6
+ var DEFAULT_MEDIA_LAYOUT = { fit: "cover", scale: 1, x: 0, y: 0 };
7
+ var MEDIA_SCALE_RANGE = [0.1, 4];
6
8
  var DEFAULT_TRACKING_OPTIONS = {
7
9
  smooth: 0.6,
8
10
  holdMs: 500,
@@ -133,7 +135,7 @@ function lerpPose(a, b, amount) {
133
135
  var CamosaicHost = class {
134
136
  peers = /* @__PURE__ */ new Map();
135
137
  stage = { w: STAGE_WIDTH, h: Math.round(STAGE_WIDTH * 9 / 16) };
136
- scene = { scene: "grid", videoUrl: "", image: null, epoch: Date.now() };
138
+ scene = { scene: "grid", videoUrl: "", image: null, epoch: Date.now(), layout: { ...DEFAULT_MEDIA_LAYOUT } };
137
139
  options = { ...DEFAULT_TRACKING_OPTIONS };
138
140
  listeners = /* @__PURE__ */ new Set();
139
141
  on(listener) {
@@ -203,6 +205,22 @@ var CamosaicHost = class {
203
205
  for (const peer of this.peers.values()) this.send(peer, this.sceneMessage());
204
206
  this.emit();
205
207
  }
208
+ /** Moves or resizes the image/video on every display. Unlike `setScene` it does not restart video sync. */
209
+ setLayout(patch) {
210
+ const next = { ...this.scene.layout, ...patch };
211
+ const clamp = (value, min, max) => Math.min(max, Math.max(min, Number.isFinite(value) ? value : 0));
212
+ this.scene = {
213
+ ...this.scene,
214
+ layout: {
215
+ fit: next.fit === "contain" ? "contain" : "cover",
216
+ scale: clamp(next.scale, MEDIA_SCALE_RANGE[0], MEDIA_SCALE_RANGE[1]),
217
+ x: clamp(next.x, -1, 1),
218
+ y: clamp(next.y, -1, 1)
219
+ }
220
+ };
221
+ for (const peer of this.peers.values()) this.send(peer, { t: "layout", layout: this.scene.layout });
222
+ this.emit();
223
+ }
206
224
  heartbeat(now = performance.now()) {
207
225
  for (const peer of [...this.peers.values()]) {
208
226
  if (peer.link.kind !== "local" && now - peer.seenAt > 9e3) {
@@ -268,10 +286,12 @@ var SceneView = class {
268
286
  imageKey = null;
269
287
  video = null;
270
288
  videoUrl = "";
289
+ layout = { ...DEFAULT_MEDIA_LAYOUT };
271
290
  lastSync = 0;
272
291
  apply(state) {
273
292
  this.scene = state.scene;
274
293
  this.epoch = state.epoch;
294
+ this.layout = state.layout ?? { ...DEFAULT_MEDIA_LAYOUT };
275
295
  if (state.image && state.image !== this.imageKey) {
276
296
  this.imageKey = state.image;
277
297
  const image = new Image();
@@ -327,10 +347,17 @@ var plasmaLut = Array.from(
327
347
  { length: 256 },
328
348
  (_, index) => hslToRgb(185 + 65 * index / 255, 0.85, 0.1 + 0.55 * index / 255)
329
349
  );
330
- function cover(ctx, source, sourceW, sourceH, stageW, stageH) {
331
- const scale = Math.max(stageW / sourceW, stageH / sourceH);
332
- const drawW = sourceW * scale, drawH = sourceH * scale;
333
- ctx.drawImage(source, (stageW - drawW) / 2, (stageH - drawH) / 2, drawW, drawH);
350
+ function mediaRect(sourceW, sourceH, stageW, stageH, layout) {
351
+ const fit = layout.fit === "contain" ? Math.min(stageW / sourceW, stageH / sourceH) : Math.max(stageW / sourceW, stageH / sourceH);
352
+ const w = sourceW * fit * layout.scale, h = sourceH * fit * layout.scale;
353
+ const cx = stageW / 2 + layout.x * stageW / 2, cy = stageH / 2 + layout.y * stageH / 2;
354
+ return { x: cx - w / 2, y: cy - h / 2, w, h };
355
+ }
356
+ function placeMedia(ctx, source, sourceW, sourceH, stageW, stageH, layout) {
357
+ const rect = mediaRect(sourceW, sourceH, stageW, stageH, layout);
358
+ ctx.fillStyle = "#000";
359
+ ctx.fillRect(0, 0, stageW, stageH);
360
+ ctx.drawImage(source, rect.x, rect.y, rect.w, rect.h);
334
361
  }
335
362
  function centerText(ctx, text, w, h, size = 48) {
336
363
  ctx.fillStyle = "rgba(255,255,255,.8)";
@@ -428,11 +455,11 @@ function drawScene(ctx, w, h, view, time, plasmaBuffer) {
428
455
  drawClock(ctx, w, h, time);
429
456
  break;
430
457
  case "image":
431
- if (view.image?.complete && view.image.naturalWidth) cover(ctx, view.image, view.image.naturalWidth, view.image.naturalHeight, w, h);
458
+ if (view.image?.complete && view.image.naturalWidth) placeMedia(ctx, view.image, view.image.naturalWidth, view.image.naturalHeight, w, h, view.layout);
432
459
  else centerText(ctx, "No image selected", w, h);
433
460
  break;
434
461
  case "video":
435
- if (view.video && view.video.readyState >= 2 && view.video.videoWidth) cover(ctx, view.video, view.video.videoWidth, view.video.videoHeight, w, h);
462
+ if (view.video && view.video.readyState >= 2 && view.video.videoWidth) placeMedia(ctx, view.video, view.video.videoWidth, view.video.videoHeight, w, h, view.layout);
436
463
  else centerText(ctx, view.videoUrl ? "Loading video" : "Video URL is not set", w, h);
437
464
  break;
438
465
  }
@@ -500,6 +527,9 @@ var CamosaicChild = class {
500
527
  case "stage":
501
528
  this.stage = message.stage;
502
529
  break;
530
+ case "layout":
531
+ this.view.layout = message.layout;
532
+ break;
503
533
  case "pose":
504
534
  this.setClock(message.now);
505
535
  this.inverse = message.m ? invertPose(message.m) : null;
@@ -580,13 +610,20 @@ function isWireMessage(value) {
580
610
  case "stage":
581
611
  return validSize(msg.stage);
582
612
  case "scene":
583
- return ["grid", "gradient", "plasma", "clock", "image", "video"].includes(String(msg.scene)) && typeof msg.videoUrl === "string" && msg.videoUrl.length <= 4096 && (msg.image === null || typeof msg.image === "string" && msg.image.length <= 8e6) && Number.isFinite(msg.epoch);
613
+ return ["grid", "gradient", "plasma", "clock", "image", "video"].includes(String(msg.scene)) && typeof msg.videoUrl === "string" && msg.videoUrl.length <= 4096 && (msg.image === null || typeof msg.image === "string" && msg.image.length <= 8e6) && Number.isFinite(msg.epoch) && validLayout(msg.layout);
614
+ case "layout":
615
+ return validLayout(msg.layout);
584
616
  case "pose":
585
617
  return (msg.m === null || validPose(msg.m)) && Number.isFinite(msg.now);
586
618
  default:
587
619
  return false;
588
620
  }
589
621
  }
622
+ function validLayout(value) {
623
+ if (!value || typeof value !== "object") return false;
624
+ const layout = value;
625
+ return (layout.fit === "cover" || layout.fit === "contain") && Number.isFinite(layout.scale) && Number(layout.scale) >= 0.01 && Number(layout.scale) <= 20 && Number.isFinite(layout.x) && Math.abs(Number(layout.x)) <= 5 && Number.isFinite(layout.y) && Math.abs(Number(layout.y)) <= 5;
626
+ }
590
627
  function validSize(value) {
591
628
  if (!value || typeof value !== "object") return false;
592
629
  const size = value;
@@ -653,6 +690,8 @@ export {
653
690
  MAX_PEERS,
654
691
  STAGE_WIDTH,
655
692
  DETECT_WIDTH,
693
+ DEFAULT_MEDIA_LAYOUT,
694
+ MEDIA_SCALE_RANGE,
656
695
  DEFAULT_TRACKING_OPTIONS,
657
696
  markerGeom,
658
697
  TAG_CORNERS,
@@ -671,6 +710,7 @@ export {
671
710
  CamosaicHost,
672
711
  createHost,
673
712
  SceneView,
713
+ mediaRect,
674
714
  drawScene,
675
715
  CamosaicChild,
676
716
  createChild,
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  deserializeMessage,
3
3
  serializeMessage
4
- } from "./chunk-7SRZBAHV.js";
4
+ } from "./chunk-D4TNKRHZ.js";
5
5
 
6
6
  // src/core/transport/chunks.ts
7
7
  var CHUNK_SIZE = 12e3;
@@ -3,7 +3,7 @@ import {
3
3
  createChild,
4
4
  createHost,
5
5
  startBroadcastHost
6
- } from "./chunk-7SRZBAHV.js";
6
+ } from "./chunk-D4TNKRHZ.js";
7
7
 
8
8
  // src/react/hooks/use-camera.ts
9
9
  import { useCallback, useEffect, useRef, useState } from "react";
@@ -1,5 +1,5 @@
1
- import { I as ImageFrame, T as TagHit, W as WireMessage, L as Link, a as CamosaicHost } from '../child-RNlfMDLs.js';
2
- export { C as CamosaicChild, D as DEFAULT_TRACKING_OPTIONS, b as DETECT_WIDTH, M as MARKER_HUES, c as MAX_PEERS, P as Peer, d as Point, e as Pose, S as STAGE_WIDTH, f as SceneState, g as SceneType, h as SceneView, i as Size, j as TrackingOptions, k as applyPose, l as createChild, m as createHost, n as drawScene, o as invertPose, p as lerpPose, q as markerGeom, r as poseFrom } from '../child-RNlfMDLs.js';
1
+ import { I as ImageFrame, T as TagHit, W as WireMessage, L as Link, a as CamosaicHost } from '../child-CbI29wcC.js';
2
+ export { C as CamosaicChild, D as DEFAULT_MEDIA_LAYOUT, b as DEFAULT_TRACKING_OPTIONS, c as DETECT_WIDTH, M as MARKER_HUES, d as MAX_PEERS, e as MEDIA_SCALE_RANGE, f as MediaLayout, P as Peer, g as Point, h as Pose, S as STAGE_WIDTH, i as SceneState, j as SceneType, k as SceneView, l as Size, m as TrackingOptions, n as applyPose, o as createChild, p as createHost, q as drawScene, r as invertPose, s as lerpPose, t as markerGeom, u as mediaRect, v as poseFrom } from '../child-CbI29wcC.js';
3
3
 
4
4
  /** Corner order: 0 = top-left, 1 = top-right, 2 = bottom-right of the display. */
5
5
  declare const TAG_CORNERS = 3;
@@ -4,14 +4,16 @@ import {
4
4
  createOffer,
5
5
  decodeSignal,
6
6
  encodeSignal
7
- } from "../chunk-2C27ROZW.js";
7
+ } from "../chunk-U2Y3V4GO.js";
8
8
  import {
9
9
  CamosaicChild,
10
10
  CamosaicHost,
11
+ DEFAULT_MEDIA_LAYOUT,
11
12
  DEFAULT_TRACKING_OPTIONS,
12
13
  DETECT_WIDTH,
13
14
  MARKER_HUES,
14
15
  MAX_PEERS,
16
+ MEDIA_SCALE_RANGE,
15
17
  STAGE_WIDTH,
16
18
  SceneView,
17
19
  TAG_CORNERS,
@@ -30,20 +32,23 @@ import {
30
32
  isWireMessage,
31
33
  lerpPose,
32
34
  markerGeom,
35
+ mediaRect,
33
36
  parseTagId,
34
37
  poseFrom,
35
38
  serializeMessage,
36
39
  startBroadcastHost,
37
40
  tagBits,
38
41
  tagId
39
- } from "../chunk-7SRZBAHV.js";
42
+ } from "../chunk-D4TNKRHZ.js";
40
43
  export {
41
44
  CamosaicChild,
42
45
  CamosaicHost,
46
+ DEFAULT_MEDIA_LAYOUT,
43
47
  DEFAULT_TRACKING_OPTIONS,
44
48
  DETECT_WIDTH,
45
49
  MARKER_HUES,
46
50
  MAX_PEERS,
51
+ MEDIA_SCALE_RANGE,
47
52
  STAGE_WIDTH,
48
53
  SceneView,
49
54
  TAG_CORNERS,
@@ -67,6 +72,7 @@ export {
67
72
  isWireMessage,
68
73
  lerpPose,
69
74
  markerGeom,
75
+ mediaRect,
70
76
  parseTagId,
71
77
  poseFrom,
72
78
  serializeMessage,
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { C as CamosaicChild, a as CamosaicHost, D as DEFAULT_TRACKING_OPTIONS, b as DETECT_WIDTH, I as ImageFrame, L as Link, M as MARKER_HUES, c as MAX_PEERS, P as Peer, d as Point, e as Pose, S as STAGE_WIDTH, f as SceneState, g as SceneType, h as SceneView, i as Size, T as TagHit, j as TrackingOptions, W as WireMessage, k as applyPose, l as createChild, m as createHost, n as drawScene, o as invertPose, p as lerpPose, q as markerGeom, r as poseFrom } from './child-RNlfMDLs.js';
1
+ export { C as CamosaicChild, a as CamosaicHost, D as DEFAULT_MEDIA_LAYOUT, b as DEFAULT_TRACKING_OPTIONS, c as DETECT_WIDTH, I as ImageFrame, L as Link, M as MARKER_HUES, d as MAX_PEERS, e as MEDIA_SCALE_RANGE, f as MediaLayout, P as Peer, g as Point, h as Pose, S as STAGE_WIDTH, i as SceneState, j as SceneType, k as SceneView, l as Size, T as TagHit, m as TrackingOptions, W as WireMessage, n as applyPose, o as createChild, p as createHost, q as drawScene, r as invertPose, s as lerpPose, t as markerGeom, u as mediaRect, v as poseFrom } from './child-CbI29wcC.js';
2
2
  export { BroadcastClient, TAG_CORNERS, TAG_FAMILY, TAG_MODULES, acceptOffer, attachDataChannel, createBroadcastClient, createOffer, decodeSignal, deserializeMessage, detectTags, drawMarkers, drawTag, encodeSignal, isWireMessage, parseTagId, serializeMessage, startBroadcastHost, tagBits, tagId } from './core/index.js';
3
3
  export { CameraDevice, CameraState, CamosaicCanvas, CamosaicCanvasProps, CamosaicSession, useCamera, useCamosaic, useTrackingSnapshot } from './react/index.js';
4
4
  import 'react';
package/dist/index.js CHANGED
@@ -4,20 +4,22 @@ import {
4
4
  createOffer,
5
5
  decodeSignal,
6
6
  encodeSignal
7
- } from "./chunk-2C27ROZW.js";
7
+ } from "./chunk-U2Y3V4GO.js";
8
8
  import {
9
9
  CamosaicCanvas,
10
10
  useCamera,
11
11
  useCamosaic,
12
12
  useTrackingSnapshot
13
- } from "./chunk-IGXM5PZK.js";
13
+ } from "./chunk-V2C7NPQR.js";
14
14
  import {
15
15
  CamosaicChild,
16
16
  CamosaicHost,
17
+ DEFAULT_MEDIA_LAYOUT,
17
18
  DEFAULT_TRACKING_OPTIONS,
18
19
  DETECT_WIDTH,
19
20
  MARKER_HUES,
20
21
  MAX_PEERS,
22
+ MEDIA_SCALE_RANGE,
21
23
  STAGE_WIDTH,
22
24
  SceneView,
23
25
  TAG_CORNERS,
@@ -36,21 +38,24 @@ import {
36
38
  isWireMessage,
37
39
  lerpPose,
38
40
  markerGeom,
41
+ mediaRect,
39
42
  parseTagId,
40
43
  poseFrom,
41
44
  serializeMessage,
42
45
  startBroadcastHost,
43
46
  tagBits,
44
47
  tagId
45
- } from "./chunk-7SRZBAHV.js";
48
+ } from "./chunk-D4TNKRHZ.js";
46
49
  export {
47
50
  CamosaicCanvas,
48
51
  CamosaicChild,
49
52
  CamosaicHost,
53
+ DEFAULT_MEDIA_LAYOUT,
50
54
  DEFAULT_TRACKING_OPTIONS,
51
55
  DETECT_WIDTH,
52
56
  MARKER_HUES,
53
57
  MAX_PEERS,
58
+ MEDIA_SCALE_RANGE,
54
59
  STAGE_WIDTH,
55
60
  SceneView,
56
61
  TAG_CORNERS,
@@ -74,6 +79,7 @@ export {
74
79
  isWireMessage,
75
80
  lerpPose,
76
81
  markerGeom,
82
+ mediaRect,
77
83
  parseTagId,
78
84
  poseFrom,
79
85
  serializeMessage,
@@ -1,4 +1,4 @@
1
- import { a as CamosaicHost, C as CamosaicChild, W as WireMessage } from '../child-RNlfMDLs.js';
1
+ import { a as CamosaicHost, C as CamosaicChild, W as WireMessage } from '../child-CbI29wcC.js';
2
2
  import * as react from 'react';
3
3
 
4
4
  interface CameraDevice {
@@ -3,8 +3,8 @@ import {
3
3
  useCamera,
4
4
  useCamosaic,
5
5
  useTrackingSnapshot
6
- } from "../chunk-IGXM5PZK.js";
7
- import "../chunk-7SRZBAHV.js";
6
+ } from "../chunk-V2C7NPQR.js";
7
+ import "../chunk-D4TNKRHZ.js";
8
8
  export {
9
9
  CamosaicCanvas,
10
10
  useCamera,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "camosaic",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Camera-based shared canvas tracking for multiple physical screens",
5
5
  "license": "MIT",
6
6
  "repository": {