JavaScript animation engine
Design tweaks
Julian Garnierd801957194a7
diff --git a/documentation/assets/css/site.css b/documentation/assets/css/site.cssindex 0dcc8529..4ad07e1a 100644--- a/documentation/assets/css/site.css+++ b/documentation/assets/css/site.css@@ -25,6 +25,7 @@ body.ready { opacity: 1; }}.logo-animation {+ pointer-events: none;display: flex;align-items: center;justify-content: center;diff --git a/documentation/assets/js/fireworks.js b/documentation/assets/js/fireworks.jsnew file mode 100644index 00000000..9c7636b8--- /dev/null+++ b/documentation/assets/js/fireworks.js@@ -0,0 +1,158 @@+var fireworks = (function() {++ function getFontSize() {+ return parseFloat(getComputedStyle(document.documentElement).fontSize);+ }++ var canvasEl = document.querySelector('.fireworks');+ var ctx = canvasEl.getContext('2d');+ var tap = ('ontouchstart' in window || navigator.msMaxTouchPoints) ? 'touchstart' : 'mousedown';+ var numberOfParticules = 24;+ var x = 0;+ var y = 0;+ var animations = [];+ var targets = [];+ var colors = ['#FF1461', '#18FF92', '#5A87FF', '#FBF38C'];++ function setCanvasSize() {+ canvasEl.width = window.innerWidth * 2;+ canvasEl.height = window.innerHeight * 2;+ canvasEl.style.width = window.innerWidth + 'px';+ canvasEl.style.height = window.innerHeight + 'px';+ canvasEl.getContext('2d').scale(2, 2);+ }++ function updateCoords(e) {+ x = e.clientX || e.touches[0].clientX;+ y = e.clientY || e.touches[0].clientY;+ }++ function createCircle(x,y) {+ var p = {};+ p.x = x;+ p.y = y;+ p.color = colors[anime.random(0, colors.length - 1)];+ p.color = '#FFF';+ p.radius = 0.1;+ p.alpha = .5;+ p.lineWidth = 6;+ p.draw = function() {+ ctx.globalAlpha = p.alpha;+ ctx.beginPath();+ ctx.arc(p.x, p.y, p.radius, 0, 2 * Math.PI, true);+ ctx.lineWidth = p.lineWidth;+ ctx.strokeStyle = p.color;+ ctx.stroke();+ ctx.globalAlpha = 1;+ }+ return p;+ }++ function createParticule(x,y) {+ var p = {};+ p.x = x;+ p.y = y;+ p.color = colors[anime.random(0, colors.length - 1)];+ p.radius = anime.random(getFontSize(), getFontSize() * 2.25);+ p.draw = function() {+ ctx.beginPath();+ ctx.arc(p.x, p.y, p.radius, 0, 2 * Math.PI, true);+ ctx.fillStyle = p.color;+ ctx.fill();+ }+ return p;+ }++ function createParticles(x,y) {+ var particules = [];+ for (var i = 0; i < numberOfParticules; i++) {+ var p = createParticule(x, y);+ particules.push(p);+ targets.push(p);+ }+ return particules;+ }++ function removeTargets(animation) {+ for (var i = 0; i < animation.animatables.length; i++) {+ var index = targets.indexOf(animation.animatables[i].target);+ if (index > -1) targets.splice(index, 1);+ }+ }++ function removeAnimation(animation) {+ removeTargets(animation);+ var index = animations.indexOf(animation);+ if (index > -1) animations.splice(index, 1);+ if (!animations.length) mainLoop.pause();+ }++ function getParticulePos(p) {+ var angle = anime.random(0, 360) * Math.PI / 180;+ var value = anime.random(50, 180);+ var radius = [-1, 1][anime.random(0, 1)] * value;+ return {+ x: p.x + radius * Math.cos(angle),+ y: p.y + radius * Math.sin(angle)+ }+ }++ function animateParticules(x, y) {+ var particules = createParticles(x, y);+ var circle = createCircle(x, y);+ targets.push(circle);+ particules.forEach(function(p) {+ var position = getParticulePos(p);+ var particuleAnimation = anime({+ targets: p,+ x: position.x,+ y: position.y,+ radius: 0.1,+ duration: function() { return anime.random(1200, 1800); },+ easing: 'easeOutExpo',+ complete: removeAnimation+ });+ animations.push(particuleAnimation);+ });+ var circleAnimation = anime({+ targets: circle,+ radius: anime.random(getFontSize() * 8, getFontSize() * 12),+ lineWidth: 0,+ alpha: {+ value: 0,+ easing: 'linear',+ duration: anime.random(600, 800),+ },+ duration: anime.random(1200, 1800),+ easing: 'easeOutExpo',+ complete: removeAnimation+ });+ animations.push(circleAnimation);+ }++ var mainLoop = anime({+ duration: Infinity,+ update: function() {+ ctx.clearRect(0, 0, canvasEl.width, canvasEl.height);+ for (var t = 0; t < targets.length; t++ ) {+ targets[t].draw();+ }+ }+ });++ document.addEventListener(tap, function(e) {+ window.human = true;+ updateCoords(e);+ mainLoop.play();+ animateParticules(x, y);+ ga('send', 'event', 'Fireworks', 'Click');+ }, false);++ window.addEventListener('resize', setCanvasSize, false);++ return {+ setCanvasSize: setCanvasSize,+ animateParticules: animateParticules+ }++})();\ No newline at end of filediff --git a/documentation/assets/js/site.js b/documentation/assets/js/site.jsindex 85ef35d1..65731ba2 100644--- a/documentation/assets/js/site.js+++ b/documentation/assets/js/site.js@@ -1,174 +1,5 @@-var fireworks = (function() {-- function getFontSize() {- return parseFloat(getComputedStyle(document.documentElement).fontSize);- }-- var canvasEl = document.querySelector('.fireworks');- var ctx = canvasEl.getContext('2d');- var tap = ('ontouchstart' in window || navigator.msMaxTouchPoints) ? 'touchstart' : 'mousedown';- var numberOfParticules = 24;- var distance = 200;- var x = 0;- var y = 0;- var animations = [];- var targets = [];- var colors = ['#FF1461', '#18FF92', '#5A87FF', '#FBF38C'];-- function setCanvasSize() {- canvasEl.width = window.innerWidth * 2;- canvasEl.height = window.innerHeight * 2;- canvasEl.style.width = window.innerWidth + 'px';- canvasEl.style.height = window.innerHeight + 'px';- canvasEl.getContext('2d').scale(2, 2);- }-- function updateCoords(e) {- x = e.clientX || e.touches[0].clientX;- y = e.clientY || e.touches[0].clientY;- }-- function createCircle(x,y) {- var p = {};- p.x = x;- p.y = y;- p.color = colors[anime.random(0, colors.length - 1)];- p.color = '#FFF';- p.radius = 0.1;- p.alpha = 1;- p.lineWidth = 6;- p.draw = function() {- ctx.globalAlpha = p.alpha;- ctx.beginPath();- ctx.arc(p.x, p.y, p.radius, 0, 2 * Math.PI, true);- ctx.lineWidth = p.lineWidth;- ctx.strokeStyle = p.color;- ctx.stroke();- ctx.globalAlpha = 1;- }- return p;- }-- function createParticule(x,y) {- var p = {};- p.x = x;- p.y = y;- p.color = colors[anime.random(0, colors.length - 1)];- p.radius = anime.random(getFontSize(), getFontSize() * 2.25);- p.draw = function() {- ctx.beginPath();- ctx.arc(p.x, p.y, p.radius, 0, 2 * Math.PI, true);- ctx.fillStyle = p.color;- ctx.fill();- }- return p;- }-- function createParticles(x,y) {- var particules = [];- for (var i = 0; i < numberOfParticules; i++) {- var p = createParticule(x, y);- particules.push(p);- targets.push(p);- }- return particules;- }-- function removeTargets(animation) {- for (var i = 0; i < animation.animatables.length; i++) {- var index = targets.indexOf(animation.animatables[i].target);- if (index > -1) targets.splice(index, 1);- }- }-- function removeAnimation(animation) {- removeTargets(animation);- var index = animations.indexOf(animation);- if (index > -1) animations.splice(index, 1);- if (!animations.length) mainLoop.pause();- }-- function getParticulePos(p) {- var angle = anime.random(0, 360) * Math.PI / 180;- var radius = anime.random(-distance, distance);- return {- x: p.x + radius * Math.cos(angle),- y: p.y + radius * Math.sin(angle)- }- }-- function animateParticules(x, y) {- var particules = createParticles(x, y);- var circle = createCircle(x, y);- targets.push(circle);- particules.forEach(function(p) {- var position = getParticulePos(p);- var particuleAnimation = anime({- targets: p,- x: position.x,- y: position.y,- radius: 0.1,- duration: function() { return anime.random(1200, 1800); },- easing: 'easeOutExpo',- complete: removeAnimation- });- animations.push(particuleAnimation);- });- var circleAnimation = anime({- targets: circle,- radius: function() { return anime.random(getFontSize() * 8.75, getFontSize() * 11.25); },- lineWidth: 0,- alpha: {- value: 0,- easing: 'linear',- duration: function() { return anime.random(400, 600); }- },- duration: function() { return anime.random(1200, 1800); },- easing: 'easeOutExpo',- complete: removeAnimation- });- animations.push(circleAnimation);- }-- var mainLoop = anime({- duration: Infinity,- update: function() {- ctx.clearRect(0, 0, canvasEl.width, canvasEl.height);- for (var t = 0; t < targets.length; t++ ) {- targets[t].draw();- }- }- });-- document.addEventListener(tap, function(e) {- updateCoords(e);- mainLoop.play();- animateParticules(x, y);- ga('send', 'event', 'Fireworks', 'Click');- }, false);-- window.addEventListener('resize', setCanvasSize, false);-- return {- setCanvasSize: setCanvasSize,- animateParticules: animateParticules- }--})();--var logoAnimation = (function() {-- var logoEl = document.querySelector('.logo-animation');- var buttonEls = document.querySelectorAll('.button');- var pathEls = document.querySelectorAll('.logo-animation path:not(.icon-curve)');- var innerWidth = window.innerWidth;- var maxWidth = 740;- var logoScale = innerWidth <= maxWidth ? innerWidth / maxWidth : 1;- var logoTimeline = anime.timeline({ autoplay: false });-- logoEl.style.transform = 'translateY(50px) scale('+logoScale+')';-- function createBouncyButton(el) {+var createBouncyButtons = (function() {+ function createButton(el) {var pathEl = el.querySelector('path');var spanEl = el.querySelector('span');pathEl.setAttribute('stroke-dashoffset', anime.setDashoffset(pathEl));@@ -200,11 +31,26 @@ var logoAnimation = (function() {}}+ var buttonEls = document.querySelectorAll('.button');+for (var i = 0; i < buttonEls.length; i++) {var el = buttonEls[i];- createBouncyButton(el);+ createButton(el);}+})();++var logoAnimation = (function() {++ var logoEl = document.querySelector('.logo-animation');+ var pathEls = document.querySelectorAll('.logo-animation path:not(.icon-curve)');+ var innerWidth = window.innerWidth;+ var maxWidth = 740;+ var logoScale = innerWidth <= maxWidth ? innerWidth / maxWidth : 1;+ var logoTimeline = anime.timeline({ autoplay: false });++ logoEl.style.transform = 'translateY(50px) scale('+logoScale+')';+for (var i = 0; i < pathEls.length; i++) {var el = pathEls[i];el.setAttribute('stroke-dashoffset', anime.setDashoffset(el));@@ -320,7 +166,14 @@ var logoAnimation = (function() {offset: 0}).add({- targets: ['.logo-animation', '.description', '.button', '.credits'],+ targets: '.logo-animation',+ translateY: [50, 0],+ scale: 1,+ easing: 'easeInOutQuad',+ offset: '-=250'+ })+ .add({+ targets: ['.description', '.button', '.credits'],translateY: [50, 0],scale: 1,opacity: 1,@@ -328,7 +181,7 @@ var logoAnimation = (function() {delay: function(el, i) {return i * 150},- offset: '-=250'+ offset: '-=500'}).add({targets: '.button path',@@ -342,6 +195,7 @@ var logoAnimation = (function() {function init() {document.body.classList.add('ready');+ // logoTimeline.seek(4000);logoTimeline.play();}diff --git a/documentation/examples/anime-canvas-fireworks.html b/documentation/examples/anime-canvas-fireworks.htmlindex be4e43c1..d04b9586 100644--- a/documentation/examples/anime-canvas-fireworks.html+++ b/documentation/examples/anime-canvas-fireworks.html@@ -19,7 +19,7 @@<script src="../../anime.js"></script></head><body>- <canvas></canvas>+ <canvas class="fireworks"></canvas><script>(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),@@ -28,174 +28,25 @@ga('create', 'UA-79927007-1', 'auto');ga('send', 'pageview');</script>+ <script src="../assets/js/fireworks.js"></script><script>-- function getFontSize() {- return parseFloat(getComputedStyle(document.documentElement).fontSize);- }-- var canvasEl = document.querySelector('canvas');- var ctx = canvasEl.getContext('2d');- var tap = ('ontouchstart' in window || navigator.msMaxTouchPoints) ? 'touchstart' : 'mousedown';- var numberOfParticules = 24;- var distance = 200;- var x = 0;- var y = 0;- var animations = [];- var targets = [];- var colors = ['#FF1461', '#18FF92', '#5A87FF', '#FBF38C'];-- function setCanvasSize() {- canvasEl.width = window.innerWidth * 2;- canvasEl.height = window.innerHeight * 2;- canvasEl.style.width = window.innerWidth + 'px';- canvasEl.style.height = window.innerHeight + 'px';- canvasEl.getContext('2d').scale(2, 2);- }-- function updateCoords(e) {- x = e.clientX || e.touches[0].clientX;- y = e.clientY || e.touches[0].clientY;- }-- function createCircle(x,y) {- var p = {};- p.x = x;- p.y = y;- p.color = colors[anime.random(0, colors.length - 1)];- p.color = '#FFF';- p.radius = 0.1;- p.alpha = 1;- p.lineWidth = 6;- p.draw = function() {- ctx.globalAlpha = p.alpha;- ctx.beginPath();- ctx.arc(p.x, p.y, p.radius, 0, 2 * Math.PI, true);- ctx.lineWidth = p.lineWidth;- ctx.strokeStyle = p.color;- ctx.stroke();- ctx.globalAlpha = 1;- }- return p;- }-- function createParticule(x,y) {- var p = {};- p.x = x;- p.y = y;- p.color = colors[anime.random(0, colors.length - 1)];- p.radius = anime.random(getFontSize(), getFontSize() * 2.25);- p.draw = function() {- ctx.beginPath();- ctx.arc(p.x, p.y, p.radius, 0, 2 * Math.PI, true);- ctx.fillStyle = p.color;- ctx.fill();- }- return p;- }-- function createParticles(x,y) {- var particules = [];- for (var i = 0; i < numberOfParticules; i++) {- var p = createParticule(x, y);- particules.push(p);- targets.push(p);- }- return particules;- }-- function removeTargets(animation) {- for (var i = 0; i < animation.animatables.length; i++) {- var index = targets.indexOf(animation.animatables[i].target);- if (index > -1) targets.splice(index, 1);+ window.human = false;+ var centerX = window.innerWidth / 2;+ var centerY = window.innerHeight / 2;++ function autoClick() {+ if (window.human) return;+ fireworks.animateParticules(+ anime.random(centerX-50, centerX+50),+ anime.random(centerY-50, centerY+50)+ );+ anime({duration: 400}).finished.then(autoClick);}- }-- function removeAnimation(animation) {- removeTargets(animation);- var index = animations.indexOf(animation);- if (index > -1) animations.splice(index, 1);- if (!animations.length) mainLoop.pause();- }-- function getParticulePos(p) {- var angle = anime.random(0, 360) * Math.PI / 180;- var radius = anime.random(-distance, distance);- return {- x: p.x + radius * Math.cos(angle),- y: p.y + radius * Math.sin(angle)- }- }-- function animateParticules(x, y) {- var particules = createParticles(x, y);- var circle = createCircle(x, y);- targets.push(circle);- particules.forEach(function(p) {- var position = getParticulePos(p);- var particuleAnimation = anime({- targets: p,- x: position.x,- y: position.y,- radius: 0.1,- duration: function() { return anime.random(1200, 1800); },- easing: 'easeOutExpo',- complete: removeAnimation- });- animations.push(particuleAnimation);- });- var circleAnimation = anime({- targets: circle,- radius: function() { return anime.random(getFontSize() * 8.75, getFontSize() * 11.25); },- lineWidth: 0,- alpha: {- value: 0,- easing: 'linear',- duration: function() { return anime.random(400, 600); }- },- duration: function() { return anime.random(1200, 1800); },- easing: 'easeOutExpo',- complete: removeAnimation- });- animations.push(circleAnimation);- }-- var mainLoop = anime({- duration: Infinity,- update: function() {- ctx.clearRect(0, 0, canvasEl.width, canvasEl.height);- for (var t = 0; t < targets.length; t++ ) {- targets[t].draw();- }- }- });-- var human = false;- var centerX = window.innerWidth / 2;- var centerY = window.innerHeight / 2;-- function autoClick() {- if (human) return;- animateParticules(- anime.random(centerX-50, centerX+50),- anime.random(centerY-50, centerY+50)- );- anime({duration: 400}).finished.then(autoClick);- }-- autoClick();-- document.addEventListener(tap, function(e) {- human = true;- updateCoords(e);- mainLoop.play();- animateParticules(x, y);- ga('send', 'event', 'Fireworks', 'Click');- }, false);-- window.addEventListener('resize', setCanvasSize, false);- window.addEventListener('load', setCanvasSize, false);+ window.addEventListener('load', function() {+ autoClick();+ fireworks.setCanvasSize();+ }, false);</script><script src="../assets/js/stats.js"></script></body>diff --git a/documentation/examples/anime-directions-testing.html b/documentation/examples/anime-directions-testing.htmlindex 32d5137c..9b2347b2 100644--- a/documentation/examples/anime-directions-testing.html+++ b/documentation/examples/anime-directions-testing.html@@ -123,7 +123,7 @@loop: 1});- createTest('Alternate 1 time (equal 2)', 1, {+ createTest('Alternate 1 time (equals 2)', 1, {direction: 'alternate',loop: 1});diff --git a/index.html b/index.htmlindex 9597db94..04478cd8 100644--- a/index.html+++ b/index.html@@ -151,6 +151,7 @@ga('send', 'pageview');</script><script src="anime.js"></script>+ <script src="documentation/assets/js/fireworks.js"></script><script src="documentation/assets/js/site.js"></script></body></html>