头闻号

广州英桐生物科技有限公司

家居用品加工|皮肤用化学品|瘦身化学品|丰胸化学品|其他日用化学品|面膜

首页 > 新闻中心 > 科技常识:基于Canvas+Vue的弹幕组件的实现
科技常识:基于Canvas+Vue的弹幕组件的实现
发布时间:2023-02-01 10:08:51        浏览次数:2        返回列表

今天小编跟大家讲解下有关基于Canvas+Vue的弹幕组件的实现 ,相信小伙伴们对这个话题应该有所关注吧,小编也收集到了有关基于Canvas+Vue的弹幕组件的实现 的相关资料,希望小伙伴们看了有所帮助。

最近由于项目需要定制化一个弹幕功能 所以尝试使用canvas来开发组件。经过测试在一些低端机的效果也没有明显的卡顿 和大家交流一下

弹幕效果

功能介绍

支持循环弹幕 弹幕不重叠 支持选择轨道数 支持弹幕发送

使用

npm i vue-barrage

参数配置

name type default desc barrageList Array [] 弹幕数据 speed Number 4 弹幕滚动速度 loop Boolean true 是否循环滚动 channels Number 2 弹幕轨道数

功能实现

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实现

监听数据源

watch: { barrageList (val) { if (val.length !== 0) { this.initData() // 数据初始化 this.render() // 开始渲染 } }}

数据初始化

barrageArray 是存储弹幕数据用的 包括默认弹幕列表和新增弹幕项

initData () { for (let i = 0; i < this.barrageList.length; i++) { // 此处处理只显示40个字符 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) }},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, // 弹幕内容 x: x + offsetWidth, // 确定每一条弹幕的初始位置 originX: x + offsetWidth, // 存储当前弹幕的位置 以便在循环的时候使用 position: position, width: this.ctx.measureText(content).width * 3, // canvas绘制内容宽度 color: color || this.getColor() // 自定义颜色 })},

初始化数据需要处理的就是计算当前弹幕的轨道、位置、宽度 以便在 canvas 绘制的时候使用

绘制 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的话在低端机型会有点卡顿},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) { // 此处判断弹幕消失时机 if (i === this.barrageArray.length - 1) { // 最后一条消失时的判断逻辑 if (!this.loop) { //如果不是循环弹幕的话就取消绘制 判断是否循环 不循环执行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) { // 判断什么时候开始绘制 如果不判断的话会导致弹幕滚动卡顿 // 绘制背景 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) } }},

此处判断绘制逻辑 包括什么时候取消 弹幕开始绘制判断 弹幕消失判断

其他函数

getPosition () { let range = this.channels let top = (this.pathWayIndex % range) * 50 + 40 this.pathWayIndex++ return top},getColor () { return '#' + ('00000' + (Math.random() * 0x1000000 << 0).toString(16)).slice(-6);},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()}

此处为弹幕服务函数

使用

<barrage ref="barrage" class="barrage" :barrage-list="barrageList" :speed="speed" :loop="loop" :channels="channels"/> import Barrage from 'vue-barrage'// 弹幕数据初始化 this.barrageList = [{ content: '试数据测试数测试数据数测试数据', color: 'white'}]// 新增弹幕this.$refs.barrage.add({ content: '增加一条新的弹幕增加一条新的弹幕', color: 'white'})

结语

总的来说这个组件还有可优化的空间 后续我会继续改进。

以上就是本文的全部内容 希望对大家的学习有所帮助 也希望大家多多支持爱蒂网。

来源:爱蒂网