@andresclua/perlindom 0.0.6 → 0.1.1
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/CHANGELOG.md +25 -0
- package/LICENSE +21 -0
- package/README.md +426 -0
- package/dist/perlindom.es.js +212 -0
- package/dist/perlindom.umd.js +1 -0
- package/package.json +14 -6
- package/dist/PerlinDOM.es.js +0 -138
- package/dist/PerlinDOM.umd.js +0 -1
- package/docs/examples.html +0 -391
- package/public/vite.svg +0 -1
- package/readme.md +0 -353
- package/src/index.html +0 -819
- package/src/js/PerlinDom.js +0 -318
- package/src/js/animated-button.js +0 -40
- package/src/js/basic-examples.js +0 -43
- package/src/js/index.js +0 -13
- package/src/js/interactive-controls.js +0 -43
- package/src/js/pause-play.js +0 -33
- package/src/js/triangle-animation.js +0 -48
- package/src/scss/style.scss +0 -20
- package/vite.config.js +0 -36
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
var C = (i) => {
|
|
2
|
+
throw TypeError(i);
|
|
3
|
+
};
|
|
4
|
+
var I = (i, t, e) => t.has(i) || C("Cannot " + e);
|
|
5
|
+
var M = (i, t, e) => (I(i, t, "read from private field"), e ? e.call(i) : t.get(i)), B = (i, t, e) => t.has(i) ? C("Cannot add the same private member more than once") : t instanceof WeakSet ? t.add(i) : t.set(i, e), D = (i, t, e, s) => (I(i, t, "write to private field"), s ? s.call(i, e) : t.set(i, e), e), h = (i, t, e) => (I(i, t, "access private method"), e);
|
|
6
|
+
const w = /* @__PURE__ */ new Set();
|
|
7
|
+
let v = null;
|
|
8
|
+
function G() {
|
|
9
|
+
v = null;
|
|
10
|
+
for (const i of [...w])
|
|
11
|
+
w.has(i) && (!i.onScreen && !i._dirty || (i._dirty = !1, i.step() === !1 && w.delete(i)));
|
|
12
|
+
w.size > 0 && v === null && (v = requestAnimationFrame(G));
|
|
13
|
+
}
|
|
14
|
+
function $(i) {
|
|
15
|
+
w.add(i), v === null && (v = requestAnimationFrame(G));
|
|
16
|
+
}
|
|
17
|
+
function Z(i) {
|
|
18
|
+
w.delete(i), w.size === 0 && v !== null && (cancelAnimationFrame(v), v = null);
|
|
19
|
+
}
|
|
20
|
+
const z = /* @__PURE__ */ new Map(), Y = /* @__PURE__ */ new WeakMap();
|
|
21
|
+
let O = null;
|
|
22
|
+
function V(i) {
|
|
23
|
+
if (typeof IntersectionObserver > "u" || !i.el || !i.pauseOffscreen || Y.has(i)) return;
|
|
24
|
+
O ?? (O = new IntersectionObserver((s) => {
|
|
25
|
+
for (const r of s) {
|
|
26
|
+
const a = z.get(r.target);
|
|
27
|
+
if (a) {
|
|
28
|
+
a.visible = r.isIntersecting;
|
|
29
|
+
for (const n of a.insts) n.onScreen = r.isIntersecting;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}));
|
|
33
|
+
const t = i.el.parentElement || i.el;
|
|
34
|
+
let e = z.get(t);
|
|
35
|
+
e || (e = { visible: !0, insts: /* @__PURE__ */ new Set() }, z.set(t, e), O.observe(t)), e.insts.add(i), Y.set(i, t), i.onScreen = e.visible;
|
|
36
|
+
}
|
|
37
|
+
function _(i) {
|
|
38
|
+
const t = Y.get(i);
|
|
39
|
+
if (!t) return;
|
|
40
|
+
Y.delete(i);
|
|
41
|
+
const e = z.get(t);
|
|
42
|
+
e && (e.insts.delete(i), e.insts.size === 0 && (z.delete(t), O.unobserve(t)));
|
|
43
|
+
}
|
|
44
|
+
var F, l, J, K, y, L, c, Q, U, S, X, E;
|
|
45
|
+
const f = class f {
|
|
46
|
+
constructor({
|
|
47
|
+
element: t,
|
|
48
|
+
x: e = null,
|
|
49
|
+
y: s = null,
|
|
50
|
+
speed: r = 0.01,
|
|
51
|
+
seed: a = 0,
|
|
52
|
+
lerpSpeed: n = 0.1,
|
|
53
|
+
preserveBaseTranslation: o = !1,
|
|
54
|
+
// <-- si querés mantener la traslación base
|
|
55
|
+
amplitude: u = 1,
|
|
56
|
+
target: m = null,
|
|
57
|
+
// {x, y} en px, offset desde la posición base
|
|
58
|
+
legacyNoise: g = !1,
|
|
59
|
+
// true = sample noise at y=0/100 like <=0.0.6 (flat spots on one axis)
|
|
60
|
+
pauseOffscreen: R = !0
|
|
61
|
+
// false = keep stepping even when the parent box is out of view
|
|
62
|
+
}) {
|
|
63
|
+
B(this, c);
|
|
64
|
+
B(this, F, 1);
|
|
65
|
+
var b, x, T, d;
|
|
66
|
+
if (this._dirty = !1, this.pauseOffscreen = R !== !1, this._ny = g ? [0, 100] : [0.5, 100.5], this.amplitude = u, this.setTarget(m), this.el = t, this.xRange = e, this.yRange = s, this.speed = r, this.seed = a, this.time = a, this.isPlaying = !0, this.lerpFactor = 1, this.lerpSpeed = n, this.targetLerpFactor = 1, this.lastX = 0, this.lastY = 0, this.preserveBaseTranslation = o, this.hasPercentages = !!(e && (h(b = f, l, y).call(b, e.min) || h(x = f, l, y).call(x, e.max)) || s && (h(T = f, l, y).call(T, s.min) || h(d = f, l, y).call(d, s.max))), t) {
|
|
67
|
+
const p = window.getComputedStyle(t);
|
|
68
|
+
p.position === "static" && (t.style.position = "relative"), this.originalInlineTransform = t.style.transform || "", this.baseTransform = h(this, c, E).call(this, p.transform, this.preserveBaseTranslation), this.hasPercentages && (this.handleResize = this.handleResize.bind(this), window.addEventListener("resize", this.handleResize));
|
|
69
|
+
} else
|
|
70
|
+
this.originalInlineTransform = "", this.baseTransform = "";
|
|
71
|
+
this.onScreen = !0, this.init(a);
|
|
72
|
+
}
|
|
73
|
+
init(t) {
|
|
74
|
+
var e;
|
|
75
|
+
this._destroyed = !1, this.noise = h(this, c, Q).call(this, h(e = f, l, K).call(e, t)), this.isPlaying = !0, this.lerpFactor = 1, this.targetLerpFactor = 1, V(this), $(this);
|
|
76
|
+
}
|
|
77
|
+
// 0 = clavado en target (o 0,0), 1 = deriva libre; siempre clampeado a [0, 1], NaN cuenta como 1
|
|
78
|
+
get amplitude() {
|
|
79
|
+
return M(this, F);
|
|
80
|
+
}
|
|
81
|
+
set amplitude(t) {
|
|
82
|
+
const e = typeof t == "number" && !Number.isNaN(t) ? Math.min(1, Math.max(0, t)) : 1;
|
|
83
|
+
e !== M(this, F) && (this._dirty = !0), D(this, F, e);
|
|
84
|
+
}
|
|
85
|
+
setTarget(t) {
|
|
86
|
+
const e = this.target;
|
|
87
|
+
if (t === null || typeof t != "object")
|
|
88
|
+
this.target = null;
|
|
89
|
+
else {
|
|
90
|
+
const r = (a) => typeof a == "number" && Number.isFinite(a) ? a : 0;
|
|
91
|
+
this.target = { x: r(t.x), y: r(t.y) };
|
|
92
|
+
}
|
|
93
|
+
const s = this.target;
|
|
94
|
+
((e == null ? void 0 : e.x) !== (s == null ? void 0 : s.x) || (e == null ? void 0 : e.y) !== (s == null ? void 0 : s.y)) && (this._dirty = !0);
|
|
95
|
+
}
|
|
96
|
+
// Permite refrescar el transform base si cambian clases/estilos en runtime
|
|
97
|
+
refreshBaseTransform(t = this.preserveBaseTranslation) {
|
|
98
|
+
if (!this.el) return;
|
|
99
|
+
const e = window.getComputedStyle(this.el);
|
|
100
|
+
this.baseTransform = h(this, c, E).call(this, e.transform, t);
|
|
101
|
+
}
|
|
102
|
+
// Handle window resize for percentage-based values
|
|
103
|
+
handleResize() {
|
|
104
|
+
}
|
|
105
|
+
pause() {
|
|
106
|
+
this.isPlaying && (this.targetLerpFactor = 0, this.isPlaying = !1);
|
|
107
|
+
}
|
|
108
|
+
play() {
|
|
109
|
+
this.isPlaying || (this.targetLerpFactor = 1, this.isPlaying = !0, this.lerpFactor === 0 && $(this));
|
|
110
|
+
}
|
|
111
|
+
// Un frame de este instance; devuelve false cuando queda totalmente pausado
|
|
112
|
+
step() {
|
|
113
|
+
var t, e, s, r, a, n, o, u, m, g, R, b;
|
|
114
|
+
if (this.lerpFactor !== this.targetLerpFactor && (this.lerpFactor = h(this, c, U).call(this, this.lerpFactor, this.targetLerpFactor, this.lerpSpeed), Math.abs(this.lerpFactor - this.targetLerpFactor) < 1e-3 && (this.lerpFactor = this.targetLerpFactor)), this.lerpFactor > 0) {
|
|
115
|
+
this.time += this.speed * this.lerpFactor;
|
|
116
|
+
const x = this.noise.perlin2(this.time, this._ny[0]), T = this.noise.perlin2(this.time, this._ny[1]);
|
|
117
|
+
if (this.el) {
|
|
118
|
+
let d, p, P, A;
|
|
119
|
+
if (this.hasPercentages) {
|
|
120
|
+
const j = this.el.parentElement || document.body, k = j.clientWidth, q = j.clientHeight;
|
|
121
|
+
this.xRange && (d = h(t = f, l, y).call(t, this.xRange.min) ? h(e = f, l, L).call(e, this.xRange.min, k) : this.xRange.min, p = h(s = f, l, y).call(s, this.xRange.max) ? h(r = f, l, L).call(r, this.xRange.max, k) : this.xRange.max), this.yRange && (P = h(a = f, l, y).call(a, this.yRange.min) ? h(n = f, l, L).call(n, this.yRange.min, q) : this.yRange.min, A = h(o = f, l, y).call(o, this.yRange.max) ? h(u = f, l, L).call(u, this.yRange.max, q) : this.yRange.max);
|
|
122
|
+
} else
|
|
123
|
+
d = (m = this.xRange) == null ? void 0 : m.min, p = (g = this.xRange) == null ? void 0 : g.max, P = (R = this.yRange) == null ? void 0 : R.min, A = (b = this.yRange) == null ? void 0 : b.max;
|
|
124
|
+
const N = h(this, c, S).call(this, this.xRange ? h(this, c, X).call(this, x, -1, 1, d, p) : 0, "x"), W = h(this, c, S).call(this, this.yRange ? h(this, c, X).call(this, T, -1, 1, P, A) : 0, "y");
|
|
125
|
+
this.lastX = N, this.lastY = W, this.el.style.transform = `translate3d(${N}px, ${W}px, 0) ${this.baseTransform}`.trim();
|
|
126
|
+
} else {
|
|
127
|
+
const d = h(this, c, S).call(this, this.xRange ? h(this, c, X).call(this, x, -1, 1, this.xRange.min, this.xRange.max) : 0, "x"), p = h(this, c, S).call(this, this.yRange ? h(this, c, X).call(this, T, -1, 1, this.yRange.min, this.yRange.max) : 0, "y");
|
|
128
|
+
this.lastX = d, this.lastY = p;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return this.lerpFactor > 0 || this.targetLerpFactor > 0;
|
|
132
|
+
}
|
|
133
|
+
// Back to the starting state, as if just created: the noise path rewinds to its seed and the
|
|
134
|
+
// element returns to its own transform (no drift). Keeps play/pause, amplitude and target.
|
|
135
|
+
reset() {
|
|
136
|
+
this._destroyed || (this.time = this.seed, this.lastX = 0, this.lastY = 0, this.el && (this.el.style.transform = this.originalInlineTransform || ""), this.isPlaying && $(this));
|
|
137
|
+
}
|
|
138
|
+
destroy() {
|
|
139
|
+
this._destroyed = !0, Z(this), _(this), this.el && (this.el.style.transform = this.originalInlineTransform || ""), this.hasPercentages && window.removeEventListener("resize", this.handleResize);
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
F = new WeakMap(), l = new WeakSet(), J = function(t) {
|
|
143
|
+
return function() {
|
|
144
|
+
let e = t += 1831565813;
|
|
145
|
+
return e = Math.imul(e ^ e >>> 15, e | 1), e ^= e + Math.imul(e ^ e >>> 7, e | 61), ((e ^ e >>> 14) >>> 0) / 4294967296;
|
|
146
|
+
};
|
|
147
|
+
}, K = function(t = 0) {
|
|
148
|
+
var r;
|
|
149
|
+
const e = Array.from({ length: 256 }, (a, n) => n), s = h(r = f, l, J).call(r, t);
|
|
150
|
+
for (let a = 255; a > 0; a--) {
|
|
151
|
+
const n = Math.floor(s() * (a + 1));
|
|
152
|
+
[e[a], e[n]] = [e[n], e[a]];
|
|
153
|
+
}
|
|
154
|
+
return e.concat(e);
|
|
155
|
+
}, y = function(t) {
|
|
156
|
+
return typeof t == "string" && t.endsWith("%");
|
|
157
|
+
}, L = function(t, e) {
|
|
158
|
+
var r;
|
|
159
|
+
return h(r = f, l, y).call(r, t) ? parseFloat(t) / 100 * e : t;
|
|
160
|
+
}, c = new WeakSet(), Q = function(t) {
|
|
161
|
+
const e = (n) => n * n * n * (n * (n * 6 - 15) + 10), s = (n, o, u) => o + n * (u - o), r = (n, o, u) => {
|
|
162
|
+
const m = n & 15, g = m < 8 ? o : u, R = m < 4 ? u : m === 12 || m === 14 ? o : 0;
|
|
163
|
+
return (m & 1 ? -g : g) + (m & 2 ? -R : R);
|
|
164
|
+
}, a = (n, o) => n & 1 ? -o : o;
|
|
165
|
+
return {
|
|
166
|
+
// Original 1D Perlin noise function
|
|
167
|
+
perlin1(n) {
|
|
168
|
+
const o = Math.floor(n) & 255;
|
|
169
|
+
n -= Math.floor(n);
|
|
170
|
+
const u = e(n);
|
|
171
|
+
return s(u, a(t[o], n), a(t[o + 1], n - 1));
|
|
172
|
+
},
|
|
173
|
+
// New 2D Perlin noise function
|
|
174
|
+
perlin2(n, o) {
|
|
175
|
+
const u = Math.floor(n) & 255, m = Math.floor(o) & 255;
|
|
176
|
+
n -= Math.floor(n), o -= Math.floor(o);
|
|
177
|
+
const g = e(n), R = e(o), b = t[u] + m, x = t[u + 1] + m, T = t[b], d = t[b + 1], p = t[x], P = t[x + 1];
|
|
178
|
+
return s(
|
|
179
|
+
R,
|
|
180
|
+
s(g, r(t[T], n, o), r(t[p], n - 1, o)),
|
|
181
|
+
s(g, r(t[d], n, o - 1), r(t[P], n - 1, o - 1))
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
}, U = function(t, e, s) {
|
|
186
|
+
return t + s * (e - t);
|
|
187
|
+
}, // target + (noise - target) * amplitude; amplitude 1 returns noise untouched (exact legacy values)
|
|
188
|
+
S = function(t, e) {
|
|
189
|
+
var r;
|
|
190
|
+
if (M(this, F) === 1) return t;
|
|
191
|
+
const s = ((r = this.target) == null ? void 0 : r[e]) ?? 0;
|
|
192
|
+
return s + (t - s) * M(this, F);
|
|
193
|
+
}, X = function(t, e, s, r, a) {
|
|
194
|
+
return (t - e) * (a - r) / (s - e) + r;
|
|
195
|
+
}, // Quita o preserva la traslación del transform base en 2D según flag
|
|
196
|
+
E = function(t, e = !1) {
|
|
197
|
+
if (!t || t === "none") return "";
|
|
198
|
+
try {
|
|
199
|
+
const s = new DOMMatrixReadOnly(t);
|
|
200
|
+
if (s.is2D) {
|
|
201
|
+
const r = e ? s.e : 0, a = e ? s.f : 0;
|
|
202
|
+
return `matrix(${s.a}, ${s.b}, ${s.c}, ${s.d}, ${r}, ${a})`;
|
|
203
|
+
} else
|
|
204
|
+
return t;
|
|
205
|
+
} catch {
|
|
206
|
+
return t;
|
|
207
|
+
}
|
|
208
|
+
}, B(f, l);
|
|
209
|
+
let H = f;
|
|
210
|
+
export {
|
|
211
|
+
H as default
|
|
212
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(r,o){typeof exports=="object"&&typeof module<"u"?module.exports=o():typeof define=="function"&&define.amd?define(o):(r=typeof globalThis<"u"?globalThis:r||self,r.PerlinDOM=o())})(this,function(){"use strict";var H=r=>{throw TypeError(r)};var N=(r,o,d)=>o.has(r)||H("Cannot "+d);var X=(r,o,d)=>(N(r,o,"read from private field"),d?d.call(r):o.get(r)),I=(r,o,d)=>o.has(r)?H("Cannot add the same private member more than once"):o instanceof WeakSet?o.add(r):o.set(r,d),G=(r,o,d,M)=>(N(r,o,"write to private field"),M?M.call(r,d):o.set(r,d),d),h=(r,o,d)=>(N(r,o,"access private method"),d);var v,f,J,K,x,z,u,Q,U,B,Y,W;const r=new Set;let o=null;function d(){o=null;for(const c of[...r])r.has(c)&&(!c.onScreen&&!c._dirty||(c._dirty=!1,c.step()===!1&&r.delete(c)));r.size>0&&o===null&&(o=requestAnimationFrame(d))}function M(c){r.add(c),o===null&&(o=requestAnimationFrame(d))}function Z(c){r.delete(c),r.size===0&&o!==null&&(cancelAnimationFrame(o),o=null)}const L=new Map,A=new WeakMap;let $=null;function V(c){if(typeof IntersectionObserver>"u"||!c.el||!c.pauseOffscreen||A.has(c))return;$??($=new IntersectionObserver(s=>{for(const n of s){const a=L.get(n.target);if(a){a.visible=n.isIntersecting;for(const i of a.insts)i.onScreen=n.isIntersecting}}}));const t=c.el.parentElement||c.el;let e=L.get(t);e||(e={visible:!0,insts:new Set},L.set(t,e),$.observe(t)),e.insts.add(c),A.set(c,t),c.onScreen=e.visible}function _(c){const t=A.get(c);if(!t)return;A.delete(c);const e=L.get(t);e&&(e.insts.delete(c),e.insts.size===0&&(L.delete(t),$.unobserve(t)))}const m=class m{constructor({element:t,x:e=null,y:s=null,speed:n=.01,seed:a=0,lerpSpeed:i=.1,preserveBaseTranslation:l=!1,amplitude:g=1,target:p=null,legacyNoise:y=!1,pauseOffscreen:F=!0}){I(this,u);I(this,v,1);var T,w,P,R;if(this._dirty=!1,this.pauseOffscreen=F!==!1,this._ny=y?[0,100]:[.5,100.5],this.amplitude=g,this.setTarget(p),this.el=t,this.xRange=e,this.yRange=s,this.speed=n,this.seed=a,this.time=a,this.isPlaying=!0,this.lerpFactor=1,this.lerpSpeed=i,this.targetLerpFactor=1,this.lastX=0,this.lastY=0,this.preserveBaseTranslation=l,this.hasPercentages=!!(e&&(h(T=m,f,x).call(T,e.min)||h(w=m,f,x).call(w,e.max))||s&&(h(P=m,f,x).call(P,s.min)||h(R=m,f,x).call(R,s.max))),t){const b=window.getComputedStyle(t);b.position==="static"&&(t.style.position="relative"),this.originalInlineTransform=t.style.transform||"",this.baseTransform=h(this,u,W).call(this,b.transform,this.preserveBaseTranslation),this.hasPercentages&&(this.handleResize=this.handleResize.bind(this),window.addEventListener("resize",this.handleResize))}else this.originalInlineTransform="",this.baseTransform="";this.onScreen=!0,this.init(a)}init(t){var e;this._destroyed=!1,this.noise=h(this,u,Q).call(this,h(e=m,f,K).call(e,t)),this.isPlaying=!0,this.lerpFactor=1,this.targetLerpFactor=1,V(this),M(this)}get amplitude(){return X(this,v)}set amplitude(t){const e=typeof t=="number"&&!Number.isNaN(t)?Math.min(1,Math.max(0,t)):1;e!==X(this,v)&&(this._dirty=!0),G(this,v,e)}setTarget(t){const e=this.target;if(t===null||typeof t!="object")this.target=null;else{const n=a=>typeof a=="number"&&Number.isFinite(a)?a:0;this.target={x:n(t.x),y:n(t.y)}}const s=this.target;((e==null?void 0:e.x)!==(s==null?void 0:s.x)||(e==null?void 0:e.y)!==(s==null?void 0:s.y))&&(this._dirty=!0)}refreshBaseTransform(t=this.preserveBaseTranslation){if(!this.el)return;const e=window.getComputedStyle(this.el);this.baseTransform=h(this,u,W).call(this,e.transform,t)}handleResize(){}pause(){this.isPlaying&&(this.targetLerpFactor=0,this.isPlaying=!1)}play(){this.isPlaying||(this.targetLerpFactor=1,this.isPlaying=!0,this.lerpFactor===0&&M(this))}step(){var t,e,s,n,a,i,l,g,p,y,F,T;if(this.lerpFactor!==this.targetLerpFactor&&(this.lerpFactor=h(this,u,U).call(this,this.lerpFactor,this.targetLerpFactor,this.lerpSpeed),Math.abs(this.lerpFactor-this.targetLerpFactor)<.001&&(this.lerpFactor=this.targetLerpFactor)),this.lerpFactor>0){this.time+=this.speed*this.lerpFactor;const w=this.noise.perlin2(this.time,this._ny[0]),P=this.noise.perlin2(this.time,this._ny[1]);if(this.el){let R,b,S,E;if(this.hasPercentages){const k=this.el.parentElement||document.body,q=k.clientWidth,C=k.clientHeight;this.xRange&&(R=h(t=m,f,x).call(t,this.xRange.min)?h(e=m,f,z).call(e,this.xRange.min,q):this.xRange.min,b=h(s=m,f,x).call(s,this.xRange.max)?h(n=m,f,z).call(n,this.xRange.max,q):this.xRange.max),this.yRange&&(S=h(a=m,f,x).call(a,this.yRange.min)?h(i=m,f,z).call(i,this.yRange.min,C):this.yRange.min,E=h(l=m,f,x).call(l,this.yRange.max)?h(g=m,f,z).call(g,this.yRange.max,C):this.yRange.max)}else R=(p=this.xRange)==null?void 0:p.min,b=(y=this.xRange)==null?void 0:y.max,S=(F=this.yRange)==null?void 0:F.min,E=(T=this.yRange)==null?void 0:T.max;const j=h(this,u,B).call(this,this.xRange?h(this,u,Y).call(this,w,-1,1,R,b):0,"x"),D=h(this,u,B).call(this,this.yRange?h(this,u,Y).call(this,P,-1,1,S,E):0,"y");this.lastX=j,this.lastY=D,this.el.style.transform=`translate3d(${j}px, ${D}px, 0) ${this.baseTransform}`.trim()}else{const R=h(this,u,B).call(this,this.xRange?h(this,u,Y).call(this,w,-1,1,this.xRange.min,this.xRange.max):0,"x"),b=h(this,u,B).call(this,this.yRange?h(this,u,Y).call(this,P,-1,1,this.yRange.min,this.yRange.max):0,"y");this.lastX=R,this.lastY=b}}return this.lerpFactor>0||this.targetLerpFactor>0}reset(){this._destroyed||(this.time=this.seed,this.lastX=0,this.lastY=0,this.el&&(this.el.style.transform=this.originalInlineTransform||""),this.isPlaying&&M(this))}destroy(){this._destroyed=!0,Z(this),_(this),this.el&&(this.el.style.transform=this.originalInlineTransform||""),this.hasPercentages&&window.removeEventListener("resize",this.handleResize)}};v=new WeakMap,f=new WeakSet,J=function(t){return function(){let e=t+=1831565813;return e=Math.imul(e^e>>>15,e|1),e^=e+Math.imul(e^e>>>7,e|61),((e^e>>>14)>>>0)/4294967296}},K=function(t=0){var n;const e=Array.from({length:256},(a,i)=>i),s=h(n=m,f,J).call(n,t);for(let a=255;a>0;a--){const i=Math.floor(s()*(a+1));[e[a],e[i]]=[e[i],e[a]]}return e.concat(e)},x=function(t){return typeof t=="string"&&t.endsWith("%")},z=function(t,e){var n;return h(n=m,f,x).call(n,t)?parseFloat(t)/100*e:t},u=new WeakSet,Q=function(t){const e=i=>i*i*i*(i*(i*6-15)+10),s=(i,l,g)=>l+i*(g-l),n=(i,l,g)=>{const p=i&15,y=p<8?l:g,F=p<4?g:p===12||p===14?l:0;return(p&1?-y:y)+(p&2?-F:F)},a=(i,l)=>i&1?-l:l;return{perlin1(i){const l=Math.floor(i)&255;i-=Math.floor(i);const g=e(i);return s(g,a(t[l],i),a(t[l+1],i-1))},perlin2(i,l){const g=Math.floor(i)&255,p=Math.floor(l)&255;i-=Math.floor(i),l-=Math.floor(l);const y=e(i),F=e(l),T=t[g]+p,w=t[g+1]+p,P=t[T],R=t[T+1],b=t[w],S=t[w+1];return s(F,s(y,n(t[P],i,l),n(t[b],i-1,l)),s(y,n(t[R],i,l-1),n(t[S],i-1,l-1)))}}},U=function(t,e,s){return t+s*(e-t)},B=function(t,e){var n;if(X(this,v)===1)return t;const s=((n=this.target)==null?void 0:n[e])??0;return s+(t-s)*X(this,v)},Y=function(t,e,s,n,a){return(t-e)*(a-n)/(s-e)+n},W=function(t,e=!1){if(!t||t==="none")return"";try{const s=new DOMMatrixReadOnly(t);if(s.is2D){const n=e?s.e:0,a=e?s.f:0;return`matrix(${s.a}, ${s.b}, ${s.c}, ${s.d}, ${n}, ${a})`}else return t}catch{return t}},I(m,f);let O=m;return O});
|
package/package.json
CHANGED
|
@@ -1,15 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@andresclua/perlindom",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.1.1",
|
|
5
5
|
"description": "A lightweight JavaScript library for creating smooth, organic animations for DOM elements using Perlin noise",
|
|
6
|
+
"homepage": "https://perlindom.js.org",
|
|
7
|
+
"repository": { "type": "git", "url": "git+https://github.com/andresclua/PerlinDOM.git" },
|
|
8
|
+
"bugs": "https://github.com/andresclua/PerlinDOM/issues",
|
|
9
|
+
"files": ["dist", "CHANGELOG.md"],
|
|
6
10
|
"type": "module",
|
|
7
|
-
"main": "./dist/
|
|
8
|
-
"module": "./dist/
|
|
11
|
+
"main": "./dist/perlindom.umd.js",
|
|
12
|
+
"module": "./dist/perlindom.es.js",
|
|
9
13
|
"exports": {
|
|
10
14
|
".": {
|
|
11
|
-
"import": "./dist/
|
|
12
|
-
"require": "./dist/
|
|
15
|
+
"import": "./dist/perlindom.es.js",
|
|
16
|
+
"require": "./dist/perlindom.umd.js"
|
|
13
17
|
}
|
|
14
18
|
},
|
|
15
19
|
"keywords": [
|
|
@@ -24,9 +28,13 @@
|
|
|
24
28
|
"scripts": {
|
|
25
29
|
"dev": "vite",
|
|
26
30
|
"build": "vite build",
|
|
27
|
-
"
|
|
31
|
+
"build:lib": "vite build",
|
|
32
|
+
"build:site": "vite build --mode site",
|
|
33
|
+
"preview": "vite preview",
|
|
34
|
+
"test": "node --test 'tests/*.test.mjs'"
|
|
28
35
|
},
|
|
29
36
|
"devDependencies": {
|
|
37
|
+
"gsap": "^3.15.1",
|
|
30
38
|
"sass": "^1.80.6",
|
|
31
39
|
"vite": "^5.4.10"
|
|
32
40
|
}
|
package/dist/PerlinDOM.es.js
DELETED
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
var D = Object.defineProperty;
|
|
2
|
-
var z = (c) => {
|
|
3
|
-
throw TypeError(c);
|
|
4
|
-
};
|
|
5
|
-
var H = (c, t, s) => t in c ? D(c, t, { enumerable: !0, configurable: !0, writable: !0, value: s }) : c[t] = s;
|
|
6
|
-
var S = (c, t, s) => H(c, typeof t != "symbol" ? t + "" : t, s), j = (c, t, s) => t.has(c) || z("Cannot " + s);
|
|
7
|
-
var A = (c, t, s) => t.has(c) ? z("Cannot add the same private member more than once") : t instanceof WeakSet ? t.add(c) : t.set(c, s);
|
|
8
|
-
var n = (c, t, s) => (j(c, t, "access private method"), s);
|
|
9
|
-
var r, E, I, d, P, f, W, C, T, B;
|
|
10
|
-
const l = class l {
|
|
11
|
-
constructor({
|
|
12
|
-
element: t,
|
|
13
|
-
x: s = null,
|
|
14
|
-
y: e = null,
|
|
15
|
-
speed: h = 0.01,
|
|
16
|
-
seed: o = 0,
|
|
17
|
-
lerpSpeed: i = 0.1,
|
|
18
|
-
preserveBaseTranslation: a = !1
|
|
19
|
-
// <-- si querés mantener la traslación base
|
|
20
|
-
}) {
|
|
21
|
-
A(this, f);
|
|
22
|
-
S(this, "animate", () => {
|
|
23
|
-
var t, s, e, h, o, i, a, m, g, p, u, R;
|
|
24
|
-
if (this.lerpFactor !== this.targetLerpFactor && (this.lerpFactor = n(this, f, C).call(this, this.lerpFactor, this.targetLerpFactor, this.lerpSpeed), Math.abs(this.lerpFactor - this.targetLerpFactor) < 1e-3 && (this.lerpFactor = this.targetLerpFactor)), this.lerpFactor > 0) {
|
|
25
|
-
this.time += this.speed * this.lerpFactor;
|
|
26
|
-
const x = this.noise.perlin2(this.time, 0), b = this.noise.perlin2(this.time, 100);
|
|
27
|
-
if (this.el) {
|
|
28
|
-
let F, y, v, L;
|
|
29
|
-
if (this.hasPercentages) {
|
|
30
|
-
const Y = this.el.parentElement || document.body, $ = Y.clientWidth, M = Y.clientHeight;
|
|
31
|
-
this.xRange && (F = n(t = l, r, d).call(t, this.xRange.min) ? n(s = l, r, P).call(s, this.xRange.min, $) : this.xRange.min, y = n(e = l, r, d).call(e, this.xRange.max) ? n(h = l, r, P).call(h, this.xRange.max, $) : this.xRange.max), this.yRange && (v = n(o = l, r, d).call(o, this.yRange.min) ? n(i = l, r, P).call(i, this.yRange.min, M) : this.yRange.min, L = n(a = l, r, d).call(a, this.yRange.max) ? n(m = l, r, P).call(m, this.yRange.max, M) : this.yRange.max);
|
|
32
|
-
} else
|
|
33
|
-
F = (g = this.xRange) == null ? void 0 : g.min, y = (p = this.xRange) == null ? void 0 : p.max, v = (u = this.yRange) == null ? void 0 : u.min, L = (R = this.yRange) == null ? void 0 : R.max;
|
|
34
|
-
const X = this.xRange ? n(this, f, T).call(this, x, -1, 1, F, y) : 0, w = this.yRange ? n(this, f, T).call(this, b, -1, 1, v, L) : 0;
|
|
35
|
-
this.lastX = X, this.lastY = w, this.el.style.transform = `translate3d(${X}px, ${w}px, 0) ${this.baseTransform}`.trim();
|
|
36
|
-
} else {
|
|
37
|
-
const F = this.xRange ? n(this, f, T).call(this, x, -1, 1, this.xRange.min, this.xRange.max) : 0, y = this.yRange ? n(this, f, T).call(this, b, -1, 1, this.yRange.min, this.yRange.max) : 0;
|
|
38
|
-
this.lastX = F, this.lastY = y;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
(this.lerpFactor > 0 || this.targetLerpFactor > 0) && (this.raf = requestAnimationFrame(this.animate));
|
|
42
|
-
});
|
|
43
|
-
var m, g, p, u;
|
|
44
|
-
if (this.el = t, this.xRange = s, this.yRange = e, this.speed = h, this.time = o, this.isPlaying = !0, this.lerpFactor = 1, this.lerpSpeed = i, this.targetLerpFactor = 1, this.lastX = 0, this.lastY = 0, this.preserveBaseTranslation = a, this.hasPercentages = !!(s && (n(m = l, r, d).call(m, s.min) || n(g = l, r, d).call(g, s.max)) || e && (n(p = l, r, d).call(p, e.min) || n(u = l, r, d).call(u, e.max))), t) {
|
|
45
|
-
const R = window.getComputedStyle(t);
|
|
46
|
-
R.position === "static" && (t.style.position = "relative"), this.originalInlineTransform = t.style.transform || "", this.baseTransform = n(this, f, B).call(this, R.transform, this.preserveBaseTranslation), this.hasPercentages && (this.handleResize = this.handleResize.bind(this), window.addEventListener("resize", this.handleResize));
|
|
47
|
-
} else
|
|
48
|
-
this.originalInlineTransform = "", this.baseTransform = "";
|
|
49
|
-
this.animate = this.animate.bind(this), this.init(o);
|
|
50
|
-
}
|
|
51
|
-
init(t) {
|
|
52
|
-
var s;
|
|
53
|
-
this.noise = n(this, f, W).call(this, n(s = l, r, I).call(s, t)), this.isPlaying = !0, this.lerpFactor = 1, this.targetLerpFactor = 1, this.raf = requestAnimationFrame(this.animate);
|
|
54
|
-
}
|
|
55
|
-
// Permite refrescar el transform base si cambian clases/estilos en runtime
|
|
56
|
-
refreshBaseTransform(t = this.preserveBaseTranslation) {
|
|
57
|
-
if (!this.el) return;
|
|
58
|
-
const s = window.getComputedStyle(this.el);
|
|
59
|
-
this.baseTransform = n(this, f, B).call(this, s.transform, t);
|
|
60
|
-
}
|
|
61
|
-
// Handle window resize for percentage-based values
|
|
62
|
-
handleResize() {
|
|
63
|
-
}
|
|
64
|
-
pause() {
|
|
65
|
-
this.isPlaying && (this.targetLerpFactor = 0, this.isPlaying = !1);
|
|
66
|
-
}
|
|
67
|
-
play() {
|
|
68
|
-
this.isPlaying || (this.targetLerpFactor = 1, this.isPlaying = !0, this.lerpFactor === 0 && (this.raf = requestAnimationFrame(this.animate)));
|
|
69
|
-
}
|
|
70
|
-
destroy() {
|
|
71
|
-
cancelAnimationFrame(this.raf), this.el && (this.el.style.transform = this.originalInlineTransform || ""), this.hasPercentages && window.removeEventListener("resize", this.handleResize);
|
|
72
|
-
}
|
|
73
|
-
};
|
|
74
|
-
r = new WeakSet(), E = function(t) {
|
|
75
|
-
return function() {
|
|
76
|
-
let s = t += 1831565813;
|
|
77
|
-
return s = Math.imul(s ^ s >>> 15, s | 1), s ^= s + Math.imul(s ^ s >>> 7, s | 61), ((s ^ s >>> 14) >>> 0) / 4294967296;
|
|
78
|
-
};
|
|
79
|
-
}, I = function(t = 0) {
|
|
80
|
-
var h;
|
|
81
|
-
const s = Array.from({ length: 256 }, (o, i) => i), e = n(h = l, r, E).call(h, t);
|
|
82
|
-
for (let o = 255; o > 0; o--) {
|
|
83
|
-
const i = Math.floor(e() * (o + 1));
|
|
84
|
-
[s[o], s[i]] = [s[i], s[o]];
|
|
85
|
-
}
|
|
86
|
-
return s.concat(s);
|
|
87
|
-
}, d = function(t) {
|
|
88
|
-
return typeof t == "string" && t.endsWith("%");
|
|
89
|
-
}, P = function(t, s) {
|
|
90
|
-
var h;
|
|
91
|
-
return n(h = l, r, d).call(h, t) ? parseFloat(t) / 100 * s : t;
|
|
92
|
-
}, f = new WeakSet(), W = function(t) {
|
|
93
|
-
const s = (i) => i * i * i * (i * (i * 6 - 15) + 10), e = (i, a, m) => a + i * (m - a), h = (i, a, m) => {
|
|
94
|
-
const g = i & 15, p = g < 8 ? a : m, u = g < 4 ? m : g === 12 || g === 14 ? a : 0;
|
|
95
|
-
return (g & 1 ? -p : p) + (g & 2 ? -u : u);
|
|
96
|
-
}, o = (i, a) => i & 1 ? -a : a;
|
|
97
|
-
return {
|
|
98
|
-
// Original 1D Perlin noise function
|
|
99
|
-
perlin1(i) {
|
|
100
|
-
const a = Math.floor(i) & 255;
|
|
101
|
-
i -= Math.floor(i);
|
|
102
|
-
const m = s(i);
|
|
103
|
-
return e(m, o(t[a], i), o(t[a + 1], i - 1));
|
|
104
|
-
},
|
|
105
|
-
// New 2D Perlin noise function
|
|
106
|
-
perlin2(i, a) {
|
|
107
|
-
const m = Math.floor(i) & 255, g = Math.floor(a) & 255;
|
|
108
|
-
i -= Math.floor(i), a -= Math.floor(a);
|
|
109
|
-
const p = s(i), u = s(a), R = t[m] + g, x = t[m + 1] + g, b = t[R], F = t[R + 1], y = t[x], v = t[x + 1];
|
|
110
|
-
return e(
|
|
111
|
-
u,
|
|
112
|
-
e(p, h(t[b], i, a), h(t[y], i - 1, a)),
|
|
113
|
-
e(p, h(t[F], i, a - 1), h(t[v], i - 1, a - 1))
|
|
114
|
-
);
|
|
115
|
-
}
|
|
116
|
-
};
|
|
117
|
-
}, C = function(t, s, e) {
|
|
118
|
-
return t + e * (s - t);
|
|
119
|
-
}, T = function(t, s, e, h, o) {
|
|
120
|
-
return (t - s) * (o - h) / (e - s) + h;
|
|
121
|
-
}, // Quita o preserva la traslación del transform base en 2D según flag
|
|
122
|
-
B = function(t, s = !1) {
|
|
123
|
-
if (!t || t === "none") return "";
|
|
124
|
-
try {
|
|
125
|
-
const e = new DOMMatrixReadOnly(t);
|
|
126
|
-
if (e.is2D) {
|
|
127
|
-
const h = s ? e.e : 0, o = s ? e.f : 0;
|
|
128
|
-
return `matrix(${e.a}, ${e.b}, ${e.c}, ${e.d}, ${h}, ${o})`;
|
|
129
|
-
} else
|
|
130
|
-
return t;
|
|
131
|
-
} catch {
|
|
132
|
-
return t;
|
|
133
|
-
}
|
|
134
|
-
}, A(l, r);
|
|
135
|
-
let q = l;
|
|
136
|
-
export {
|
|
137
|
-
q as default
|
|
138
|
-
};
|
package/dist/PerlinDOM.umd.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
(function(h,s){typeof exports=="object"&&typeof module<"u"?module.exports=s():typeof define=="function"&&define.amd?define(s):(h=typeof globalThis<"u"?globalThis:h||self,h.PerlinDOM=s())})(this,function(){"use strict";var j=Object.defineProperty;var S=h=>{throw TypeError(h)};var C=(h,s,u)=>s in h?j(h,s,{enumerable:!0,configurable:!0,writable:!0,value:u}):h[s]=u;var q=(h,s,u)=>C(h,typeof s!="symbol"?s+"":s,u),H=(h,s,u)=>s.has(h)||S("Cannot "+u);var B=(h,s,u)=>s.has(h)?S("Cannot add the same private member more than once"):s instanceof WeakSet?s.add(h):s.set(h,u);var r=(h,s,u)=>(H(h,s,"access private method"),u);var s,D,E,R,P,g,I,W,b,X;const c=class c{constructor({element:t,x:i=null,y:n=null,speed:o=.01,seed:l=0,lerpSpeed:e=.1,preserveBaseTranslation:a=!1}){B(this,g);q(this,"animate",()=>{var t,i,n,o,l,e,a,m,f,p,d,y;if(this.lerpFactor!==this.targetLerpFactor&&(this.lerpFactor=r(this,g,W).call(this,this.lerpFactor,this.targetLerpFactor,this.lerpSpeed),Math.abs(this.lerpFactor-this.targetLerpFactor)<.001&&(this.lerpFactor=this.targetLerpFactor)),this.lerpFactor>0){this.time+=this.speed*this.lerpFactor;const T=this.noise.perlin2(this.time,0),L=this.noise.perlin2(this.time,100);if(this.el){let F,x,v,A;if(this.hasPercentages){const Y=this.el.parentElement||document.body,$=Y.clientWidth,z=Y.clientHeight;this.xRange&&(F=r(t=c,s,R).call(t,this.xRange.min)?r(i=c,s,P).call(i,this.xRange.min,$):this.xRange.min,x=r(n=c,s,R).call(n,this.xRange.max)?r(o=c,s,P).call(o,this.xRange.max,$):this.xRange.max),this.yRange&&(v=r(l=c,s,R).call(l,this.yRange.min)?r(e=c,s,P).call(e,this.yRange.min,z):this.yRange.min,A=r(a=c,s,R).call(a,this.yRange.max)?r(m=c,s,P).call(m,this.yRange.max,z):this.yRange.max)}else F=(f=this.xRange)==null?void 0:f.min,x=(p=this.xRange)==null?void 0:p.max,v=(d=this.yRange)==null?void 0:d.min,A=(y=this.yRange)==null?void 0:y.max;const w=this.xRange?r(this,g,b).call(this,T,-1,1,F,x):0,M=this.yRange?r(this,g,b).call(this,L,-1,1,v,A):0;this.lastX=w,this.lastY=M,this.el.style.transform=`translate3d(${w}px, ${M}px, 0) ${this.baseTransform}`.trim()}else{const F=this.xRange?r(this,g,b).call(this,T,-1,1,this.xRange.min,this.xRange.max):0,x=this.yRange?r(this,g,b).call(this,L,-1,1,this.yRange.min,this.yRange.max):0;this.lastX=F,this.lastY=x}}(this.lerpFactor>0||this.targetLerpFactor>0)&&(this.raf=requestAnimationFrame(this.animate))});var m,f,p,d;if(this.el=t,this.xRange=i,this.yRange=n,this.speed=o,this.time=l,this.isPlaying=!0,this.lerpFactor=1,this.lerpSpeed=e,this.targetLerpFactor=1,this.lastX=0,this.lastY=0,this.preserveBaseTranslation=a,this.hasPercentages=!!(i&&(r(m=c,s,R).call(m,i.min)||r(f=c,s,R).call(f,i.max))||n&&(r(p=c,s,R).call(p,n.min)||r(d=c,s,R).call(d,n.max))),t){const y=window.getComputedStyle(t);y.position==="static"&&(t.style.position="relative"),this.originalInlineTransform=t.style.transform||"",this.baseTransform=r(this,g,X).call(this,y.transform,this.preserveBaseTranslation),this.hasPercentages&&(this.handleResize=this.handleResize.bind(this),window.addEventListener("resize",this.handleResize))}else this.originalInlineTransform="",this.baseTransform="";this.animate=this.animate.bind(this),this.init(l)}init(t){var i;this.noise=r(this,g,I).call(this,r(i=c,s,E).call(i,t)),this.isPlaying=!0,this.lerpFactor=1,this.targetLerpFactor=1,this.raf=requestAnimationFrame(this.animate)}refreshBaseTransform(t=this.preserveBaseTranslation){if(!this.el)return;const i=window.getComputedStyle(this.el);this.baseTransform=r(this,g,X).call(this,i.transform,t)}handleResize(){}pause(){this.isPlaying&&(this.targetLerpFactor=0,this.isPlaying=!1)}play(){this.isPlaying||(this.targetLerpFactor=1,this.isPlaying=!0,this.lerpFactor===0&&(this.raf=requestAnimationFrame(this.animate)))}destroy(){cancelAnimationFrame(this.raf),this.el&&(this.el.style.transform=this.originalInlineTransform||""),this.hasPercentages&&window.removeEventListener("resize",this.handleResize)}};s=new WeakSet,D=function(t){return function(){let i=t+=1831565813;return i=Math.imul(i^i>>>15,i|1),i^=i+Math.imul(i^i>>>7,i|61),((i^i>>>14)>>>0)/4294967296}},E=function(t=0){var o;const i=Array.from({length:256},(l,e)=>e),n=r(o=c,s,D).call(o,t);for(let l=255;l>0;l--){const e=Math.floor(n()*(l+1));[i[l],i[e]]=[i[e],i[l]]}return i.concat(i)},R=function(t){return typeof t=="string"&&t.endsWith("%")},P=function(t,i){var o;return r(o=c,s,R).call(o,t)?parseFloat(t)/100*i:t},g=new WeakSet,I=function(t){const i=e=>e*e*e*(e*(e*6-15)+10),n=(e,a,m)=>a+e*(m-a),o=(e,a,m)=>{const f=e&15,p=f<8?a:m,d=f<4?m:f===12||f===14?a:0;return(f&1?-p:p)+(f&2?-d:d)},l=(e,a)=>e&1?-a:a;return{perlin1(e){const a=Math.floor(e)&255;e-=Math.floor(e);const m=i(e);return n(m,l(t[a],e),l(t[a+1],e-1))},perlin2(e,a){const m=Math.floor(e)&255,f=Math.floor(a)&255;e-=Math.floor(e),a-=Math.floor(a);const p=i(e),d=i(a),y=t[m]+f,T=t[m+1]+f,L=t[y],F=t[y+1],x=t[T],v=t[T+1];return n(d,n(p,o(t[L],e,a),o(t[x],e-1,a)),n(p,o(t[F],e,a-1),o(t[v],e-1,a-1)))}}},W=function(t,i,n){return t+n*(i-t)},b=function(t,i,n,o,l){return(t-i)*(l-o)/(n-i)+o},X=function(t,i=!1){if(!t||t==="none")return"";try{const n=new DOMMatrixReadOnly(t);if(n.is2D){const o=i?n.e:0,l=i?n.f:0;return`matrix(${n.a}, ${n.b}, ${n.c}, ${n.d}, ${o}, ${l})`}else return t}catch{return t}},B(c,s);let h=c;return h});
|