JavaScript animation engine
Use container instead of target to calculate scale, otherwise calling . clientHeight on the target will return null on SVG elements
Julian Garnier8fac68d8d9ae
diff --git a/dist/modules/events/scroll.cjs b/dist/modules/events/scroll.cjsindex 7af25e53..e212969b 100644--- a/dist/modules/events/scroll.cjs+++ b/dist/modules/events/scroll.cjs@@ -95,6 +95,8 @@ class ScrollContainer {/** @type {Number} */this.top = 0;/** @type {Number} */+ this.scale = 1;+ /** @type {Number} */this.zIndex = 0;/** @type {Number} */this.scrollX = 0;@@ -206,6 +208,7 @@ class ScrollContainer {height = $el.clientHeight;this.top = elRect.top;this.left = elRect.left;+ this.scale = elRect.width ? width / elRect.width : (elRect.height ? height / elRect.height : 1);}this.width = width;this.height = height;@@ -731,10 +734,9 @@ class ScrollObserver {}}const rect = $target.getBoundingClientRect();- const offsetUnscaled = isHori ? rect.left + container.scrollX - container.left : rect.top + container.scrollY - container.top;- const scale = rect.width ? $target.clientWidth / rect.width : (rect.height ? $target.clientHeight / rect.height : 1);- const offset = offsetUnscaled * scale;- const targetSize = isHori ? rect.width * scale : rect.height * scale;+ const scale = container.scale;+ const offset = (isHori ? rect.left + container.scrollX - container.left : rect.top + container.scrollY - container.top) * scale;+ const targetSize = (isHori ? rect.width : rect.height) * scale;const containerSize = isHori ? container.width : container.height;const scrollSize = isHori ? container.scrollWidth : container.scrollHeight;const maxScroll = scrollSize - containerSize;diff --git a/dist/modules/events/scroll.d.ts b/dist/modules/events/scroll.d.tsindex 14f0261c..2545f4b7 100644--- a/dist/modules/events/scroll.d.ts+++ b/dist/modules/events/scroll.d.ts@@ -134,6 +134,8 @@ declare class ScrollContainer {/** @type {Number} */top: number;/** @type {Number} */+ scale: number;+ /** @type {Number} */zIndex: number;/** @type {Number} */scrollX: number;diff --git a/dist/modules/events/scroll.js b/dist/modules/events/scroll.jsindex f7472ee1..f2459c4b 100644--- a/dist/modules/events/scroll.js+++ b/dist/modules/events/scroll.js@@ -93,6 +93,8 @@ class ScrollContainer {/** @type {Number} */this.top = 0;/** @type {Number} */+ this.scale = 1;+ /** @type {Number} */this.zIndex = 0;/** @type {Number} */this.scrollX = 0;@@ -204,6 +206,7 @@ class ScrollContainer {height = $el.clientHeight;this.top = elRect.top;this.left = elRect.left;+ this.scale = elRect.width ? width / elRect.width : (elRect.height ? height / elRect.height : 1);}this.width = width;this.height = height;@@ -729,10 +732,9 @@ class ScrollObserver {}}const rect = $target.getBoundingClientRect();- const offsetUnscaled = isHori ? rect.left + container.scrollX - container.left : rect.top + container.scrollY - container.top;- const scale = rect.width ? $target.clientWidth / rect.width : (rect.height ? $target.clientHeight / rect.height : 1);- const offset = offsetUnscaled * scale;- const targetSize = isHori ? rect.width * scale : rect.height * scale;+ const scale = container.scale;+ const offset = (isHori ? rect.left + container.scrollX - container.left : rect.top + container.scrollY - container.top) * scale;+ const targetSize = (isHori ? rect.width : rect.height) * scale;const containerSize = isHori ? container.width : container.height;const scrollSize = isHori ? container.scrollWidth : container.scrollHeight;const maxScroll = scrollSize - containerSize;diff --git a/src/events/scroll.js b/src/events/scroll.jsindex d61cc602..951a00bf 100644--- a/src/events/scroll.js+++ b/src/events/scroll.js@@ -137,6 +137,8 @@ class ScrollContainer {/** @type {Number} */this.top = 0;/** @type {Number} */+ this.scale = 1;+ /** @type {Number} */this.zIndex = 0;/** @type {Number} */this.scrollX = 0;@@ -248,6 +250,7 @@ class ScrollContainer {height = $el.clientHeight;this.top = elRect.top;this.left = elRect.left;+ this.scale = elRect.width ? width / elRect.width : (elRect.height ? height / elRect.height : 1);}this.width = width;this.height = height;@@ -773,10 +776,9 @@ export class ScrollObserver {}}const rect = $target.getBoundingClientRect();- const offsetUnscaled = isHori ? rect.left + container.scrollX - container.left : rect.top + container.scrollY - container.top;- const scale = rect.width ? $target.clientWidth / rect.width : (rect.height ? $target.clientHeight / rect.height : 1);- const offset = offsetUnscaled * scale;- const targetSize = isHori ? rect.width * scale : rect.height * scale;+ const scale = container.scale;+ const offset = (isHori ? rect.left + container.scrollX - container.left : rect.top + container.scrollY - container.top) * scale;+ const targetSize = (isHori ? rect.width : rect.height) * scale;const containerSize = isHori ? container.width : container.height;const scrollSize = isHori ? container.scrollWidth : container.scrollHeight;const maxScroll = scrollSize - containerSize;diff --git a/tests/playground/onscroll/debug.html b/tests/playground/onscroll/debug.htmlindex 33c5625a..2313196a 100644--- a/tests/playground/onscroll/debug.html+++ b/tests/playground/onscroll/debug.html@@ -10,6 +10,7 @@.containers {display: flex;flex-wrap: wrap;+ scale: .8;}.container-wrapper {margin: 1rem;diff --git a/tests/playground/sandbox/index.html b/tests/playground/sandbox/index.htmlindex 406f9b60..3a62ee88 100644--- a/tests/playground/sandbox/index.html+++ b/tests/playground/sandbox/index.html@@ -6,67 +6,46 @@<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"><link href="../assets/css/styles.css" rel="stylesheet"><style>- body {- display: flex;- flex-direction: row;- flex-wrap: wrap;- justify-content: center;- align-items: center;- height: 100vh;- --test-color: #00FF00;- --x: 5rem;- --y: 20rem;- }+body {+ display: flex;+ flex-direction: column;+ justify-content: center;+ align-items: center;+ width: 100%;+ height: 500px;+}- .container {- display: flex;- flex-wrap: wrap;- justify-content: center;- align-items: center;- flex-direction: column;- gap: 1rem;- padding: 1rem;- }+main {+ height: 400px;+ width: 400px;+ background-color: wheat;+ overflow-y: auto;+ scale: .8;+}- .square {- width: 100px;- height: 100px;- background: currentColor;- border-radius: 1rem;- padding: 1rem;- }+.page {+ width: 100%;+ height: 1000px;+ position: relative;+}++.rectangle {+ width: 100px;+ height: 100px;+ background: purple;+ position: absolute;+ bottom: 100px;+}- .button {- border-radius: 1rem;- padding: 1rem;- }- .demo .css-variables.square:after {- position: absolute;- opacity: .5;- top: 0;- left: 0;- content: "";- display: block;- width: 100%;- height: 100%;- background: currentColor;- border-radius: inherit;- transform: scale(var(--pseudo-el-after-scale));- }</style></head><body>- <div class="demo">- <div class="medium row">- <div class="css-variables square"></div>- </div>- <div class="medium row">- <div class="css-variables square"></div>- </div>- <div class="medium row">- <div class="css-variables square"></div>+ <main>+ <div class="page">+ <div class="rectangle" id="target">+ </div></div>- </div>+ </main><script type="module" src="./index.js"></script></body></html>diff --git a/tests/playground/sandbox/index.js b/tests/playground/sandbox/index.jsindex 7f11eb24..f102423b 100644--- a/tests/playground/sandbox/index.js+++ b/tests/playground/sandbox/index.js@@ -1,46 +1,14 @@-import { waapi, animate, $, set, stagger, spring, random, utils } from '../../../dist/modules/index.js';--// const targets = $('.green');--// const [$red] = $('.red');--// set(targets, { y: () => random(-200, 200), x: () => random(-200, 200) });--// set(targets, { backgroundColor: '#FF0000' });--// let reverse = false;--// const animation = animate(targets, {-// x: 'var(--x)',-// y: 'var(--y)',-// backgroundColor: 'var(--test-color, #FF00FF)',-// borderRadius: '40%',-// rotate: 90,-// scale: 1.25,-// delay: stagger(100),-// loop: true,-// alternate: true,-// onComplete: () => {-// console.log('completed');-// }-// });--// document.body.addEventListener('click', () => {-// animation.revert();-// });--// Set the CSS variables as properties on the animated elements-utils.set('.square', {- '--radius': '4px',- '--x': '0rem',- '--pseudo-el-after-scale': '1', // applied to the pseudo element "::after"- borderRadius: 'var(--radius)',- translateX: 'var(--x)',+import { waapi, animate, onScroll, $, set, stagger, spring, random, utils } from '../../../dist/modules/index.js';++const container = document.querySelector("main");+animate(".rectangle", {+ x: "70px",+ ease: "inOutCirc",+ delay: 1000,+ loopDelay: 1000,+ loop: true,+ autoplay: onScroll({+ container,+ debug: true+ })});--// Animate the values of the CSS variables-animate('.square', {- '--radius': '20px',- '--x': '16.5rem',- '--pseudo-el-after-scale': '1.55' // Animates the ":after" pseudo element of the element-});\ No newline at end of file