| 1 | const A4W = 210; |
| 2 | const A4H = 297; |
| 3 | const PEN_MM = 0.4; |
| 4 | |
| 5 | const $ = (id) => document.getElementById(id); |
| 6 | const canvas = $("page"); |
| 7 | const ctx = canvas.getContext("2d"); |
| 8 | |
| 9 | let objects = []; |
| 10 | let selectedId = null; |
| 11 | let clipboard = null; |
| 12 | let preview = null; |
| 13 | let previewGen = 0; |
| 14 | let interacting = false; |
| 15 | let zoom = 1; |
| 16 | let panX = 0; |
| 17 | let panY = 0; |
| 18 | let filling = false; |
| 19 | let textTimer = 0; |
| 20 | let userDisconnected = false; |
| 21 | let autoTried = ""; |
| 22 | const PORT_KEY = "timdraw-port"; |
| 23 | |
| 24 | const defaults = { |
| 25 | drawLine: true, |
| 26 | drawFill: false, |
| 27 | hatchAngle: 45, |
| 28 | hatchSpacing: 2, |
| 29 | crosshatch: false, |
| 30 | grayscale: false, |
| 31 | graySteps: 5, |
| 32 | spacingMin: 0.8, |
| 33 | spacingMax: 4, |
| 34 | }; |
| 35 | |
| 36 | function selected () |
| 37 | { |
| 38 | return objects.find((obj) => obj.id === selectedId) || null; |
| 39 | } |
| 40 | |
| 41 | let toastTimer = 0; |
| 42 | |
| 43 | function toast (message, hideAfter) |
| 44 | { |
| 45 | const el = $("toast"); |
| 46 | clearTimeout(toastTimer); |
| 47 | el.hidden = !message; |
| 48 | el.textContent = message || ""; |
| 49 | if (message && hideAfter) |
| 50 | { |
| 51 | toastTimer = setTimeout(() => |
| 52 | { |
| 53 | el.hidden = true; |
| 54 | el.textContent = ""; |
| 55 | }, hideAfter); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | async function api (path, body) |
| 60 | { |
| 61 | const options = body |
| 62 | ? { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body) } |
| 63 | : {}; |
| 64 | const response = await fetch(path, options); |
| 65 | const data = await response.json().catch(() => ({})); |
| 66 | if (!response.ok) throw new Error(data.error || response.statusText); |
| 67 | return data; |
| 68 | } |
| 69 | |
| 70 | function readSettings () |
| 71 | { |
| 72 | return { |
| 73 | drawSpeed: Number($("draw-speed").value), |
| 74 | travelSpeed: Number($("travel-speed").value), |
| 75 | penUpZ: Number($("pen-up").value), |
| 76 | penDownZ: Number($("pen-down").value), |
| 77 | dwellMs: Number($("dwell").value), |
| 78 | origin: $("origin").value, |
| 79 | perObject: $("per-object").checked, |
| 80 | showTravel: $("show-travel").checked, |
| 81 | jogStep: Number($("jog-step").value), |
| 82 | }; |
| 83 | } |
| 84 | |
| 85 | function styleFlags (obj) |
| 86 | { |
| 87 | if (obj.drawLine != null || obj.drawFill != null) |
| 88 | return { drawLine: !!obj.drawLine, drawFill: !!obj.drawFill }; |
| 89 | if (obj.mode === "fill") return { drawLine: false, drawFill: true }; |
| 90 | return { drawLine: true, drawFill: false }; |
| 91 | } |
| 92 | |
| 93 | function styleLabel (obj) |
| 94 | { |
| 95 | const flags = styleFlags(obj); |
| 96 | if (flags.drawLine && flags.drawFill) return "lijn + vulling"; |
| 97 | if (flags.drawFill) return "vulling"; |
| 98 | if (flags.drawLine) return "lijn"; |
| 99 | return "uit"; |
| 100 | } |
| 101 | |
| 102 | function hatchFromForm () |
| 103 | { |
| 104 | return { |
| 105 | drawLine: $("prop-line").checked, |
| 106 | drawFill: $("prop-fill").checked, |
| 107 | hatchAngle: Number($("prop-angle").value), |
| 108 | hatchSpacing: Number($("prop-spacing").value), |
| 109 | crosshatch: $("prop-cross").checked, |
| 110 | grayscale: $("prop-gray").checked, |
| 111 | graySteps: Number($("prop-steps").value), |
| 112 | spacingMin: Number($("prop-dark").value), |
| 113 | spacingMax: Number($("prop-light").value), |
| 114 | }; |
| 115 | } |
| 116 | |
| 117 | function layout () |
| 118 | { |
| 119 | const rect = canvas.getBoundingClientRect(); |
| 120 | const margin = 28; |
| 121 | const fit = Math.min((rect.width - margin * 2) / A4W, (rect.height - margin * 2) / A4H); |
| 122 | const scale = fit * zoom; |
| 123 | return { |
| 124 | scale, |
| 125 | ox: (rect.width - A4W * scale) / 2 + panX, |
| 126 | oy: (rect.height - A4H * scale) / 2 + panY, |
| 127 | width: rect.width, |
| 128 | height: rect.height, |
| 129 | }; |
| 130 | } |
| 131 | |
| 132 | function mmToPx (x, y) |
| 133 | { |
| 134 | const view = layout(); |
| 135 | return [view.ox + x * view.scale, view.oy + (A4H - y) * view.scale]; |
| 136 | } |
| 137 | |
| 138 | function pxToMm (px, py) |
| 139 | { |
| 140 | const view = layout(); |
| 141 | return [(px - view.ox) / view.scale, A4H - (py - view.oy) / view.scale]; |
| 142 | } |
| 143 | |
| 144 | function objPoint (obj, x, y) |
| 145 | { |
| 146 | const scale = obj.scale || 1; |
| 147 | x *= scale; |
| 148 | y *= scale; |
| 149 | const angle = (obj.rotation || 0) * Math.PI / 180; |
| 150 | const ca = Math.cos(angle); |
| 151 | const sa = Math.sin(angle); |
| 152 | return [x * ca - y * sa + obj.x, x * sa + y * ca + obj.y]; |
| 153 | } |
| 154 | |
| 155 | function eachLocal (obj, fn) |
| 156 | { |
| 157 | for (const stroke of obj.strokes || []) |
| 158 | { |
| 159 | for (const point of stroke.pts || stroke) fn(point[0], point[1]); |
| 160 | } |
| 161 | for (const fill of obj.fills || []) |
| 162 | { |
| 163 | for (const ring of fill.rings || []) |
| 164 | { |
| 165 | for (const point of ring) fn(point[0], point[1]); |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | function bbox (obj) |
| 171 | { |
| 172 | let minx = Infinity; |
| 173 | let miny = Infinity; |
| 174 | let maxx = -Infinity; |
| 175 | let maxy = -Infinity; |
| 176 | eachLocal(obj, (x, y) => |
| 177 | { |
| 178 | const point = objPoint(obj, x, y); |
| 179 | minx = Math.min(minx, point[0]); |
| 180 | miny = Math.min(miny, point[1]); |
| 181 | maxx = Math.max(maxx, point[0]); |
| 182 | maxy = Math.max(maxy, point[1]); |
| 183 | }); |
| 184 | if (!Number.isFinite(minx)) |
| 185 | { |
| 186 | return { minx: obj.x - 5, miny: obj.y - 5, maxx: obj.x + 5, maxy: obj.y + 5 }; |
| 187 | } |
| 188 | return { minx, miny, maxx, maxy }; |
| 189 | } |
| 190 | |
| 191 | function resize () |
| 192 | { |
| 193 | const rect = canvas.getBoundingClientRect(); |
| 194 | const dpr = window.devicePixelRatio || 1; |
| 195 | canvas.width = Math.max(1, rect.width * dpr); |
| 196 | canvas.height = Math.max(1, rect.height * dpr); |
| 197 | ctx.setTransform(dpr, 0, 0, dpr, 0, 0); |
| 198 | draw(); |
| 199 | } |
| 200 | |
| 201 | function drawSheet (view) |
| 202 | { |
| 203 | const origin = mmToPx(0, 0); |
| 204 | const far = mmToPx(A4W, A4H); |
| 205 | const x = Math.min(origin[0], far[0]); |
| 206 | const y = Math.min(origin[1], far[1]); |
| 207 | const w = Math.abs(far[0] - origin[0]); |
| 208 | const h = Math.abs(far[1] - origin[1]); |
| 209 | ctx.save(); |
| 210 | ctx.fillStyle = "rgba(0,0,0,0.35)"; |
| 211 | ctx.fillRect(x + 8, y + 10, w, h); |
| 212 | ctx.fillStyle = "#f6f1e6"; |
| 213 | ctx.fillRect(x, y, w, h); |
| 214 | ctx.strokeStyle = "rgba(80,60,40,0.18)"; |
| 215 | ctx.lineWidth = 1; |
| 216 | ctx.beginPath(); |
| 217 | for (let i = 10; i < A4W; i += 10) |
| 218 | { |
| 219 | const a = mmToPx(i, 0); |
| 220 | const b = mmToPx(i, A4H); |
| 221 | ctx.moveTo(a[0], a[1]); |
| 222 | ctx.lineTo(b[0], b[1]); |
| 223 | } |
| 224 | for (let i = 10; i < A4H; i += 10) |
| 225 | { |
| 226 | const a = mmToPx(0, i); |
| 227 | const b = mmToPx(A4W, i); |
| 228 | ctx.moveTo(a[0], a[1]); |
| 229 | ctx.lineTo(b[0], b[1]); |
| 230 | } |
| 231 | ctx.stroke(); |
| 232 | ctx.strokeStyle = "#2b241e"; |
| 233 | ctx.lineWidth = 1.5; |
| 234 | ctx.strokeRect(x, y, w, h); |
| 235 | ctx.fillStyle = "#8a7b6c"; |
| 236 | ctx.font = "12px Liberation Sans, sans-serif"; |
| 237 | const originTop = $("origin").value === "top-left"; |
| 238 | ctx.fillText("0,0", x + 6, originTop ? y + 16 : y + h - 8); |
| 239 | ctx.fillText("A4", x + w - 26, originTop ? y + h - 8 : y + 16); |
| 240 | ctx.restore(); |
| 241 | } |
| 242 | |
| 243 | function tracePoly (points) |
| 244 | { |
| 245 | if (!points.length) return; |
| 246 | const first = mmToPx(points[0][0], points[0][1]); |
| 247 | ctx.moveTo(first[0], first[1]); |
| 248 | for (let i = 1; i < points.length; i++) |
| 249 | { |
| 250 | const point = mmToPx(points[i][0], points[i][1]); |
| 251 | ctx.lineTo(point[0], point[1]); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | function drawObject (obj, active) |
| 256 | { |
| 257 | ctx.save(); |
| 258 | ctx.lineWidth = active ? 1.6 : 1.15; |
| 259 | ctx.strokeStyle = "#1d1a17"; |
| 260 | ctx.fillStyle = "rgba(224,122,61,0.13)"; |
| 261 | ctx.lineJoin = "round"; |
| 262 | ctx.lineCap = "round"; |
| 263 | ctx.beginPath(); |
| 264 | for (const fill of obj.fills || []) |
| 265 | { |
| 266 | for (const ring of fill.rings || []) |
| 267 | { |
| 268 | const page = ring.map((point) => objPoint(obj, point[0], point[1])); |
| 269 | tracePoly(page); |
| 270 | ctx.closePath(); |
| 271 | } |
| 272 | } |
| 273 | ctx.fill("evenodd"); |
| 274 | ctx.beginPath(); |
| 275 | for (const stroke of obj.strokes || []) |
| 276 | { |
| 277 | const pts = (stroke.pts || stroke).map((point) => objPoint(obj, point[0], point[1])); |
| 278 | tracePoly(pts); |
| 279 | } |
| 280 | ctx.stroke(); |
| 281 | ctx.restore(); |
| 282 | } |
| 283 | |
| 284 | function drawPreview () |
| 285 | { |
| 286 | ctx.save(); |
| 287 | ctx.lineCap = "round"; |
| 288 | ctx.lineJoin = "round"; |
| 289 | ctx.strokeStyle = "#1d1a17"; |
| 290 | ctx.lineWidth = 1.15; |
| 291 | ctx.beginPath(); |
| 292 | for (const line of preview.draw || []) tracePoly(line); |
| 293 | ctx.stroke(); |
| 294 | if ($("show-travel").checked) |
| 295 | { |
| 296 | ctx.strokeStyle = "rgba(176,122,62,0.85)"; |
| 297 | ctx.setLineDash([4, 4]); |
| 298 | ctx.lineWidth = 1; |
| 299 | ctx.beginPath(); |
| 300 | for (const line of preview.travel || []) tracePoly(line); |
| 301 | ctx.stroke(); |
| 302 | } |
| 303 | ctx.restore(); |
| 304 | } |
| 305 | |
| 306 | function drawHandles (obj) |
| 307 | { |
| 308 | const box = bbox(obj); |
| 309 | const corners = [ |
| 310 | mmToPx(box.minx, box.maxy), |
| 311 | mmToPx(box.maxx, box.maxy), |
| 312 | mmToPx(box.maxx, box.miny), |
| 313 | mmToPx(box.minx, box.miny), |
| 314 | ]; |
| 315 | ctx.save(); |
| 316 | ctx.strokeStyle = "#e07a3d"; |
| 317 | ctx.fillStyle = "#fff"; |
| 318 | ctx.lineWidth = 1.4; |
| 319 | ctx.beginPath(); |
| 320 | corners.forEach((point, index) => index ? ctx.lineTo(point[0], point[1]) : ctx.moveTo(point[0], point[1])); |
| 321 | ctx.closePath(); |
| 322 | ctx.stroke(); |
| 323 | const top = mmToPx((box.minx + box.maxx) / 2, box.maxy); |
| 324 | ctx.beginPath(); |
| 325 | ctx.moveTo(top[0], top[1]); |
| 326 | ctx.lineTo(top[0], top[1] - 22); |
| 327 | ctx.stroke(); |
| 328 | ctx.beginPath(); |
| 329 | ctx.arc(top[0], top[1] - 22, 5, 0, Math.PI * 2); |
| 330 | ctx.fill(); |
| 331 | ctx.stroke(); |
| 332 | for (const point of corners) |
| 333 | { |
| 334 | ctx.fillRect(point[0] - 4, point[1] - 4, 8, 8); |
| 335 | ctx.strokeRect(point[0] - 4, point[1] - 4, 8, 8); |
| 336 | } |
| 337 | ctx.restore(); |
| 338 | } |
| 339 | |
| 340 | function draw () |
| 341 | { |
| 342 | const view = layout(); |
| 343 | ctx.clearRect(0, 0, view.width, view.height); |
| 344 | drawSheet(view); |
| 345 | const current = selected(); |
| 346 | if (preview && !interacting) drawPreview(); |
| 347 | else objects.forEach((obj) => drawObject(obj, obj === current)); |
| 348 | if (current) drawHandles(current); |
| 349 | } |
| 350 | |
| 351 | const SETTINGS_KEY = "timdraw-settings"; |
| 352 | |
| 353 | function persist () |
| 354 | { |
| 355 | const state = { |
| 356 | settings: readSettings(), |
| 357 | defaults, |
| 358 | composer: { |
| 359 | text: $("new-text").value, |
| 360 | font: $("new-font").value, |
| 361 | size: Number($("new-size").value), |
| 362 | }, |
| 363 | view: { zoom, panX, panY }, |
| 364 | }; |
| 365 | localStorage.setItem(SETTINGS_KEY, JSON.stringify(state)); |
| 366 | } |
| 367 | |
| 368 | function applySaved (saved) |
| 369 | { |
| 370 | if (!saved) return; |
| 371 | Object.assign(defaults, saved.defaults || {}); |
| 372 | const settings = saved.settings || {}; |
| 373 | if (settings.drawSpeed) $("draw-speed").value = settings.drawSpeed; |
| 374 | if (settings.travelSpeed) $("travel-speed").value = settings.travelSpeed; |
| 375 | if (settings.penUpZ != null) $("pen-up").value = settings.penUpZ; |
| 376 | if (settings.penDownZ != null) $("pen-down").value = settings.penDownZ; |
| 377 | if (settings.dwellMs != null) $("dwell").value = settings.dwellMs; |
| 378 | if (localStorage.getItem("timdraw-origin-v2") && settings.origin) $("origin").value = settings.origin; |
| 379 | else $("origin").value = "top-left"; |
| 380 | localStorage.setItem("timdraw-origin-v2", "1"); |
| 381 | $("per-object").checked = !!settings.perObject; |
| 382 | $("show-travel").checked = !!settings.showTravel; |
| 383 | if (settings.jogStep) $("jog-step").value = String(settings.jogStep); |
| 384 | const composer = saved.composer || {}; |
| 385 | if (composer.text != null) $("new-text").value = composer.text; |
| 386 | if (composer.font) $("new-font").value = composer.font; |
| 387 | if (composer.size) $("new-size").value = composer.size; |
| 388 | const view = saved.view || {}; |
| 389 | if (view.zoom) zoom = view.zoom; |
| 390 | if (view.panX) panX = view.panX; |
| 391 | if (view.panY) panY = view.panY; |
| 392 | } |
| 393 | |
| 394 | function schedulePreview () |
| 395 | { |
| 396 | clearTimeout(schedulePreview.timer); |
| 397 | schedulePreview.timer = setTimeout(refreshPreview, 160); |
| 398 | } |
| 399 | |
| 400 | async function refreshPreview () |
| 401 | { |
| 402 | const gen = ++previewGen; |
| 403 | try |
| 404 | { |
| 405 | const data = await api("/api/preview", { objects, settings: readSettings() }); |
| 406 | if (gen !== previewGen) return; |
| 407 | preview = data; |
| 408 | const stats = data.stats || {}; |
| 409 | const minutes = Math.floor((stats.seconds || 0) / 60); |
| 410 | const seconds = Math.round((stats.seconds || 0) % 60); |
| 411 | $("stats").textContent = objects.length |
| 412 | ? `${stats.drawMm || 0} mm tekenen · ${stats.travelMm || 0} mm reizen · ongeveer ${minutes} min ${seconds} s` |
| 413 | : ""; |
| 414 | if (data.warnings && data.warnings.length) toast(data.warnings[0]); |
| 415 | } catch (error) |
| 416 | { |
| 417 | toast(error.message); |
| 418 | } |
| 419 | draw(); |
| 420 | persist(); |
| 421 | } |
| 422 | |
| 423 | function renderList () |
| 424 | { |
| 425 | const list = $("layers"); |
| 426 | list.innerHTML = ""; |
| 427 | objects.forEach((obj) => |
| 428 | { |
| 429 | const item = document.createElement("li"); |
| 430 | const button = document.createElement("button"); |
| 431 | button.type = "button"; |
| 432 | button.className = obj.id === selectedId ? "active" : ""; |
| 433 | button.innerHTML = `<span>${obj.name || "Object"}</span><span class="tag">${styleLabel(obj)}</span>`; |
| 434 | button.onclick = () => |
| 435 | { |
| 436 | selectedId = obj.id; |
| 437 | renderList(); |
| 438 | fillForm(); |
| 439 | draw(); |
| 440 | }; |
| 441 | item.appendChild(button); |
| 442 | list.appendChild(item); |
| 443 | }); |
| 444 | } |
| 445 | |
| 446 | function fillForm () |
| 447 | { |
| 448 | const obj = selected() || defaults; |
| 449 | filling = true; |
| 450 | $("prop-title").textContent = selected() ? (selected().name || "Object") : "Voor nieuwe objecten"; |
| 451 | const flags = styleFlags(obj); |
| 452 | $("prop-line").checked = flags.drawLine; |
| 453 | $("prop-fill").checked = flags.drawFill; |
| 454 | $("prop-angle").value = obj.hatchAngle; |
| 455 | $("prop-spacing").value = obj.hatchSpacing; |
| 456 | $("prop-percent").value = Math.round(Math.min(100, Math.max(2, (PEN_MM / obj.hatchSpacing) * 100))); |
| 457 | $("prop-cross").checked = !!obj.crosshatch; |
| 458 | $("prop-gray").checked = !!obj.grayscale; |
| 459 | $("prop-steps").value = obj.graySteps; |
| 460 | $("prop-dark").value = obj.spacingMin; |
| 461 | $("prop-light").value = obj.spacingMax; |
| 462 | $("gray-props").hidden = !obj.grayscale; |
| 463 | const isText = selected() && selected().kind === "text"; |
| 464 | $("text-props").hidden = !isText; |
| 465 | if (isText) |
| 466 | { |
| 467 | $("prop-text").value = selected().text || ""; |
| 468 | $("prop-font").value = selected().font || "futural"; |
| 469 | $("prop-size").value = selected().fontSize || 12; |
| 470 | } |
| 471 | filling = false; |
| 472 | } |
| 473 | |
| 474 | function applyForm () |
| 475 | { |
| 476 | if (filling) return; |
| 477 | const values = hatchFromForm(); |
| 478 | const obj = selected(); |
| 479 | if (obj) Object.assign(obj, values); |
| 480 | else Object.assign(defaults, values); |
| 481 | $("gray-props").hidden = !values.grayscale; |
| 482 | renderList(); |
| 483 | schedulePreview(); |
| 484 | } |
| 485 | |
| 486 | function syncPercent (fromPercent) |
| 487 | { |
| 488 | if (filling) return; |
| 489 | if (fromPercent) |
| 490 | { |
| 491 | const percent = Math.min(100, Math.max(2, Number($("prop-percent").value) || 2)); |
| 492 | $("prop-spacing").value = (PEN_MM / (percent / 100)).toFixed(2); |
| 493 | } else |
| 494 | { |
| 495 | const spacing = Math.max(0.3, Number($("prop-spacing").value) || 0.3); |
| 496 | $("prop-percent").value = Math.round(Math.min(100, (PEN_MM / spacing) * 100)); |
| 497 | } |
| 498 | applyForm(); |
| 499 | } |
| 500 | |
| 501 | function addObject (geo, name, extra = {}) |
| 502 | { |
| 503 | const grays = [...new Set((geo.fills || []).map((fill) => Math.round(fill.gray * 20)))]; |
| 504 | const obj = { |
| 505 | id: crypto.randomUUID(), |
| 506 | name, |
| 507 | kind: extra.kind || "svg", |
| 508 | x: geo.x, |
| 509 | y: geo.y, |
| 510 | scale: 1, |
| 511 | rotation: 0, |
| 512 | strokes: geo.strokes || [], |
| 513 | fills: geo.fills || [], |
| 514 | ...defaults, |
| 515 | grayscale: grays.length > 1 || defaults.grayscale, |
| 516 | ...extra, |
| 517 | drawLine: extra.drawLine != null ? extra.drawLine : defaults.drawLine, |
| 518 | drawFill: extra.drawFill != null ? extra.drawFill : (grays.length > 1 || defaults.drawFill), |
| 519 | }; |
| 520 | objects.push(obj); |
| 521 | selectedId = obj.id; |
| 522 | renderList(); |
| 523 | fillForm(); |
| 524 | if (geo.warnings && geo.warnings.length) toast(geo.warnings.join(" ")); |
| 525 | schedulePreview(); |
| 526 | } |
| 527 | |
| 528 | async function importSvgText (svg, name) |
| 529 | { |
| 530 | const geo = await api("/api/svg", { svg, name }); |
| 531 | addObject(geo, geo.name || name || "SVG"); |
| 532 | } |
| 533 | |
| 534 | function cloneSelected (offset) |
| 535 | { |
| 536 | const obj = selected(); |
| 537 | if (!obj) return; |
| 538 | const copy = structuredClone(obj); |
| 539 | copy.id = crypto.randomUUID(); |
| 540 | copy.x += offset; |
| 541 | copy.y += offset; |
| 542 | copy.name = `${obj.name} kopie`; |
| 543 | objects.push(copy); |
| 544 | selectedId = copy.id; |
| 545 | renderList(); |
| 546 | fillForm(); |
| 547 | schedulePreview(); |
| 548 | } |
| 549 | |
| 550 | async function rebuildText (obj) |
| 551 | { |
| 552 | const geo = await api("/api/text", { |
| 553 | text: obj.text, |
| 554 | font: obj.font, |
| 555 | fontSize: obj.fontSize, |
| 556 | lineSpacing: 1.4, |
| 557 | }); |
| 558 | obj.strokes = geo.strokes; |
| 559 | obj.fills = geo.fills; |
| 560 | schedulePreview(); |
| 561 | draw(); |
| 562 | } |
| 563 | |
| 564 | function handleAt (px, py) |
| 565 | { |
| 566 | const obj = selected(); |
| 567 | if (!obj) return null; |
| 568 | const box = bbox(obj); |
| 569 | const corners = { |
| 570 | nw: mmToPx(box.minx, box.maxy), |
| 571 | ne: mmToPx(box.maxx, box.maxy), |
| 572 | se: mmToPx(box.maxx, box.miny), |
| 573 | sw: mmToPx(box.minx, box.miny), |
| 574 | }; |
| 575 | const top = mmToPx((box.minx + box.maxx) / 2, box.maxy); |
| 576 | if (Math.hypot(top[0] - px, top[1] - 22 - py) < 9) return "rot"; |
| 577 | for (const [name, point] of Object.entries(corners)) |
| 578 | { |
| 579 | if (Math.hypot(point[0] - px, point[1] - py) < 9) return name; |
| 580 | } |
| 581 | return null; |
| 582 | } |
| 583 | |
| 584 | function hitObject (x, y) |
| 585 | { |
| 586 | const view = layout(); |
| 587 | const pad = 5 / view.scale; |
| 588 | for (let i = objects.length - 1; i >= 0; i--) |
| 589 | { |
| 590 | const box = bbox(objects[i]); |
| 591 | if (x >= box.minx - pad && x <= box.maxx + pad && y >= box.miny - pad && y <= box.maxy + pad) return objects[i]; |
| 592 | } |
| 593 | return null; |
| 594 | } |
| 595 | |
| 596 | let drag = null; |
| 597 | |
| 598 | canvas.addEventListener("pointerdown", (event) => |
| 599 | { |
| 600 | if (event.button === 1 || event.altKey) |
| 601 | { |
| 602 | drag = { type: "pan", x: event.clientX, y: event.clientY, panX, panY }; |
| 603 | canvas.setPointerCapture(event.pointerId); |
| 604 | return; |
| 605 | } |
| 606 | const rect = canvas.getBoundingClientRect(); |
| 607 | const px = event.clientX - rect.left; |
| 608 | const py = event.clientY - rect.top; |
| 609 | const handle = handleAt(px, py); |
| 610 | const [x, y] = pxToMm(px, py); |
| 611 | const obj = selected(); |
| 612 | if (handle && obj) |
| 613 | { |
| 614 | const box = bbox(obj); |
| 615 | const cx = (box.minx + box.maxx) / 2; |
| 616 | const cy = (box.miny + box.maxy) / 2; |
| 617 | drag = { |
| 618 | type: handle === "rot" ? "rot" : "scale", |
| 619 | id: obj.id, |
| 620 | cx, cy, |
| 621 | startAngle: Math.atan2(y - cy, x - cx), |
| 622 | startRot: obj.rotation || 0, |
| 623 | startDist: Math.hypot(x - cx, y - cy) || 1, |
| 624 | startScale: obj.scale || 1, |
| 625 | }; |
| 626 | } else |
| 627 | { |
| 628 | const hit = hitObject(x, y); |
| 629 | selectedId = hit ? hit.id : null; |
| 630 | renderList(); |
| 631 | fillForm(); |
| 632 | if (hit) drag = { type: "move", id: hit.id, x, y, ox: hit.x, oy: hit.y }; |
| 633 | } |
| 634 | interacting = !!drag; |
| 635 | canvas.setPointerCapture(event.pointerId); |
| 636 | draw(); |
| 637 | }); |
| 638 | |
| 639 | canvas.addEventListener("pointermove", (event) => |
| 640 | { |
| 641 | if (!drag) return; |
| 642 | if (drag.type === "pan") |
| 643 | { |
| 644 | panX = drag.panX + (event.clientX - drag.x); |
| 645 | panY = drag.panY + (event.clientY - drag.y); |
| 646 | draw(); |
| 647 | return; |
| 648 | } |
| 649 | const rect = canvas.getBoundingClientRect(); |
| 650 | const [x, y] = pxToMm(event.clientX - rect.left, event.clientY - rect.top); |
| 651 | const obj = objects.find((item) => item.id === drag.id); |
| 652 | if (!obj) return; |
| 653 | if (drag.type === "move") |
| 654 | { |
| 655 | obj.x = drag.ox + (x - drag.x); |
| 656 | obj.y = drag.oy + (y - drag.y); |
| 657 | } else if (drag.type === "scale") |
| 658 | { |
| 659 | const dist = Math.hypot(x - drag.cx, y - drag.cy); |
| 660 | obj.scale = Math.max(0.05, Math.min(20, drag.startScale * (dist / drag.startDist))); |
| 661 | } else if (drag.type === "rot") |
| 662 | { |
| 663 | const angle = Math.atan2(y - drag.cy, x - drag.cx); |
| 664 | obj.rotation = Math.round(drag.startRot + (angle - drag.startAngle) * 180 / Math.PI); |
| 665 | } |
| 666 | draw(); |
| 667 | }); |
| 668 | |
| 669 | function endDrag () |
| 670 | { |
| 671 | if (drag && drag.type !== "pan") schedulePreview(); |
| 672 | else if (drag && drag.type === "pan") persist(); |
| 673 | drag = null; |
| 674 | interacting = false; |
| 675 | } |
| 676 | |
| 677 | canvas.addEventListener("pointerup", endDrag); |
| 678 | canvas.addEventListener("pointercancel", endDrag); |
| 679 | canvas.addEventListener("wheel", (event) => |
| 680 | { |
| 681 | event.preventDefault(); |
| 682 | zoom = Math.max(0.35, Math.min(8, zoom * (event.deltaY < 0 ? 1.08 : 0.92))); |
| 683 | draw(); |
| 684 | persist(); |
| 685 | }, { passive: false }); |
| 686 | |
| 687 | window.addEventListener("keydown", (event) => |
| 688 | { |
| 689 | const typing = ["INPUT", "TEXTAREA", "SELECT"].includes(event.target.tagName); |
| 690 | if ((event.key === "Delete" || event.key === "Backspace") && !typing) |
| 691 | { |
| 692 | objects = objects.filter((obj) => obj.id !== selectedId); |
| 693 | selectedId = null; |
| 694 | renderList(); |
| 695 | fillForm(); |
| 696 | schedulePreview(); |
| 697 | } else if (event.key === "Escape") |
| 698 | { |
| 699 | selectedId = null; |
| 700 | renderList(); |
| 701 | fillForm(); |
| 702 | draw(); |
| 703 | } else if (!typing && (event.key.startsWith("Arrow"))) |
| 704 | { |
| 705 | const obj = selected(); |
| 706 | if (!obj) return; |
| 707 | const step = event.shiftKey ? 0.1 : 1; |
| 708 | if (event.key === "ArrowLeft") obj.x -= step; |
| 709 | if (event.key === "ArrowRight") obj.x += step; |
| 710 | if (event.key === "ArrowUp") obj.y += step; |
| 711 | if (event.key === "ArrowDown") obj.y -= step; |
| 712 | event.preventDefault(); |
| 713 | schedulePreview(); |
| 714 | draw(); |
| 715 | } else if ((event.ctrlKey || event.metaKey) && event.key.toLowerCase() === "c" && !typing) |
| 716 | { |
| 717 | clipboard = selected() ? structuredClone(selected()) : clipboard; |
| 718 | } else if ((event.ctrlKey || event.metaKey) && event.key.toLowerCase() === "v" && !typing) |
| 719 | { |
| 720 | if (!clipboard) return; |
| 721 | const copy = structuredClone(clipboard); |
| 722 | copy.id = crypto.randomUUID(); |
| 723 | copy.x += 8; |
| 724 | copy.y += 8; |
| 725 | objects.push(copy); |
| 726 | selectedId = copy.id; |
| 727 | renderList(); |
| 728 | fillForm(); |
| 729 | schedulePreview(); |
| 730 | } else if ((event.ctrlKey || event.metaKey) && event.key.toLowerCase() === "d" && !typing) |
| 731 | { |
| 732 | event.preventDefault(); |
| 733 | cloneSelected(8); |
| 734 | } |
| 735 | }); |
| 736 | |
| 737 | document.querySelectorAll('#prop-angle,#prop-steps,#prop-dark,#prop-light').forEach((el) => el.addEventListener("input", applyForm)); |
| 738 | $("prop-spacing").addEventListener("input", () => syncPercent(false)); |
| 739 | $("prop-percent").addEventListener("input", () => syncPercent(true)); |
| 740 | $("prop-cross").addEventListener("change", applyForm); |
| 741 | $("prop-gray").addEventListener("change", applyForm); |
| 742 | $("prop-line").addEventListener("change", applyForm); |
| 743 | $("prop-fill").addEventListener("change", applyForm); |
| 744 | |
| 745 | function queueTextRebuild () |
| 746 | { |
| 747 | const obj = selected(); |
| 748 | if (!obj || obj.kind !== "text" || filling) return; |
| 749 | obj.text = $("prop-text").value; |
| 750 | obj.font = $("prop-font").value; |
| 751 | obj.fontSize = Number($("prop-size").value); |
| 752 | obj.name = (obj.text || "Tekst").split("\n")[0].slice(0, 28) || "Tekst"; |
| 753 | clearTimeout(textTimer); |
| 754 | textTimer = setTimeout(() => rebuildText(obj).catch((error) => toast(error.message)), 180); |
| 755 | renderList(); |
| 756 | } |
| 757 | $("prop-text").addEventListener("input", queueTextRebuild); |
| 758 | $("prop-font").addEventListener("change", queueTextRebuild); |
| 759 | $("prop-size").addEventListener("input", queueTextRebuild); |
| 760 | |
| 761 | $("btn-import").onclick = () => $("file").click(); |
| 762 | $("btn-open").onclick = () => $("open-file").click(); |
| 763 | $("btn-save").onclick = saveSheet; |
| 764 | $("file").onchange = async () => |
| 765 | { |
| 766 | const file = $("file").files[0]; |
| 767 | $("file").value = ""; |
| 768 | if (!file) return; |
| 769 | try |
| 770 | { |
| 771 | await importSvgText(await file.text(), file.name); |
| 772 | } catch (error) |
| 773 | { |
| 774 | toast(error.message); |
| 775 | } |
| 776 | }; |
| 777 | |
| 778 | function saveSheet () |
| 779 | { |
| 780 | const payload = { format: "timdraw", version: 1, objects }; |
| 781 | const blob = new Blob([JSON.stringify(payload)], { type: "application/json" }); |
| 782 | const link = document.createElement("a"); |
| 783 | const raw = (objects[0] && objects[0].name) || "tekening"; |
| 784 | link.href = URL.createObjectURL(blob); |
| 785 | link.download = `${raw.replace(/[^\w.-]+/g, "_").slice(0, 40) || "tekening"}.tdrw`; |
| 786 | link.click(); |
| 787 | setTimeout(() => URL.revokeObjectURL(link.href), 1000); |
| 788 | } |
| 789 | |
| 790 | function loadSheet (data) |
| 791 | { |
| 792 | if (!data || data.format !== "timdraw" || !Array.isArray(data.objects)) |
| 793 | throw new Error("Dit is geen Timdraw-bestand."); |
| 794 | objects = data.objects.map((obj) => ({ ...obj, id: obj.id || crypto.randomUUID() })); |
| 795 | selectedId = null; |
| 796 | clipboard = null; |
| 797 | renderList(); |
| 798 | fillForm(); |
| 799 | schedulePreview(); |
| 800 | } |
| 801 | |
| 802 | $("open-file").onchange = async () => |
| 803 | { |
| 804 | const file = $("open-file").files[0]; |
| 805 | $("open-file").value = ""; |
| 806 | if (!file) return; |
| 807 | try |
| 808 | { |
| 809 | loadSheet(JSON.parse(await file.text())); |
| 810 | } catch (error) |
| 811 | { |
| 812 | toast(error instanceof SyntaxError ? "Dit is geen Timdraw-bestand." : error.message); |
| 813 | } |
| 814 | }; |
| 815 | |
| 816 | $("demos").onchange = async () => |
| 817 | { |
| 818 | const name = $("demos").value; |
| 819 | if (!name) return; |
| 820 | try |
| 821 | { |
| 822 | const response = await fetch("/api/demos/" + encodeURIComponent(name)); |
| 823 | if (!response.ok) throw new Error("Voorbeeld niet gevonden"); |
| 824 | await importSvgText(await response.text(), name); |
| 825 | } catch (error) |
| 826 | { |
| 827 | toast(error.message); |
| 828 | } |
| 829 | $("demos").value = ""; |
| 830 | }; |
| 831 | |
| 832 | $("btn-text").onclick = async () => |
| 833 | { |
| 834 | try |
| 835 | { |
| 836 | const text = $("new-text").value || "Timdraw"; |
| 837 | const font = $("new-font").value; |
| 838 | const fontSize = Number($("new-size").value) || 12; |
| 839 | const geo = await api("/api/text", { text, font, fontSize, lineSpacing: 1.4 }); |
| 840 | addObject(geo, text.split("\n")[0].slice(0, 28) || "Tekst", { |
| 841 | kind: "text", text, font, fontSize, lineSpacing: 1.4, drawLine: true, drawFill: false, grayscale: false, |
| 842 | }); |
| 843 | } catch (error) |
| 844 | { |
| 845 | toast(error.message); |
| 846 | } |
| 847 | }; |
| 848 | |
| 849 | $("btn-copy").onclick = () => { clipboard = selected() ? structuredClone(selected()) : clipboard; }; |
| 850 | $("btn-paste").onclick = () => |
| 851 | { |
| 852 | if (!clipboard) return; |
| 853 | const copy = structuredClone(clipboard); |
| 854 | copy.id = crypto.randomUUID(); |
| 855 | copy.x += 8; |
| 856 | copy.y += 8; |
| 857 | objects.push(copy); |
| 858 | selectedId = copy.id; |
| 859 | renderList(); |
| 860 | fillForm(); |
| 861 | schedulePreview(); |
| 862 | }; |
| 863 | $("btn-dup").onclick = () => cloneSelected(8); |
| 864 | $("btn-delete").onclick = () => |
| 865 | { |
| 866 | objects = objects.filter((obj) => obj.id !== selectedId); |
| 867 | selectedId = null; |
| 868 | renderList(); |
| 869 | fillForm(); |
| 870 | schedulePreview(); |
| 871 | }; |
| 872 | $("btn-front").onclick = () => moveLayer(1); |
| 873 | $("btn-back").onclick = () => moveLayer(-1); |
| 874 | |
| 875 | function moveLayer (dir) |
| 876 | { |
| 877 | const index = objects.findIndex((obj) => obj.id === selectedId); |
| 878 | const next = index + dir; |
| 879 | if (index < 0 || next < 0 || next >= objects.length) return; |
| 880 | const [item] = objects.splice(index, 1); |
| 881 | objects.splice(next, 0, item); |
| 882 | renderList(); |
| 883 | schedulePreview(); |
| 884 | } |
| 885 | |
| 886 | $("btn-fit").onclick = () => { zoom = 1; panX = 0; panY = 0; draw(); persist(); }; |
| 887 | $("show-travel").onchange = () => { draw(); persist(); }; |
| 888 | $("per-object").addEventListener("change", schedulePreview); |
| 889 | $("jog-step").addEventListener("change", persist); |
| 890 | $("new-text").addEventListener("input", persist); |
| 891 | $("new-font").addEventListener("change", persist); |
| 892 | $("new-size").addEventListener("change", persist); |
| 893 | |
| 894 | ["draw-speed", "travel-speed", "pen-up", "pen-down", "dwell", "origin"].forEach((id) => |
| 895 | { |
| 896 | $(id).addEventListener("change", schedulePreview); |
| 897 | }); |
| 898 | |
| 899 | async function machineCall (path, body, confirmText) |
| 900 | { |
| 901 | if (confirmText && !confirm(confirmText)) return; |
| 902 | try |
| 903 | { |
| 904 | toast(""); |
| 905 | const result = await api(path, body || {}); |
| 906 | if (result && result.notice) toast(result.notice, 5000); |
| 907 | } catch (error) |
| 908 | { |
| 909 | toast(error.message); |
| 910 | $("machine-error").hidden = false; |
| 911 | $("machine-error").textContent = error.message; |
| 912 | } |
| 913 | } |
| 914 | |
| 915 | $("btn-connect").onclick = () => |
| 916 | { |
| 917 | if ($("btn-connect").dataset.on === "1") |
| 918 | { |
| 919 | userDisconnected = true; |
| 920 | machineCall("/api/disconnect"); |
| 921 | } |
| 922 | else |
| 923 | { |
| 924 | userDisconnected = false; |
| 925 | autoTried = ""; |
| 926 | machineCall("/api/connect", { port: $("port").value }); |
| 927 | } |
| 928 | }; |
| 929 | document.querySelectorAll(".jog button").forEach((button) => |
| 930 | { |
| 931 | button.onclick = () => |
| 932 | { |
| 933 | const step = Number($("jog-step").value); |
| 934 | machineCall("/api/jog", { |
| 935 | x: Number(button.dataset.x) * step, |
| 936 | y: Number(button.dataset.y) * step, |
| 937 | feed: Number($("travel-speed").value), |
| 938 | origin: $("origin").value, |
| 939 | }); |
| 940 | }; |
| 941 | }); |
| 942 | $("btn-pen-up").onclick = () => machineCall("/api/pen", { down: false, ...readSettings() }); |
| 943 | $("btn-pen-down").onclick = () => machineCall("/api/pen", { down: true, ...readSettings() }); |
| 944 | $("btn-unlock").onclick = () => machineCall("/api/unlock"); |
| 945 | $("btn-home").onclick = () => machineCall("/api/home", {}, "Homen beweegt naar de eindschakelaars. Doorgaan?"); |
| 946 | $("btn-release").onclick = () => machineCall("/api/release"); |
| 947 | $("btn-plot").onclick = () => machineCall("/api/plot", { objects, settings: readSettings() }); |
| 948 | $("btn-trace").onclick = () => machineCall("/api/trace", { objects, settings: readSettings() }); |
| 949 | $("btn-pause").onclick = async () => |
| 950 | { |
| 951 | const path = $("btn-pause").dataset.paused === "1" ? "/api/resume" : "/api/pause"; |
| 952 | machineCall(path); |
| 953 | }; |
| 954 | $("btn-stop").onclick = () => machineCall("/api/stop"); |
| 955 | let connectOpen = false; |
| 956 | |
| 957 | $("machine-name").onclick = () => |
| 958 | { |
| 959 | connectOpen = true; |
| 960 | syncConnectModal(); |
| 961 | }; |
| 962 | |
| 963 | $("connect-close").onclick = () => |
| 964 | { |
| 965 | if ($("btn-connect").dataset.on !== "1") return; |
| 966 | connectOpen = false; |
| 967 | syncConnectModal(); |
| 968 | }; |
| 969 | |
| 970 | $("connect-modal").addEventListener("cancel", (event) => |
| 971 | { |
| 972 | if ($("btn-connect").dataset.on !== "1") |
| 973 | { |
| 974 | event.preventDefault(); |
| 975 | return; |
| 976 | } |
| 977 | connectOpen = false; |
| 978 | }); |
| 979 | |
| 980 | $("connect-modal").addEventListener("click", (event) => |
| 981 | { |
| 982 | if (event.target !== $("connect-modal") || $("btn-connect").dataset.on !== "1") return; |
| 983 | connectOpen = false; |
| 984 | syncConnectModal(); |
| 985 | }); |
| 986 | |
| 987 | function syncConnectModal () |
| 988 | { |
| 989 | const modal = $("connect-modal"); |
| 990 | const connected = $("btn-connect").dataset.on === "1"; |
| 991 | $("connect-close").hidden = !connected; |
| 992 | const show = !connected || connectOpen; |
| 993 | if (show && !modal.open) modal.showModal(); |
| 994 | if (!show && modal.open) modal.close(); |
| 995 | } |
| 996 | |
| 997 | function paintStatus (status) |
| 998 | { |
| 999 | const lamp = $("lamp"); |
| 1000 | const state = (status.state || "").toLowerCase(); |
| 1001 | lamp.className = "lamp"; |
| 1002 | if (!status.connected) lamp.classList.add("bad"); |
| 1003 | else if (state.startsWith("run") || state.startsWith("hold") || status.jobActive) lamp.classList.add("busy"); |
| 1004 | else if (state.startsWith("alarm")) lamp.classList.add("bad"); |
| 1005 | else lamp.classList.add("on"); |
| 1006 | $("machine-state").textContent = status.connecting |
| 1007 | ? "Verbinden…" |
| 1008 | : (status.connected ? (status.state || "Verbonden") : "Niet verbonden"); |
| 1009 | $("firmware").textContent = status.firmware || ""; |
| 1010 | $("btn-connect").textContent = status.connected ? "Verbreek" : (status.connecting ? "Verbinden…" : "Verbinden"); |
| 1011 | $("btn-connect").dataset.on = status.connected ? "1" : "0"; |
| 1012 | if (!status.connected) connectOpen = false; |
| 1013 | syncConnectModal(); |
| 1014 | if (status.localUrl) |
| 1015 | { |
| 1016 | $("local-url").href = status.localUrl; |
| 1017 | $("local-url").textContent = status.localUrl.replace(/^https?:\/\//, "").replace(/\/$/, ""); |
| 1018 | } |
| 1019 | $("btn-pause").textContent = status.jobPaused ? "Verder" : "Pauze"; |
| 1020 | $("btn-pause").dataset.paused = status.jobPaused ? "1" : "0"; |
| 1021 | const w = status.wpos || [0, 0, 0]; |
| 1022 | $("position").textContent = status.connected |
| 1023 | ? `X ${w[0].toFixed(2)} Y ${w[1].toFixed(2)} Z ${w[2].toFixed(2)}` |
| 1024 | : "X — Y — Z —"; |
| 1025 | if (status.error) |
| 1026 | { |
| 1027 | $("machine-error").hidden = false; |
| 1028 | $("machine-error").textContent = status.error; |
| 1029 | } else if (status.connected) |
| 1030 | { |
| 1031 | $("machine-error").hidden = true; |
| 1032 | } |
| 1033 | if (!drag) |
| 1034 | { |
| 1035 | const ports = status.ports || []; |
| 1036 | const current = $("port").value; |
| 1037 | const remembered = status.lastPort || localStorage.getItem(PORT_KEY) || ""; |
| 1038 | const preferred = (status.connected && status.port) || remembered; |
| 1039 | if ($("port").dataset.list !== ports.join(",")) |
| 1040 | { |
| 1041 | $("port").innerHTML = ports.map((port) => `<option value="${port}">${portLabel(port)}</option>`).join("") |
| 1042 | || "<option value=''>—</option>"; |
| 1043 | if (current && ports.includes(current)) $("port").value = current; |
| 1044 | else if (preferred && ports.includes(preferred)) $("port").value = preferred; |
| 1045 | else if (status.port && ports.includes(status.port)) $("port").value = status.port; |
| 1046 | $("port").dataset.list = ports.join(","); |
| 1047 | } |
| 1048 | if (status.connected && status.port) localStorage.setItem(PORT_KEY, status.port); |
| 1049 | } |
| 1050 | const progress = status.jobActive ? (status.progress || 0) : (status.progress === 1 ? 1 : 0); |
| 1051 | $("bar").style.width = `${Math.round((progress || 0) * 100)}%`; |
| 1052 | if (status.message && status.jobActive) toast(status.message); |
| 1053 | } |
| 1054 | |
| 1055 | function portLabel (port) |
| 1056 | { |
| 1057 | return String(port).split("/").pop() |
| 1058 | .replace(/^usb-/, "") |
| 1059 | .replace(/-if\d+-port\d+$/, "") |
| 1060 | .replaceAll("_", " "); |
| 1061 | } |
| 1062 | |
| 1063 | async function maybeAutoConnect (status) |
| 1064 | { |
| 1065 | if (userDisconnected || status.connected || status.connecting) return; |
| 1066 | const port = status.lastPort || localStorage.getItem(PORT_KEY) || ""; |
| 1067 | if (!port || !(status.ports || []).includes(port) || autoTried === port) return; |
| 1068 | autoTried = port; |
| 1069 | try |
| 1070 | { |
| 1071 | await api("/api/connect", { port }); |
| 1072 | } catch (error) |
| 1073 | { |
| 1074 | toast(error.message); |
| 1075 | $("machine-error").hidden = false; |
| 1076 | $("machine-error").textContent = error.message; |
| 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | async function poll () |
| 1081 | { |
| 1082 | try |
| 1083 | { |
| 1084 | const status = await api("/api/status"); |
| 1085 | paintStatus(status); |
| 1086 | await maybeAutoConnect(status); |
| 1087 | } catch (error) |
| 1088 | { |
| 1089 | $("machine-error").hidden = false; |
| 1090 | $("machine-error").textContent = error.message; |
| 1091 | } |
| 1092 | } |
| 1093 | |
| 1094 | async function boot () |
| 1095 | { |
| 1096 | try |
| 1097 | { |
| 1098 | const saved = JSON.parse(localStorage.getItem(SETTINGS_KEY) || "null") |
| 1099 | || JSON.parse(localStorage.getItem("timdraw-doc") || "null"); |
| 1100 | applySaved(saved); |
| 1101 | } catch { /* lege start */ } |
| 1102 | fillForm(); |
| 1103 | renderList(); |
| 1104 | resize(); |
| 1105 | schedulePreview(); |
| 1106 | try |
| 1107 | { |
| 1108 | const demos = await api("/api/demos"); |
| 1109 | for (const name of demos.demos || []) |
| 1110 | { |
| 1111 | const option = document.createElement("option"); |
| 1112 | option.value = name; |
| 1113 | option.textContent = name; |
| 1114 | $("demos").appendChild(option); |
| 1115 | } |
| 1116 | } catch { /* geen demomap */ } |
| 1117 | poll(); |
| 1118 | setInterval(poll, 700); |
| 1119 | } |
| 1120 | |
| 1121 | window.addEventListener("resize", resize); |
| 1122 | boot(); |