Skip to content

Commit

Permalink
[to squash?] Improve raf encapsulation
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel fljot authored and fljot committed Jul 19, 2020
1 parent d6f096b commit 545f5f1
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 55 deletions.
37 changes: 19 additions & 18 deletions lib/anime.es.js
Original file line number Diff line number Diff line change
Expand Up @@ -850,9 +850,10 @@ function createNewInstance(params) {
// Core

var activeInstances = [];
var raf;

var engine = (function () {
var raf;

function play() {
if (!raf && !isDocumentHidden() && activeInstances.length > 0) {
raf = requestAnimationFrame(step);
Expand All @@ -876,30 +877,30 @@ var engine = (function () {
}
raf = i > 0 ? requestAnimationFrame(step) : undefined;
}

function handleVisibilityChange() {
if (isDocumentHidden()) {
// suspend ticks
raf = cancelAnimationFrame(raf);
} else { // is back to active tab
// first adjust animations to consider the time that ticks were suspended
activeInstances.forEach(
function (instance) { return instance ._onDocumentVisibility(); }
);
engine();
}
}
if (typeof document !== 'undefined') {
document.addEventListener('visibilitychange', handleVisibilityChange);
}

return play;
})();

function isDocumentHidden() {
return !!document && document.hidden;
}

function handleVisibilityChange() {
if (isDocumentHidden()) {
// suspend ticks
raf = cancelAnimationFrame(raf);
} else { // is back to active tab
// first adjust animations to consider the time that ticks were suspended
activeInstances.forEach(
function (instance) { return instance._onDocumentVisibility(); }
);
engine();
}
}

if (typeof document !== 'undefined') {
document.addEventListener('visibilitychange', handleVisibilityChange);
}

// Public Instance

function anime(params) {
Expand Down
37 changes: 19 additions & 18 deletions lib/anime.js
Original file line number Diff line number Diff line change
Expand Up @@ -852,9 +852,10 @@ function createNewInstance(params) {
// Core

var activeInstances = [];
var raf;

var engine = (function () {
var raf;

function play() {
if (!raf && !isDocumentHidden() && activeInstances.length > 0) {
raf = requestAnimationFrame(step);
Expand All @@ -878,30 +879,30 @@ var engine = (function () {
}
raf = i > 0 ? requestAnimationFrame(step) : undefined;
}

function handleVisibilityChange() {
if (isDocumentHidden()) {
// suspend ticks
raf = cancelAnimationFrame(raf);
} else { // is back to active tab
// first adjust animations to consider the time that ticks were suspended
activeInstances.forEach(
function (instance) { return instance ._onDocumentVisibility(); }
);
engine();
}
}
if (typeof document !== 'undefined') {
document.addEventListener('visibilitychange', handleVisibilityChange);
}

return play;
})();

function isDocumentHidden() {
return !!document && document.hidden;
}

function handleVisibilityChange() {
if (isDocumentHidden()) {
// suspend ticks
raf = cancelAnimationFrame(raf);
} else { // is back to active tab
// first adjust animations to consider the time that ticks were suspended
activeInstances.forEach(
function (instance) { return instance._onDocumentVisibility(); }
);
engine();
}
}

if (typeof document !== 'undefined') {
document.addEventListener('visibilitychange', handleVisibilityChange);
}

// Public Instance

function anime(params) {
Expand Down
2 changes: 1 addition & 1 deletion lib/anime.min.js

Large diffs are not rendered by default.

37 changes: 19 additions & 18 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -834,9 +834,10 @@ function createNewInstance(params) {
// Core

let activeInstances = [];
let raf;

const engine = (() => {
let raf;

function play() {
if (!raf && !isDocumentHidden() && activeInstances.length > 0) {
raf = requestAnimationFrame(step);
Expand All @@ -860,30 +861,30 @@ const engine = (() => {
}
raf = i > 0 ? requestAnimationFrame(step) : undefined;
}

function handleVisibilityChange() {
if (isDocumentHidden()) {
// suspend ticks
raf = cancelAnimationFrame(raf);
} else { // is back to active tab
// first adjust animations to consider the time that ticks were suspended
activeInstances.forEach(
instance => instance ._onDocumentVisibility()
);
engine();
}
}
if (typeof document !== 'undefined') {
document.addEventListener('visibilitychange', handleVisibilityChange);
}

return play;
})();

function isDocumentHidden() {
return !!document && document.hidden;
}

function handleVisibilityChange() {
if (isDocumentHidden()) {
// suspend ticks
raf = cancelAnimationFrame(raf);
} else { // is back to active tab
// first adjust animations to consider the time that ticks were suspended
activeInstances.forEach(
instance => instance._onDocumentVisibility()
);
engine();
}
}

if (typeof document !== 'undefined') {
document.addEventListener('visibilitychange', handleVisibilityChange);
}

// Public Instance

function anime(params = {}) {
Expand Down

0 comments on commit 545f5f1

Please sign in to comment.