hello-orb-flow.html 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>pretext hello — text flowing around an orb</title>
  6. <style>
  7. html,body { margin:0; padding:0; height:100%; background:#0c0d10; color:#e8e6df; overflow:hidden; }
  8. body { font-family: "Iowan Old Style", Georgia, serif; }
  9. canvas { display:block; width:100vw; height:100vh; }
  10. </style>
  11. </head>
  12. <body>
  13. <canvas id="c"></canvas>
  14. <script type="module">
  15. // Minimal pretext starter: long paragraph flows around a moving orb.
  16. // Uses layoutNextLineRange + variable-width streaming — the "killer app"
  17. // pattern that only pretext can do cheaply in the browser.
  18. import {
  19. prepareWithSegments,
  20. layoutNextLineRange,
  21. materializeLineRange,
  22. } from "https://esm.sh/@chenglou/pretext@0.0.6";
  23. const TEXT = `Pretext measures text without touching the DOM. It returns numbers — widths, line breaks, cursors — and those numbers, arranged with a little imagination, become layouts the browser could never draw on its own. Here, a paragraph flows around a moving orb. Each line is asked for its own width, live. No reflows. No cheats. Just measurement. `.repeat(18);
  24. const FONT = '17px/1.4 "Iowan Old Style", Georgia, serif';
  25. const LINE_H = 24;
  26. const c = document.getElementById("c");
  27. const ctx = c.getContext("2d");
  28. let W, H, DPR;
  29. function resize() {
  30. DPR = Math.min(devicePixelRatio || 1, 2);
  31. W = innerWidth; H = innerHeight;
  32. c.width = W*DPR; c.height = H*DPR;
  33. c.style.width = W+"px"; c.style.height = H+"px";
  34. ctx.setTransform(DPR,0,0,DPR,0,0);
  35. }
  36. addEventListener("resize", resize); resize();
  37. const prepared = prepareWithSegments(TEXT, FONT);
  38. // Orb follows mouse (or bobs idly)
  39. const orb = { x: innerWidth*0.45, y: innerHeight*0.5, r: 140 };
  40. addEventListener("mousemove", e => { orb.x = e.clientX; orb.y = e.clientY; });
  41. function frame(t) {
  42. ctx.fillStyle = "#0c0d10"; ctx.fillRect(0,0,W,H);
  43. // glowing orb
  44. const g = ctx.createRadialGradient(orb.x, orb.y, 0, orb.x, orb.y, orb.r);
  45. g.addColorStop(0, "rgba(255,200,120,0.35)");
  46. g.addColorStop(0.6, "rgba(255,140,80,0.10)");
  47. g.addColorStop(1, "rgba(0,0,0,0)");
  48. ctx.fillStyle = g; ctx.fillRect(0,0,W,H);
  49. // flow text as a column, routing around the orb row-by-row
  50. const COL_X = 60, COL_W = W - 120;
  51. let cursor = { segmentIndex: 0, graphemeIndex: 0 };
  52. let y = 72;
  53. ctx.fillStyle = "#e8e6df";
  54. ctx.font = FONT;
  55. ctx.textBaseline = "alphabetic";
  56. while (y < H - 40) {
  57. // does this row intersect the orb band?
  58. const dy = y - orb.y;
  59. const bandY = Math.abs(dy) < orb.r;
  60. // lane = (left, width) skipping over the orb horizontally
  61. let x = COL_X, lineMaxW = COL_W;
  62. if (bandY) {
  63. const half = Math.sqrt(orb.r*orb.r - dy*dy);
  64. const orbLeft = orb.x - half, orbRight = orb.x + half;
  65. // choose the wider side, simple heuristic
  66. const leftWidth = Math.max(0, orbLeft - COL_X);
  67. const rightWidth = Math.max(0, COL_X + COL_W - orbRight);
  68. if (leftWidth >= rightWidth) { x = COL_X; lineMaxW = leftWidth - 12; }
  69. else { x = orbRight + 12; lineMaxW = rightWidth - 12; }
  70. if (lineMaxW < 40) { y += LINE_H; continue; }
  71. }
  72. const range = layoutNextLineRange(prepared, cursor, lineMaxW);
  73. if (!range) break;
  74. const line = materializeLineRange(prepared, range);
  75. ctx.fillText(line.text, x, y);
  76. cursor = range.end;
  77. y += LINE_H;
  78. }
  79. requestAnimationFrame(frame);
  80. }
  81. requestAnimationFrame(frame);
  82. </script>
  83. </body>
  84. </html>