loadding.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. document.addEventListener('DOMContentLoaded', () => {
  2. function getRandomInteger(min, max) {
  3. var random = min + Math.random() * (max + 1 - min);
  4. random = Math.floor(random);
  5. return random;
  6. }
  7. // password recovery animation
  8. let canvas;
  9. let ctx;
  10. let config = {
  11. sparkFreq: 2,
  12. meanSparkSize: 0.002,
  13. meanSparkLife: 500,
  14. meanSparkVelocity: [ 0, 4 ],
  15. sparkSizeVariation: 50,
  16. sparkBlink: 15, // Lower is more blink
  17. floorHeight: 0
  18. };
  19. var resize = window.resize = function() {
  20. canvas.height = document.body.offsetHeight;
  21. canvas.width = document.body.offsetWidth;
  22. if (document.body.offsetWidth < 768) {
  23. config.sparkFreq = 1;
  24. config.meanSparkVelocity = [0, 3];
  25. }
  26. };
  27. window.onload = function() {
  28. canvas = document.querySelector('.password-intro__canvas');
  29. if (canvas) {
  30. ctx = canvas.getContext('2d');
  31. resize();
  32. config.meanSparkSize = canvas.width*config.meanSparkSize;
  33. var fire = new Fire(ctx, canvas, canvas.height-canvas.height*config.floorHeight, config);
  34. var loop = function() {
  35. window.requestAnimFrame(loop);
  36. ctx.clearRect(0, 0, canvas.width, canvas.height);
  37. fire.update();
  38. for (var i=0; i<config.sparkFreq; i++) {
  39. fire.spark(Math.random()*canvas.width);
  40. }
  41. };
  42. window.requestAnimFrame = function(){
  43. return window.requestAnimationFrame ||
  44. window.webkitRequestAnimationFrame ||
  45. window.mozRequestAnimationFrame ||
  46. window.oRequestAnimationFrame ||
  47. window.msRequestAnimationFrame ||
  48. function(a) { window.setTimeout(a,1E3/60); };
  49. }();
  50. window.onresize = function () {
  51. resize();
  52. };
  53. if (window.DeviceMotionEvent && document.body.offsetWidth < 1024) {
  54. config.meanSparkLife = 1000;
  55. // window.addEventListener('deviceorientation', function (event) {
  56. // // // console.log(event.gamma);
  57. //
  58. // if (event.gamma > 45) {
  59. // // console.log('l90')
  60. // } else if (event.gamma < -45) {
  61. // // console.log('r90');
  62. // }
  63. // }, false); // variant for refactoring
  64. const deviceIsAndroid = /android/i.test(navigator.userAgent);
  65. window.addEventListener('devicemotion', function (event) {
  66. fire.sparks.forEach(function (spark) {
  67. // spark.v[1] -= event.accelerationIncludingGravity.y + 6;
  68. if (deviceIsAndroid) {
  69. spark.v[0] -= event.accelerationIncludingGravity.x / 150;
  70. } else {
  71. spark.v[0] += event.accelerationIncludingGravity.x / 150;
  72. }
  73. if (window.orientation === 90 || window.orientation === -90) {
  74. spark.v[0] = config.meanSparkVelocity[0]*(Math.random()-0.5);
  75. }
  76. });
  77. }, false);
  78. }
  79. loop();
  80. }
  81. };
  82. //////////////////////////////////////////////////
  83. var Fire = function Fire(ctx, canvas, y, config) {
  84. this.ctx = ctx;
  85. this.canvas = canvas;
  86. this.y = y;
  87. this.r = 255;
  88. this.config = config;
  89. this.sparks = [ ];
  90. };
  91. Fire.prototype.spark = function(x) {
  92. this.sparks.push(new Spark(this.ctx, x, this.y, this.config));
  93. };
  94. Fire.prototype.updateColor = function() {
  95. this.r += (Math.random()-0.5)*10;
  96. this.r = Math.round(Math.min(80, Math.max(60, this.r)));
  97. };
  98. Fire.prototype.update = function() {
  99. this.updateColor();
  100. this.ctx.beginPath();
  101. this.ctx.rect(0, this.y, this.canvas.width, this.config.meanSparkSize);
  102. this.ctx.fillStyle = 'rgba('+this.r+', 0, 0, 1)';
  103. this.ctx.fill();
  104. for (var i=0; i<this.sparks.length; i++) {
  105. if (this.sparks[i].update()) { // Spark died
  106. this.sparks.splice(i, 1);
  107. }
  108. }
  109. };
  110. ///////////////////////////////////////////////
  111. var Spark = function Spark(ctx, x, y, config) {
  112. this.ctx = ctx;
  113. this.pos = [ x, y ];
  114. this.size = config.meanSparkSize+(Math.random()-0.5)*config.sparkSizeVariation;
  115. this.v = [
  116. config.meanSparkVelocity[0]*(Math.random()-0.5),
  117. -1*config.meanSparkVelocity[1]*Math.random()
  118. ];
  119. this.c = [
  120. 0,
  121. getRandomInteger(111, 276),
  122. getRandomInteger(16, 176)
  123. ];
  124. this.life = this.lifeOrig = Math.floor(config.meanSparkLife*Math.random());
  125. this.config = config;
  126. };
  127. Spark.prototype.move = function() {
  128. for (var i=0; i<2; i++) {
  129. this.pos[i] += this.v[i]*(1-this.life/this.lifeOrig);
  130. }
  131. };
  132. Spark.prototype.getAlpha = function() {
  133. return Math.sqrt(this.life/this.lifeOrig)+((Math.random()-0.9)/this.config.sparkBlink);
  134. };
  135. Spark.prototype.update = function() {
  136. this.move();
  137. if (!(this.life--)) { return true; }
  138. this.ctx.beginPath();
  139. this.ctx.rect(this.pos[0], this.pos[1], this.size, this.size);
  140. this.ctx.fillStyle = 'rgba('+this.c[0]+', '+this.c[1]+', '+this.c[2]+', 1)';
  141. this.ctx.fill();
  142. };
  143. });