SOURCE / PINNED RELEASE
Made of little things.
Nappakura
- Release
- a75ca0f8b596…
- Author-recorded commit
- 4463126213fa…
- License
- LICENSE
- Author’s source reference
- nostr://npub182jczunncwe0jn6frpqwq3e0qjws7yqqnc3auccqv9nte2dnd63scjm4rf/wss%3A%2F%2Fgit.napplet.soy%2F/n-8db03001b62
Archive hash verified: b448d77b767ae754…. The source-to-build association is the author’s claim; it has not been independently rebuilt.
import type { V2, V3 } from './mesh.js';
export type TransformMode = 'move' | 'scale' | 'rotate';
export interface GizmoState {
center: V3;
operation: TransformMode;
size: number;
}
export interface Handle {
axes: number[];
points: V2[];
closed: boolean;
color: string;
label: string;
}
const colors = ['#ff8b79', '#b6e67c', '#79bfff'];
const plus = (a: V2, b: V2, n = 1): V2 => [a[0] + b[0] * n, a[1] + b[1] * n];
const delta = (a: V2, b: V2): V2 => [a[0] - b[0], a[1] - b[1]];
const norm = (v: V2) => Math.hypot(...v);
export function planeCoordinates(p: V2, a: V2, b: V2): V2 | undefined {
const det = a[0] * b[1] - a[1] * b[0];
if (Math.abs(det) < 0.08) return;
return [(p[0] * b[1] - p[1] * b[0]) / det, (a[0] * p[1] - a[1] * p[0]) / det];
}
/** Pixel displacement is resolved in the frozen camera's projected world axes. */
export function dragValues(
mode: TransformMode,
axes: number[],
basis: V2[],
start: V2,
end: V2,
origin: V2,
pixelsPerMm: number,
snap: boolean,
): V3 {
const d = delta(end, start),
values: V3 = mode === 'scale' ? [1, 1, 1] : [0, 0, 0];
if (mode === 'rotate') {
const axis = axes[0],
a = (axis + 1) % 3,
b = (axis + 2) % 3;
const s = planeCoordinates(delta(start, origin), basis[a], basis[b]);
const e = planeCoordinates(delta(end, origin), basis[a], basis[b]);
if (!s || !e) return values;
let angle = ((Math.atan2(e[1], e[0]) - Math.atan2(s[1], s[0])) * 180) / Math.PI;
if (angle > 180) angle -= 360;
if (angle < -180) angle += 360;
values[axis] = snap ? Math.round(angle / 15) * 15 : angle;
} else {
const amounts =
axes.length === 1
? [
(d[0] * basis[axes[0]][0] + d[1] * basis[axes[0]][1]) /
Math.max(0.08, norm(basis[axes[0]]) ** 2),
]
: (planeCoordinates(d, basis[axes[0]], basis[axes[1]]) ?? [0, 0]);
axes.forEach((axis, i) => {
if (mode === 'move')
values[axis] = snap ? Math.round(amounts[i] / pixelsPerMm) : amounts[i] / pixelsPerMm;
else {
const factor = 1 + amounts[i] / 80;
values[axis] = Math.max(0.01, snap ? Math.round(factor * 10) / 10 : factor);
}
});
}
return values;
}
export function gizmoHandles(state: GizmoState, origin: V2, basis: V2[]): Handle[] {
const handles: Handle[] = [];
if (state.operation === 'rotate') {
for (let axis = 0; axis < 3; axis++) {
const a = (axis + 1) % 3,
b = (axis + 2) % 3;
if (!planeCoordinates([0, 0], basis[a], basis[b])) continue;
const points = Array.from({ length: 65 }, (_, i) =>
plus(
plus(origin, basis[a], Math.cos((i * Math.PI) / 32) * 65),
basis[b],
Math.sin((i * Math.PI) / 32) * 65,
),
);
handles.push({
axes: [axis],
points,
closed: false,
color: colors[axis],
label: 'XYZ'[axis],
});
}
} else {
for (const [a, b] of [
[0, 1],
[1, 2],
[2, 0],
]) {
if (!planeCoordinates([0, 0], basis[a], basis[b])) continue;
handles.push({
axes: [a, b],
points: [
[18, 18],
[36, 18],
[36, 36],
[18, 36],
].map(([u, v]) => plus(plus(origin, basis[a], u), basis[b], v)),
closed: true,
color: colors[3 - a - b],
label: 'XYZ'[a] + 'XYZ'[b],
});
}
for (let axis = 0; axis < 3; axis++)
if (norm(basis[axis]) > 0.25)
handles.push({
axes: [axis],
points: [plus(origin, basis[axis], 9), plus(origin, basis[axis], 80)],
closed: false,
color: colors[axis],
label: 'XYZ'[axis],
});
}
return handles;
}
export function hitHandle(handles: Handle[], p: V2): Handle | undefined {
let best: Handle | undefined,
distance = 10;
for (const h of handles) {
if (h.closed) {
let inside = false;
for (let i = 0, j = h.points.length - 1; i < h.points.length; j = i++) {
const a = h.points[i],
b = h.points[j];
if (
a[1] > p[1] !== b[1] > p[1] &&
p[0] < ((b[0] - a[0]) * (p[1] - a[1])) / (b[1] - a[1]) + a[0]
)
inside = !inside;
}
if (inside) {
best = h;
distance = 0;
}
}
for (let i = 1; i < h.points.length; i++) {
const a = h.points[i - 1],
v = delta(h.points[i], a),
d = delta(p, a);
const t = Math.max(0, Math.min(1, (d[0] * v[0] + d[1] * v[1]) / (norm(v) ** 2 || 1)));
const n = norm(delta(p, plus(a, v, t)));
if (n < distance) {
distance = n;
best = h;
}
}
}
return best;
}
export function drawGizmo(
ctx: CanvasRenderingContext2D,
handles: Handle[],
mode: TransformMode,
active?: Handle,
) {
ctx.save();
ctx.lineJoin = 'round';
ctx.font = 'bold 12px system-ui';
for (const h of handles) {
ctx.beginPath();
h.points.forEach((p, i) => (i ? ctx.lineTo(...p) : ctx.moveTo(...p)));
if (h.closed) {
ctx.closePath();
ctx.fillStyle = h.color + '45';
ctx.fill();
}
ctx.strokeStyle = '#101c29';
ctx.lineWidth = 6;
ctx.stroke();
ctx.strokeStyle = h.color;
ctx.lineWidth = active?.label === h.label ? 4 : 2.5;
ctx.stroke();
const end = h.points[h.closed ? 2 : mode === 'rotate' ? 8 : 1];
if (!h.closed && mode !== 'rotate') {
ctx.fillStyle = h.color;
if (mode === 'scale') ctx.fillRect(end[0] - 5, end[1] - 5, 10, 10);
else {
const a = h.points[0],
angle = Math.atan2(end[1] - a[1], end[0] - a[0]);
ctx.beginPath();
ctx.moveTo(...end);
for (const sign of [-1, 1])
ctx.lineTo(
end[0] - Math.cos(angle + sign * 0.5) * 13,
end[1] - Math.sin(angle + sign * 0.5) * 13,
);
ctx.closePath();
ctx.fill();
}
}
ctx.strokeStyle = '#101c29';
ctx.lineWidth = 4;
ctx.strokeText(h.label, end[0] + 8, end[1] - 6);
ctx.fillStyle = h.color;
ctx.fillText(h.label, end[0] + 8, end[1] - 6);
}
ctx.restore();
}
