import { BoundaryProcessing } from "./utils.js"; import { BollData } from "./BollData.js"; export class VideoBarrage { #videoWap; #data; canvasWap; paused; constructor(video, canvas, options) { if (!BoundaryProcessing(video, canvas, options)) return; this.#videoWap = video; this.canvasWap = canvas; this.#data = options.barrageData; this.#initCanvas(options); } #initCanvas(options) { this.canvasWap.width = this.#videoWap.offsetWidth; this.canvasWap.height = this.#videoWap.offsetHeight; this.ctx = this.canvasWap.getContext("2d"); this.paused = true; Object.assign(this, options, { speed: 2, runTime: 2, color: "#ffffff", }); this.barrageBoolData = this.createData(); this.render(); }
createData() { return this.#data.map((item) => new BollData(item, this)); } render() { this.clearRect(); this.drawData(); if (!this.paused) { requestAnimationFrame(this.render.bind(this)); } } drawData() { let currTime = this.#videoWap.currentTime; this.barrageBoolData.map((item) => { if (!item.stop && currTime >= item.runTime) { if (!item.isInit) { item.init(); item.isInit = true; } item.textX -= item.speed; item.draw(); if (item.textX <= item.width * -1) { item.stop = true; } } }); }
clearRect() { this.ctx.clearRect(0, 0, this.canvasWap.width, this.canvasWap.height); }
reset() { this.clearRect(); let currentTime = this.#videoWap.currentTime; this.barrageBoolData.map((item) => { item.stop = false; if (currentTime <= item.runTime) { item.isInit = false; } else { item.stop = true; } }); } addData(data) { this.barrageBoolData.push(new BollData(data, this)); } }
|