sceneview-mcp 4.3.4 → 4.5.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,127 @@
1
+ /**
2
+ * Dependency-free isometric Canvas 2D preview. Every vertex uses metres, including
3
+ * the 1.75 m person and the explicitly labelled 0.75 m reference table. No GLB,
4
+ * WebGL context or WASM is needed to explain a box and its dimensions.
5
+ * Embedded only in the show_in_space resource; the host bridge and QR are shared.
6
+ */
7
+ export const SPACE_DRAWING_JS = `
8
+ var spaceData = null;
9
+ function drawSpace(data) {
10
+ spaceData = data;
11
+ var ctx = canvas.getContext("2d");
12
+ if (!ctx) throw new Error("This host cannot draw the size preview. Use the phone link below.");
13
+ var width = Math.max(1, canvas.clientWidth), height = Math.max(1, canvas.clientHeight);
14
+ // Keep dimensions and table legs left of the desktop AR card. The canvas
15
+ // retains its full height; on a phone the action is below the drawing.
16
+ var drawingWidth = width - (arEl.classList.contains("wide") ? 216 : 0);
17
+ var ratio = Math.min(window.devicePixelRatio || 1, 2);
18
+ canvas.width = Math.round(width * ratio); canvas.height = Math.round(height * ratio);
19
+ ctx.setTransform(ratio, 0, 0, ratio, 0, 0);
20
+ ctx.clearRect(0, 0, width, height);
21
+
22
+ // sizeCm is always width, height, depth. Preserve proportions at every size.
23
+ var w = data.sizeCm[0] / 100, h = data.sizeCm[1] / 100, d = data.sizeCm[2] / 100;
24
+ var base = data.placement === "table" ? 0.75 : 0;
25
+ // Move towards the viewer as we move left, so the person's feet share
26
+ // the box's front floor line (x + z = d), rather than receding behind it.
27
+ var humanX = -0.85, humanZ = d - humanX;
28
+ function humanPoint(x, y) { return [humanX + x / Math.SQRT2, y, humanZ - x / Math.SQRT2]; }
29
+ function iso(x, y, z) { return [0.866 * (x - z), 0.4 * (x + z) - y]; }
30
+ var anchors = [iso.apply(null, humanPoint(-0.35, 0)), iso.apply(null, humanPoint(0.35, 1.96))];
31
+ // Fit the actual objects and floor, not an empty cuboid above the whole
32
+ // floor: that cuboid made both the box and the person unnecessarily small.
33
+ [0, w + 0.45].forEach(function (x) {
34
+ [base - 0.25, base + h + 0.25].forEach(function (y) {
35
+ [-0.3, d + 0.45].forEach(function (z) { anchors.push(iso(x, y, z)); });
36
+ });
37
+ });
38
+ var floor = [[humanX-0.3,0,-0.3],[w+0.4,0,-0.3],[w+0.4,0,d+0.4],
39
+ [0,0,d+0.4],[humanX-0.3,0,humanZ+0.3]];
40
+ floor.forEach(function (v) { anchors.push(iso.apply(null, v)); });
41
+ var minX = Math.min.apply(null, anchors.map(function (p) { return p[0]; }));
42
+ var maxX = Math.max.apply(null, anchors.map(function (p) { return p[0]; }));
43
+ var minY = Math.min.apply(null, anchors.map(function (p) { return p[1]; }));
44
+ var maxY = Math.max.apply(null, anchors.map(function (p) { return p[1]; }));
45
+ var scale = Math.min(Math.max(1, drawingWidth - 56) / (maxX - minX), Math.max(1, height - 36) / (maxY - minY));
46
+ function point(x, y, z) {
47
+ var p = iso(x, y, z);
48
+ return [drawingWidth / 2 + (p[0] - (minX + maxX) / 2) * scale,
49
+ height / 2 + (p[1] - (minY + maxY) / 2) * scale];
50
+ }
51
+ function path(vertices, fill, stroke, closed) {
52
+ ctx.beginPath();
53
+ vertices.forEach(function (v, i) {
54
+ var p = point(v[0], v[1], v[2]);
55
+ if (i) ctx.lineTo(p[0], p[1]); else ctx.moveTo(p[0], p[1]);
56
+ });
57
+ if (closed !== false) ctx.closePath();
58
+ if (fill) { ctx.fillStyle = fill; ctx.fill(); }
59
+ if (stroke) { ctx.strokeStyle = stroke; ctx.lineWidth = 1; ctx.stroke(); }
60
+ }
61
+ function label(text, p, color) {
62
+ ctx.font = "500 12px system-ui, sans-serif";
63
+ ctx.textAlign = "center"; ctx.textBaseline = "middle";
64
+ var tw = ctx.measureText(text).width + 14;
65
+ // Keep labels readable even on a narrow phone or for very small boxes.
66
+ p[0] = Math.max(tw / 2 + 3, Math.min(width - tw / 2 - 3, p[0]));
67
+ p[1] = Math.max(12, Math.min(height - 12, p[1]));
68
+ ctx.fillStyle = "#0b0f16"; ctx.fillRect(p[0] - tw / 2, p[1] - 10, tw, 20);
69
+ ctx.fillStyle = color || "#c9d9ff"; ctx.fillText(text, p[0], p[1]);
70
+ }
71
+ function dimension(a, b, text, offsetX, offsetY) {
72
+ var p = point.apply(null, a), q = point.apply(null, b);
73
+ var length = Math.hypot(q[0] - p[0], q[1] - p[1]) || 1;
74
+ var nx = -(q[1] - p[1]) / length * 4, ny = (q[0] - p[0]) / length * 4;
75
+ ctx.beginPath(); ctx.moveTo(p[0], p[1]); ctx.lineTo(q[0], q[1]);
76
+ [p, q].forEach(function (v) { ctx.moveTo(v[0] - nx, v[1] - ny); ctx.lineTo(v[0] + nx, v[1] + ny); });
77
+ ctx.strokeStyle = "#adc6ff"; ctx.lineWidth = 1; ctx.stroke();
78
+ label(text, [(p[0] + q[0]) / 2 + offsetX, (p[1] + q[1]) / 2 + offsetY]);
79
+ }
80
+
81
+ // Ground plane and quiet grid, shared by the person and the reference table.
82
+ path(floor, "#151d2b", "#2c384d");
83
+ for (var gx = 0; gx <= w; gx += Math.max(0.25, w / 12))
84
+ path([[gx,0,-0.3],[gx,0,d+0.4]], null, "#202c3f", false);
85
+ for (var gz = 0; gz <= d; gz += Math.max(0.25, d / 12))
86
+ path([[humanX-0.3,0,gz],[w+0.4,0,gz]], null, "#202c3f", false);
87
+ // A shared contact line makes the zero-height plane explicit.
88
+ path([humanPoint(-0.3,0),[0,0,d]], null, "#64748b", false);
89
+ if (base) {
90
+ [[-0.12,-0.12],[w+0.12,-0.12],[-0.12,d+0.12],[w+0.12,d+0.12]].forEach(function (v) {
91
+ path([[v[0],0,v[1]],[v[0],base,v[1]]], null, "#64748b", false);
92
+ });
93
+ path([[-0.16,base,-0.16],[w+0.16,base,-0.16],[w+0.16,base,d+0.16],[-0.16,base,d+0.16]], "#263348", "#64748b");
94
+ }
95
+ // A soft footprint and three translucent faces of the dimensioned volume.
96
+ path([[0,base,0],[w,base,0],[w,base,d],[0,base,d]], "rgba(0,0,0,0.35)");
97
+ path([[0,base,d],[w,base,d],[w,base+h,d],[0,base+h,d]], "rgba(0,91,193,0.32)", "#adc6ff");
98
+ path([[w,base,0],[w,base,d],[w,base+h,d],[w,base+h,0]], "rgba(80,141,229,0.24)", "#adc6ff");
99
+ path([[0,base+h,0],[w,base+h,0],[w,base+h,d],[0,base+h,d]], "rgba(173,198,255,0.25)", "#c9d9ff");
100
+ ctx.setLineDash([3,4]);
101
+ path([[0,base,d],[0,base,0],[w,base,0]], null, "#6b8abd", false);
102
+ path([[0,base,0],[0,base+h,0]], null, "#6b8abd", false);
103
+ ctx.setLineDash([]);
104
+
105
+ // Flat human outline: feet at zero, crown at 1.75 metres.
106
+ var body = [[-.16,1.47],[-.24,1.39],[-.32,.96],[-.24,.93],[-.14,1.23],[-.13,.85],[-.17,0],[-.06,0],[0,.75],[.06,0],[.17,0],[.13,.85],[.14,1.23],[.24,.93],[.32,.96],[.24,1.39],[.16,1.47]];
107
+ path(body.map(function (v) { return humanPoint(v[0], v[1]); }), "rgba(179,198,244,0.18)", "#8d9bb4");
108
+ var head = point(humanX,1.64,humanZ);
109
+ ctx.beginPath(); ctx.ellipse(head[0],head[1],0.1*Math.SQRT2*0.866*scale,0.11*scale,0,0,Math.PI*2);
110
+ ctx.fillStyle = "rgba(179,198,244,0.18)"; ctx.fill(); ctx.strokeStyle = "#8d9bb4"; ctx.stroke();
111
+ label("1.75 m", point(humanX,1.96,humanZ), "#b3c6f4");
112
+ dimension([0,base-.13,d+.2],[w,base-.13,d+.2], data.sizeCm[0] + " cm wide", 0, 12);
113
+ dimension([w+.2,base,0],[w+.2,base+h,0], data.sizeCm[1] + " cm tall", 38, 0);
114
+ dimension([-.18,base,0],[-.18,base,d], data.sizeCm[2] + " cm deep", -35, 8);
115
+
116
+ document.getElementById("engine").textContent = base ? "Table · 75 cm reference" : "Floor placement";
117
+ document.getElementById("hint").textContent = "Human reference · 1.75 m";
118
+ formatEl.textContent = "cm";
119
+ sizeEl.textContent = data.sizeCm.join(" × ") + " cm" + (data.dimensionSource === "estimated" ? " · estimated size" : "");
120
+ sizeEl.hidden = false;
121
+ canvas.setAttribute("aria-label", data.label + ": " + sizeEl.textContent + ". " + data.placement + " placement. Human reference: 1.75 metres.");
122
+ }
123
+ var spaceObserver = typeof ResizeObserver === "function" ? new ResizeObserver(function () {
124
+ if (spaceData && !disposed) drawSpace(spaceData);
125
+ }) : null;
126
+ if (spaceObserver) spaceObserver.observe(canvasWrap);
127
+ `;