displacement.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /**
  2. * Liquid glass optics.
  3. *
  4. * The glass is modelled as a slab whose edge (the bezel) rises with a convex
  5. * squircle profile. For every pixel we trace a vertical ray through that
  6. * surface with Snell's law (air -> glass, n = 1.5) and record how far
  7. * sideways it lands on the background. The offsets are encoded into the
  8. * red/green channels of an image that drives an SVG feDisplacementMap.
  9. */
  10. export type LensOptions = {
  11. width: number;
  12. height: number;
  13. /** Corner radius in px. Clamped to half of the shorter side. */
  14. radius: number;
  15. /** Width of the curved rim in px. */
  16. bezel: number;
  17. /** Glass thickness in px: more thickness, stronger refraction at the rim. */
  18. thickness: number;
  19. /** >1 magnifies the whole lens, like a loupe. */
  20. magnify?: number;
  21. ior?: number;
  22. };
  23. export type LensMap = {
  24. url: string;
  25. scale: number;
  26. width: number;
  27. height: number;
  28. };
  29. const SAMPLES = 128;
  30. /** Convex squircle: flat top, soft roll-off into the edge (what Apple uses). */
  31. const profile = (t: number) => Math.pow(1 - Math.pow(1 - t, 4), 0.25);
  32. function rimTable(bezel: number, thickness: number, ior: number) {
  33. const table = new Float32Array(SAMPLES);
  34. const eta = 1 / ior;
  35. const d = 1 / (SAMPLES * 4);
  36. for (let i = 0; i < SAMPLES; i++) {
  37. const t = (i + 0.5) / SAMPLES;
  38. const h = thickness * profile(t);
  39. const slope =
  40. (thickness *
  41. (profile(Math.min(1, t + d)) - profile(Math.max(0, t - d)))) /
  42. (2 * d) /
  43. bezel;
  44. const len = Math.hypot(slope, 1);
  45. const nu = -slope / len;
  46. const nz = 1 / len;
  47. const k = 1 - eta * eta * (1 - nz * nz);
  48. if (k < 0) continue;
  49. const a = eta * nz - Math.sqrt(k);
  50. const tu = a * nu;
  51. const tz = -eta + a * nz;
  52. table[i] = h * (tu / -tz);
  53. }
  54. return table;
  55. }
  56. const cache = new Map<string, LensMap>();
  57. export function buildLensMap(o: LensOptions): LensMap {
  58. const width = Math.max(2, Math.round(o.width));
  59. const height = Math.max(2, Math.round(o.height));
  60. const radius = Math.min(o.radius, width / 2, height / 2);
  61. const bezel = Math.max(1, Math.min(o.bezel, Math.min(width, height) / 2));
  62. const magnify = o.magnify ?? 1;
  63. const key = [width, height, radius, bezel, o.thickness, magnify, o.ior].join(
  64. ":",
  65. );
  66. const hit = cache.get(key);
  67. if (hit) return hit;
  68. const rim = rimTable(bezel, o.thickness, o.ior ?? 1.5);
  69. const zoom = magnify > 1 ? 1 - 1 / magnify : 0;
  70. const dx = new Float32Array(width * height);
  71. const dy = new Float32Array(width * height);
  72. const cx = width / 2;
  73. const cy = height / 2;
  74. const hx = cx - radius;
  75. const hy = cy - radius;
  76. let max = 0.0001;
  77. for (let y = 0; y < height; y++) {
  78. const py = y + 0.5 - cy;
  79. const qy = Math.abs(py) - hy;
  80. for (let x = 0; x < width; x++) {
  81. const px = x + 0.5 - cx;
  82. const qx = Math.abs(px) - hx;
  83. let sd: number;
  84. let nx = 0;
  85. let ny = 0;
  86. if (qx > 0 && qy > 0) {
  87. const l = Math.hypot(qx, qy);
  88. sd = l - radius;
  89. nx = (qx / l) * Math.sign(px);
  90. ny = (qy / l) * Math.sign(py);
  91. } else if (qx > qy) {
  92. sd = qx - radius;
  93. nx = Math.sign(px);
  94. } else {
  95. sd = qy - radius;
  96. ny = Math.sign(py);
  97. }
  98. const inside = -sd;
  99. if (inside < 0) continue;
  100. let ox = 0;
  101. let oy = 0;
  102. if (inside < bezel) {
  103. const offset =
  104. rim[Math.min(SAMPLES - 1, Math.floor((inside / bezel) * SAMPLES))];
  105. ox -= nx * offset;
  106. oy -= ny * offset;
  107. }
  108. if (zoom) {
  109. ox -= px * zoom;
  110. oy -= py * zoom;
  111. }
  112. const i = y * width + x;
  113. dx[i] = ox;
  114. dy[i] = oy;
  115. max = Math.max(max, Math.abs(ox), Math.abs(oy));
  116. }
  117. }
  118. const canvas = document.createElement("canvas");
  119. canvas.width = width;
  120. canvas.height = height;
  121. const ctx = canvas.getContext("2d");
  122. if (!ctx) return { url: "", scale: 0, width, height };
  123. const img = ctx.createImageData(width, height);
  124. for (let i = 0; i < width * height; i++) {
  125. const p = i * 4;
  126. img.data[p] = 128 + (dx[i] / max) * 127;
  127. img.data[p + 1] = 128 + (dy[i] / max) * 127;
  128. img.data[p + 2] = 128;
  129. img.data[p + 3] = 255;
  130. }
  131. ctx.putImageData(img, 0, 0);
  132. // feDisplacementMap offsets by scale * (channel - 0.5), so double the max.
  133. const map = { url: canvas.toDataURL(), scale: max * 2, width, height };
  134. cache.set(key, map);
  135. return map;
  136. }
  137. let refraction: boolean | undefined;
  138. /**
  139. * Only Chromium renders SVG filters inside backdrop-filter. Firefox parses
  140. * them and then draws nothing, so feature detection can't be trusted here.
  141. */
  142. export function supportsBackdropRefraction() {
  143. if (refraction !== undefined) return refraction;
  144. const nav = navigator as Navigator & {
  145. userAgentData?: { brands?: { brand: string }[] };
  146. };
  147. refraction = !!nav.userAgentData?.brands?.some((b) => b.brand === "Chromium");
  148. return refraction;
  149. }