JavaScript animation engine
Improve demos
Julian Garnier1573fcf8fa50
diff --git a/documentation/assets/css/site.css b/documentation/assets/css/site.cssindex 4ad07e1a..12f3684a 100644--- a/documentation/assets/css/site.css+++ b/documentation/assets/css/site.css@@ -112,6 +112,7 @@ body.ready { opacity: 1; }}.links {+ opacity: 0;display: flex;flex-direction: column;justify-content: center;@@ -127,7 +128,6 @@ body.ready { opacity: 1; }}.button {- opacity: 0;position: relative;display: flex;flex-direction: column;@@ -157,6 +157,10 @@ body.ready { opacity: 1; }stroke: currentColor;}+.button.green svg {+ transform: rotate(180deg);+}+.credits {opacity: 0;margin-top: 20px;diff --git a/documentation/assets/js/fireworks.js b/documentation/assets/js/fireworks.jsindex 9c7636b8..5fbeaf26 100644--- a/documentation/assets/js/fireworks.js+++ b/documentation/assets/js/fireworks.js@@ -1,17 +1,11 @@var fireworks = (function() {- function getFontSize() {- return parseFloat(getComputedStyle(document.documentElement).fontSize);- }-var canvasEl = document.querySelector('.fireworks');var ctx = canvasEl.getContext('2d');+ var numberOfParticules = 32;+ var pointerX = 0;+ var pointerY = 0;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() {@@ -23,100 +17,80 @@ var fireworks = (function() {}function updateCoords(e) {- x = e.clientX || e.touches[0].clientX;- y = e.clientY || e.touches[0].clientY;+ pointerX = e.clientX || e.touches[0].clientX;+ pointerY = e.clientY || e.touches[0].clientY;}- function createCircle(x,y) {+ function setParticuleDirection(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 createParticule(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.radius = anime.random(16, 40);+ p.endPos = setParticuleDirection(p);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;+ ctx.fillStyle = p.color;+ ctx.fill();}return p;}- function createParticule(x,y) {+ function createCircle(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.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.fillStyle = p.color;- ctx.fill();+ ctx.lineWidth = p.lineWidth;+ ctx.strokeStyle = p.color;+ ctx.stroke();+ ctx.globalAlpha = 1;}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 renderParticule(anim) {+ for (var i = 0; i < anim.animatables.length; i++) {+ anim.animatables[i].target.draw();}}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 particules = [];+ for (var i = 0; i < numberOfParticules; i++) {+ particules.push(createParticule(x, y));+ }+ anime({+ targets: particules,+ x: function(p) { return p.endPos.x; },+ y: function(p) { return p.endPos.y; },+ radius: 0.1,+ duration: anime.random(1200, 1800),+ easing: 'easeOutExpo',+ update: renderParticule});- var circleAnimation = anime({+ anime({targets: circle,- radius: anime.random(getFontSize() * 8, getFontSize() * 12),+ radius: anime.random(128, 192),lineWidth: 0,alpha: {value: 0,@@ -125,32 +99,30 @@ var fireworks = (function() {},duration: anime.random(1200, 1800),easing: 'easeOutExpo',- complete: removeAnimation+ update: renderParticule});- animations.push(circleAnimation);}- var mainLoop = anime({+ var render = anime({duration: Infinity,+ autoplay: false,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);+ render.play();+ animateParticules(pointerX, pointerY);ga('send', 'event', 'Fireworks', 'Click');}, false);window.addEventListener('resize', setCanvasSize, false);return {+ render: render,setCanvasSize: setCanvasSize,animateParticules: animateParticules}diff --git a/documentation/assets/js/site.js b/documentation/assets/js/site.jsindex 65731ba2..0f4f7dc0 100644--- a/documentation/assets/js/site.js+++ b/documentation/assets/js/site.js@@ -166,36 +166,28 @@ var logoAnimation = (function() {offset: 0}).add({- targets: '.logo-animation',- translateY: [50, 0],- scale: 1,- easing: 'easeInOutQuad',- offset: '-=250'- })- .add({- targets: ['.description', '.button', '.credits'],+ targets: ['.logo-animation', '.description', '.links', '.credits'],translateY: [50, 0],scale: 1,opacity: 1,easing: 'easeOutExpo',- delay: function(el, i) {+ delay: function(el, i, l) {return i * 150},- offset: '-=500'+ offset: '-=250'}).add({targets: '.button path',strokeDashoffset: [anime.setDashoffset, 0],easing: 'easeInOutQuart',- duration: 500,- delay: function(el, i) { return i * 250; },- offset: '-=1250'+ duration: 750,+ offset: '-=1100'})function init() {document.body.classList.add('ready');- // logoTimeline.seek(4000);+ // logoTimeline.seek(2500);logoTimeline.play();}diff --git a/documentation/examples/anime-DOM-stress-test.html b/documentation/examples/anime-DOM-stress-test.htmlindex cbf5710a..9f01b0bf 100644--- a/documentation/examples/anime-DOM-stress-test.html+++ b/documentation/examples/anime-DOM-stress-test.html@@ -21,10 +21,6 @@<!-- <script src="../assets/js/anime.1.0.js"></script> --><style>- body {- overflow: hidden;- }-.particule {position: absolute;top: 50%;@@ -32,9 +28,8 @@width: 1rem;height: 1rem;margin: -.5rem 0 0 -.5rem;- background: currentColor;- transform: scale(1);- border-radius: 50%;+ border: 1px solid currentColor;+ transform: scale(0);}</style>@@ -42,10 +37,11 @@<body></body><script>- var maxElements = 350;+ var maxElements = 400;var duration = 1500;var toAnimate = [];var radius = window.innerWidth < window.innerHeight ? window.innerWidth : window.innerHeight;+ var distance = radius / 4 <= 150 ? 150 : radius / 4;var colors = ['#FF1461', '#18FF92', '#5A87FF', '#FBF38C'];var createElements = (function() {@@ -53,7 +49,7 @@for (var i = 0; i < maxElements; i++) {var el = document.createElement('div');el.classList.add('particule');- el.style.background = colors[anime.random(0, 3)];+ el.style.color = colors[anime.random(0, 3)];toAnimate.push(el);fragment.appendChild(el);}@@ -62,16 +58,15 @@var animate = function(el, i) {var angle = Math.random() * Math.PI * 2;- var delay = (duration / maxElements) * i;anime({targets: el,- translateX: [0, Math.cos(angle) * (radius / 4)],- translateY: [0, Math.sin(angle) * (radius / 4)],+ translateX: [0, Math.cos(angle) * distance],+ translateY: [0, Math.sin(angle) * distance],scale: [{value: [0, 1], duration: 400, easing: 'easeOutBack'},- {value: 0, duration: 400, delay: duration - 900, easing: 'easeInBack'}+ {value: 0, duration: 400, delay: duration - 800, easing: 'easeInBack'}],- delay: (duration / maxElements) * i,+ offset: (duration / maxElements) * i,duration: duration,easing: 'easeOutSine',loop: truediff --git a/documentation/examples/anime-canvas-fireworks.html b/documentation/examples/anime-canvas-fireworks.htmlindex d04b9586..e1733a50 100644--- a/documentation/examples/anime-canvas-fireworks.html+++ b/documentation/examples/anime-canvas-fireworks.html@@ -40,10 +40,11 @@anime.random(centerX-50, centerX+50),anime.random(centerY-50, centerY+50));- anime({duration: 400}).finished.then(autoClick);+ anime({duration: 200}).finished.then(autoClick);}window.addEventListener('load', function() {+ fireworks.render.play();autoClick();fireworks.setCanvasSize();}, false);