displacement.ts 5.0 KB

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