基于Canvas+Vue的彈幕組件的實(shí)現(xiàn)
最近由于項(xiàng)目需要定制化一個(gè)彈幕功能,所以嘗試使用canvas來(lái)開(kāi)發(fā)組件。經(jīng)過(guò)測(cè)試在一些低端機(jī)的效果也沒(méi)有明顯的卡頓,和大家交流一下
彈幕效果

功能介紹
- 支持循環(huán)彈幕
- 彈幕不重疊
- 支持選擇軌道數(shù)
- 支持彈幕發(fā)送
使用
npm i vue-barrage
參數(shù)配置
| name | type | default | desc |
|---|---|---|---|
| barrageList | Array | [] | 彈幕數(shù)據(jù) |
| speed | Number | 4 | 彈幕滾動(dòng)速度 |
| loop | Boolean | true | 是否循環(huán)滾動(dòng) |
| channels | Number | 2 | 彈幕軌道數(shù) |
功能實(shí)現(xiàn)
html樣式
<template>
<div class="barrage-container">
<div
class="container"
:style="{height: barrageHeight/2+'px'}">
<canvas
id="canvas"
ref="canvas"
:width="barrageWidth"
:height="barrageHeight"
:style="{'width': barrageWidth/2 + 'px','height': barrageHeight/2 + 'px'}"/>
</div>
</div>
</template>
js實(shí)現(xiàn)
監(jiān)聽(tīng)數(shù)據(jù)源
watch: {
barrageList (val) {
if (val.length !== 0) {
this.initData() // 數(shù)據(jù)初始化
this.render() // 開(kāi)始渲染
}
}
}
數(shù)據(jù)初始化
barrageArray 是存儲(chǔ)彈幕數(shù)據(jù)用的,包括默認(rèn)彈幕列表和新增彈幕項(xiàng)
/**
* 數(shù)據(jù)初始化
*/
initData () {
for (let i = 0; i < this.barrageList.length; i++) { // 此處處理只顯示40個(gè)字符
let content = this.barrageList[i].content.length > 40 ? `${this.barrageList[i].content.substring(0, 40)}...` : this.barrageList[i].content
this.pushMessage(content, this.barrageList[i].color)
}
},
/**
* 增加數(shù)據(jù)
* @param content
* @param color
*/
pushMessage (content, color) {
let position = this.getPosition() // 確定跑道位置
let x = this.barrageWidth // 初始位置
let offsetWidth = 0
for (let i = 0, len = this.barrageArray.length; i < len; i++) {
let item = this.barrageArray[i]
if (position === item.position) { // 如果同跑道,則往后排
offsetWidth += Math.floor(this.ctx.measureText(item.content).width * 3 + 60)
}
}
this.barrageArray.push({
content: content, // 彈幕內(nèi)容
x: x + offsetWidth, // 確定每一條彈幕的初始位置
originX: x + offsetWidth, // 存儲(chǔ)當(dāng)前彈幕的位置,以便在循環(huán)的時(shí)候使用
position: position,
width: this.ctx.measureText(content).width * 3, // canvas繪制內(nèi)容寬度
color: color || this.getColor() // 自定義顏色
})
},
初始化數(shù)據(jù)需要處理的就是計(jì)算當(dāng)前彈幕的軌道、位置、寬度,以便在 canvas 繪制的時(shí)候使用
繪制 canvas
/**
* 渲染
*/
render () {
this.ctx.clearRect(0, 0, this.barrageWidth, this.barrageHeight)
this.ctx.font = '30px Microsoft YaHei'
this.draw()
window.requestAnimationFrame(this.render) // 每隔16.6毫秒渲染一次,如果使用setInterval的話在低端機(jī)型會(huì)有點(diǎn)卡頓
},
/**
* 開(kāi)始繪制 文字和背景
*/
draw () {
for (let i = 0, len = this.barrageArray.length; i < len; i++) {
let barrage = this.barrageArray[i]
try {
barrage.x -= this.speed
if (barrage.x < -barrage.width - 100) { // 此處判斷彈幕消失時(shí)機(jī)
if (i === this.barrageArray.length - 1) { // 最后一條消失時(shí)的判斷邏輯
if (!this.loop) { //如果不是循環(huán)彈幕的話就取消繪制 判斷是否循環(huán),不循環(huán)執(zhí)行cancelAnimationFrame
cancelAnimationFrame(this.render)
return
}
if (this.addArray.length !== 0) { // 此處判斷增加彈幕的邏輯
this.barrageArray = this.barrageArray.concat(this.addArray)
this.addArray = []
}
for (let j = 0; j < this.barrageArray.length; j++) { // 給每條彈幕的x初始值
this.barrageArray[j].x = this.barrageArray[j].originX
}
}
}
if (barrage.x <= 2 * document.body.clientWidth + barrage.width) { // 判斷什么時(shí)候開(kāi)始繪制,如果不判斷的話會(huì)導(dǎo)致彈幕滾動(dòng)卡頓
// 繪制背景
this.drawRoundRect(this.ctx, barrage.x - 15, barrage.position - 30, barrage.width + 30, 40, 20, `rgba(0,0,0,0.75)`)
// 繪制文字
this.ctx.fillStyle = `${barrage.color}`
this.ctx.fillText(barrage.content, barrage.x, barrage.position)
}
} catch (e) {
console.log(e)
}
}
},
此處判斷繪制邏輯,包括什么時(shí)候取消,彈幕開(kāi)始繪制判斷,彈幕消失判斷
其他函數(shù)
/**
* 獲取文字位置
* 使用pathWayIndex來(lái)確認(rèn)每一條彈幕所在的軌道
* 返回距離頂部的距離
* @TODO此處還可以?xún)?yōu)化,根據(jù)每條軌道的距離來(lái)判斷下一條彈幕出現(xiàn)位置
*/
getPosition () {
let range = this.channels
let top = (this.pathWayIndex % range) * 50 + 40
this.pathWayIndex++
return top
},
/**
* 獲取隨機(jī)顏色
*/
getColor () {
return '#' + ('00000' + (Math.random() * 0x1000000 << 0).toString(16)).slice(-6);
},
/**
* 繪畫(huà)圓角矩形
* @param context
* @param x
* @param y
* @param width
* @param height
* @param radius
* @param color
*/
drawRoundRect (context, x, y, width, height, radius, color) {
context.beginPath()
context.fillStyle = color
context.arc(x + radius, y + radius, radius, Math.PI, Math.PI * 3 / 2)
context.lineTo(width - radius + x, y)
context.arc(width - radius + x, radius + y, radius, Math.PI * 3 / 2, Math.PI * 2)
context.lineTo(width + x, height + y - radius)
context.arc(width - radius + x, height - radius + y, radius, 0, Math.PI / 2)
context.lineTo(radius + x, height + y)
context.arc(radius + x, height - radius + y, radius, Math.PI / 2, Math.PI)
context.fill()
context.closePath()
}
此處為彈幕服務(wù)函數(shù)
使用
<barrage
ref="barrage"
class="barrage"
:barrage-list="barrageList"
:speed="speed"
:loop="loop"
:channels="channels"/>
import Barrage from 'vue-barrage'
// 彈幕數(shù)據(jù)初始化
this.barrageList = [{
content: '試數(shù)據(jù)測(cè)試數(shù)測(cè)試數(shù)據(jù)數(shù)測(cè)試數(shù)據(jù)',
color: 'white'
}]
// 新增彈幕
this.$refs.barrage.add({
content: '增加一條新的彈幕增加一條新的彈幕', color: 'white'
})
結(jié)語(yǔ)
總的來(lái)說(shuō)這個(gè)組件還有可優(yōu)化的空間,后續(xù)我會(huì)繼續(xù)改進(jìn)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章

前端實(shí)現(xiàn)彈幕效果的方法總結(jié)(包含css3和canvas的實(shí)現(xiàn)方式)
這篇文章主要介紹了前端實(shí)現(xiàn)彈幕效果的方法總結(jié)(包含css3和canvas的實(shí)現(xiàn)方式)的相關(guān)資料,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-12
html5使用canvas實(shí)現(xiàn)彈幕功能示例
這篇文章主要介紹了html5使用canvas實(shí)現(xiàn)彈幕功能示例的相關(guān)資料,需要的朋友可以參考下2017-09-11- 最近在做大作業(yè)的時(shí)候需要做一個(gè)彈幕播放器,今天小編給大家分享基于HTML使用canvas實(shí)現(xiàn)彈幕功能,需要的的朋友參考下吧2017-06-21

HTML5 canvas實(shí)現(xiàn)的靜態(tài)循環(huán)滾動(dòng)播放彈幕
這篇文章主要介紹了HTML5 canvas實(shí)現(xiàn)的靜態(tài)循環(huán)滾動(dòng)播放彈幕,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)2021-01-05



