您好,登錄后才能下訂單哦!
前言:
在開發(fā)公司項(xiàng)目的時(shí)候,遇到了直播間的一些功能,其中點(diǎn)贊冒泡飄心,就折騰了好久,canvas學(xué)的不好,自己寫不來(lái),百度找了一堆都是js原生寫法,遷移到vue項(xiàng)目里來(lái)好多問題,百度也解決不了。自己試著慢慢解決,竟然在不知不覺中通了!廢話不多說(shuō),直接上代碼,復(fù)制粘貼即可使用
示例:
不動(dòng)就不動(dòng)吧.png
```第一步```:先在外部新建一個(gè)js文件,取名index.js(名字自己隨便?。? index.js代碼內(nèi)容如下: /** * 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} 心起點(diǎn)位置x * @object.y {number} 心起點(diǎn)位置y * @object.endX {number} 心結(jié)束位置x * @object.endY {number} 心結(jié)束位置y * @object.height {number} 高 * @object.width {number} 寬 * @object.angelBegin {number} 左右搖擺起始角度(可為負(fù)值) * @object.angelEnd {number} 左右搖擺結(jié)束角度 * @object.angleLeft {bool} 是否起始從坐往右搖擺 * @object.noScale {bool} 是否使用縮放心動(dòng)畫 * @object.scaleDis {number} 縮放心臨界值(默認(rèn)從起始位置到升高50) * @object.noFadeOut {bool} 是否使用fadeOut * @object.opacityDis {number} fadeout心臨界值(默認(rèn)距離結(jié)束位置40) * @object.speed {number} 上升速度 * @object.bezierPoint {obj} 貝塞爾曲線4個(gè)點(diǎn)的值參考https://aaaaaaaty.github.io/bezierMaker.js/playground/playground.html * @object.fadeOut {function} 每個(gè)心fadeOut之后回調(diào) * @object.image {obj} 圖片對(duì)象 */ 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; this.noAngel = opt.noAngel; this.opacity = 1; this.speed = opt.speed || 0.0027; this.bezierPoint = opt.bezierPoint; this.bezierDis = 0; this.onFadeOut = opt.onFadeOut; this.IMG = opt.image; this.move = function (ctx) { if (this.opacity === 0) { this.onFadeOut && this.onFadeOut(this); } this.y = getBezierLine(this).yt; this.x = getBezierLine(this).xt; this.angle = rangeAngle(this); this.scale = getFScale(this); this.opacity = getFAlpha(this); ctx.save(); ctx.translate(this.x, this.y); ctx.rotate(this.angle*(Math.PI/180)); ctx.scale(this.scale, this.scale); ctx.globalAlpha = this.opacity; ctx.drawImage(this.IMG, -(this.IMG.width/2), -(this.IMG.height/2), this.width, this.height); ctx.restore(); }; }; /** * 計(jì)算心左右搖擺的方法 */ function rangeAngle(heart) { if (heart.noAngel) { return 0; } let _angle = heart.angle; // 心介于[start, end]之間不斷變化角度 if(_angle >= heart.angelEnd) { // 角度不斷變小,向左搖擺 heart.angleLeft = false; } else if (_angle <= heart.angelBegin){ // 角度不斷變大,向又搖擺 heart.angleLeft = true; } // 動(dòng)態(tài)改變角度 if (heart.angleLeft) { _angle = _angle + 1; } else { _angle = _angle - 1; } return _angle; } /** * 計(jì)算縮放角度的方法 */ function getFScale(heart){ if (heart.noScale) { return 1; } let _scale = heart.scale; // 隨著距離起始點(diǎn)的距離增加,scale不斷變大 let dis = heart.orignY - heart.y; _scale = (dis / heart.scaleDis); // 當(dāng)大于設(shè)置的閾值時(shí)變成1 if (dis >= heart.scaleDis) { _scale = 1; } return _scale; } /** * 計(jì)算透明度的方法 */ function getFAlpha(heart) { let _opacity = heart.opacity; let dis = heart.y - heart.endY; if (dis <= heart.opacityDis) { _opacity = Math.max((dis / heart.opacityDis), 0); } else { _opacity = 1; } return _opacity; } /** * 獲得min-max的隨機(jī)整數(shù) */ function rand (min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } /** * 獲得貝塞爾曲線路徑 * 一共4個(gè)點(diǎn) */ function getBezierLine(heart){ var obj = heart.bezierPoint; var p0 = obj.p0; var p1 = obj.p1; var p2 = obj.p2; var p3 = obj.p3; var t = heart.bezierDis; var cx = 3 * (p1.x - p0.x), bx = 3 * (p2.x - p1.x) - cx, ax = p3.x - p0.x - cx - bx, cy = 3 * (p1.y - p0.y), by = 3 * (p2.y - p1.y) - cy, ay = p3.y - p0.y - cy - by, xt = ax * (t * t * t) + bx * (t * t) + cx * t + p0.x, yt = ay * (t * t * t) + by * (t * t) + cy * t + p0.y; heart.bezierDis += heart.speed; return { xt: xt, yt: yt } } return LikeHeart; }); ```第二步```:引入需要用到的頁(yè)面 import LikeHeart from "../../../static/js/index"; ```第三步```:直接復(fù)制下面這一段 <script> import LikeHeart from "../../../static/js/index"; export default { props: ["ClassTimePlayer", "videoUrl"], data() { return { width: 175, //初始寬度 height: 400, //初始高度 heartList: [], //初始數(shù)組 heartCount: 0 //累加計(jì)數(shù)初始值 }; }, methods: { getRandomDis() { if (Math.random() > 0.5) { return -(Math.random() * 43); } else { return +(Math.random() * 43); } }, createHeart() { this.heartCount++; let positionArray = [ { x: 100, y: 400, endX: 100, endY: 100 } ]; let img = new Image(); // img.src = "../../static/img/" + Math.ceil(Math.random() * 2) + ".png"; img.src = `../../static/img/${Math.ceil(Math.random() * 5)}.png`; let p1 = { x: 100 + this.getRandomDis(), y: 300 + this.getRandomDis() }; let p2 = { x: 100 + this.getRandomDis(), y: 200 + this.getRandomDis() }; return new LikeHeart({ id: this.heartCount, x: positionArray[0].x, y: positionArray[0].y, endX: positionArray[0].endX, endY: positionArray[0].endY, onFadeOut: this.removeItem, noAngel: true,//決定是否從小到大 // noScale: true,//決定是否左右擺動(dòng) width: 30, //決定心的大小 height: 30, image: img, bezierPoint: { p0: { x: positionArray[0].x, y: positionArray[0].y }, p1: p1, p2: p2, p3: { x: positionArray[0].endX, y: positionArray[0].endY } } }); }, removeItem(item) { var array = []; for (var i = 0; i < this.heartList.length; i++) { if (this.heartList[i].id !== item.id) { array.push(this.heartList[i]); } } this.heartList = array; }, }, mounted() { // 飄心 var _this = this; var ctx = document.getElementById("cvs").getContext("2d"); (ctx.canvas.width = _this.width), (ctx.canvas.height = _this.height), (function loop() { ctx.clearRect(0, 0, _this.width, _this.height); _this.heartList.forEach(function(item) { item && item.move(ctx); }); requestAnimationFrame(loop); })(); setInterval(function() { _this.heartList.push(_this.createHeart()); }, 700); document.getElementById("cvs").addEventListener( "click", function() { console.log(111111) _this.heartList.push(_this.createHeart()); }, false ); }, }; </script> 圖片自己去換,至于在哪里換 img.src = `../../static/img/${Math.ceil(Math.random() * 5)}.png`; 這個(gè)就是咯 ```最后一步```:在html里,寫上這個(gè) <!-- 飄心 --> <canvas id="cvs"></canvas>
收尾:
然后就實(shí)現(xiàn)了。這個(gè)代碼我也是百度的某個(gè)大神的,最后說(shuō)明下不是我寫的哈。遷移到vue中稍微修改了一下,勿噴。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。