Skip to content

armgit / anime

Public Imported from GitHub
0

JavaScript animation engine

Fix onEnter called even if the target is outside the viewport if an other onScroll instance is already running

Julian Garnierf53be7add20b
diff --git a/lib/anime.esm.js b/lib/anime.esm.js
index f257c7f3..7dcc0e50 100644
--- a/lib/anime.esm.js
+++ b/lib/anime.esm.js
@@ -1,6 +1,6 @@
/**
* anime.js - ESM
- * @version v4.1.3
+ * @version v4.1.4
* @author Julian Garnier
* @license MIT
* @copyright (c) 2025 Julian Garnier
@@ -760,7 +760,7 @@ const globals = {
tickThreshold: 200,
};
-const globalVersions = { version: '4.1.3', engine: null };
+const globalVersions = { version: '4.1.4', engine: null };
if (isBrowser) {
if (!win.AnimeJS) win.AnimeJS = [];
@@ -901,7 +901,7 @@ const shuffle = items => {
* @param {Number} v
* @return {Number}
*/
-const clampInfinity = v => v === Infinity ? maxValue : v === -Infinity ? -1e12 : v;
+const clampInfinity = v => v === Infinity ? maxValue : v === -Infinity ? -maxValue : v;
/**
* @param {Number} v
@@ -1908,7 +1908,7 @@ const createDrawableProxy = ($el, start, end) => {
// const v1 = round(+value.slice(0, spaceIndex), precision);
// const v2 = round(+value.slice(spaceIndex + 1), precision);
const scaleFactor = getScaleFactor($scalled);
- const os = v1 * -1e3 * scaleFactor;
+ const os = v1 * -pathLength * scaleFactor;
const d1 = (v2 * pathLength * scaleFactor) + os;
const d2 = (pathLength * scaleFactor +
((v1 === 0 && v2 === 1) || (v1 === 1 && v2 === 0) ? 0 : 10 * scaleFactor) - d1);
@@ -5441,7 +5441,7 @@ class Spring {
this.m = clamp(setValue(parameters.mass, 1), 0, maxSpringParamValue);
this.s = clamp(setValue(parameters.stiffness, 100), 1, maxSpringParamValue);
this.d = clamp(setValue(parameters.damping, 10), .1, maxSpringParamValue);
- this.v = clamp(setValue(parameters.velocity, 0), -1e4, maxSpringParamValue);
+ this.v = clamp(setValue(parameters.velocity, 0), -maxSpringParamValue, maxSpringParamValue);
this.w0 = 0;
this.zeta = 0;
this.wd = 0;
@@ -5519,7 +5519,7 @@ class Spring {
}
set velocity(v) {
- this.v = clamp(setValue(v, 0), -1e4, maxSpringParamValue);
+ this.v = clamp(setValue(v, 0), -maxSpringParamValue, maxSpringParamValue);
this.compute();
}
}
@@ -5789,7 +5789,7 @@ class Draggable {
/** @type {[Number, Number, Number, Number]} */
this.dragArea = [0, 0, 0, 0]; // x, y, w, h
/** @type {[Number, Number, Number, Number]} */
- this.containerBounds = [-1e12, maxValue, maxValue, -1e12]; // t, r, b, l
+ this.containerBounds = [-maxValue, maxValue, maxValue, -maxValue]; // t, r, b, l
/** @type {[Number, Number, Number, Number]} */
this.scrollBounds = [0, 0, 0, 0]; // t, r, b, l
/** @type {[Number, Number, Number, Number]} */
@@ -6252,10 +6252,10 @@ class Draggable {
const canScroll = this.canScroll;
if (!this.containerArray && this.isOutOfBounds(scrollBounds, x, y)) {
const [ st, sr, sb, sl ] = scrollBounds;
- const t = round(clamp(y - st, -1e12, 0), 0);
+ const t = round(clamp(y - st, -maxValue, 0), 0);
const r = round(clamp(x - sr, 0, maxValue), 0);
const b = round(clamp(y - sb, 0, maxValue), 0);
- const l = round(clamp(x - sl, -1e12, 0), 0);
+ const l = round(clamp(x - sl, -maxValue, 0), 0);
new JSAnimation(scroll, {
x: round(scroll.x + (l ? l - gap : r ? r + gap : 0), 0),
y: round(scroll.y + (t ? t - gap : b ? b + gap : 0), 0),
@@ -7328,6 +7328,8 @@ class ScrollObserver {
this.forceEnter = false;
/** @type {Boolean} */
this.hasEntered = false;
+ /** @type {Boolean} */
+ this.isReady = false;
// /** @type {Array.<Number>} */
// this.offsets = [];
/** @type {Number} */
@@ -7416,6 +7418,8 @@ class ScrollObserver {
}
refresh() {
+ // This flag is used to prevent running handleScroll() outside of this.refresh() with values not calculated
+ this.isReady = true;
this.reverted = false;
const params = this._params;
this.repeat = setValue(parseScrollObserverFunctionParameter(params.repeat, this), true);
@@ -7681,6 +7685,7 @@ class ScrollObserver {
}
handleScroll() {
+ if (!this.isReady) return;
const linked = this.linked;
const sync = this.sync;
const syncEase = this.syncEase;
@@ -7712,7 +7717,7 @@ class ScrollObserver {
if (syncSmooth && isNum(syncSmooth)) {
if (/** @type {Number} */(syncSmooth) < 1) {
const step = 0.0001;
- const snap = lp < p && p === 1 ? step : lp > p && !p ? -1e-4 : 0;
+ const snap = lp < p && p === 1 ? step : lp > p && !p ? -step : 0;
p = round(lerp(lp, p, interpolate(.01, .2, /** @type {Number} */(syncSmooth)), false) + snap, 6);
}
} else if (syncEase) {
@@ -7804,6 +7809,7 @@ class ScrollObserver {
this.removeDebug();
}
this.reverted = true;
+ this.isReady = false;
return this;
}
diff --git a/package.json b/package.json
index 7555eb38..0d856ff1 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "animejs",
- "version": "4.1.3",
+ "version": "4.1.4",
"homepage": "https://animejs.com",
"repository": {
"type": "git",
diff --git a/src/scroll.js b/src/scroll.js
index 6b187fc8..a2ff3ba0 100644
--- a/src/scroll.js
+++ b/src/scroll.js
@@ -449,6 +449,8 @@ export class ScrollObserver {
this.forceEnter = false;
/** @type {Boolean} */
this.hasEntered = false;
+ /** @type {Boolean} */
+ this.isReady = false;
// /** @type {Array.<Number>} */
// this.offsets = [];
/** @type {Number} */
@@ -537,6 +539,8 @@ export class ScrollObserver {
}
refresh() {
+ // This flag is used to prevent running handleScroll() outside of this.refresh() with values not calculated
+ this.isReady = true;
this.reverted = false;
const params = this._params;
this.repeat = setValue(parseScrollObserverFunctionParameter(params.repeat, this), true);
@@ -802,6 +806,7 @@ export class ScrollObserver {
}
handleScroll() {
+ if (!this.isReady) return;
const linked = this.linked;
const sync = this.sync;
const syncEase = this.syncEase;
@@ -925,6 +930,7 @@ export class ScrollObserver {
this.removeDebug();
}
this.reverted = true;
+ this.isReady = false;
return this;
}
diff --git a/tests/playground/sandbox/index.html b/tests/playground/sandbox/index.html
index 4e80804e..bce596e6 100644
--- a/tests/playground/sandbox/index.html
+++ b/tests/playground/sandbox/index.html
@@ -8,11 +8,11 @@
<style>
body {
display: flex;
- flex-direction: column;
+ flex-direction: row;
flex-wrap: wrap;
justify-content: center;
align-items: center;
- height: 100vh;
+ height: 200vh;
}
.container {
@@ -20,14 +20,15 @@
flex-wrap: wrap;
justify-content: center;
align-items: center;
- gap: .5rem;
+ flex-direction: column;
+ gap: 10rem;
padding: 1rem;
}
.square {
width: 100px;
height: 100px;
- background: var(--red);
+ background: currentColor;
border-radius: 1rem;
padding: 1rem;
}
@@ -46,11 +47,11 @@
<button class="button">1000</button>
</div>
<div class="container">
- <div class="square"></div>
- <div class="square"></div>
- <div class="square"></div>
- <div class="square"></div>
- <div class="square"></div>
+ <div class="square red"></div>
+ <div class="square green"></div>
+ <div class="square green"></div>
+ <div class="square green"></div>
+ <div class="square green"></div>
</div>
<script type="module" src="./index.js"></script>
</body>
diff --git a/tests/playground/sandbox/index.js b/tests/playground/sandbox/index.js
index 16be5e52..e5531150 100644
--- a/tests/playground/sandbox/index.js
+++ b/tests/playground/sandbox/index.js
@@ -1,17 +1,20 @@
-import {
- animate,
- createTimeline,
- utils,
-} from '../../../lib/anime.esm.js';
+import { animate, onScroll, utils } from '../../../lib/anime.esm.js';
-utils.set('.square', {
- y: 0,
- rotate: 45,
-})
-
-animate('.square', {
- y: '+=50',
- rotate: () => '+=10',
- loop: true,
- onLoop: self => self.refresh()
+animate('.red', {
+ id: 'test',
+ rotate: 360,
+ autoplay: onScroll()
});
+
+utils.$('.green').forEach($el => {
+ setTimeout(() => {
+ onScroll({
+ target: $el,
+ repeat: false,
+ debug: true,
+ onEnter: () => {
+ console.log("enter scroll")
+ }
+ })
+ }, 60)
+})
diff --git a/types/index.d.ts b/types/index.d.ts
index 95069076..c255d88d 100644
--- a/types/index.d.ts
+++ b/types/index.d.ts
@@ -947,6 +947,7 @@ declare class ScrollObserver {
isInView: boolean;
forceEnter: boolean;
hasEntered: boolean;
+ isReady: boolean;
offset: number;
offsetStart: number;
offsetEnd: number;
diff --git a/types/index.js b/types/index.js
index 3ac3bd60..72183f76 100644
--- a/types/index.js
+++ b/types/index.js
@@ -1,6 +1,6 @@
/**
* anime.js - ESM
- * @version v4.1.3
+ * @version v4.1.4
* @author Julian Garnier
* @license MIT
* @copyright (c) 2025 Julian Garnier
@@ -142,7 +142,7 @@ const globals = {
/** @type {Number} */
tickThreshold: 200,
};
-const globalVersions = { version: '4.1.3', engine: null };
+const globalVersions = { version: '4.1.4', engine: null };
if (isBrowser) {
if (!win.AnimeJS)
win.AnimeJS = [];
@@ -274,7 +274,7 @@ const shuffle = items => {
* @param {Number} v
* @return {Number}
*/
-const clampInfinity = v => v === Infinity ? maxValue : v === -Infinity ? -1e12 : v;
+const clampInfinity = v => v === Infinity ? maxValue : v === -Infinity ? -maxValue : v;
/**
* @param {Number} v
* @return {Number}
@@ -1215,7 +1215,7 @@ const createDrawableProxy = ($el, start, end) => {
// const v1 = round(+value.slice(0, spaceIndex), precision);
// const v2 = round(+value.slice(spaceIndex + 1), precision);
const scaleFactor = getScaleFactor($scalled);
- const os = v1 * -1e3 * scaleFactor;
+ const os = v1 * -pathLength * scaleFactor;
const d1 = (v2 * pathLength * scaleFactor) + os;
const d2 = (pathLength * scaleFactor +
((v1 === 0 && v2 === 1) || (v1 === 1 && v2 === 0) ? 0 : 10 * scaleFactor) - d1);
@@ -4504,7 +4504,7 @@ class Spring {
this.m = clamp(setValue(parameters.mass, 1), 0, maxSpringParamValue);
this.s = clamp(setValue(parameters.stiffness, 100), 1, maxSpringParamValue);
this.d = clamp(setValue(parameters.damping, 10), .1, maxSpringParamValue);
- this.v = clamp(setValue(parameters.velocity, 0), -1e4, maxSpringParamValue);
+ this.v = clamp(setValue(parameters.velocity, 0), -maxSpringParamValue, maxSpringParamValue);
this.w0 = 0;
this.zeta = 0;
this.wd = 0;
@@ -4574,7 +4574,7 @@ class Spring {
return this.v;
}
set velocity(v) {
- this.v = clamp(setValue(v, 0), -1e4, maxSpringParamValue);
+ this.v = clamp(setValue(v, 0), -maxSpringParamValue, maxSpringParamValue);
this.compute();
}
}
@@ -4846,7 +4846,7 @@ class Draggable {
/** @type {[Number, Number, Number, Number]} */
this.dragArea = [0, 0, 0, 0]; // x, y, w, h
/** @type {[Number, Number, Number, Number]} */
- this.containerBounds = [-1e12, maxValue, maxValue, -1e12]; // t, r, b, l
+ this.containerBounds = [-maxValue, maxValue, maxValue, -maxValue]; // t, r, b, l
/** @type {[Number, Number, Number, Number]} */
this.scrollBounds = [0, 0, 0, 0]; // t, r, b, l
/** @type {[Number, Number, Number, Number]} */
@@ -5305,10 +5305,10 @@ class Draggable {
const canScroll = this.canScroll;
if (!this.containerArray && this.isOutOfBounds(scrollBounds, x, y)) {
const [st, sr, sb, sl] = scrollBounds;
- const t = round(clamp(y - st, -1e12, 0), 0);
+ const t = round(clamp(y - st, -maxValue, 0), 0);
const r = round(clamp(x - sr, 0, maxValue), 0);
const b = round(clamp(y - sb, 0, maxValue), 0);
- const l = round(clamp(x - sl, -1e12, 0), 0);
+ const l = round(clamp(x - sl, -maxValue, 0), 0);
new JSAnimation(scroll, {
x: round(scroll.x + (l ? l - gap : r ? r + gap : 0), 0),
y: round(scroll.y + (t ? t - gap : b ? b + gap : 0), 0),
@@ -6340,6 +6340,8 @@ class ScrollObserver {
this.forceEnter = false;
/** @type {Boolean} */
this.hasEntered = false;
+ /** @type {Boolean} */
+ this.isReady = false;
// /** @type {Array.<Number>} */
// this.offsets = [];
/** @type {Number} */
@@ -6425,6 +6427,8 @@ class ScrollObserver {
return p === Infinity || isNaN(p) ? 0 : round(clamp(p, 0, 1), 6);
}
refresh() {
+ // This flag is used to prevent running handleScroll() outside of this.refresh() with values not calculated
+ this.isReady = true;
this.reverted = false;
const params = this._params;
this.repeat = setValue(parseScrollObserverFunctionParameter(params.repeat, this), true);
@@ -6688,6 +6692,8 @@ class ScrollObserver {
}
}
handleScroll() {
+ if (!this.isReady)
+ return;
const linked = this.linked;
const sync = this.sync;
const syncEase = this.syncEase;
@@ -6716,7 +6722,7 @@ class ScrollObserver {
if (syncSmooth && isNum(syncSmooth)) {
if ( /** @type {Number} */(syncSmooth) < 1) {
const step = 0.0001;
- const snap = lp < p && p === 1 ? step : lp > p && !p ? -1e-4 : 0;
+ const snap = lp < p && p === 1 ? step : lp > p && !p ? -step : 0;
p = round(lerp(lp, p, interpolate(.01, .2, /** @type {Number} */ (syncSmooth)), false) + snap, 6);
}
}
@@ -6808,6 +6814,7 @@ class ScrollObserver {
this.removeDebug();
}
this.reverted = true;
+ this.isReady = false;
return this;
}
}