当前位置:网站首页>cavans实现静态滚动弹幕
cavans实现静态滚动弹幕
2022-07-26 10:24:00 【唐策】
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<!-- 弹幕文字 -->
<div class="video-x"></div>
<script type="text/javascript" src="https://cdn.bootcss.com/jquery/2.1.4/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
// 弹幕
var dataBarrage = [{
value: "使用的是静态死数据",
color: "blue",
range: [0, 0.5]
},
{
value: "随机循环播放",
color: "blue",
range: [0, 0.6]
},
{
value: "可以控制区域和垂直分布范围",
color: "blue",
range: [0, 0.5]
},
{
value: "字体大小和速度在方法内设置",
color: "black",
range: [0.1, 1]
},
{
value: "适合用在一些静态页面上",
color: "black",
range: [0.2, 1]
},
{
value: "基于canvas实现",
color: "black",
range: [0.2, 0.9]
},
{
value: "因此IE9+浏览器才支持",
color: "black",
range: [0.2, 1]
},
{
value: "可以设置边框颜色",
color: "black",
range: [0.2, 1]
},
{
value: "文字颜色默认都是白色",
color: "black",
range: [0.2, 0.9]
},
{
value: "若文字颜色不想白色",
color: "black",
range: [0.2, 1]
},
{
value: "需要自己调整下JS",
color: "black",
range: [0.6, 0.7]
},
{
value: "如果需要的是真实和视频交互的弹幕",
color: "black",
range: [0.2, 1]
},
{
value: "可以回到原文",
color: "black",
range: [0, 0.9]
},
{
value: "查看另外一个demo",
color: "black",
range: [0.7, 1]
},
{
value: "下面就是占位弹幕了",
color: "black",
range: [0.7, 0.95]
},
{
value: "前方高能预警!!!",
color: "orange",
range: [0.5, 0.8]
},
{
value: "前方高能预警!!!",
color: "orange",
range: [0.5, 0.9]
},
{
value: "前方高能预警!!!",
color: "orange",
range: [0, 1]
},
{
value: "前方高能预警!!!",
color: "orange",
range: [0, 1]
}
];
// 弹幕方法
var canvasBarrage = function (canvas, data) {
if (!canvas || !data || !data.length) {
return;
}
if (typeof canvas == "string") {
canvas = document.querySelector(canvas);
canvasBarrage(canvas, data);
return;
}
var context = canvas.getContext("2d");
// canvas.width = '';
// canvas.height = '';
// 存储实例
var store = {};
// 字号大小
var fontSize = 12;
// 实例方法
var Barrage = function (obj, index) {
// 随机x坐标也就是横坐标,对于y纵坐标,以及变化量moveX
this.x = (1 + (index * 0.1) / Math.random()) * canvas.width;
this.y =
obj.range[0] * canvas.height +
(obj.range[1] - obj.range[0]) * canvas.height * Math.random() +
36;
if (this.y < fontSize) {
this.y = fontSize;
} else if (this.y > canvas.height - fontSize) {
this.y = canvas.height - fontSize;
}
this.moveX = 1 + Math.random() * 3;
this.opacity = 0.8 + 0.2 * Math.random();
this.params = obj;
this.draw = function () {
var params = this.params;
// 根据此时x位置绘制文本
context.strokeStyle = params.color;
context.font =
"bold " + fontSize + 'px "microsoft yahei", sans-serif';
context.fillStyle = "rgba(255,255,255," + this.opacity + ")";
context.fillText(params.value, this.x, this.y);
context.strokeText(params.value, this.x, this.y);
};
};
data.forEach(function (obj, index) {
store[index] = new Barrage(obj, index);
});
// 绘制弹幕文本
var draw = function () {
for (var index in store) {
var barrage = store[index];
// 位置变化
barrage.x -= barrage.moveX;
if (barrage.x < -1 * canvas.width * 1.5) {
// 移动到画布外部时候从左侧开始继续位移
barrage.x = (1 + (index * 0.1) / Math.random()) * canvas.width;
barrage.y =
(barrage.params.range[0] +
(barrage.params.range[1] - barrage.params.range[0]) *
Math.random()) *
canvas.height;
if (barrage.y < fontSize) {
barrage.y = fontSize;
} else if (barrage.y > canvas.height - fontSize) {
barrage.y = canvas.height - fontSize;
}
barrage.moveX = 1 + Math.random() * 3;
}
// 根据新位置绘制圆圈圈
store[index].draw();
}
};
// 画布渲染
var render = function () {
// 清除画布
context.clearRect(0, 0, canvas.width, canvas.height);
// 绘制画布上所有的圆圈圈
draw();
// 继续渲染
requestAnimationFrame(render);
};
render();
};
var canvas = '<canvas id="canvasBarrage" class="canvas-barrage"></canvas>'
$('.video-x').html(canvas);
canvasBarrage("#canvasBarrage", dataBarrage);
})
</script>
</body>
</html>
边栏推荐
- Dynamically determine file types through links
- [gossip] error loading psychopg2 module: no module named psychopg2
- 句句解析js中的完美 / 缓冲运动框架(新手专用)
- INSTALL_ FAILED_ SHARED_ USER_ Incompatible error resolution
- Meeting OA project (III) -- my meeting (meeting seating and submission for approval)
- SQL Server 2008 R2 installation problems
- String null to empty string (what does empty string mean)
- [award-winning question] ask Judea pearl, the Turing prize winner and the father of Bayesian networks
- Study notes of the first week of sophomore year
- The problem of incomplete or partial display of the last recyclerview is solved
猜你喜欢

【有奖提问】向图灵奖得主、贝叶斯网络之父 Judea Pearl 提问啦

Learning about opencv (1)

Session based recommendations with recurrent neural networks
![[Halcon vision] software programming ideas](/img/9b/a27338689ee4598dac88f6e5d92053.png)
[Halcon vision] software programming ideas

Learning about opencv (4)
![[Halcon vision] array](/img/29/905d93795a24538fded18d2d377e52.png)
[Halcon vision] array

Vs Code configures go locale and successfully installs go related plug-ins in vscode problem: Tools failed to install

AirTest

数通基础-Telnet远程管理设备

equals与==的区别
随机推荐
Basics of data communication - basic knowledge of network
Study notes of the fifth week of sophomore year
String null to empty string (what does empty string mean)
All codes of Tetris
Google与Pixar开发Draco支持USD格式 加速3D对象传输&lt;转发&gt;
Yarn 'TSC' is not an internal or external command, nor is it a runnable program or batch file. The problem that the command cannot be found after installing the global package
Necessary for beginners: debug breakpoint debugging skills in idea and common breakpoint skills
The practice of OpenCV -- bank card number recognition
Meeting OA project (III) -- my meeting (meeting seating and submission for approval)
[C language] LINQ overview
Map key not configured and uniapp routing configuration and jump are reported by the uniapp < map >< /map > component
软件打不开了
SPARK中 DS V2 push down(下推)的一些说明
畅听,网文流量竞争的下一站?
【Halcon视觉】形态学腐蚀
Under win10 64 bit, matlab fails to configure notebook
The problem of incomplete or partial display of the last recyclerview is solved
Solution of inputting whole line string after inputting integer
数通基础-Telnet远程管理设备
About automatic operation on Web pages