firework.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /**
  2. * 烟花特效
  3. */
  4. function Event() {
  5. this.events = {};
  6. }
  7. Event.prototype.addEventListener = function(type, listener) {
  8. this.events[type] = this.events[type] || [];
  9. this.events[type].push(listener);
  10. };
  11. Event.prototype.trigger = function() {
  12. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  13. args[_key] = arguments[_key];
  14. }
  15. var type = args[0];
  16. var params = args.slice(1);
  17. if (!!this.events[type]) {
  18. // console.log("type:",type);
  19. this.events[type].forEach(function(listener) {
  20. try {
  21. listener.apply(null, params);
  22. } catch (e) {
  23. console.error(e);
  24. }
  25. });
  26. }
  27. };
  28. // get a random number within a range
  29. function random(min, max) {
  30. return Math.random() * (max - min) + min;
  31. }
  32. // calculate the distance between two points
  33. function calculateDistance(p1x, p1y, p2x, p2y) {
  34. var xDistance = p1x - p2x,
  35. yDistance = p1y - p2y;
  36. return Math.sqrt(Math.pow(xDistance, 2) + Math.pow(yDistance, 2));
  37. }
  38. // create firework
  39. var Firework = function Firework(image, typeImage, direction, tx, ty, cw, ch, offest, hiddenMidImage) {
  40. this.event = new Event();
  41. //图片信息
  42. this.image = image;
  43. this.typeImage = typeImage;
  44. this.direction = direction;
  45. this.position = {
  46. "EF_baozha_0": [0, 0, 312, 312],
  47. "EF_baozha_1": [312, 0, 312, 312],
  48. "EF_baozha_2": [624, 0, 312, 312],
  49. "EF_baozha_3": [936, 0, 312, 312],
  50. "EF_baozha_4": [0, 312, 312, 312],
  51. "EF_baozha_5": [312, 312, 312, 312],
  52. "EF_baozha_6": [624, 312, 312, 312],
  53. "EF_baozha_7": [936, 312, 312, 312],
  54. "EF_baozha_8": [0, 624, 312, 312],
  55. "EF_baozha_9": [312, 624, 312, 312],
  56. "EF_baozha_10": [624, 624, 312, 312],
  57. "EF_baozha_11": [936, 624, 312, 312],
  58. "EF_baozha_12": [0, 936, 312, 312],
  59. "EF_baozha_13": [312, 936, 312, 312],
  60. "EF_baozha_14": [624, 936, 312, 312],
  61. "EF_baozha_15": [936, 936, 312, 312],
  62. "EF_baozha_16": [0, 1248, 312, 312],
  63. "EF_baozha_17": [312, 1248, 312, 312],
  64. "EF_baozha_18": [624, 1248, 312, 312],
  65. "EF_baozha_19": [936, 1248, 312, 312]
  66. };
  67. // target coordinates
  68. this.tx = tx;
  69. this.ty = ty;
  70. this.cw = cw;
  71. this.ch = ch;
  72. this.offest = offest;
  73. this.index = 0;
  74. // let _this = this;
  75. // this.animationInstance = new Animation({
  76. // timing: 'easeIn',
  77. // duration: 1000,
  78. // onProcess: function onProcess(process) {
  79. // _this.event.trigger('renderProcess', process);
  80. // },
  81. // onAnimationFinish: function onAnimationFinish() {
  82. // _this.event.trigger('renderComplete');
  83. // }
  84. // });
  85. this.hiddenMidImage = hiddenMidImage || false;
  86. // const innerAudioContext = uni.createInnerAudioContext();
  87. // innerAudioContext.autoplay = true;
  88. // innerAudioContext.src = '/static/elect/hit.mp3';
  89. // innerAudioContext.onPlay(() => {
  90. // // console.log('开始播放');
  91. // });
  92. // innerAudioContext.onError((res) => {
  93. // console.log(res.errMsg);
  94. // console.log(res.errCode);
  95. // });
  96. // setTimeout(() => {
  97. // innerAudioContext.destroy();
  98. // }, 800)
  99. // let p = plus.audio.createPlayer("/static/elect/hit.mp3");
  100. // p.play(function() {
  101. // alert("Audio play success!");
  102. // }, function(e) {
  103. // alert("Audio play error: " + e.message);
  104. // });
  105. // setTimeout(() => {
  106. // p.close();
  107. // }, 800)
  108. }
  109. Firework.prototype.addEventListener = function(type, listener) {
  110. this.event.addEventListener(type, listener);
  111. };
  112. // draw firework
  113. Firework.prototype.draw = function(ctx, callback) {
  114. if (this.index > 19) return;
  115. let i = this.index;
  116. let temp = this.position["EF_baozha_" + i];
  117. // console.log("draw:"+this.direction);
  118. let tempX = this.tx * this.direction;
  119. let tempValue = this.direction < 0 ? 65 : -25
  120. ctx.drawImage(this.image.path,
  121. temp[0] //截取原始图片的 x坐标
  122. , temp[1] //截取原始图片的 y坐标
  123. , 312 //截取原始图片的 宽度
  124. , 312 // 截取的高度
  125. , tempX - 264 * 0.5 - tempValue + this.offest //图片在canvas画布上的x坐标
  126. , -50 //图片在canvas画布上的y坐标
  127. , 264 //绘制图片的宽度
  128. , 264 //绘制图片的高度
  129. );
  130. if (!this.hiddenMidImage) {
  131. //19
  132. let _r = this.index / 32 + 1;
  133. ctx.save();
  134. //如果是相反绘制,需要加多一个自身位置偏移
  135. let _pos = this.direction < 0 ? this.typeImage.width : 0;
  136. //左边位置
  137. let left = (this.typeImage.width + tempX - 164 * 0.5) - (this.typeImage.width / 2 - _pos) * _r + 20 -
  138. tempValue + this.offest;
  139. // 中心点 this.cw / 2 - this.typeImage.width / 2 * _r
  140. ctx.translate(left, this.ch / 2 - this.typeImage.height / 2 * _r);
  141. ctx.scale(_r * this.direction, _r);
  142. ctx.drawImage(this.typeImage.path, 0, 0);
  143. ctx.restore();
  144. }
  145. if (i === 19 && callback) {
  146. callback();
  147. }
  148. this.index++;
  149. }
  150. if (typeof module === "object" && typeof module.exports === "object") {
  151. module.exports = Firework;
  152. }