()
| 164 | }; |
| 165 | |
| 166 | async function main(): Promise<void> { |
| 167 | const cartesianEl = document.getElementById('chart-cartesian'); |
| 168 | const pieEl = document.getElementById('chart-pie'); |
| 169 | if (!(cartesianEl instanceof HTMLElement) || !(pieEl instanceof HTMLElement)) { |
| 170 | throw new Error('Chart containers not found'); |
| 171 | } |
| 172 | |
| 173 | const btnUpdate = document.getElementById('btn-update'); |
| 174 | const toggleAnimate = document.getElementById('toggle-animate'); |
| 175 | const toggleAuto = document.getElementById('toggle-auto'); |
| 176 | if (!(btnUpdate instanceof HTMLButtonElement)) throw new Error('Update button not found'); |
| 177 | if (!(toggleAnimate instanceof HTMLInputElement)) throw new Error('Animate toggle not found'); |
| 178 | if (!(toggleAuto instanceof HTMLInputElement)) throw new Error('Auto toggle not found'); |
| 179 | |
| 180 | // First render: disable animation so only subsequent setOption() updates animate. |
| 181 | const initialRng = createRng(1); |
| 182 | const initialCartesian = makeCartesianData(64, initialRng, { phase: 0, amplitude: 1.2, offset: 0.0 }); |
| 183 | const initialPie = makePieData(initialRng); |
| 184 | |
| 185 | const cartesianChart = await ChartGPU.create( |
| 186 | cartesianEl, |
| 187 | createCartesianOptions(initialCartesian, /* animation */ false) |
| 188 | ); |
| 189 | const pieChart = await ChartGPU.create(pieEl, createPieOptions(initialPie, /* animation */ false)); |
| 190 | |
| 191 | const ro = attachCoalescedResizeObserver([cartesianEl, pieEl], [cartesianChart, pieChart]); |
| 192 | |
| 193 | // Initial sizing/render. |
| 194 | cartesianChart.resize(); |
| 195 | pieChart.resize(); |
| 196 | |
| 197 | let step = 0; |
| 198 | const updateCharts = (source: 'auto' | 'manual'): void => { |
| 199 | step++; |
| 200 | const rng = createRng(1000 + step * 97); |
| 201 | |
| 202 | // Make domain changes obvious: vary amplitude and offset. |
| 203 | const phase = step * 0.7; |
| 204 | const amplitude = 0.9 + (step % 4) * 0.65; // 0.9..2.85 |
| 205 | const offset = (step % 2 === 0 ? -0.35 : 0.55) + (rng() - 0.5) * 0.15; |
| 206 | |
| 207 | const cartesianData = makeCartesianData(64, rng, { phase, amplitude, offset }); |
| 208 | const pieSlices = makePieData(rng); |
| 209 | |
| 210 | const animation = createAnimationConfig(toggleAnimate.checked); |
| 211 | |
| 212 | cartesianChart.setOption(createCartesianOptions(cartesianData, animation)); |
| 213 | pieChart.setOption(createPieOptions(pieSlices, animation)); |
| 214 | |
| 215 | const label = toggleAnimate.checked ? 'animated' : 'instant'; |
| 216 | setStatus(`Updated (${source}, ${label}) · step ${step} · amp ${amplitude.toFixed(2)} · offset ${clamp(offset, -5, 5).toFixed(2)}`); |
| 217 | }; |
| 218 | |
| 219 | btnUpdate.addEventListener('click', () => updateCharts('manual')); |
| 220 | toggleAnimate.addEventListener('change', () => { |
| 221 | setStatus(toggleAnimate.checked ? 'Animations enabled (next update).' : 'Animations disabled (next update).'); |
| 222 | }); |
| 223 |
no test coverage detected