iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >怎么在html5中利用canvas实现一个弹幕功能
  • 551
分享到

怎么在html5中利用canvas实现一个弹幕功能

2023-06-09 15:06:35 551人浏览 薄情痞子
摘要

本篇文章为大家展示了怎么在HTML5中利用canvas实现一个弹幕功能,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。(function () {   

本篇文章为大家展示了怎么在HTML5中利用canvas实现一个弹幕功能,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

(function () {    window.onload=function () {        var video = document.getElementsByTagName("video")[0]        var cav = document.getElementsByTagName("canvas")[0]        //设置常量canvas的高度以及宽度        var cavWidth = 800         var cavHeight = 420        cav.width=cavWidth        cav.height=cavHeight         var ctx = cav.getContext("2d")        //存储弹幕对象的数组        var capObjs = []        var lastItemTime        var capHeight = 20         var inputEle = document.getElementsByClassName("caption-input-text")[0]        var sendEle = document.getElementsByClassName("caption-sendButton")[0]        var colorUl = document.getElementsByClassName("colorItems")[0]        var ismoveInputEle = document.getElementsByClassName("caption-input-ismove")[0]        //弹幕颜色        var colors=["#fff","#FFCCCC","#CCFFCC","#CCCCFF","#FFFFCC","#CCFFFF"]        var selectedColorIndex = 0        var prevPlayTime = 0        //测试数据的数组        var testArrayCopy = []        var capobjId = 0        //弹幕在画布中高度可能值组成的数组        var topObjs = [{blank:true , value : 20 ,index:0},                        {blank:true , value : 50 ,index:1},                        {blank:true , value : 80 ,index:2},                        {blank:true , value : 110 ,index:3},                        {blank:true , value : 140 ,index:4},                        {blank:true , value : 170 ,index:5},                        {blank:true , value : 200 ,index:6},                        {blank:true , value : 230 ,index:7},                        {blank:true , value : 260 ,index:8},                        {blank:true , value : 290 ,index:9},                        {blank:true , value : 320 ,index:10},                        {blank:true , value : 350 ,index:11},                        {blank:true , value : 380 ,index:12},                        {blank:true , value : 410 ,index:13}]//test data 测试数据var testArray = [{content:"ABCDEFGHIJKLMNOPQRSTUVWXYZ",time:"1",ismove:false,colorIndex:0},{content:"233333333333333",time:"2",ismove:true,colorIndex:0},{content:"干杯,哈哈哈~~~~~~",time:"2",ismove:true,colorIndex:5},{content:"干杯,哈哈哈~~~~~~",time:"2",ismove:true,colorIndex:4},{content:"干杯,哈哈哈~~~~~~",time:"2",ismove:true,colorIndex:4},{content:"干杯,哈哈哈~~~~~~",time:"2",ismove:true,colorIndex:0},{content:"干杯,哈哈哈~~~~~~",time:"2",ismove:true,colorIndex:0},{content:"233333333333333",time:"3",ismove:true,colorIndex:0},{content:"233333333333333",time:"3",ismove:true,colorIndex:0},{content:"233333333333333",time:"3",ismove:true,colorIndex:0},{content:"233333333333333",time:"3",ismove:true,colorIndex:0},{content:"233333333333333",time:"3",ismove:true,colorIndex:0},{content:"233333333333333",time:"3",ismove:true,colorIndex:0},{content:"233333333333333",time:"4",ismove:false,colorIndex:0},{content:"233333333333333",time:"5",ismove:true,colorIndex:4},{content:"233333333333333",time:"6",ismove:true,colorIndex:2},{content:"233333333333333",time:"7",ismove:true,colorIndex:2},{content:"233333333333333",time:"7",ismove:true,colorIndex:2},{content:"233333333333333",time:"7",ismove:true,colorIndex:2},{content:"233333333333333",time:"7",ismove:true,colorIndex:2},{content:"233333333333333",time:"7",ismove:true,colorIndex:2},{content:"233333333333333",time:"7",ismove:true,colorIndex:2},{content:"233333333333333",time:"8",ismove:true,colorIndex:0},{content:"233333333333333",time:"9",ismove:true,colorIndex:0},{content:"233333333333333",time:"10",ismove:true,colorIndex:0},{content:"老师说的非常好,我要好好学习了》》》》",time:"12",ismove:true,colorIndex:0},{content:"老师说的非常好,我要好好学习了》》》》",time:"13",ismove:true,colorIndex:0},{content:"老师说的非常好,我要好好学习了》》》》",time:"14",ismove:true,colorIndex:2},{content:"老师说的非常好,我要好好学习了》》》》",time:"15",ismove:false,colorIndex:0},{content:"老师说的非常好,我要好好学习了》》》》",time:"16",ismove:true,colorIndex:2},{content:"老师说的非常好,我要好好学习了》》》》",time:"17",ismove:true,colorIndex:3},{content:"老师说的非常好,我要好好学习了》》》》",time:"18",ismove:true,colorIndex:2},{content:"老师说的非常好,我要好好学习了》》》》",time:"19",ismove:true,colorIndex:0},{content:"老师说的非常好,我要好好学习了》》》》",time:"20",ismove:true,colorIndex:3},{content:"老师说的非常好,我要好好学习了》》》》",time:"21",ismove:true,colorIndex:0},{content:"老师说的非常好,我要好好学习了》》》》",time:"22",ismove:true,colorIndex:0},{content:"老铁们,小礼物走一波了,小汽车小火箭刷起来吧=========",time:"23",ismove:true,colorIndex:0},{content:"老铁们,小礼物走一波了,小汽车小火箭刷起来吧=========",time:"24",ismove:true,colorIndex:0},{content:"老铁们,小礼物走一波了,小汽车小火箭刷起来吧=========",time:"25",ismove:true,colorIndex:3},{content:"老铁们,小礼物走一波了,小汽车小火箭刷起来吧=========",time:"26",ismove:true,colorIndex:0},{content:"老铁们,小礼物走一波了,小汽车小火箭刷起来吧=========",time:"27",ismove:true,colorIndex:5},{content:"老铁们,小礼物走一波了,小汽车小火箭刷起来吧=========",time:"28",ismove:false,colorIndex:5},{content:"老铁们,小礼物走一波了,小汽车小火箭刷起来吧=========",time:"29",ismove:true,colorIndex:5},{content:"老铁们,小礼物走一波了,小汽车小火箭刷起来吧=========",time:"30",ismove:true,colorIndex:5},{content:"马上就下课了,瓦罗蓝大陆走起了~~~",time:"31",ismove:true,colorIndex:5},{content:"马上就下课了,瓦罗蓝大陆走起了~~~",time:"32",ismove:true,colorIndex:2},{content:"马上就下课了,瓦罗蓝大陆走起了~~~",time:"33",ismove:true,colorIndex:2},{content:"马上就下课了,瓦罗蓝大陆走起了~~~",time:"33",ismove:true,colorIndex:5},{content:"马上就下课了,瓦罗蓝大陆走起了~~~",time:"34",ismove:true,colorIndex:5},{content:"马上就下课了,瓦罗蓝大陆走起了~~~",time:"35",ismove:true,colorIndex:5},{content:"马上就下课了,瓦罗蓝大陆走起了~~~",time:"36",ismove:true,colorIndex:2},{content:"马上就下课了,瓦罗蓝大陆走起了~~~",time:"37",ismove:true,colorIndex:2}]        //将测试数据备份        copyArray(testArray , testArrayCopy)                        function Caption( ismove , spe , col , text ) {            this.isMove = ismove            this.speed = spe            this.color = col || "#ff0"            this.content = text            this.latestTime = 0             this.width = text.length * 20             this.id = 0            this.topIndex = 0            this.occupyPos = true             this.top = 300            this.left = 0            this.setLeftValue()            this.setTopValue()        }        Caption.prototype.setTopValue = function  () {            for(var i = 0 ,len = topObjs.length ; i < len ; i++){                if (topObjs[i].blank) {                    this.top = topObjs[i].value                    this.topIndex = i                    topObjs[i].blank = false                     break                }            }        }        Caption.prototype.setLeftValue = function  () {            if (this.isMove) {                this.left = cavWidth            }            else {                var contentLength = this.content.length                var nowItemLeft = 420 - contentLength * 9                this.left = nowItemLeft            }        }        Caption.prototype.moving = function () {            if (this.isMove) {                this.left-=this.speed                if ( this.left + this.width < cavWidth && this.occupyPos) {                    this.occupyPos = false                     topObjs[this.topIndex].blank = true                 }            }             else{                this.latestTime += 1                if (this.latestTime > 450) {                    topObjs[this.topIndex].blank = true                 }            }        }        Caption.prototype.setId = function  () {            this.id = capobjId            capobjId++        }        var cap1 = new Caption(  false , 1 , 0 , "小礼物走一波,双击6666。。。。")        capObjs.push(cap1)        cap1.setId()                //循环遍历数组,根据对象的属性绘制在画布上        function drawAllText () {            ctx.clearRect( 0 , 0 , cavWidth , cavHeight)            ctx.beginPath()            for(var i=0 , len = capObjs . length ; i < len ; i++ ){                ctx.fillStyle = capObjs[i].color                ctx.font = "bold 20px Courier New"                ctx.fillText( capObjs[i].content , capObjs[i].left , capObjs[i].top )                ctx.closePath()                capObjs[i].moving()                // if (capObjs[i].left < - cavWidth ) {                    // capObjs.splice (i ,1)                    // if excute this statement , will has fault because some item in array is null                    // solution is : write a new function to refresh the array                   // }            }        }                //更新数组,当对象已经超出范围的时候从数组删除这个对象        function refreshObjs(objs) {            for (var i = objs.length - 1; i >= 0; i--) {                if (objs[i].left < - cavWidth || objs[i].latestTime > 450 ) {                    objs.splice(i , 1)                }            }        }                //更新保存弹幕对象的数组        function updateArray () {            var now = parseInt( video.currentTime )            for (var i = testArray.length - 1; i >= 0; i--) {                var nowItemTime = parseInt(testArray[i].time)                 if ( nowItemTime == now ) {                    //首次写的控制高度的方式,空间利用不充分,后来改为setTopValue中的方式                    // var nowItemLeft = getLeftValue(testArray[i])                    // var diffTime = Math.abs(nowItemTime - lastItemTime)                    // if (diffTime < 6) {                     //     capHeight += 30                    //     capHeight = capHeight > 400 ? 20 : capHeight                    // }                        var temcolor = colors[testArray[i].colorIndex]                    var temcap = new Caption (  testArray[i].ismove , 1 , temcolor , testArray[i].content  )                    capObjs.push(temcap)                    capObjs[capObjs.length - 1].setId()                    temcap = null                    testArray.splice(i,1)                }            }        }                //当用户点击send发送弹幕的回调函数        function sendCaption (argument) {            var inputEleTxt = inputEle.value            var now = parseInt( video.currentTime )            var inputIsmoveValue = ismoveInputEle.checked            var temObj = {content:inputEleTxt,time:now,ismove:inputIsmoveValue,colorIndex:selectedColorIndex}            testArray.push(temObj)            inputEle.value = ""        }        // function getLeftValue (obj) {        //     if (obj.ismove) {        //         return 0        //     }        //     else {        //         var contentLength = obj.content.length        //         var nowItemLeft = 420 - contentLength * 9        //         return nowItemLeft        //     }        // }                //重新启动canvas,用在人为导致进度条时间的改变        function reinitCav (argument) {            // testArray = testArrayCopy            copyArray(testArrayCopy , testArray)            capObjs = []            capHeight = 0            clearInterval(canvasTimer)            canvasTimer = null            initCanvas()        }        var canvasTimer = null                 //初始化canvas,用在开始播放时         function initCanvas () {             if (canvasTimer == null ) {                canvasTimer = setInterval(function (argument) {                    drawAllText()                    updateArray()                    refreshObjs(capObjs)                },10)             }                    }//end function initCanvas                //复制数组        function copyArray (arr1 , arr2) {            for (var i =0 , len=arr1.length ; i < len ; i++) {                    arr2[i] = arr1[i]                }            }        //color select event 用户发送弹幕的颜色控制代码        colorUl.addEventListener("click", function( e ){            var prevSelectItemId = ""            switch (selectedColorIndex) {                case 0:                    prevSelectItemId = "colorItemFrist"                    break;                case 1:                    prevSelectItemId = "colorItemSecond"                    break;                case 2:                    prevSelectItemId = "colorItemThrid"                    break;                case 3:                    prevSelectItemId = "colorItemFourth"                    break;                case 4:                    prevSelectItemId = "colorItemFifth"                     break;                case 5:                    prevSelectItemId = "colorItemSixth"                    break;                default:                    // statements_def                    break;            }            var prevSelectItem = document.getElementById(prevSelectItemId)            prevSelectItem.className = ""            var eventTarget = e.target            eventTarget.className = "selectedColor"            var eveTarId = eventTarget.id.substring(9)            switch (eveTarId) {                case "Frist":                    selectedColorIndex = 0                    break;                case "Second":                    selectedColorIndex = 1                    break;                case "Thrid":                    selectedColorIndex = 2                    break;                case "Fourth":                    selectedColorIndex = 3                    break;                case "Fifth":                    selectedColorIndex = 4                    break;                case "Sixth":                    selectedColorIndex = 5                    break;                default:                    // statements_def                    break;            }        }, false)        video.addEventListener("playing" , function () {            initCanvas()        })                //进度条改变执行代码        video.addEventListener("timeupdate", function  () {            var nowPlayTime = video.currentTime            var diffTime = Math.abs(nowPlayTime - prevPlayTime)            prevPlayTime = nowPlayTime            if (diffTime > 1) {                reinitCav()            }        }, false)                //视频暂停执行代码        video.addEventListener("pause" , function () {            clearInterval(canvasTimer)            canvasTimer = null         })                //点击send的监听事件        sendEle.addEventListener("click" , sendCaption)                //input的回车监听事件        inputEle.addEventListener("keydown", function(e) {            var keynum = 0            keynum = window.event ? e.keyCode : e.which            if (keynum == 13) {                sendCaption()            }        })        var aaaa = function() {            alert(1)        }        aaaa()        // function b(aaaa){        //     return aaaa()        // }        // b()        }//end})()

上述内容就是怎么在html5中利用canvas实现一个弹幕功能,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注编程网精选频道。

--结束END--

本文标题: 怎么在html5中利用canvas实现一个弹幕功能

本文链接: https://www.lsjlt.com/news/256633.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 怎么在html5中利用canvas实现一个弹幕功能
    本篇文章为大家展示了怎么在html5中利用canvas实现一个弹幕功能,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。(function () {   ...
    99+
    2023-06-09
  • 怎么在HTML5中实现一个视频弹幕功能
    这篇文章给大家介绍怎么在HTML5中实现一个视频弹幕功能,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。1>html代码展示<div class="barrage">&nb...
    99+
    2023-06-09
  • 怎么在Vue中使用Canvas实现一个弹幕组件
    本篇文章为大家展示了怎么在Vue中使用Canvas实现一个弹幕组件,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。功能介绍支持循环弹幕弹幕不重叠支持选择轨道数支持弹幕发送使用npm i vue-bar...
    99+
    2023-06-09
  • 使用CSS3怎么实现一个弹幕功能
    本篇文章给大家分享的是有关使用CSS3怎么实现一个弹幕功能,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。1.首先创建弹幕区域<div class="b...
    99+
    2023-06-08
  • 使用css3怎么实现一个蒙版弹幕功能
    今天就跟大家聊聊有关使用css3怎么实现一个蒙版弹幕功能,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。css是什么意思css是一种用来表现HTML或XML等文件样式的计算机语言,主要...
    99+
    2023-06-08
  • Android中怎么利用EasyBarrage实现一个弹幕效果
    Android中怎么利用EasyBarrage实现一个弹幕效果,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。概述EasyBarrage是Android平台的一种轻量级弹幕效...
    99+
    2023-05-30
    android easybarrage
  • 怎么在HTML5中实现一个移动端弹幕动画效果
    怎么在HTML5中实现一个移动端弹幕动画效果?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。思路把单个内容编辑好,计算自身宽度,确定初始位置 移动的距离是屏幕宽度js动...
    99+
    2023-06-09
  • 怎么在HTML5中Blob利用实现一个文件下载功能
    怎么在HTML5中Blob利用实现一个文件下载功能?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。$("#exportAll").on(&qu...
    99+
    2023-06-09
  • html5 中怎么利用canvas实现一个圆形时钟
    这篇文章给大家介绍html5 中怎么利用canvas实现一个圆形时钟,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。<!DOCTYPE html><html>&l...
    99+
    2022-10-19
  • 怎么在html5中实现一个时钟功能
    这期内容当中小编将会给大家带来有关怎么在html5中实现一个时钟功能,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。<!DOCTYPE HTML><html><hea...
    99+
    2023-06-09
  • 怎么在html5中利用canvas绘制一个圆环
    今天就跟大家聊聊有关怎么在html5中利用canvas绘制一个圆环,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。实现过程:首先:html部分代码如下:<canvas ...
    99+
    2023-06-09
  • 使用canvas怎么实现一个在线签名功能
    这期内容当中小编将会给大家带来有关使用canvas怎么实现一个在线签名功能,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。什么是 Canvas?Canvas 是 HTML5 新增的元素,用于在网页上绘制图形...
    99+
    2023-06-09
  • html5中怎么利用canvas实现一个刮刮卡效果
    这篇文章给大家介绍html5中怎么利用canvas实现一个刮刮卡效果,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。代码如下:<!DOCTYPE html><...
    99+
    2022-10-19
  • 怎么在Html5中实现一个剪切板功能
    这期内容当中小编将会给大家带来有关怎么在Html5中实现一个剪切板功能,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。1.不带input输入框的原生js方法这种情况适用于复制非输入框中的文本到剪切板&nbs...
    99+
    2023-06-09
  • 怎么在HTML5中利用Canvas绘制一个K线图
    本篇文章为大家展示了怎么在HTML5中利用Canvas绘制一个K线图,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。SVG 是一种使用 XML 描述 2D 图形的语言。 Canvas 通过 JavaS...
    99+
    2023-06-09
  • 使用python实怎么编写一个自动发送弹幕功能
    这期内容当中小编将会给大家带来有关使用python实怎么编写一个自动发送弹幕功能,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。基本开发环境· Python 3.6· Pycharm相关模块使用import...
    99+
    2023-06-06
  • 使用Html5怎么实现一个移动端弹幕动画效果
    这期内容当中小编将会给大家带来有关使用Html5怎么实现一个移动端弹幕动画效果,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。思路把单个内容编辑好,计算自身宽度,确定初始位置移动的距离是屏幕宽度js动态的添...
    99+
    2023-06-09
  • 怎么在html5中使用Geolocation实现一个定位功能
    今天就跟大家聊聊有关怎么在html5中使用Geolocation实现一个定位功能,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。1.获取当前位置 我们将使用getCurrentPosi...
    99+
    2023-06-09
  • 如何在python中利用tkinter在屏幕中间实现一个倒计时功能
    如何在python中利用tkinter在屏幕中间实现一个倒计时功能?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。Python主要用来做什么Python主要应用于:1、Web...
    99+
    2023-06-07
  • 使用canvas怎么实现一个拼图功能
    使用canvas怎么实现一个拼图功能?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。实现的思路其实挺简单的,主要是通过服务端获取图片链接,图片宽度,图片高度,然后...
    99+
    2023-06-09
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作