MapBackground.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. return new Granim({
  69. element: canvas,
  70. direction: 'custom',
  71. customDirection: custDir,
  72. isPausedWhenNotInView: true,
  73. states : {
  74. "default-state": {
  75. gradients: gradients,
  76. transitionSpeed: 60000
  77. }
  78. },
  79. })
  80. },
  81. getRandBGColor (hue) {
  82. let h = !hue ? Math.floor(Math.random()*360) : hue + -30 + Math.floor(Math.random()*60);
  83. let s = 40 + Math.floor(Math.random()*20);
  84. let l = 40 + Math.floor(Math.random()*10);
  85. let a = `${parseFloat(Math.random()*.9).toFixed(3)}`.slice(1);
  86. let hsla = `hsla(${h}, ${s}%, ${l}%, ${a})`;
  87. // console.log(hsla);
  88. return hsla;
  89. },
  90. initTrame () {
  91. let canvasBackgroundTrame = this.$refs['canvas-background-trame'];
  92. canvasBackgroundTrame.width = canvasBackgroundTrame.parentElement.clientWidth;
  93. canvasBackgroundTrame.height = canvasBackgroundTrame.parentElement.clientHeight;
  94. let ctx = canvasBackgroundTrame.getContext('2d');
  95. let step = 1;
  96. for (let i = 0; i < parseInt(canvasBackgroundTrame.width); i+=step) {
  97. for (let j = 0; j < parseInt(canvasBackgroundTrame.height); j+=step) {
  98. if (Math.random() > 0.95) {
  99. ctx.beginPath();
  100. ctx.arc(i, j, 0.5, 0, 2 * Math.PI, false);
  101. ctx.fillStyle = "rgba(200,200,200,0.7)";
  102. ctx.fill();
  103. }
  104. }
  105. }
  106. }
  107. }
  108. }
  109. </script>
  110. <template>
  111. <canvas class="map-bg-canvas gradient" ref="canvas-background-gradient1"></canvas>
  112. <canvas class="map-bg-canvas gradient" ref="canvas-background-gradient2"></canvas>
  113. <canvas class="map-bg-canvas trame" ref="canvas-background-trame"></canvas>
  114. </template>
  115. <style lang="css" scoped>
  116. </style>