vue实现直播间点赞飘心效果的示例代码

2020-06-12 21:05:07易采站长站整理

前言:

在开发公司项目的时候,遇到了直播间的一些功能,其中点赞冒泡飘心,就折腾了好久,canvas学的不好,自己写不来,百度找了一堆都是js原生写法,迁移到vue项目里来好多问题,百度也解决不了。自己试着慢慢解决,竟然在不知不觉中通了!废话不多说,直接上代码,复制粘贴即可使用

示例:

不动就不动吧.png


```第一步```:先在外部新建一个js文件,取名index.js(名字自己随便取)

index.js代码内容如下:

/**
* LikeHeart
* @version: 1.0.0
* @author tennylv
* @date 2018-05-24
*
*/
'use strict';
(function (root, factory) {
if (typeof exports === 'object') {
module.exports = factory();
//CMD
} else if (typeof define === 'function' && define.amd) {
define(factory);
//AMD
} else {
//WINDOW
root.LikeHeart = factory();
}
})(this, function() {

var LikeHeart = function(opt) {

/**
* 初始化心
*
* @param {object}
* @object.x {number} 心起点位置x
* @object.y {number} 心起点位置y
* @object.endX {number} 心结束位置x
* @object.endY {number} 心结束位置y
* @object.height {number} 高
* @object.width {number} 宽
* @object.angelBegin {number} 左右摇摆起始角度(可为负值)
* @object.angelEnd {number} 左右摇摆结束角度
* @object.angleLeft {bool} 是否起始从坐往右摇摆
* @object.noScale {bool} 是否使用缩放心动画
* @object.scaleDis {number} 缩放心临界值(默认从起始位置到升高50)
* @object.noFadeOut {bool} 是否使用fadeOut
* @object.opacityDis {number} fadeout心临界值(默认距离结束位置40)
* @object.speed {number} 上升速度
* @object.bezierPoint {obj} 贝塞尔曲线4个点的值参考https://aaaaaaaty.github.io/bezierMaker.js/playground/playground.html
* @object.fadeOut {function} 每个心fadeOut之后回调
* @object.image {obj} 图片对象
*/

this.id = opt.id;
this.x = opt.x;
this.y = opt.y;
this.endX = opt.endX;
this.endY = opt.endY;
this.orignY = opt.y;
this.height = opt.height;
this.width = opt.width;
this.angle = 0;
this.angleLeft = opt.angleLeft;
this.angelBegin = opt.angelBegin || (-20 + rand(1,2));
this.angelEnd = opt.angelEnd || (20 + rand(1,4));
this.scale = 0;
this.scaleDis = opt.scaleDis || 50;
this.opacityDis = opt.opacityDis || 40;
this.noScale = opt.noScale;