@ivanalbizu/astro-webgl-hover 0.0.6 → 0.0.8

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ivanalbizu/astro-webgl-hover",
3
- "version": "0.0.6",
3
+ "version": "0.0.8",
4
4
  "type": "module",
5
5
  "description": "WebGL image hover effects for Astro with displacement transitions using Curtains.js and GSAP",
6
6
  "author": "Ivan Albizu",
@@ -39,15 +39,15 @@
39
39
  "preview": "astro preview",
40
40
  "astro": "astro"
41
41
  },
42
- "peerDependencies": {
43
- "astro": "^4.0.0 || ^5.0.0",
44
- "curtainsjs": "^8.0.0",
45
- "gsap": "^3.0.0"
46
- },
47
- "devDependencies": {
48
- "astro": "^5.16.9",
42
+ "dependencies": {
49
43
  "curtainsjs": "^8.1.6",
50
44
  "gsap": "^3.12.7",
51
45
  "lil-gui": "^0.19.2"
46
+ },
47
+ "peerDependencies": {
48
+ "astro": "^4.0.0 || ^5.0.0"
49
+ },
50
+ "devDependencies": {
51
+ "astro": "^5.16.9"
52
52
  }
53
53
  }
@@ -51,8 +51,21 @@ const {
51
51
  <slot />
52
52
 
53
53
  <script>
54
- import { initWebglHover } from '../lib/webgl-hover';
54
+ import { initWebglHover, destroyWebglHover } from '../lib/webgl-hover';
55
+
56
+ // Initial load
55
57
  initWebglHover();
58
+
59
+ // Re-initialize after View Transitions navigation
60
+ document.addEventListener('astro:page-load', () => {
61
+ destroyWebglHover();
62
+ initWebglHover();
63
+ });
64
+
65
+ // Cleanup before navigation
66
+ document.addEventListener('astro:before-swap', () => {
67
+ destroyWebglHover();
68
+ });
56
69
  </script>
57
70
 
58
71
  <style is:global>
@@ -19,6 +19,10 @@ export * from './config';
19
19
  export * from './performance';
20
20
  export * from './utils';
21
21
 
22
+ // Store references for cleanup
23
+ let currentInstances: WebglHover[] = [];
24
+ let currentCurtains: any = null;
25
+
22
26
  function getConfigFromDOM(): WebglHoverConfig {
23
27
  const configEl = document.getElementById('webgl-hover-config');
24
28
 
@@ -250,9 +254,31 @@ function initDebugMode(instances: WebglHover[], config: WebglHoverConfig): void
250
254
  });
251
255
  }
252
256
 
257
+ export function destroyWebglHover(): void {
258
+ // Destroy all instances
259
+ currentInstances.forEach((instance) => instance.destroy());
260
+ currentInstances = [];
261
+
262
+ // Dispose Curtains
263
+ if (currentCurtains) {
264
+ currentCurtains.dispose();
265
+ currentCurtains = null;
266
+ }
267
+
268
+ // Remove canvas container
269
+ const container = document.getElementById('canvas-container');
270
+ if (container) {
271
+ container.remove();
272
+ }
273
+ }
274
+
253
275
  export function initWebglHover(): WebglHover[] {
254
276
  const planes = document.querySelectorAll('.whi-plane');
255
277
 
278
+ if (!planes.length) {
279
+ return [];
280
+ }
281
+
256
282
  if (shouldUseFallback()) {
257
283
  applyFallbackStyles(planes);
258
284
  return [];
@@ -261,19 +287,19 @@ export function initWebglHover(): WebglHover[] {
261
287
  const config = getConfigFromDOM();
262
288
  const container = createCanvasContainer();
263
289
 
264
- const webGLCurtain = new Curtains({
290
+ currentCurtains = new Curtains({
265
291
  container,
266
292
  pixelRatio: Math.min(1.5, window.devicePixelRatio),
267
293
  });
268
294
 
269
- const instances: WebglHover[] = [];
295
+ currentInstances = [];
270
296
 
271
297
  document.querySelectorAll('.whi-slide').forEach((slide) => {
272
298
  const planeElement = slide.querySelector('.whi-plane') as HTMLElement;
273
299
 
274
300
  if (planeElement) {
275
301
  const options = getOptionsFromSlide(slide, config);
276
- instances.push(new WebglHover(webGLCurtain, planeElement, options));
302
+ currentInstances.push(new WebglHover(currentCurtains, planeElement, options));
277
303
  }
278
304
  });
279
305
 
@@ -281,13 +307,13 @@ export function initWebglHover(): WebglHover[] {
281
307
  window.addEventListener('resize', () => {
282
308
  clearTimeout(resizeTimeout);
283
309
  resizeTimeout = setTimeout(() => {
284
- instances.forEach((instance) => instance.resize());
310
+ currentInstances.forEach((instance) => instance.resize());
285
311
  }, 150);
286
312
  });
287
313
 
288
314
  if (config.debug) {
289
- initDebugMode(instances, config);
315
+ initDebugMode(currentInstances, config);
290
316
  }
291
317
 
292
- return instances;
318
+ return currentInstances;
293
319
  }