| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 | 
							- /**
 
-  * 烟花特效
 
-  */
 
- function Event() {
 
- 	this.events = {};
 
- }
 
- Event.prototype.addEventListener = function(type, listener) {
 
- 	this.events[type] = this.events[type] || [];
 
- 	this.events[type].push(listener);
 
- };
 
- Event.prototype.trigger = function() {
 
- 	for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
 
- 		args[_key] = arguments[_key];
 
- 	}
 
- 	var type = args[0];
 
- 	var params = args.slice(1);
 
- 	if (!!this.events[type]) {
 
- 		// console.log("type:",type);
 
- 		this.events[type].forEach(function(listener) {
 
- 			try {
 
- 				listener.apply(null, params);
 
- 			} catch (e) {
 
- 				console.error(e);
 
- 			}
 
- 		});
 
- 	}
 
- };
 
- // get a random number within a range
 
- function random(min, max) {
 
- 	return Math.random() * (max - min) + min;
 
- }
 
- // calculate the distance between two points
 
- function calculateDistance(p1x, p1y, p2x, p2y) {
 
- 	var xDistance = p1x - p2x,
 
- 		yDistance = p1y - p2y;
 
- 	return Math.sqrt(Math.pow(xDistance, 2) + Math.pow(yDistance, 2));
 
- }
 
- // create firework
 
- var Firework = function Firework(image, typeImage, direction, tx, ty, cw, ch, offest, hiddenMidImage) {
 
- 	this.event = new Event();
 
- 	//图片信息
 
- 	this.image = image;
 
- 	this.typeImage = typeImage;
 
- 	this.direction = direction;
 
- 	this.position = {
 
- 		"EF_baozha_0": [0, 0, 312, 312],
 
- 		"EF_baozha_1": [312, 0, 312, 312],
 
- 		"EF_baozha_2": [624, 0, 312, 312],
 
- 		"EF_baozha_3": [936, 0, 312, 312],
 
- 		"EF_baozha_4": [0, 312, 312, 312],
 
- 		"EF_baozha_5": [312, 312, 312, 312],
 
- 		"EF_baozha_6": [624, 312, 312, 312],
 
- 		"EF_baozha_7": [936, 312, 312, 312],
 
- 		"EF_baozha_8": [0, 624, 312, 312],
 
- 		"EF_baozha_9": [312, 624, 312, 312],
 
- 		"EF_baozha_10": [624, 624, 312, 312],
 
- 		"EF_baozha_11": [936, 624, 312, 312],
 
- 		"EF_baozha_12": [0, 936, 312, 312],
 
- 		"EF_baozha_13": [312, 936, 312, 312],
 
- 		"EF_baozha_14": [624, 936, 312, 312],
 
- 		"EF_baozha_15": [936, 936, 312, 312],
 
- 		"EF_baozha_16": [0, 1248, 312, 312],
 
- 		"EF_baozha_17": [312, 1248, 312, 312],
 
- 		"EF_baozha_18": [624, 1248, 312, 312],
 
- 		"EF_baozha_19": [936, 1248, 312, 312]
 
- 	};
 
- 	// target coordinates
 
- 	this.tx = tx;
 
- 	this.ty = ty;
 
- 	this.cw = cw;
 
- 	this.ch = ch;
 
- 	this.offest = offest;
 
- 	this.index = 0;
 
- 	// let _this = this;
 
- 	// this.animationInstance = new Animation({
 
- 	// 	timing: 'easeIn',
 
- 	// 	duration: 1000,
 
- 	// 	onProcess: function onProcess(process) {
 
- 	// 		_this.event.trigger('renderProcess', process);
 
- 	// 	},
 
- 	// 	onAnimationFinish: function onAnimationFinish() {
 
- 	// 		_this.event.trigger('renderComplete');
 
- 	// 	}
 
- 	// });
 
- 	this.hiddenMidImage = hiddenMidImage || false;
 
- 	const innerAudioContext = uni.createInnerAudioContext();
 
- 	innerAudioContext.autoplay = true;
 
- 	innerAudioContext.src = '/static/elect/hit.mp3';
 
- 	innerAudioContext.onPlay(() => {
 
- 		// console.log('开始播放');
 
- 	});
 
- 	innerAudioContext.onError((res) => {
 
- 		console.log(res.errMsg);
 
- 		console.log(res.errCode);
 
- 	});
 
- 	setTimeout(() => {
 
- 		innerAudioContext.destroy();
 
- 	}, 800)
 
- }
 
- Firework.prototype.addEventListener = function(type, listener) {
 
- 	this.event.addEventListener(type, listener);
 
- };
 
- // draw firework
 
- Firework.prototype.draw = function(ctx, callback) {
 
- 	if (this.index > 19) return;
 
- 	let i = this.index;
 
- 	let temp = this.position["EF_baozha_" + i];
 
- 	// console.log("draw:"+this.direction);
 
- 	let tempX = this.tx * this.direction;
 
- 	let tempValue = this.direction < 0 ? 65 : -25
 
- 	ctx.drawImage(this.image.path,
 
- 		temp[0] //截取原始图片的 x坐标
 
- 		, temp[1] //截取原始图片的 y坐标
 
- 		, 312 //截取原始图片的 宽度
 
- 		, 312 // 截取的高度
 
- 		, tempX - 264 * 0.5 - tempValue + this.offest //图片在canvas画布上的x坐标
 
- 		, -50 //图片在canvas画布上的y坐标
 
- 		, 264 //绘制图片的宽度
 
- 		, 264 //绘制图片的高度
 
- 	);
 
- 	if (!this.hiddenMidImage) {
 
- 		//19
 
- 		let _r = this.index / 32 + 1;
 
- 		ctx.save();
 
- 		//如果是相反绘制,需要加多一个自身位置偏移
 
- 		let _pos = this.direction < 0 ? this.typeImage.width : 0;
 
- 		//左边位置
 
- 		let left = (this.typeImage.width + tempX - 164 * 0.5) - (this.typeImage.width / 2 - _pos) * _r + 20 -
 
- 			tempValue + this.offest;
 
- 		// 中心点 this.cw / 2 - this.typeImage.width / 2 * _r
 
- 		ctx.translate(left, this.ch / 2 - this.typeImage.height / 2 * _r);
 
- 		ctx.scale(_r * this.direction, _r);
 
- 		ctx.drawImage(this.typeImage.path, 0, 0);
 
- 		ctx.restore();
 
- 	}
 
- 	if (i === 19 && callback) {
 
- 		callback();
 
- 	}
 
- 	this.index++;
 
- }
 
- if (typeof module === "object" && typeof module.exports === "object") {
 
- 	module.exports = Firework;
 
- }
 
 
  |