thanos-image
Version:
Thanos snap effect component built with Lit
458 lines (453 loc) • 16.4 kB
JavaScript
import { css as U, LitElement as C, html as M } from "lit";
/**
* @license
* Copyright 2017 Google LLC
* SPDX-License-Identifier: BSD-3-Clause
*/
const O = (r) => (t, e) => {
e !== void 0 ? e.addInitializer(() => {
customElements.define(r, t);
}) : customElements.define(r, t);
};
/**
* @license
* Copyright 2019 Google LLC
* SPDX-License-Identifier: BSD-3-Clause
*/
const f = globalThis, y = f.ShadowRoot && (f.ShadyCSS === void 0 || f.ShadyCSS.nativeShadow) && "adoptedStyleSheets" in Document.prototype && "replace" in CSSStyleSheet.prototype, P = Symbol(), b = /* @__PURE__ */ new WeakMap();
let N = class {
constructor(t, e, s) {
if (this._$cssResult$ = !0, s !== P) throw Error("CSSResult is not constructable. Use `unsafeCSS` or `css` instead.");
this.cssText = t, this.t = e;
}
get styleSheet() {
let t = this.o;
const e = this.t;
if (y && t === void 0) {
const s = e !== void 0 && e.length === 1;
s && (t = b.get(e)), t === void 0 && ((this.o = t = new CSSStyleSheet()).replaceSync(this.cssText), s && b.set(e, t));
}
return t;
}
toString() {
return this.cssText;
}
};
const R = (r) => new N(typeof r == "string" ? r : r + "", void 0, P), T = (r, t) => {
if (y) r.adoptedStyleSheets = t.map((e) => e instanceof CSSStyleSheet ? e : e.styleSheet);
else for (const e of t) {
const s = document.createElement("style"), i = f.litNonce;
i !== void 0 && s.setAttribute("nonce", i), s.textContent = e.cssText, r.appendChild(s);
}
}, _ = y ? (r) => r : (r) => r instanceof CSSStyleSheet ? ((t) => {
let e = "";
for (const s of t.cssRules) e += s.cssText;
return R(e);
})(r) : r;
/**
* @license
* Copyright 2017 Google LLC
* SPDX-License-Identifier: BSD-3-Clause
*/
const { is: q, defineProperty: j, getOwnPropertyDescriptor: D, getOwnPropertyNames: x, getOwnPropertySymbols: z, getPrototypeOf: k } = Object, l = globalThis, v = l.trustedTypes, L = v ? v.emptyScript : "", g = l.reactiveElementPolyfillSupport, d = (r, t) => r, m = { toAttribute(r, t) {
switch (t) {
case Boolean:
r = r ? L : null;
break;
case Object:
case Array:
r = r == null ? r : JSON.stringify(r);
}
return r;
}, fromAttribute(r, t) {
let e = r;
switch (t) {
case Boolean:
e = r !== null;
break;
case Number:
e = r === null ? null : Number(r);
break;
case Object:
case Array:
try {
e = JSON.parse(r);
} catch {
e = null;
}
}
return e;
} }, E = (r, t) => !q(r, t), w = { attribute: !0, type: String, converter: m, reflect: !1, useDefault: !1, hasChanged: E };
Symbol.metadata ?? (Symbol.metadata = Symbol("metadata")), l.litPropertyMetadata ?? (l.litPropertyMetadata = /* @__PURE__ */ new WeakMap());
class p extends HTMLElement {
static addInitializer(t) {
this._$Ei(), (this.l ?? (this.l = [])).push(t);
}
static get observedAttributes() {
return this.finalize(), this._$Eh && [...this._$Eh.keys()];
}
static createProperty(t, e = w) {
if (e.state && (e.attribute = !1), this._$Ei(), this.prototype.hasOwnProperty(t) && ((e = Object.create(e)).wrapped = !0), this.elementProperties.set(t, e), !e.noAccessor) {
const s = Symbol(), i = this.getPropertyDescriptor(t, s, e);
i !== void 0 && j(this.prototype, t, i);
}
}
static getPropertyDescriptor(t, e, s) {
const { get: i, set: o } = D(this.prototype, t) ?? { get() {
return this[e];
}, set(n) {
this[e] = n;
} };
return { get: i, set(n) {
const a = i == null ? void 0 : i.call(this);
o == null || o.call(this, n), this.requestUpdate(t, a, s);
}, configurable: !0, enumerable: !0 };
}
static getPropertyOptions(t) {
return this.elementProperties.get(t) ?? w;
}
static _$Ei() {
if (this.hasOwnProperty(d("elementProperties"))) return;
const t = k(this);
t.finalize(), t.l !== void 0 && (this.l = [...t.l]), this.elementProperties = new Map(t.elementProperties);
}
static finalize() {
if (this.hasOwnProperty(d("finalized"))) return;
if (this.finalized = !0, this._$Ei(), this.hasOwnProperty(d("properties"))) {
const e = this.properties, s = [...x(e), ...z(e)];
for (const i of s) this.createProperty(i, e[i]);
}
const t = this[Symbol.metadata];
if (t !== null) {
const e = litPropertyMetadata.get(t);
if (e !== void 0) for (const [s, i] of e) this.elementProperties.set(s, i);
}
this._$Eh = /* @__PURE__ */ new Map();
for (const [e, s] of this.elementProperties) {
const i = this._$Eu(e, s);
i !== void 0 && this._$Eh.set(i, e);
}
this.elementStyles = this.finalizeStyles(this.styles);
}
static finalizeStyles(t) {
const e = [];
if (Array.isArray(t)) {
const s = new Set(t.flat(1 / 0).reverse());
for (const i of s) e.unshift(_(i));
} else t !== void 0 && e.push(_(t));
return e;
}
static _$Eu(t, e) {
const s = e.attribute;
return s === !1 ? void 0 : typeof s == "string" ? s : typeof t == "string" ? t.toLowerCase() : void 0;
}
constructor() {
super(), this._$Ep = void 0, this.isUpdatePending = !1, this.hasUpdated = !1, this._$Em = null, this._$Ev();
}
_$Ev() {
var t;
this._$ES = new Promise((e) => this.enableUpdating = e), this._$AL = /* @__PURE__ */ new Map(), this._$E_(), this.requestUpdate(), (t = this.constructor.l) == null || t.forEach((e) => e(this));
}
addController(t) {
var e;
(this._$EO ?? (this._$EO = /* @__PURE__ */ new Set())).add(t), this.renderRoot !== void 0 && this.isConnected && ((e = t.hostConnected) == null || e.call(t));
}
removeController(t) {
var e;
(e = this._$EO) == null || e.delete(t);
}
_$E_() {
const t = /* @__PURE__ */ new Map(), e = this.constructor.elementProperties;
for (const s of e.keys()) this.hasOwnProperty(s) && (t.set(s, this[s]), delete this[s]);
t.size > 0 && (this._$Ep = t);
}
createRenderRoot() {
const t = this.shadowRoot ?? this.attachShadow(this.constructor.shadowRootOptions);
return T(t, this.constructor.elementStyles), t;
}
connectedCallback() {
var t;
this.renderRoot ?? (this.renderRoot = this.createRenderRoot()), this.enableUpdating(!0), (t = this._$EO) == null || t.forEach((e) => {
var s;
return (s = e.hostConnected) == null ? void 0 : s.call(e);
});
}
enableUpdating(t) {
}
disconnectedCallback() {
var t;
(t = this._$EO) == null || t.forEach((e) => {
var s;
return (s = e.hostDisconnected) == null ? void 0 : s.call(e);
});
}
attributeChangedCallback(t, e, s) {
this._$AK(t, s);
}
_$ET(t, e) {
var o;
const s = this.constructor.elementProperties.get(t), i = this.constructor._$Eu(t, s);
if (i !== void 0 && s.reflect === !0) {
const n = (((o = s.converter) == null ? void 0 : o.toAttribute) !== void 0 ? s.converter : m).toAttribute(e, s.type);
this._$Em = t, n == null ? this.removeAttribute(i) : this.setAttribute(i, n), this._$Em = null;
}
}
_$AK(t, e) {
var o, n;
const s = this.constructor, i = s._$Eh.get(t);
if (i !== void 0 && this._$Em !== i) {
const a = s.getPropertyOptions(i), h = typeof a.converter == "function" ? { fromAttribute: a.converter } : ((o = a.converter) == null ? void 0 : o.fromAttribute) !== void 0 ? a.converter : m;
this._$Em = i;
const S = h.fromAttribute(e, a.type);
this[i] = S ?? ((n = this._$Ej) == null ? void 0 : n.get(i)) ?? S, this._$Em = null;
}
}
requestUpdate(t, e, s) {
var i;
if (t !== void 0) {
const o = this.constructor, n = this[t];
if (s ?? (s = o.getPropertyOptions(t)), !((s.hasChanged ?? E)(n, e) || s.useDefault && s.reflect && n === ((i = this._$Ej) == null ? void 0 : i.get(t)) && !this.hasAttribute(o._$Eu(t, s)))) return;
this.C(t, e, s);
}
this.isUpdatePending === !1 && (this._$ES = this._$EP());
}
C(t, e, { useDefault: s, reflect: i, wrapped: o }, n) {
s && !(this._$Ej ?? (this._$Ej = /* @__PURE__ */ new Map())).has(t) && (this._$Ej.set(t, n ?? e ?? this[t]), o !== !0 || n !== void 0) || (this._$AL.has(t) || (this.hasUpdated || s || (e = void 0), this._$AL.set(t, e)), i === !0 && this._$Em !== t && (this._$Eq ?? (this._$Eq = /* @__PURE__ */ new Set())).add(t));
}
async _$EP() {
this.isUpdatePending = !0;
try {
await this._$ES;
} catch (e) {
Promise.reject(e);
}
const t = this.scheduleUpdate();
return t != null && await t, !this.isUpdatePending;
}
scheduleUpdate() {
return this.performUpdate();
}
performUpdate() {
var s;
if (!this.isUpdatePending) return;
if (!this.hasUpdated) {
if (this.renderRoot ?? (this.renderRoot = this.createRenderRoot()), this._$Ep) {
for (const [o, n] of this._$Ep) this[o] = n;
this._$Ep = void 0;
}
const i = this.constructor.elementProperties;
if (i.size > 0) for (const [o, n] of i) {
const { wrapped: a } = n, h = this[o];
a !== !0 || this._$AL.has(o) || h === void 0 || this.C(o, void 0, n, h);
}
}
let t = !1;
const e = this._$AL;
try {
t = this.shouldUpdate(e), t ? (this.willUpdate(e), (s = this._$EO) == null || s.forEach((i) => {
var o;
return (o = i.hostUpdate) == null ? void 0 : o.call(i);
}), this.update(e)) : this._$EM();
} catch (i) {
throw t = !1, this._$EM(), i;
}
t && this._$AE(e);
}
willUpdate(t) {
}
_$AE(t) {
var e;
(e = this._$EO) == null || e.forEach((s) => {
var i;
return (i = s.hostUpdated) == null ? void 0 : i.call(s);
}), this.hasUpdated || (this.hasUpdated = !0, this.firstUpdated(t)), this.updated(t);
}
_$EM() {
this._$AL = /* @__PURE__ */ new Map(), this.isUpdatePending = !1;
}
get updateComplete() {
return this.getUpdateComplete();
}
getUpdateComplete() {
return this._$ES;
}
shouldUpdate(t) {
return !0;
}
update(t) {
this._$Eq && (this._$Eq = this._$Eq.forEach((e) => this._$ET(e, this[e]))), this._$EM();
}
updated(t) {
}
firstUpdated(t) {
}
}
p.elementStyles = [], p.shadowRootOptions = { mode: "open" }, p[d("elementProperties")] = /* @__PURE__ */ new Map(), p[d("finalized")] = /* @__PURE__ */ new Map(), g == null || g({ ReactiveElement: p }), (l.reactiveElementVersions ?? (l.reactiveElementVersions = [])).push("2.1.1");
/**
* @license
* Copyright 2017 Google LLC
* SPDX-License-Identifier: BSD-3-Clause
*/
const F = { attribute: !0, type: String, converter: m, reflect: !1, hasChanged: E }, I = (r = F, t, e) => {
const { kind: s, metadata: i } = e;
let o = globalThis.litPropertyMetadata.get(i);
if (o === void 0 && globalThis.litPropertyMetadata.set(i, o = /* @__PURE__ */ new Map()), s === "setter" && ((r = Object.create(r)).wrapped = !0), o.set(e.name, r), s === "accessor") {
const { name: n } = e;
return { set(a) {
const h = t.get.call(this);
t.set.call(this, a), this.requestUpdate(n, h, r);
}, init(a) {
return a !== void 0 && this.C(n, void 0, r, a), a;
} };
}
if (s === "setter") {
const { name: n } = e;
return function(a) {
const h = this[n];
t.call(this, a), this.requestUpdate(n, h, r);
};
}
throw Error("Unsupported decorator location: " + s);
};
function $(r) {
return (t, e) => typeof e == "object" ? I(r, t, e) : ((s, i, o) => {
const n = i.hasOwnProperty(o);
return i.constructor.createProperty(o, s), n ? Object.getOwnPropertyDescriptor(i, o) : void 0;
})(r, t, e);
}
/**
* @license
* Copyright 2017 Google LLC
* SPDX-License-Identifier: BSD-3-Clause
*/
function A(r) {
return $({ ...r, state: !0, attribute: !1 });
}
var G = Object.defineProperty, B = Object.getOwnPropertyDescriptor, u = (r, t, e, s) => {
for (var i = s > 1 ? void 0 : s ? B(t, e) : t, o = r.length - 1, n; o >= 0; o--)
(n = r[o]) && (i = (s ? n(t, e, i) : n(i)) || i);
return s && i && G(t, e, i), i;
};
let c = class extends C {
constructor() {
super(...arguments), this.src = "", this.class = "", this.filterUrl = "none", this.isAnimating = !1, this.uniqueId = `dissolve-filter-${Math.random().toString(36).slice(2, 9)}`, this.bigNoiseEl = null, this.displacementMapEl = null, this.config = { d: 1e3, max: 2e3, ease: (r) => 1 - (1 - r) ** 3 }, this.imageEl = null;
}
get isSafari() {
return typeof window < "u" ? /Safari/.test(navigator == null ? void 0 : navigator.userAgent) && !/Chrome/.test(navigator == null ? void 0 : navigator.userAgent) : !1;
}
connectedCallback() {
super.connectedCallback(), !this.isSafari && (this.filterUrl = `url(#${this.uniqueId})`);
}
animateDisappear(r) {
const t = performance.now(), { d: e, max: s, ease: i } = this.config;
this.bigNoiseEl && this.bigNoiseEl.setAttribute("seed", Math.floor(Math.random() * 1e3).toString());
const o = (n) => {
const a = Math.min((n - t) / e, 1), h = i(a);
this.displacementMapEl && this.displacementMapEl.setAttribute("scale", String(h * s)), this.imageEl && (this.imageEl.style.transform = `scale(${1 + 0.1 * h})`, this.imageEl.style.opacity = a < 0.5 ? "1" : String(1 - (a - 0.5) / 0.5)), a < 1 ? requestAnimationFrame(o) : r();
};
requestAnimationFrame(o);
}
playImageDisappearAnimation() {
return new Promise((r) => {
if (this.isAnimating || !this.imageEl) {
r();
return;
}
if (this.isSafari) {
this.dispatchEvent(new CustomEvent("disappeared")), r();
return;
}
this.isAnimating = !0, this.animateDisappear(() => {
this.imageEl && this.displacementMapEl && (this.imageEl.style.transform = "scale(1)", this.imageEl.style.opacity = "1", this.displacementMapEl.setAttribute("scale", "0")), this.dispatchEvent(new CustomEvent("disappeared")), this.isAnimating = !1, r();
});
});
}
firstUpdated() {
var r, t, e;
this.imageEl = ((r = this.shadowRoot) == null ? void 0 : r.querySelector("img")) || null, this.bigNoiseEl = ((t = this.shadowRoot) == null ? void 0 : t.querySelector("feTurbulence")) || null, this.displacementMapEl = ((e = this.shadowRoot) == null ? void 0 : e.querySelector("feDisplacementMap")) || null;
}
render() {
return this.src ? M`
<svg>
<defs>
<filter
id=${this.uniqueId}
x="-200%"
y="-200%"
width="500%"
height="500%"
color-interpolation-filters="sRGB"
overflow="visible"
>
<feTurbulence
type="fractalNoise"
baseFrequency="0.004"
numOctaves="1"
result="bigNoise"
></feTurbulence>
<feComponentTransfer in="bigNoise" result="bigNoiseAdjusted">
<feFuncR type="linear" slope="5" intercept="-2"></feFuncR>
<feFuncG type="linear" slope="5" intercept="-2"></feFuncG>
</feComponentTransfer>
<feTurbulence
type="fractalNoise"
baseFrequency="1"
numOctaves="1"
result="fineNoise"
></feTurbulence>
<feMerge result="mergedNoise">
<feMergeNode in="bigNoiseAdjusted"></feMergeNode>
<feMergeNode in="fineNoise"></feMergeNode>
</feMerge>
<feDisplacementMap
in="SourceGraphic"
in2="mergedNoise"
scale="0"
xChannelSelector="R"
yChannelSelector="G"
></feDisplacementMap>
</filter>
</defs>
</svg>
<img
src=${this.src}
class=${this.class}
style=${this.isSafari ? "" : `filter: ${this.filterUrl}; -webkit-filter: ${this.filterUrl};`}
alt="Thanos Image"
/>
` : null;
}
};
c.styles = U`
:host {
display: block;
overflow: visible;
}
img {
width: 100%;
height: 100%;
object-fit: contain;
will-change: transform, opacity;
}
svg {
display: none;
}
`;
u([
$({ type: String })
], c.prototype, "src", 2);
u([
$({ type: String })
], c.prototype, "class", 2);
u([
A()
], c.prototype, "filterUrl", 2);
u([
A()
], c.prototype, "isAnimating", 2);
c = u([
O("thanos-image")
], c);
export {
c as ThanosImage
};