MapBackground.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <script>
  2. import Granim from 'granim'
  3. export default {
  4. data() {
  5. return {
  6. granim1: null,
  7. granim2: null
  8. }
  9. },
  10. mounted() {
  11. this.initGradients()
  12. this.initTrame()
  13. window.addEventListener("resize", this.onWindowResize.bind(this));
  14. },
  15. // computed: {
  16. // },
  17. // created () {
  18. // },
  19. methods: {
  20. initGradients () {
  21. let canvasBackground1 = this.$refs['canvas-background-gradient1'];
  22. canvasBackground1.width = canvasBackground1.parentElement.clientWidth;
  23. canvasBackground1.height = canvasBackground1.parentElement.clientHeight;
  24. this.granim1 = this.createGranim(canvasBackground1, 1)
  25. let canvasBackground2 = this.$refs['canvas-background-gradient2'];
  26. canvasBackground2.width = canvasBackground2.parentElement.clientWidth;
  27. canvasBackground2.height = canvasBackground2.parentElement.clientHeight;
  28. this.granim2 = this.createGranim(canvasBackground2, 2)
  29. },
  30. createGranim(canvas, direction){
  31. let gradients = [];
  32. let num_colors = Math.floor(2 + Math.random()*2);
  33. for (let i = 0; i < Math.floor(2 + Math.random()*4); i++) {
  34. let colors = [];
  35. // let hue = Math.floor(Math.random()*360)
  36. for (let j = 0; j < num_colors; j++) {
  37. let pos = 1/num_colors*j + 1/num_colors/3 + Math.random()*1/num_colors/3
  38. colors.push({
  39. color: this.getRandBGColor(),
  40. // pos: parseFloat(`${parseFloat(Math.random()).toFixed(2)}`.slice(1))
  41. pos: parseFloat(`${parseFloat(pos).toFixed(2)}`.slice(1))
  42. })
  43. }
  44. // colors.sort((a,b) => {
  45. // return a.pos > b.pos ? 1 : -1
  46. // })
  47. gradients.push(colors)
  48. }
  49. console.log(gradients);
  50. let custDir;
  51. // switch (direction) {
  52. // case 1:
  53. // custDir = {
  54. // x0: `${Math.random() * canvas.width}px`,
  55. // y0: `-100px`,
  56. // x1: `${Math.random() * canvas.width}px`,
  57. // y1: `${canvas.height + 100}px`
  58. // }
  59. // break;
  60. // case 2:
  61. // custDir = {
  62. // x0: `-100px`,
  63. // y0: `${Math.random() * canvas.height}px`,
  64. // x1: `${canvas.width + 100}px`,
  65. // y1: `${Math.random() * canvas.height}px`
  66. // }
  67. // break;
  68. // }
  69. switch (direction) {
  70. case 1:
  71. custDir = {
  72. x0: `${Math.random() * canvas.width/3}px`,
  73. y0: `-100px`,
  74. x1: `${canvas.width/3*2 + Math.random() * canvas.width/3}px`,
  75. y1: `${canvas.height + 100}px`
  76. }
  77. break;
  78. case 2:
  79. custDir = {
  80. x0: `${canvas.width/3*2 + Math.random() * canvas.width/3}px`,
  81. y0: `-100px`,
  82. x1: `${Math.random() * canvas.width/3}px`,
  83. y1: `${canvas.height + 100}px`
  84. }
  85. break;
  86. }
  87. return new Granim({
  88. element: canvas,
  89. direction: 'custom',
  90. customDirection: custDir,
  91. isPausedWhenNotInView: true,
  92. states : {
  93. "default-state": {
  94. gradients: gradients,
  95. transitionSpeed: 20000 + Math.random() * 20000
  96. }
  97. },
  98. })
  99. },
  100. getRandBGColor (hue) {
  101. let h = !hue ? Math.floor(Math.random()*360) : hue + -30 + Math.floor(Math.random()*60);
  102. let s = 25 + Math.floor(Math.random()*5);
  103. let l = 40 + Math.floor(Math.random()*10);
  104. let a = `${parseFloat(0.2 + Math.random()*.4).toFixed(3)}`.slice(1);
  105. let hsla = `hsla(${h}, ${s}%, ${l}%, ${a})`;
  106. // console.log(hsla);
  107. return hsla;
  108. },
  109. initTrame () {
  110. let canvasBackgroundTrame = this.$refs['canvas-background-trame'];
  111. canvasBackgroundTrame.width = canvasBackgroundTrame.parentElement.clientWidth;
  112. canvasBackgroundTrame.height = canvasBackgroundTrame.parentElement.clientHeight;
  113. let ctx = canvasBackgroundTrame.getContext('2d');
  114. ctx.clearRect(0,0, canvasBackgroundTrame.width, canvasBackgroundTrame.height);
  115. let step = 1;
  116. for (let i = 0; i < parseInt(canvasBackgroundTrame.width); i+=step) {
  117. for (let j = 0; j < parseInt(canvasBackgroundTrame.height); j+=step) {
  118. if (Math.random() > 0.6) {
  119. ctx.beginPath();
  120. // ctx.arc(i, j, 0.5, 0, 2 * Math.PI, false);
  121. ctx.rect(i, j, 1, 1);
  122. ctx.fillStyle = "rgba(0,0,0,0.1)";
  123. ctx.fill();
  124. }
  125. }
  126. }
  127. },
  128. onWindowResize () {
  129. this.initTrame()
  130. }
  131. }
  132. }
  133. </script>
  134. <template>
  135. <canvas class="map-bg-canvas gradient" ref="canvas-background-gradient1"></canvas>
  136. <canvas class="map-bg-canvas trame" ref="canvas-background-trame"></canvas>
  137. <canvas class="map-bg-canvas gradient" ref="canvas-background-gradient2"></canvas>
  138. </template>
  139. <style lang="css" scoped>
  140. </style>