File size: 5,053 Bytes
65d68f5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | import type { CanvasRegion, VisualAction, VisualBeatGraph } from "./contracts";
export type CanvasObjectState = {
objectId: string;
kind: "particle_field" | "slinky" | "wave_graph" | "formula" | "numerical" | "note" | "recap" | "text" | "label" | "arrow" | "measurement";
region: CanvasRegion;
text?: string;
role?: "title" | "explanation" | "definition" | "caption" | "recap";
equation?: string;
explanation?: string;
tokens?: string[];
answer?: string;
answerExplanation?: string;
label?: string;
dimmed?: boolean;
highlighted?: boolean;
appearedAtMs: number;
};
export type ContinuousCanvasState = {
objects: CanvasObjectState[];
activeBeatIndex: number;
activeBeatProgress: number;
};
function defaultTextRegion(action: Extract<VisualAction, { type: "write_text" }>): CanvasRegion {
if (action.role === "title") return "title";
if (action.role === "recap") return "centre";
return "right";
}
export function deriveContinuousCanvasState(graph: VisualBeatGraph, timeMs: number): ContinuousCanvasState {
const objects = new Map<string, CanvasObjectState>();
let activeBeatIndex = graph.beats.findIndex(
(beat) => timeMs >= beat.estimatedStartMs && timeMs < beat.estimatedEndMs,
);
if (activeBeatIndex < 0) activeBeatIndex = Math.max(0, graph.beats.length - 1);
for (const beat of graph.beats) {
if (beat.estimatedStartMs > timeMs) break;
for (const action of beat.actions) {
applyAction(objects, action, beat.estimatedStartMs);
}
}
const active = graph.beats[activeBeatIndex];
const activeBeatProgress = active
? Math.max(0, Math.min(1, (timeMs - active.estimatedStartMs) / (active.estimatedEndMs - active.estimatedStartMs)))
: 0;
return { objects: [...objects.values()], activeBeatIndex, activeBeatProgress };
}
function applyAction(objects: Map<string, CanvasObjectState>, action: VisualAction, appearedAtMs: number) {
switch (action.type) {
case "add_object":
objects.set(action.objectId, {
objectId: action.objectId,
kind: action.objectType,
region: action.region,
appearedAtMs,
});
return;
case "remove_object":
objects.delete(action.objectId);
return;
case "clear_region":
for (const [id, object] of objects) {
if (object.region === action.region) objects.delete(id);
}
return;
case "move_object": {
const object = objects.get(action.objectId);
if (object) objects.set(action.objectId, { ...object, region: action.region });
return;
}
case "write_text":
objects.set(action.objectId, {
objectId: action.objectId,
kind: "text",
region: defaultTextRegion(action),
text: action.text,
role: action.role,
appearedAtMs,
});
return;
case "write_equation": {
const current = objects.get(action.objectId);
objects.set(action.objectId, {
objectId: action.objectId,
kind: "formula",
region: current?.region ?? "formula",
equation: action.equation,
explanation: action.explanation,
tokens: action.tokens,
appearedAtMs,
});
return;
}
case "question": {
const current = objects.get(action.objectId);
objects.set(action.objectId, {
objectId: action.objectId,
kind: "numerical",
region: current?.region ?? "centre",
text: action.prompt,
appearedAtMs,
});
return;
}
case "answer_reveal": {
const current = objects.get(action.objectId);
if (current) {
objects.set(action.objectId, {
...current,
answer: action.answer,
answerExplanation: action.explanation,
});
}
return;
}
case "reveal_label":
objects.set(action.objectId, {
objectId: action.objectId,
kind: "label",
region: "centre",
text: action.text,
label: action.targetId,
appearedAtMs,
});
return;
case "draw_arrow":
objects.set(action.objectId, {
objectId: action.objectId,
kind: "arrow",
region: "centre",
label: action.label,
appearedAtMs,
});
return;
case "show_measurement":
objects.set(action.objectId, {
objectId: action.objectId,
kind: "measurement",
region: "centre",
label: action.label,
appearedAtMs,
});
return;
case "highlight": {
const object = objects.get(action.targetId);
if (object) objects.set(action.targetId, { ...object, highlighted: true, dimmed: false });
return;
}
case "dim": {
const object = objects.get(action.targetId);
if (object) objects.set(action.targetId, { ...object, dimmed: true });
return;
}
case "focus": {
const object = objects.get(action.targetId);
if (object) objects.set(action.targetId, { ...object, dimmed: false });
return;
}
default:
return;
}
}
|