mixin.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * @Author: wangjiacheng
  3. * @Date: 2021-10-15 10:58:28
  4. * @LastEditors: wangjiacheng
  5. * @LastEditTime: 2021-10-15 14:17:38
  6. * @Description:
  7. */
  8. export default {
  9. methods: {
  10. /* eslint-disable */
  11. fireworkss() {
  12. // window.addEventListener("resize", resizeCanvas, false);
  13. // window.addEventListener("DOMContentLoaded", onLoad, false);
  14. onLoad()
  15. window.requestAnimationFrame =
  16. window.requestAnimationFrame ||
  17. window.webkitRequestAnimationFrame ||
  18. window.mozRequestAnimationFrame ||
  19. window.oRequestAnimationFrame ||
  20. window.msRequestAnimationFrame ||
  21. function (callback) {
  22. window.setTimeout(callback, 1000 / 60);
  23. };
  24. var canvas, ctx, w, h, particles = [], probability = 0.04,
  25. xPoint, yPoint;
  26. function onLoad() {
  27. canvas = document.getElementById("canvas");
  28. ctx = canvas.getContext("2d");
  29. resizeCanvas();
  30. window.requestAnimationFrame(updateWorld);
  31. }
  32. function resizeCanvas() {
  33. if (!!canvas) {
  34. w = canvas.width = window.innerWidth;
  35. h = canvas.height = window.innerHeight;
  36. }
  37. }
  38. function updateWorld() {
  39. update();
  40. paint();
  41. window.requestAnimationFrame(updateWorld);
  42. }
  43. function update() {
  44. if (particles.length < 500 && Math.random() < probability) {
  45. createFirework();
  46. }
  47. var alive = [];
  48. for (var i = 0; i < particles.length; i++) {
  49. if (particles[i].move()) {
  50. alive.push(particles[i]);
  51. }
  52. }
  53. particles = alive;
  54. }
  55. function paint() {
  56. ctx.globalCompositeOperation = 'source-over';
  57. ctx.fillStyle = "rgba(0,0,0,0.2)";
  58. ctx.fillRect(0, 0, w, h);
  59. ctx.globalCompositeOperation = 'lighter';
  60. for (var i = 0; i < particles.length; i++) {
  61. particles[i].draw(ctx);
  62. }
  63. }
  64. function createFirework() {
  65. xPoint = Math.random() * (w - 200) + 100;
  66. yPoint = Math.random() * (h - 200) + 100;
  67. var nFire = Math.random() * 50 + 100;
  68. var c = "rgb(" + (~~(Math.random() * 200 + 55)) + ","
  69. + (~~(Math.random() * 200 + 55)) + "," + (~~(Math.random() * 200 + 55)) + ")";
  70. for (var i = 0; i < nFire; i++) {
  71. var particle = new Particle();
  72. particle.color = c;
  73. var vy = Math.sqrt(25 - particle.vx * particle.vx);
  74. if (Math.abs(particle.vy) > vy) {
  75. particle.vy = particle.vy > 0 ? vy : -vy;
  76. }
  77. particles.push(particle);
  78. }
  79. }
  80. function Particle() {
  81. this.w = this.h = Math.random() * 4 + 1;
  82. this.x = xPoint - this.w / 2;
  83. this.y = yPoint - this.h / 2;
  84. this.vx = (Math.random() - 0.5) * 10;
  85. this.vy = (Math.random() - 0.5) * 10;
  86. this.alpha = Math.random() * .5 + .5;
  87. this.color;
  88. }
  89. Particle.prototype = {
  90. gravity: 0.05,
  91. move: function () {
  92. this.x += this.vx;
  93. this.vy += this.gravity;
  94. this.y += this.vy;
  95. this.alpha -= 0.01;
  96. if (this.x <= -this.w || this.x >= screen.width ||
  97. this.y >= screen.height ||
  98. this.alpha <= 0) {
  99. return false;
  100. }
  101. return true;
  102. },
  103. draw: function (c) {
  104. c.save();
  105. c.beginPath();
  106. c.translate(this.x + this.w / 2, this.y + this.h / 2);
  107. c.arc(0, 0, this.w, 0, Math.PI * 2);
  108. c.fillStyle = this.color;
  109. c.globalAlpha = this.alpha;
  110. c.closePath();
  111. c.fill();
  112. c.restore();
  113. }
  114. }
  115. },
  116. fireworks() {
  117. var _createClass = function () {
  118. function defineProperties(target, props) {
  119. for (var i = 0; i < props.length; i++) {
  120. var descriptor = props[i];
  121. descriptor.enumerable = descriptor.enumerable || false;
  122. descriptor.configurable = true;
  123. if ("value" in descriptor) descriptor.writable = true;
  124. Object.defineProperty(target, descriptor.key, descriptor);
  125. }
  126. }
  127. return function (Constructor, protoProps, staticProps) {
  128. if (protoProps) defineProperties(Constructor.prototype, protoProps);
  129. if (staticProps) defineProperties(Constructor, staticProps);
  130. return Constructor;
  131. };
  132. }();
  133. function _classCallCheck(instance, Constructor) {
  134. if (!(instance instanceof Constructor)) {
  135. throw new TypeError("Cannot call a class as a function");
  136. }
  137. }
  138. var Progress = function () {
  139. function Progress() {
  140. var param = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
  141. _classCallCheck(this, Progress);
  142. this.timestamp = null;
  143. this.duration = param.duration || Progress.CONST.DURATION;
  144. this.progress = 0;
  145. this.delta = 0;
  146. this.progress = 0;
  147. this.isLoop = !!param.isLoop;
  148. this.reset();
  149. }
  150. Progress.prototype.reset = function reset() {
  151. this.timestamp = null;
  152. };
  153. Progress.prototype.start = function start(now) {
  154. this.timestamp = now;
  155. };
  156. Progress.prototype.tick = function tick(now) {
  157. if (this.timestamp) {
  158. this.delta = now - this.timestamp;
  159. this.progress = Math.min(this.delta / this.duration, 1);
  160. if (this.progress >= 1 && this.isLoop) {
  161. this.start(now);
  162. }
  163. return this.progress;
  164. } else {
  165. return 0;
  166. }
  167. };
  168. _createClass(Progress, null, [{
  169. key: "CONST",
  170. get: function get() {
  171. return {
  172. DURATION: 1000
  173. };
  174. }
  175. }]);
  176. return Progress;
  177. }();
  178. var Confetti = function () {
  179. function Confetti(param) {
  180. _classCallCheck(this, Confetti);
  181. this.parent = param.elm || document.body;
  182. this.canvas = document.createElement("canvas");
  183. this.ctx = this.canvas.getContext("2d");
  184. this.width = param.width || this.parent.offsetWidth;
  185. this.height = param.height || this.parent.offsetHeight;
  186. this.length = param.length || Confetti.CONST.PAPER_LENGTH;
  187. this.yRange = param.yRange || this.height * 2;
  188. this.progress = new Progress({
  189. duration: param.duration,
  190. isLoop: true
  191. });
  192. this.rotationRange = typeof param.rotationLength === "number" ? param.rotationRange : 10;
  193. this.speedRange = typeof param.speedRange === "number" ? param.speedRange : 10;
  194. this.sprites = [];
  195. this.canvas.style.cssText = ["display: block", "position: absolute", "top: 0", "left: 0", "pointer-events: none"].join(";");
  196. this.render = this.render.bind(this);
  197. this.build();
  198. this.parent.append(this.canvas);
  199. this.progress.start(performance.now());
  200. requestAnimationFrame(this.render);
  201. }
  202. Confetti.prototype.build = function build() {
  203. for (var i = 0; i < this.length; ++i) {
  204. var canvas = document.createElement("canvas"),
  205. ctx = canvas.getContext("2d");
  206. canvas.width = Confetti.CONST.SPRITE_WIDTH;
  207. canvas.height = Confetti.CONST.SPRITE_HEIGHT;
  208. canvas.position = {
  209. initX: Math.random() * this.width,
  210. initY: -canvas.height - Math.random() * this.yRange
  211. };
  212. canvas.rotation = this.rotationRange / 2 - Math.random() * this.rotationRange;
  213. canvas.speed = this.speedRange / 2 + Math.random() * (this.speedRange / 2);
  214. ctx.save();
  215. ctx.fillStyle = Confetti.CONST.COLORS[Math.random() * Confetti.CONST.COLORS.length | 0];
  216. ctx.fillRect(0, 0, canvas.width, canvas.height);
  217. ctx.restore();
  218. this.sprites.push(canvas);
  219. }
  220. };
  221. Confetti.prototype.render = function render(now) {
  222. var progress = this.progress.tick(now);
  223. this.canvas.width = this.width;
  224. this.canvas.height = this.height;
  225. for (var i = 0; i < this.length; ++i) {
  226. this.ctx.save();
  227. this.ctx.translate(this.sprites[i].position.initX + this.sprites[i].rotation * Confetti.CONST.ROTATION_RATE * progress, this.sprites[i].position.initY + progress * (this.height + this.yRange));
  228. this.ctx.rotate(this.sprites[i].rotation);
  229. this.ctx.drawImage(this.sprites[i], -Confetti.CONST.SPRITE_WIDTH * Math.abs(Math.sin(progress * Math.PI * 2 * this.sprites[i].speed)) / 2, -Confetti.CONST.SPRITE_HEIGHT / 2, Confetti.CONST.SPRITE_WIDTH * Math.abs(Math.sin(progress * Math.PI * 2 * this.sprites[i].speed)), Confetti.CONST.SPRITE_HEIGHT);
  230. this.ctx.restore();
  231. }
  232. requestAnimationFrame(this.render);
  233. };
  234. _createClass(Confetti, null, [{
  235. key: "CONST",
  236. get: function get() {
  237. return {
  238. SPRITE_WIDTH: 9,
  239. SPRITE_HEIGHT: 16,
  240. PAPER_LENGTH: 100,
  241. DURATION: 8000,
  242. ROTATION_RATE: 50,
  243. COLORS: ["#EF5350", "#EC407A", "#AB47BC", "#7E57C2", "#5C6BC0", "#42A5F5", "#29B6F6", "#26C6DA", "#26A69A", "#66BB6A", "#9CCC65", "#D4E157", "#FFEE58", "#FFCA28", "#FFA726", "#FF7043", "#8D6E63", "#BDBDBD", "#78909C"]
  244. };
  245. }
  246. }]);
  247. return Confetti;
  248. }();
  249. (function () {
  250. var DURATION = 6000,
  251. LENGTH = 220;
  252. new Confetti({
  253. width: window.innerWidth,
  254. height: window.innerHeight,
  255. length: LENGTH,
  256. duration: DURATION
  257. });
  258. setTimeout(function () {
  259. new Confetti({
  260. width: window.innerWidth,
  261. height: window.innerHeight,
  262. length: LENGTH,
  263. duration: DURATION
  264. });
  265. }, DURATION / 2);
  266. })();
  267. }
  268. }
  269. }