<style>
.video-x {
position: relative;
width: 640px;
margin: auto;
}
.canvas-barrage {
position: absolute;
width: 640px;
height: 360px;
}
.video-placeholder {
height: 360px;
background-color: #000;
animation: bgColor 10s infinite alternate;
}
@keyframes bgColor {
25% {
background-color: darkred;
}
50% {
background-color: darkgreen;
}
75% {
background-color: darkblue;
}
100% {
background-color: sliver;
}
}
</style>
<div class="video-x">
<canvas id="canvasBarrage" class="canvas-barrage"></canvas>
<div class="video-placeholder"></div>
</div>
<script>
// 彈幕數據
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.clientWidth;
canvas.height = canvas.clientHeight;
// 存儲實例
var store = {};
// 字號大小
var fontSize = 28;
// 實例方法
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();
};
canvasBarrage('#canvasBarrage', dataBarrage);
</script>
到此這篇關于HTML5 canvas實現的靜態循環滾動播放彈幕的文章就介紹到這了,更多相關canvas靜態循環彈幕內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章,希望大家以后多多支持腳本之家!