Hazel reports · ^report(46) · the Witch's picture · one page per output · updated 2026-11-08 08:03 MST
The Witch's picture view: JavaScript by hand
One of six pages, one per version of the Witch slide's ^picture view (^report(46), a view): Hazel · hazel-jit's output · ReScript 12.3.1's output · Melange 5.0.0's output · js_of_ocaml 5.9.1's output · JavaScript by hand.
What a JavaScript programmer might write: arrays and for…of, the bounds in four variables instead of a tuple remade per point, attributes as pairs. The fastest of the five here, by a little.
By hand: the view on the picture as JSON (arrays, plain objects)
// by hand: the view as a JS programmer might write it, on the picture as JSON (arrays, plain objects) function handView(pic) { const coords = pts => { let s = ""; for (const p of pts) s += p[0].toFixed(1) + "," + p[1].toFixed(1) + " "; return s; }; const frame = pic.find(s => s.kind === "Path" && s.name === "frame"); const boxed = frame ? [frame] : pic; const first = boxed.length && boxed[0].points.length ? boxed[0].points[0] : [0, 0]; let x0 = first[0], y0 = first[1], x1 = first[0], y1 = first[1]; if (!(boxed.length && boxed[0].points.length)) x0 = y0 = x1 = y1 = 0; for (const s of boxed) for (const [x, y] of s.points) { if (x - 12 < x0) x0 = x - 12; if (y - 12 < y0) y0 = y - 12; if (x + 12 > x1) x1 = x + 12; if (y + 12 > y1) y1 = y + 12; } const w = x1 - x0, h = y1 - y0; const [w2, h2] = w * 440 > h * 320 ? [w, w * 440 / 320] : [h * 320 / 440, h]; const viewBox = (x0 - (w2 - w) / 2).toFixed(1) + " " + (y0 - (h2 - h) / 2).toFixed(1) + " " + w2.toFixed(1) + " " + h2.toFixed(1); const kids = pic.map(s => s.kind === "Polygon" ? { tag: "polygon", attrs: [["points", coords(s.points)], ["fill", s.fill], ["stroke", s.stroke.color], ["stroke-width", s.stroke.width.toFixed(1)], ["stroke-linejoin", "round"]], kids: [] } : { tag: "polyline", attrs: [["points", coords(s.points)], ["fill", "none"], ["stroke", s.stroke.color], ["stroke-width", s.stroke.width.toFixed(1)], ["stroke-linejoin", "round"], ["stroke-linecap", "round"]], kids: [] }); return { tag: "svg", attrs: [["viewBox", viewBox], ["width", "320"], ["height", "440"], ["style", "border: 1px solid #ccc;background: #fafafa"]], kids }; } const handMarkup = h => `<${h.tag}${h.attrs.map(([k, v]) => ` ${k}="${v}"`).join("")}>${h.kids.map(handMarkup).join("")}</${h.tag}>`;
The files, in 046/data/witch (sources, outputs, the benchmark, the timings): Witch.res, Witch.res.js, bench-runs.jsonl, edit-share.json, edit-share.mjs, firefox-answer-probes.json, firefox-answer.json, firefox-before.json, firefox-cuts.json, firefox-edit.json, firefox-noshare.json, firefox-timeline.json, firefox.json, hazeljit-runtime-src.js, hazeljit-witch-pretty.js, hazeljit-witch-src.js, jsoo-excerpt.js, jsoo-runs.jsonl, picture.hz, picture.json, raw-run1.json, raw-run2.json, timings.json, witch-bench.mjs, witch-core.ml, witch-jsoo-tofixed.ml, witch-jsoo.ml, witch-melange.js, witch-melange.ml.