document.addEventListener('DOMContentLoaded', () => { function getRandomInteger(min, max) { var random = min + Math.random() * (max + 1 - min); random = Math.floor(random); return random; } // password recovery animation let canvas; let ctx; let config = { sparkFreq: 2, meanSparkSize: 0.002, meanSparkLife: 500, meanSparkVelocity: [ 0, 4 ], sparkSizeVariation: 50, sparkBlink: 15, // Lower is more blink floorHeight: 0 }; var resize = window.resize = function() { canvas.height = document.body.offsetHeight; canvas.width = document.body.offsetWidth; if (document.body.offsetWidth < 768) { config.sparkFreq = 1; config.meanSparkVelocity = [0, 3]; } }; window.onload = function() { canvas = document.querySelector('.password-intro__canvas'); if (canvas) { ctx = canvas.getContext('2d'); resize(); config.meanSparkSize = canvas.width*config.meanSparkSize; var fire = new Fire(ctx, canvas, canvas.height-canvas.height*config.floorHeight, config); var loop = function() { window.requestAnimFrame(loop); ctx.clearRect(0, 0, canvas.width, canvas.height); fire.update(); for (var i=0; i 45) { // // console.log('l90') // } else if (event.gamma < -45) { // // console.log('r90'); // } // }, false); // variant for refactoring const deviceIsAndroid = /android/i.test(navigator.userAgent); window.addEventListener('devicemotion', function (event) { fire.sparks.forEach(function (spark) { // spark.v[1] -= event.accelerationIncludingGravity.y + 6; if (deviceIsAndroid) { spark.v[0] -= event.accelerationIncludingGravity.x / 150; } else { spark.v[0] += event.accelerationIncludingGravity.x / 150; } if (window.orientation === 90 || window.orientation === -90) { spark.v[0] = config.meanSparkVelocity[0]*(Math.random()-0.5); } }); }, false); } loop(); } }; ////////////////////////////////////////////////// var Fire = function Fire(ctx, canvas, y, config) { this.ctx = ctx; this.canvas = canvas; this.y = y; this.r = 255; this.config = config; this.sparks = [ ]; }; Fire.prototype.spark = function(x) { this.sparks.push(new Spark(this.ctx, x, this.y, this.config)); }; Fire.prototype.updateColor = function() { this.r += (Math.random()-0.5)*10; this.r = Math.round(Math.min(80, Math.max(60, this.r))); }; Fire.prototype.update = function() { this.updateColor(); this.ctx.beginPath(); this.ctx.rect(0, this.y, this.canvas.width, this.config.meanSparkSize); this.ctx.fillStyle = 'rgba('+this.r+', 0, 0, 1)'; this.ctx.fill(); for (var i=0; i