MapBackground.vue 4.5 KB

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