@cosmictraveler002/anim-kit 1.2.0 → 1.3.0
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 +203 -31
- package/dist/anim-kit.standalone.js +5 -5
- package/dist/anim-kit.standalone.js.map +1 -1
- package/dist/core/gsap.d.ts +1 -1
- package/dist/core/gsap.d.ts.map +1 -1
- package/dist/core/gsap.js +1 -1
- package/dist/core/gsap.js.map +1 -1
- package/dist/core/smooth-scroll.d.ts +1 -1
- package/dist/core/smooth-scroll.js +1 -1
- package/dist/effects/drag-rail.d.ts +23 -0
- package/dist/effects/drag-rail.d.ts.map +1 -0
- package/dist/effects/drag-rail.js +196 -0
- package/dist/effects/drag-rail.js.map +1 -0
- package/dist/effects/preloader.js +1 -1
- package/dist/effects/reel-text.d.ts +19 -0
- package/dist/effects/reel-text.d.ts.map +1 -0
- package/dist/effects/reel-text.js +163 -0
- package/dist/effects/reel-text.js.map +1 -0
- package/dist/index.d.ts +7 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -1
- package/dist/index.js.map +1 -1
- package/dist/three/index.d.ts +13 -0
- package/dist/three/index.d.ts.map +1 -0
- package/dist/three/index.js +12 -0
- package/dist/three/index.js.map +1 -0
- package/dist/three/webgl-media.d.ts +21 -0
- package/dist/three/webgl-media.d.ts.map +1 -0
- package/dist/three/webgl-media.js +268 -0
- package/dist/three/webgl-media.js.map +1 -0
- package/package.json +17 -1
- package/src/core/gsap.ts +1 -1
- package/src/core/smooth-scroll.ts +1 -1
- package/src/effects/drag-rail.ts +236 -0
- package/src/effects/preloader.ts +1 -1
- package/src/effects/reel-text.ts +212 -0
- package/src/index.ts +9 -1
- package/src/three/index.ts +12 -0
- package/src/three/webgl-media.ts +325 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { CommonOptions, Destroy, TargetLike } from "../core/types.js";
|
|
2
|
+
export interface WebglMediaOptions extends CommonOptions {
|
|
3
|
+
/** Image source — defaults to the wrapper's first `<img>`. */
|
|
4
|
+
src?: string;
|
|
5
|
+
/** Corner radius, px. @default 16 */
|
|
6
|
+
corner?: number;
|
|
7
|
+
/** Dent depth pressed into the card on hover, px. @default 70 */
|
|
8
|
+
dent?: number;
|
|
9
|
+
/** Chromatic split at full hover, px. @default 2 */
|
|
10
|
+
chroma?: number;
|
|
11
|
+
/** Wipe the card in when its texture loads. @default true */
|
|
12
|
+
reveal?: boolean;
|
|
13
|
+
/** Reveal duration, seconds. @default 1.1 */
|
|
14
|
+
revealDuration?: number;
|
|
15
|
+
/** Hover response duration, seconds. @default 0.6 */
|
|
16
|
+
hoverDuration?: number;
|
|
17
|
+
/** Device-pixel-ratio cap. @default 2 */
|
|
18
|
+
dpr?: number;
|
|
19
|
+
}
|
|
20
|
+
export declare function webglMedia(target: TargetLike, options?: WebglMediaOptions): Destroy;
|
|
21
|
+
//# sourceMappingURL=webgl-media.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"webgl-media.d.ts","sourceRoot":"","sources":["../../src/three/webgl-media.ts"],"names":[],"mappings":"AAuCA,OAAO,KAAK,EAAE,aAAa,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAE3E,MAAM,WAAW,iBAAkB,SAAQ,aAAa;IACtD,8DAA8D;IAC9D,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,qCAAqC;IACrC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,iEAAiE;IACjE,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,oDAAoD;IACpD,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,6DAA6D;IAC7D,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,6CAA6C;IAC7C,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,qDAAqD;IACrD,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,yCAAyC;IACzC,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAyED,wBAAgB,UAAU,CAAC,MAAM,EAAE,UAAU,EAAE,OAAO,GAAE,iBAAsB,GAAG,OAAO,CAiMvF"}
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WebGL media — the shader card: a DOM `<img>` re-rendered as a rounded,
|
|
3
|
+
* hover-reactive GL plane.
|
|
4
|
+
*
|
|
5
|
+
* The image keeps owning layout and alt text; the effect paints it onto a
|
|
6
|
+
* canvas that covers the wrapper, so the card can do what CSS can't: press a
|
|
7
|
+
* *dent* into the picture on hover (sample squeeze + fake dome lighting),
|
|
8
|
+
* split its channels (chroma), and wipe in on reveal — all inside one
|
|
9
|
+
* fragment shader. This is the media-card pattern from three.js portfolios,
|
|
10
|
+
* kept dependency-honest:
|
|
11
|
+
*
|
|
12
|
+
* - **Optional peer.** Lives behind the `@cosmictraveler002/anim-kit/three`
|
|
13
|
+
* subpath; the core barrel stays gsap+lenis only. `three` is an optional
|
|
14
|
+
* peer — install it to use this entry, the core never imports it.
|
|
15
|
+
* - **Silent no-op ladder.** Missing target → no-op. No
|
|
16
|
+
* `WebGLRenderingContext` (jsdom, SSR, WebGL-disabled browsers) → no-op
|
|
17
|
+
* *before* a context is ever requested (a probe call would itself log).
|
|
18
|
+
* Renderer construction throws → no-op. Texture 404s → the plain `<img>`
|
|
19
|
+
* stays visible. Under `prefers-reduced-motion` the canvas never mounts.
|
|
20
|
+
* - **Cover crop in-shader.** The texture is sampled with `object-fit: cover`
|
|
21
|
+
* maths (`scale = uRes / uTexSize`, windowed around the centre), so any
|
|
22
|
+
* source aspect fills the card without letterboxing.
|
|
23
|
+
* - **Rounded corners are shader-true.** A rounded-box SDF with a 1px smooth
|
|
24
|
+
* edge — not `border-radius` on the canvas — so the dent/chroma never bleed
|
|
25
|
+
* into the corner.
|
|
26
|
+
* - **Fidelity.** `destroy()` kills ticker + tweens + observers/listeners,
|
|
27
|
+
* disposes geometry/material/texture/renderer, removes the canvas and
|
|
28
|
+
* restores the `<img>` opacity and wrapper position it changed.
|
|
29
|
+
*
|
|
30
|
+
* webglMedia("[data-gl]", { corner: 18, dent: 70, chroma: 2 })
|
|
31
|
+
*
|
|
32
|
+
* Markup: a wrapper element containing `<img>` (or pass `src` explicitly):
|
|
33
|
+
*
|
|
34
|
+
* <figure class="gl-card" data-gl><img src="photo.jpg" alt="" /></figure>
|
|
35
|
+
*/
|
|
36
|
+
import * as THREE from "three";
|
|
37
|
+
import { gsap, initGSAP } from "../core/gsap.js";
|
|
38
|
+
import { guard } from "../core/guard.js";
|
|
39
|
+
import { one } from "../core/util.js";
|
|
40
|
+
const VERT = /* glsl */ `
|
|
41
|
+
varying vec2 vUv;
|
|
42
|
+
uniform float uHover;
|
|
43
|
+
uniform float uDent;
|
|
44
|
+
uniform float uTime;
|
|
45
|
+
|
|
46
|
+
void main() {
|
|
47
|
+
vUv = uv;
|
|
48
|
+
vec3 p = position;
|
|
49
|
+
float dome = max(cos(uv.x * 3.14159265) * cos(uv.y * 3.14159265), 0.0);
|
|
50
|
+
dome *= 0.94 + 0.06 * sin(uTime * 1.6);
|
|
51
|
+
// Pressed-in curvature while hovered — the fragment does the visible work.
|
|
52
|
+
p.z -= uHover * uDent * dome;
|
|
53
|
+
gl_Position = projectionMatrix * modelViewMatrix * vec4(p, 1.0);
|
|
54
|
+
}
|
|
55
|
+
`;
|
|
56
|
+
const FRAG = /* glsl */ `
|
|
57
|
+
precision highp float;
|
|
58
|
+
varying vec2 vUv;
|
|
59
|
+
uniform sampler2D uTex;
|
|
60
|
+
uniform vec2 uRes;
|
|
61
|
+
uniform vec2 uTexSize;
|
|
62
|
+
uniform float uCorner;
|
|
63
|
+
uniform float uHover;
|
|
64
|
+
uniform float uDent;
|
|
65
|
+
uniform float uChroma;
|
|
66
|
+
uniform float uProgress;
|
|
67
|
+
uniform float uTime;
|
|
68
|
+
|
|
69
|
+
float sdRoundBox(vec2 p, vec2 b, float r) {
|
|
70
|
+
vec2 q = abs(p) - b + r;
|
|
71
|
+
return min(max(q.x, q.y), 0.0) + length(max(q, 0.0)) - r;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
void main() {
|
|
75
|
+
vec2 px = (vUv - 0.5) * uRes;
|
|
76
|
+
float d = sdRoundBox(px, uRes * 0.5, uCorner);
|
|
77
|
+
float alpha = 1.0 - smoothstep(-1.0, 1.0, d); // 1px-ish AA on the corner
|
|
78
|
+
|
|
79
|
+
// Entrance wipe: grows from the bottom with a soft leading edge.
|
|
80
|
+
if (uProgress < 1.0) {
|
|
81
|
+
alpha *= 1.0 - smoothstep(uProgress - 0.05, uProgress, vUv.y);
|
|
82
|
+
}
|
|
83
|
+
if (alpha < 0.003) discard;
|
|
84
|
+
|
|
85
|
+
// object-fit: cover — window the texture around its centre.
|
|
86
|
+
vec2 scale = uRes / max(uTexSize, vec2(1.0));
|
|
87
|
+
vec2 cover = (vUv - 0.5) * (scale / max(scale.x, scale.y)) + 0.5;
|
|
88
|
+
|
|
89
|
+
// Hover dent: squeeze the sample toward the centre (breathing slightly).
|
|
90
|
+
vec2 c = cover - 0.5;
|
|
91
|
+
float dome = max(cos(vUv.x * 3.14159265) * cos(vUv.y * 3.14159265), 0.0);
|
|
92
|
+
dome *= 0.94 + 0.06 * sin(uTime * 1.6);
|
|
93
|
+
vec2 suv = 0.5 + c * (1.0 - uHover * uDent * 0.0016 * dome);
|
|
94
|
+
|
|
95
|
+
float chroma = uChroma * uHover * dome / max(uRes.x, 1.0);
|
|
96
|
+
vec3 col;
|
|
97
|
+
col.r = texture2D(uTex, suv + vec2(chroma, 0.0)).r;
|
|
98
|
+
col.g = texture2D(uTex, suv).g;
|
|
99
|
+
col.b = texture2D(uTex, suv - vec2(chroma, 0.0)).b;
|
|
100
|
+
|
|
101
|
+
// Fake lighting off the dome slope: sheen at the crest, shade at the rim.
|
|
102
|
+
float light = dome * uHover;
|
|
103
|
+
col += light * 0.12;
|
|
104
|
+
col *= 1.0 - 0.12 * uHover * (1.0 - dome);
|
|
105
|
+
|
|
106
|
+
gl_FragColor = vec4(col, alpha);
|
|
107
|
+
}
|
|
108
|
+
`;
|
|
109
|
+
export function webglMedia(target, options = {}) {
|
|
110
|
+
initGSAP();
|
|
111
|
+
const host = one(target);
|
|
112
|
+
if (!host)
|
|
113
|
+
return () => { };
|
|
114
|
+
const { src, corner = 16, dent = 70, chroma = 2, reveal = true, revealDuration = 1.1, hoverDuration = 0.6, dpr = 2, } = options;
|
|
115
|
+
return guard(options, () => {
|
|
116
|
+
/* ---------------- availability ladder (silent, no probes) ---------------- */
|
|
117
|
+
if (typeof window === "undefined" || !("WebGLRenderingContext" in window))
|
|
118
|
+
return () => { };
|
|
119
|
+
let renderer;
|
|
120
|
+
try {
|
|
121
|
+
renderer = new THREE.WebGLRenderer({ alpha: true, antialias: true });
|
|
122
|
+
}
|
|
123
|
+
catch {
|
|
124
|
+
return () => { }; // context refused (disabled/blacklisted) — plain img stays
|
|
125
|
+
}
|
|
126
|
+
const img = host.tagName === "IMG" ? null : host.querySelector("img");
|
|
127
|
+
const textureSrc = src || img?.currentSrc || img?.src;
|
|
128
|
+
if (!textureSrc) {
|
|
129
|
+
renderer.dispose();
|
|
130
|
+
return () => { };
|
|
131
|
+
}
|
|
132
|
+
/* ---------------- layout + styles we must restore ---------------- */
|
|
133
|
+
const prevPosition = host.style.position;
|
|
134
|
+
if (!prevPosition && getComputedStyle(host).position === "static") {
|
|
135
|
+
host.style.position = "relative";
|
|
136
|
+
}
|
|
137
|
+
const prevImgOpacity = img?.style.opacity ?? "";
|
|
138
|
+
host.dataset.akGl = "true";
|
|
139
|
+
const w = Math.max(1, host.clientWidth);
|
|
140
|
+
const h = Math.max(1, host.clientHeight);
|
|
141
|
+
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, dpr));
|
|
142
|
+
renderer.setSize(w, h, false);
|
|
143
|
+
const canvas = renderer.domElement;
|
|
144
|
+
canvas.style.cssText =
|
|
145
|
+
"position:absolute;inset:0;width:100%;height:100%;display:block;pointer-events:none;";
|
|
146
|
+
const scene = new THREE.Scene();
|
|
147
|
+
const camera = new THREE.OrthographicCamera(-w / 2, w / 2, h / 2, -h / 2, -100, 100);
|
|
148
|
+
camera.position.z = 10;
|
|
149
|
+
const geometry = new THREE.PlaneGeometry(1, 1, 1, 1);
|
|
150
|
+
const uniforms = {
|
|
151
|
+
uTex: { value: null },
|
|
152
|
+
uRes: { value: new THREE.Vector2(w, h) },
|
|
153
|
+
uTexSize: { value: new THREE.Vector2(1, 1) },
|
|
154
|
+
uCorner: { value: corner },
|
|
155
|
+
uHover: { value: 0 },
|
|
156
|
+
uDent: { value: dent },
|
|
157
|
+
uChroma: { value: chroma },
|
|
158
|
+
uProgress: { value: reveal ? 0 : 1 },
|
|
159
|
+
uTime: { value: 0 },
|
|
160
|
+
};
|
|
161
|
+
const material = new THREE.ShaderMaterial({
|
|
162
|
+
vertexShader: VERT,
|
|
163
|
+
fragmentShader: FRAG,
|
|
164
|
+
uniforms,
|
|
165
|
+
transparent: true,
|
|
166
|
+
});
|
|
167
|
+
const mesh = new THREE.Mesh(geometry, material);
|
|
168
|
+
mesh.scale.set(w, h, 1);
|
|
169
|
+
mesh.visible = false;
|
|
170
|
+
scene.add(mesh);
|
|
171
|
+
host.appendChild(canvas);
|
|
172
|
+
/* ---------------- texture ---------------- */
|
|
173
|
+
let disposed = false;
|
|
174
|
+
const loader = new THREE.TextureLoader();
|
|
175
|
+
let revealTween = null;
|
|
176
|
+
loader.load(textureSrc, (texture) => {
|
|
177
|
+
if (disposed) {
|
|
178
|
+
texture.dispose();
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
texture.minFilter = THREE.LinearFilter;
|
|
182
|
+
texture.magFilter = THREE.LinearFilter;
|
|
183
|
+
// No sRGB decode: the raw shader passes sampled values through, so the
|
|
184
|
+
// texture must stay an ordinary RGBA8 upload (built-in colorSpace
|
|
185
|
+
// conversion only happens inside three's own material chunks).
|
|
186
|
+
uniforms.uTex.value = texture;
|
|
187
|
+
uniforms.uTexSize.value.set(texture.image.width || 1, texture.image.height || 1);
|
|
188
|
+
mesh.visible = true;
|
|
189
|
+
if (img)
|
|
190
|
+
img.style.opacity = "0"; // canvas owns the pixels from here
|
|
191
|
+
if (reveal) {
|
|
192
|
+
revealTween = gsap.fromTo(uniforms.uProgress, { value: 0 }, { value: 1, duration: revealDuration, ease: "power2.inOut" });
|
|
193
|
+
}
|
|
194
|
+
}, undefined, () => {
|
|
195
|
+
/* 404 / decode failure — the plain <img> was never hidden. */
|
|
196
|
+
});
|
|
197
|
+
/* ---------------- hover ---------------- */
|
|
198
|
+
let hoverTween = null;
|
|
199
|
+
const hoverTo = (value) => {
|
|
200
|
+
hoverTween = gsap.to(uniforms.uHover, {
|
|
201
|
+
value,
|
|
202
|
+
duration: hoverDuration,
|
|
203
|
+
ease: "power3.out",
|
|
204
|
+
overwrite: "auto",
|
|
205
|
+
});
|
|
206
|
+
};
|
|
207
|
+
const onEnter = () => hoverTo(1);
|
|
208
|
+
const onLeave = () => hoverTo(0);
|
|
209
|
+
host.addEventListener("pointerenter", onEnter);
|
|
210
|
+
host.addEventListener("pointerleave", onLeave);
|
|
211
|
+
/* ---------------- loop / observers ---------------- */
|
|
212
|
+
let visible = true;
|
|
213
|
+
let io = null;
|
|
214
|
+
if (typeof IntersectionObserver === "function") {
|
|
215
|
+
io = new IntersectionObserver(([entry]) => (visible = entry.isIntersecting), {
|
|
216
|
+
rootMargin: "120px",
|
|
217
|
+
});
|
|
218
|
+
io.observe(host);
|
|
219
|
+
}
|
|
220
|
+
let ro = null;
|
|
221
|
+
const resize = () => {
|
|
222
|
+
const rw = Math.max(1, host.clientWidth);
|
|
223
|
+
const rh = Math.max(1, host.clientHeight);
|
|
224
|
+
renderer.setSize(rw, rh, false);
|
|
225
|
+
camera.left = -rw / 2;
|
|
226
|
+
camera.right = rw / 2;
|
|
227
|
+
camera.top = rh / 2;
|
|
228
|
+
camera.bottom = -rh / 2;
|
|
229
|
+
camera.updateProjectionMatrix();
|
|
230
|
+
mesh.scale.set(rw, rh, 1);
|
|
231
|
+
uniforms.uRes.value.set(rw, rh);
|
|
232
|
+
};
|
|
233
|
+
if (typeof ResizeObserver === "function") {
|
|
234
|
+
ro = new ResizeObserver(resize);
|
|
235
|
+
ro.observe(host);
|
|
236
|
+
}
|
|
237
|
+
const render = () => {
|
|
238
|
+
if (!visible || !mesh.visible)
|
|
239
|
+
return;
|
|
240
|
+
uniforms.uTime.value = gsap.ticker.time;
|
|
241
|
+
renderer.render(scene, camera);
|
|
242
|
+
};
|
|
243
|
+
gsap.ticker.add(render);
|
|
244
|
+
/* ---------------- teardown ---------------- */
|
|
245
|
+
return () => {
|
|
246
|
+
disposed = true;
|
|
247
|
+
gsap.ticker.remove(render);
|
|
248
|
+
hoverTween?.kill();
|
|
249
|
+
revealTween?.kill();
|
|
250
|
+
gsap.killTweensOf(uniforms.uHover);
|
|
251
|
+
gsap.killTweensOf(uniforms.uProgress);
|
|
252
|
+
host.removeEventListener("pointerenter", onEnter);
|
|
253
|
+
host.removeEventListener("pointerleave", onLeave);
|
|
254
|
+
io?.disconnect();
|
|
255
|
+
ro?.disconnect();
|
|
256
|
+
uniforms.uTex.value?.dispose();
|
|
257
|
+
geometry.dispose();
|
|
258
|
+
material.dispose();
|
|
259
|
+
renderer.dispose();
|
|
260
|
+
canvas.remove();
|
|
261
|
+
delete host.dataset.akGl;
|
|
262
|
+
host.style.position = prevPosition;
|
|
263
|
+
if (img)
|
|
264
|
+
img.style.opacity = prevImgOpacity;
|
|
265
|
+
};
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
//# sourceMappingURL=webgl-media.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"webgl-media.js","sourceRoot":"","sources":["../../src/three/webgl-media.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAC/B,OAAO,EAAE,IAAI,EAAE,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E,CAAC;YACpB,IAAI,CAAC,YAAY,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;YACnC,IAAI,CAAC,YAAY,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC;YACtC,IAAI,CAAC,mBAAmB,CAAC,cAAc,EAAE,OAAO,CAAC,CAAC;YAClD,IAAI,CAAC,mBAAmB,CAAC,cAAc,EAAE,OAAO,CAAC,CAAC;YAClD,EAAE,EAAE,UAAU,EAAE,CAAC;YACjB,EAAE,EAAE,UAAU,EAAE,CAAC;YACjB,QAAQ,CAAC,IAAI,CAAC,KAAK,EAAE,OAAO,EAAE,CAAC;YAC/B,QAAQ,CAAC,OAAO,EAAE,CAAC;YACnB,QAAQ,CAAC,OAAO,EAAE,CAAC;YACnB,QAAQ,CAAC,OAAO,EAAE,CAAC;YACnB,MAAM,CAAC,MAAM,EAAE,CAAC;YAChB,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC;YACzB,IAAI,CAAC,KAAK,CAAC,QAAQ,GAAG,YAAY,CAAC;YACnC,IAAI,GAAG;gBAAE,GAAG,CAAC,KAAK,CAAC,OAAO,GAAG,cAAc,CAAC;QAC9C,CAAC,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cosmictraveler002/anim-kit",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "Modular, framework-agnostic animation library — GSAP + ScrollTrigger + Lenis effects packaged as composable ES modules.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -17,6 +17,11 @@
|
|
|
17
17
|
"import": "./dist/anim-kit.standalone.js",
|
|
18
18
|
"default": "./dist/anim-kit.standalone.js"
|
|
19
19
|
},
|
|
20
|
+
"./three": {
|
|
21
|
+
"types": "./dist/three/index.d.ts",
|
|
22
|
+
"import": "./dist/three/index.js",
|
|
23
|
+
"default": "./dist/three/index.js"
|
|
24
|
+
},
|
|
20
25
|
"./effects/*": {
|
|
21
26
|
"types": "./dist/effects/*.d.ts",
|
|
22
27
|
"import": "./dist/effects/*.js",
|
|
@@ -42,6 +47,7 @@
|
|
|
42
47
|
"typecheck": "tsc --noEmit -p tsconfig.json",
|
|
43
48
|
"dev": "tsc -p tsconfig.json --watch",
|
|
44
49
|
"demo": "node scripts/serve.mjs",
|
|
50
|
+
"sync:live": "node scripts/sync-demo-live.mjs",
|
|
45
51
|
"test": "npm run build && node scripts/smoke.mjs && node scripts/demo-smoke.mjs",
|
|
46
52
|
"smoke": "node scripts/smoke.mjs && node scripts/demo-smoke.mjs",
|
|
47
53
|
"prepublishOnly": "npm run build && npm run smoke"
|
|
@@ -75,8 +81,18 @@
|
|
|
75
81
|
"gsap": "^3.13.0",
|
|
76
82
|
"lenis": "^1.3.11"
|
|
77
83
|
},
|
|
84
|
+
"peerDependencies": {
|
|
85
|
+
"three": ">=0.160.0"
|
|
86
|
+
},
|
|
87
|
+
"peerDependenciesMeta": {
|
|
88
|
+
"three": {
|
|
89
|
+
"optional": true
|
|
90
|
+
}
|
|
91
|
+
},
|
|
78
92
|
"devDependencies": {
|
|
93
|
+
"@types/three": "^0.186.0",
|
|
79
94
|
"jsdom": "^25.0.1",
|
|
95
|
+
"three": "^0.186.1",
|
|
80
96
|
"tsup": "^8.5.1",
|
|
81
97
|
"typescript": "^5.7.3"
|
|
82
98
|
}
|
package/src/core/gsap.ts
CHANGED
|
@@ -16,7 +16,7 @@ import type { TargetLike } from "./types.js";
|
|
|
16
16
|
|
|
17
17
|
export { gsap, ScrollTrigger, SplitText, Draggable, CustomEase, ScrollSmoother, Flip };
|
|
18
18
|
|
|
19
|
-
/** Named eases
|
|
19
|
+
/** Named eases from kalakritico.in's bundles. */
|
|
20
20
|
export const EASES = {
|
|
21
21
|
/** `.76,0,.2,1` — the studio's signature menu / nav curtain ease. */
|
|
22
22
|
curtain: "ak-curtain",
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
* clocks on the same frame, which is what stops ScrollTrigger from stuttering.
|
|
11
11
|
*
|
|
12
12
|
* `useScrollerProxy: true` additionally proxies `document.documentElement`,
|
|
13
|
-
* matching
|
|
13
|
+
* matching kalakritico.in's setup — only needed if you scroll a nested
|
|
14
14
|
* element instead of the window.
|
|
15
15
|
*/
|
|
16
16
|
import Lenis from "lenis";
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Drag rail — the bounded, inertia-driven horizontal rail.
|
|
3
|
+
*
|
|
4
|
+
* A grab-and-throw rail with real physics, extracted from the drag-driven
|
|
5
|
+
* portfolio rail pattern: pointer drag, wheel and trackpad all feed one
|
|
6
|
+
* *intent* value, and the track follows it with a per-frame lerp. Why it is
|
|
7
|
+
* shaped this way (audit notes):
|
|
8
|
+
*
|
|
9
|
+
* - **Two values, not one.** `intent` is where the rail wants to be (drag
|
|
10
|
+
* delta, wheel delta, throw momentum); `pos` is what is rendered. The tick
|
|
11
|
+
* lerps intent → pos (`lerp * deltaRatio`), which is what gives the
|
|
12
|
+
* buttery catch-up after a hard throw. Writing the transform directly from
|
|
13
|
+
* pointermove (v1) felt like dragging a brick.
|
|
14
|
+
* - **tanh rubber-band.** Past either end, intent is squashed through
|
|
15
|
+
* `edge * tanh(overshoot / edge)` — the further you pull, the less each
|
|
16
|
+
* pixel buys you, but it never hard-stops. On release a restore force pulls
|
|
17
|
+
* the overscrolled intent back inside the bounds while the lerp chases it,
|
|
18
|
+
* producing the settle. This is the same soft-edge maths as the original
|
|
19
|
+
* rail's `a.t = a.a + tanh(l / t) * t`.
|
|
20
|
+
* - **Release throw.** Velocity is sampled as a smoothed per-frame pointer
|
|
21
|
+
* delta (0.7 smoothing) and multiplied by `throwScale` into intent — a flick
|
|
22
|
+
* coasts, a slow release barely moves. No InertiaPlugin dependency; the
|
|
23
|
+
* ticker *is* the inertia.
|
|
24
|
+
* - **Wheel coexistence.** While the rail can still move in the wheel's
|
|
25
|
+
* direction the event is consumed (`preventDefault` + `stopPropagation` —
|
|
26
|
+
* the stop matters: Lenis listens above us and would scroll the page in
|
|
27
|
+
* parallel). Once overscrolled a full `edge`, the wheel passes through to
|
|
28
|
+
* the page again, so the section never traps the reader.
|
|
29
|
+
* - **Velocity-reactive items.** `tilt` rotates children proportionally to the
|
|
30
|
+
* rendered per-frame velocity (the cards lean into motion and spring flat at
|
|
31
|
+
* rest); `onTick` exposes pos/velocity for WebGL or custom consumers.
|
|
32
|
+
* - **Fidelity.** `destroy()` removes every listener + the ticker, kills the
|
|
33
|
+
* tilt state and restores the inline cursor/user-select/touch-action the
|
|
34
|
+
* effect had overwritten.
|
|
35
|
+
*
|
|
36
|
+
* dragRail(".ak-rail-track", { tilt: 0.05 })
|
|
37
|
+
*
|
|
38
|
+
* CSS: viewport `overflow: hidden`; track `display: flex; width: max-content;
|
|
39
|
+
* gap: 1rem; cursor: grab; user-select: none; touch-action: pan-y`.
|
|
40
|
+
*/
|
|
41
|
+
import { gsap, initGSAP } from "../core/gsap.js";
|
|
42
|
+
import { guard } from "../core/guard.js";
|
|
43
|
+
import { one, toArray } from "../core/util.js";
|
|
44
|
+
import type { CommonOptions, Destroy, TargetLike } from "../core/types.js";
|
|
45
|
+
|
|
46
|
+
export interface DragRailOptions extends CommonOptions {
|
|
47
|
+
/** Scroll viewport around the track — defaults to `track.parentElement`. */
|
|
48
|
+
viewport?: TargetLike;
|
|
49
|
+
/** Children that tilt with velocity. @default `":scope > *"` */
|
|
50
|
+
item?: string;
|
|
51
|
+
/** Follow speed toward the intent, per 60fps frame (0..1). @default 0.1 */
|
|
52
|
+
lerp?: number;
|
|
53
|
+
/** Rubber-band resistance distance past the ends, px. @default 140 */
|
|
54
|
+
edge?: number;
|
|
55
|
+
/** Momentum multiplier on release (px of coast per px/frame of flick). @default 14 */
|
|
56
|
+
throwScale?: number;
|
|
57
|
+
/** Wheel / trackpad drives the rail (consumed while it can still move). @default true */
|
|
58
|
+
wheel?: boolean;
|
|
59
|
+
/** Degrees of tilt per px/frame of velocity — `0` disables. @default 0 */
|
|
60
|
+
tilt?: number;
|
|
61
|
+
/** Tilt clamp, degrees. @default 8 */
|
|
62
|
+
tiltMax?: number;
|
|
63
|
+
/** `(pos, velocity) => {}` called every rendered frame while mounted. */
|
|
64
|
+
onTick?: (pos: number, velocity: number) => void;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function dragRail(target: TargetLike, options: DragRailOptions = {}): Destroy {
|
|
68
|
+
initGSAP();
|
|
69
|
+
|
|
70
|
+
const track = one<HTMLElement>(target);
|
|
71
|
+
if (!track) return () => {};
|
|
72
|
+
|
|
73
|
+
const {
|
|
74
|
+
viewport,
|
|
75
|
+
item = ":scope > *",
|
|
76
|
+
lerp = 0.1,
|
|
77
|
+
edge = 140,
|
|
78
|
+
throwScale = 14,
|
|
79
|
+
wheel = true,
|
|
80
|
+
tilt = 0,
|
|
81
|
+
tiltMax = 8,
|
|
82
|
+
onTick,
|
|
83
|
+
} = options;
|
|
84
|
+
|
|
85
|
+
return guard(options, () => {
|
|
86
|
+
const vp = one<HTMLElement>(viewport) ?? track.parentElement ?? track;
|
|
87
|
+
const prevStyle = {
|
|
88
|
+
cursor: track.style.cursor,
|
|
89
|
+
userSelect: track.style.userSelect,
|
|
90
|
+
touchAction: track.style.touchAction,
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
/* ---------------- bounds ---------------- */
|
|
94
|
+
|
|
95
|
+
let min = 0; // most-negative resting x (content overflows left)
|
|
96
|
+
let max = 0; // start of the rail
|
|
97
|
+
let pos = 0; // rendered x
|
|
98
|
+
let intent = 0; // wanted x (drag/wheel/throw feed this)
|
|
99
|
+
|
|
100
|
+
const measure = () => {
|
|
101
|
+
const overflow = Math.max(0, track.scrollWidth - vp.clientWidth);
|
|
102
|
+
min = -overflow;
|
|
103
|
+
max = 0;
|
|
104
|
+
// Fold an out-of-range position back through the soft edge (no jump).
|
|
105
|
+
intent = soft(intent);
|
|
106
|
+
pos = soft(pos);
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
/** tanh soft-edge: unbounded input, saturating output around [min, max]. */
|
|
110
|
+
function soft(v: number): number {
|
|
111
|
+
if (v > max) return max + edge * Math.tanh((v - max) / edge);
|
|
112
|
+
if (v < min) return min + edge * Math.tanh((v - min) / edge);
|
|
113
|
+
return v;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/* ---------------- pointer drag ---------------- */
|
|
117
|
+
|
|
118
|
+
let dragging = false;
|
|
119
|
+
let lastX = 0;
|
|
120
|
+
let velocity = 0; // smoothed pointer delta (px/frame-ish)
|
|
121
|
+
let renderedVel = 0; // rendered per-frame velocity (drives tilt/onTick)
|
|
122
|
+
|
|
123
|
+
const onPointerDown = (e: PointerEvent) => {
|
|
124
|
+
if (e.pointerType === "mouse" && e.button !== 0) return;
|
|
125
|
+
dragging = true;
|
|
126
|
+
lastX = e.clientX;
|
|
127
|
+
velocity = 0;
|
|
128
|
+
try {
|
|
129
|
+
vp.setPointerCapture(e.pointerId);
|
|
130
|
+
} catch {
|
|
131
|
+
/* capture is best-effort — moves still land while the pointer is down */
|
|
132
|
+
}
|
|
133
|
+
track.style.cursor = "grabbing";
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
const onPointerMove = (e: PointerEvent) => {
|
|
137
|
+
if (!dragging) return;
|
|
138
|
+
const dx = e.clientX - lastX;
|
|
139
|
+
lastX = e.clientX;
|
|
140
|
+
velocity = velocity * 0.7 + dx * 0.3; // smooth out event-rate jitter
|
|
141
|
+
intent = soft(intent + dx);
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
const endDrag = (e?: PointerEvent) => {
|
|
145
|
+
if (!dragging) return;
|
|
146
|
+
dragging = false;
|
|
147
|
+
track.style.cursor = prevStyle.cursor || "grab";
|
|
148
|
+
if (e) {
|
|
149
|
+
try {
|
|
150
|
+
vp.releasePointerCapture(e.pointerId);
|
|
151
|
+
} catch {
|
|
152
|
+
/* already released */
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
// A flick coasts on its sampled momentum, then the soft edge reclaims it.
|
|
156
|
+
intent = soft(intent + velocity * throwScale);
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
const onPointerUp = (e: PointerEvent) => endDrag(e);
|
|
160
|
+
const onPointerCancel = (e: PointerEvent) => endDrag(e);
|
|
161
|
+
const onDragStart = (e: Event) => e.preventDefault(); // no native img drag
|
|
162
|
+
|
|
163
|
+
/* ---------------- wheel / trackpad ---------------- */
|
|
164
|
+
|
|
165
|
+
const onWheel = (e: WheelEvent) => {
|
|
166
|
+
if (!wheel) return;
|
|
167
|
+
const delta = Math.abs(e.deltaX) > Math.abs(e.deltaY) ? e.deltaX : e.deltaY;
|
|
168
|
+
const bound = delta > 0 ? max : min;
|
|
169
|
+
const outward = (intent - bound) * (delta > 0 ? 1 : -1);
|
|
170
|
+
if (outward > edge) return; // over-extended — hand the scroll back to the page
|
|
171
|
+
e.preventDefault();
|
|
172
|
+
e.stopPropagation(); // Lenis listens above us; it must not scroll in parallel
|
|
173
|
+
intent = soft(intent + delta);
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
/* ---------------- tick ---------------- */
|
|
177
|
+
|
|
178
|
+
const items = tilt ? toArray<HTMLElement>(item, track) : [];
|
|
179
|
+
if (items.length) gsap.set(items, { transformOrigin: "50% 50%" });
|
|
180
|
+
|
|
181
|
+
const tick = () => {
|
|
182
|
+
const ratio = gsap.ticker.deltaRatio(60); // frames since last tick, clamped
|
|
183
|
+
// Release restore: once input stops, overscrolled intent springs home.
|
|
184
|
+
if (!dragging) {
|
|
185
|
+
const bound = intent < min ? min : intent > max ? max : null;
|
|
186
|
+
if (bound !== null) intent += (bound - intent) * Math.min(1, 0.18 * ratio);
|
|
187
|
+
}
|
|
188
|
+
const prev = pos;
|
|
189
|
+
pos += (intent - pos) * Math.min(1, lerp * ratio);
|
|
190
|
+
renderedVel = pos - prev;
|
|
191
|
+
if (renderedVel === 0 && pos === intent) return; // idle: skip the write
|
|
192
|
+
gsap.set(track, { x: pos });
|
|
193
|
+
if (items.length) {
|
|
194
|
+
gsap.set(items, {
|
|
195
|
+
rotation: gsap.utils.clamp(-tiltMax, tiltMax, renderedVel * tilt),
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
onTick?.(pos, renderedVel);
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
/* ---------------- wiring ---------------- */
|
|
202
|
+
|
|
203
|
+
measure();
|
|
204
|
+
track.dataset.akRail = "true";
|
|
205
|
+
track.style.cursor = "grab";
|
|
206
|
+
track.style.userSelect = "none";
|
|
207
|
+
track.style.touchAction = "pan-y";
|
|
208
|
+
|
|
209
|
+
vp.addEventListener("pointerdown", onPointerDown);
|
|
210
|
+
vp.addEventListener("pointermove", onPointerMove);
|
|
211
|
+
vp.addEventListener("pointerup", onPointerUp);
|
|
212
|
+
vp.addEventListener("pointercancel", onPointerCancel);
|
|
213
|
+
vp.addEventListener("wheel", onWheel, { passive: false });
|
|
214
|
+
track.addEventListener("dragstart", onDragStart);
|
|
215
|
+
const onResize = () => measure();
|
|
216
|
+
window.addEventListener("resize", onResize);
|
|
217
|
+
gsap.ticker.add(tick);
|
|
218
|
+
|
|
219
|
+
return () => {
|
|
220
|
+
window.removeEventListener("resize", onResize);
|
|
221
|
+
vp.removeEventListener("pointerdown", onPointerDown);
|
|
222
|
+
vp.removeEventListener("pointermove", onPointerMove);
|
|
223
|
+
vp.removeEventListener("pointerup", onPointerUp);
|
|
224
|
+
vp.removeEventListener("pointercancel", onPointerCancel);
|
|
225
|
+
vp.removeEventListener("wheel", onWheel);
|
|
226
|
+
track.removeEventListener("dragstart", onDragStart);
|
|
227
|
+
gsap.ticker.remove(tick);
|
|
228
|
+
delete track.dataset.akRail;
|
|
229
|
+
track.style.cursor = prevStyle.cursor;
|
|
230
|
+
track.style.userSelect = prevStyle.userSelect;
|
|
231
|
+
track.style.touchAction = prevStyle.touchAction;
|
|
232
|
+
gsap.set(track, { clearProps: "transform" });
|
|
233
|
+
if (items.length) gsap.set(items, { clearProps: "rotation,transformOrigin" });
|
|
234
|
+
};
|
|
235
|
+
});
|
|
236
|
+
}
|
package/src/effects/preloader.ts
CHANGED