liquidglass-ts 0.1.4 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -14,8 +14,8 @@
14
14
  A drop-in, TypeScript-only take on glassmorphism, aiming for a 1:1 recreation of Apple’s Liquid Glass. Live lenses bend text and pictures as you drag. Controls expand and spring back when held. Zero runtime dependencies. No WebGL.
15
15
 
16
16
  <p align="center"><a href="https://bradleyseanf.github.io/liquidglass-ts/"><picture>
17
- <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/bradleyseanf/liquidglass-ts/main/assets/demo-pill-dark.png">
18
- <img width="164" height="62" alt="Try Demo" src="https://raw.githubusercontent.com/bradleyseanf/liquidglass-ts/main/assets/demo-pill-light.png">
17
+ <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/bradleyseanf/liquidglass-ts/main/assets/clear-bubble-dark.png">
18
+ <img width="164" height="62" alt="Try Demo" src="https://raw.githubusercontent.com/bradleyseanf/liquidglass-ts/main/assets/clear-bubble-light.png">
19
19
  </picture></a></p>
20
20
 
21
21
  Install with `npm install liquidglass-ts`.
@@ -43,4 +43,6 @@ document.body.append(wifi.element);
43
43
  | Slider mid-drag | <img width="205" alt="Expanded glass slider thumb bending the blue track" src="https://raw.githubusercontent.com/bradleyseanf/liquidglass-ts/main/assets/slider-held.png"> |
44
44
  | Switch held on | <img width="106" alt="Expanded switch lens revealing the green rail" src="https://raw.githubusercontent.com/bradleyseanf/liquidglass-ts/main/assets/switch-held.png"> |
45
45
 
46
+ For smoked glass, call `setMaterial({ tint: "#000000", tintOpacity: 0.35 })` on a lens or control. Choose another CSS color for a colored tint, or set opacity to zero for clear glass. Canvas lenses take the same options in `draw`.
47
+
46
48
  For HTML lenses, position the overlay above a separate backdrop element and pass both to `attachGlass`. For canvas lenses, redraw after the source or lens moves.
@@ -10,6 +10,10 @@ export interface Material {
10
10
  refraction: number;
11
11
  depth: number;
12
12
  highlight: number;
13
+ /** A CSS color, such as "#000000" for smoked glass. */
14
+ tint?: string;
15
+ /** Tint strength from 0 to 1. A supplied color defaults to 0.25. */
16
+ tintOpacity?: number;
13
17
  }
14
18
  export declare const defaultMaterial: Material;
15
19
  /** Draw one lens above a live, origin-clean source. Call draw after content or position changes. */
package/dist/src/glass.js CHANGED
@@ -109,6 +109,13 @@ export class LiquidGlass {
109
109
  }
110
110
  }
111
111
  this.ctx.putImageData(output, 0, 0);
112
+ if (options.tint && options.tintOpacity !== 0) {
113
+ this.ctx.globalAlpha = clamp(options.tintOpacity ?? 0.25, 1);
114
+ this.ctx.fillStyle = "transparent";
115
+ this.ctx.fillStyle = options.tint;
116
+ this.ctx.fillRect(0, 0, w, h);
117
+ this.ctx.globalAlpha = 1;
118
+ }
112
119
  Object.assign(this.canvas.style, {
113
120
  width: lens.width + "px",
114
121
  height: lens.height + "px",
@@ -146,7 +153,12 @@ export function attachGlass(element, initial = {}, radius = Infinity, source, pr
146
153
  filter.append(image, warp);
147
154
  svg.append(filter);
148
155
  document.body.append(svg);
149
- const props = ["backdrop-filter", "border-radius", "box-shadow"];
156
+ const props = [
157
+ "backdrop-filter",
158
+ "border-radius",
159
+ "box-shadow",
160
+ "--lg-glass-tint",
161
+ ];
150
162
  const previous = props.map((p) => element.style.getPropertyValue(p));
151
163
  let stopMirror, activeSource;
152
164
  function setBackdrop(next) {
@@ -160,8 +172,8 @@ export function attachGlass(element, initial = {}, radius = Infinity, source, pr
160
172
  !CSS.supports("backdrop-filter", "url(#" + id + ")")))
161
173
  stopMirror = mirrorBackdrop(element, next, id);
162
174
  element.style.backdropFilter = stopMirror ? "none" : "url(#" + id + ")";
175
+ update();
163
176
  }
164
- setBackdrop(source);
165
177
  let options = { ...initial };
166
178
  let lastKey = "";
167
179
  function update() {
@@ -170,6 +182,13 @@ export function attachGlass(element, initial = {}, radius = Infinity, source, pr
170
182
  return;
171
183
  const shape = geometry({ x: 0, y: 0, width: w, height: h, radius }, options);
172
184
  const key = [w, h, shape.radius, shape.bend, shape.depth, shape.light].join(",");
185
+ const opacity = clamp(options.tintOpacity ?? 0.25, 1);
186
+ const tint = options.tint && opacity && CSS.supports("color", options.tint)
187
+ ? `color-mix(in srgb, ${options.tint} ${opacity * 100}%, transparent)`
188
+ : "transparent";
189
+ element.style.setProperty("--lg-glass-tint", tint);
190
+ const fill = stopMirror ? "" : ", inset 0 0 0 100vmax var(--lg-glass-tint)";
191
+ element.style.boxShadow = `inset .5px 1px 1px rgba(255,255,255,${shape.light * 0.62}), inset -.6px -.8px .8px rgba(16,30,42,${shape.light * 0.3}), var(--lg-shadow,0 2px 5px #10182019)${fill}`;
173
192
  if (lastKey === key)
174
193
  return;
175
194
  lastKey = key;
@@ -195,9 +214,9 @@ export function attachGlass(element, initial = {}, radius = Infinity, source, pr
195
214
  warp.setAttribute("scale", String(2 * peak));
196
215
  element.style.backdropFilter = stopMirror ? "none" : "url(#" + id + ")";
197
216
  element.style.borderRadius = shape.radius + "px";
198
- element.style.boxShadow = `inset .5px 1px 1px rgba(255,255,255,${shape.light * 0.62}), inset -.6px -.8px .8px rgba(16,30,42,${shape.light * 0.3}), var(--lg-shadow,0 2px 5px #10182019)`;
199
217
  }
200
218
  const observer = new ResizeObserver(update);
219
+ setBackdrop(source);
201
220
  observer.observe(element);
202
221
  update();
203
222
  return {
@@ -12,7 +12,8 @@ export function mirrorBackdrop(host, source, filterId) {
12
12
  "position:absolute;inset:0;overflow:hidden;border-radius:inherit;pointer-events:none;z-index:-1";
13
13
  layer.style.cssText = "position:absolute;inset:0;pointer-events:none;";
14
14
  layer.style.filter = "url(#" + filterId + ")";
15
- veil.style.cssText = "position:absolute;inset:0;border-radius:inherit";
15
+ veil.style.cssText =
16
+ "position:absolute;inset:0;border-radius:inherit;box-shadow:inset 0 0 0 100vmax var(--lg-glass-tint,transparent)";
16
17
  clip.append(layer, veil);
17
18
  host.prepend(clip);
18
19
  let copy, dirty = true, frame = 0, visible = false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "liquidglass-ts",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {