FHWWCTeam

Untitled

Sep 29th, 2021 (edited)
526
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 KB | None | 0 0
  1. const staticImg = "https://img.swordpal.cn/imgs/qing.png";
  2. const weatherImg = "https://img.swordpal.cn/imgs/spring.png";
  3. const img = new Image();
  4. img.src = weatherImg;
  5. img.onload = function () {
  6. startSakura();
  7. }
  8.  
  9.  
  10. staticWeather();
  11.  
  12.  
  13. var stop;
  14.  
  15. function Sakura(x, y, s, r, fn) {
  16. this.x = x;
  17. this.y = y;
  18. this.s = s;
  19. this.r = r;
  20. this.fn = fn;
  21. }
  22.  
  23. Sakura.prototype.draw = function (cxt) {
  24. cxt.save();
  25. const img = new Image();
  26. img.src = weatherImg;
  27. cxt.translate(this.x, this.y);
  28. cxt.rotate(this.r);
  29. cxt.drawImage(img, 0, 0, 20 * this.s, 20 * this.s)
  30. cxt.restore();
  31. }
  32.  
  33. Sakura.prototype.update = function () {
  34. this.x = this.fn.x(this.x, this.y);
  35. this.y = this.fn.y(this.y, this.y);
  36. this.r = this.fn.r(this.r);
  37. if (this.x > window.innerWidth ||
  38. this.x < 0 ||
  39. this.y > window.innerHeight ||
  40. this.y < 0
  41. ) {
  42. this.r = getRandom('fnr');
  43. if (Math.random() > 0.4) {
  44. this.x = getRandom('x');
  45. this.y = 0;
  46. this.s = getRandom('s');
  47. this.r = getRandom('r');
  48. } else {
  49. this.x = window.innerWidth;
  50. this.y = getRandom('y');
  51. this.s = getRandom('s');
  52. this.r = getRandom('r');
  53. }
  54. }
  55. }
  56.  
  57. SakuraList = function () {
  58. this.list = [];
  59. }
  60. SakuraList.prototype.push = function (sakura) {
  61. this.list.push(sakura);
  62. }
  63. SakuraList.prototype.update = function () {
  64. for (var i = 0, len = this.list.length; i < len; i++) {
  65. this.list[i].update();
  66. }
  67. }
  68. SakuraList.prototype.draw = function (cxt) {
  69. for (var i = 0, len = this.list.length; i < len; i++) {
  70. this.list[i].draw(cxt);
  71. }
  72. }
  73. SakuraList.prototype.get = function (i) {
  74. return this.list[i];
  75. }
  76. SakuraList.prototype.size = function () {
  77. return this.list.length;
  78. }
  79.  
  80. function getRandom(option) {
  81. var ret, random;
  82. switch (option) {
  83. case 'x':
  84. ret = Math.random() * window.innerWidth;
  85. break;
  86. case 'y':
  87. ret = Math.random() * window.innerHeight;
  88. break;
  89. case 's':
  90. ret = Math.random();
  91. break;
  92. case 'r':
  93. ret = Math.random() * 6;
  94. break;
  95. case 'fnx':
  96. random = -0.5 + Math.random() * 1;
  97. ret = function (x, y) {
  98. return x + 0.5 * random - 1.7;
  99. };
  100. break;
  101. case 'fny':
  102. random = 1.5 + Math.random() * 0.7
  103. ret = function (x, y) {
  104. return y + random;
  105. };
  106. break;
  107. case 'fnr':
  108. random = Math.random() * 0.03;
  109. ret = function (r) {
  110. return r + random;
  111. };
  112. break;
  113. }
  114. return ret;
  115. }
  116.  
  117. function startSakura() {
  118.  
  119. requestAnimationFrame = window.requestAnimationFrame ||
  120. window.mozRequestAnimationFrame ||
  121. window.webkitRequestAnimationFrame ||
  122. window.msRequestAnimationFrame ||
  123. window.oRequestAnimationFrame;
  124. var canvas = document.createElement('canvas'),
  125. cxt;
  126. canvas.height = document.getElementById('page-header').offsetHeight;
  127. canvas.width = window.innerWidth;
  128. canvas.setAttribute('style', 'position: absolute;left: 0;top: 0;pointer-events: none;z-index: 99;');
  129. canvas.setAttribute('id', 'canvas_sakura');
  130. document.getElementsByTagName('body')[0].appendChild(canvas);
  131. cxt = canvas.getContext('2d');
  132. var sakuraList = new SakuraList();
  133. for (var i = 0; i < 50; i++) {
  134. var sakura, randomX, randomY, randomS, randomR, randomFnx, randomFny;
  135. randomX = getRandom('x');
  136. randomY = getRandom('y');
  137. randomR = getRandom('r');
  138. randomS = getRandom('s');
  139. randomFnx = getRandom('fnx');
  140. randomFny = getRandom('fny');
  141. randomFnR = getRandom('fnr');
  142. sakura = new Sakura(randomX, randomY, randomS, randomR, {
  143. x: randomFnx,
  144. y: randomFny,
  145. r: randomFnR
  146. });
  147. sakura.draw(cxt);
  148. sakuraList.push(sakura);
  149. }
  150. stop = requestAnimationFrame(function () {
  151. cxt.clearRect(0, 0, canvas.width, canvas.height);
  152. sakuraList.update();
  153. sakuraList.draw(cxt);
  154. stop = requestAnimationFrame(arguments.callee);
  155. })
  156. }
  157.  
  158.  
  159. function staticWeather() {
  160. const canvas = document.createElement('canvas');
  161. let height = document.getElementById('page-header').offsetHeight;
  162. let width = window.innerWidth;
  163. canvas.height = height;
  164. canvas.width = width;
  165. canvas.setAttribute('style', 'position: absolute;left: 0;top:0;pointer-events: none;z-index: 99;');
  166. canvas.setAttribute('id', 'sun');
  167. document.getElementsByTagName('body')[0].appendChild(canvas);
  168. const c = document.getElementById("sun");
  169. const ctx = c.getContext("2d");
  170.  
  171. const img = new Image();
  172. img.src = staticImg;
  173.  
  174. img.onload = function () {
  175. ctx.drawImage(img, width / 2, height / 7)
  176. }
  177. }
  178.  
  179.  
  180.  
  181. <canvas height="360" width="725" style="position: absolute;left: 0;top: 0;pointer-events: none;z-index: 99;" id="canvas_sakura"></canvas>
Add Comment
Please, Sign In to add comment