fly-barrage 前端弹幕库(5):高级弹幕的设计与实现

时间:2025-04-07 16:16:56
export default class SeniorBarrage extends BaseBarrage { constructor(seniorBarrageOptions: SeniorBarrageOptions, barrageRenderer: BarrageRenderer) { this.calcActualLocation(); } /** * 计算关键点的实际坐标 */ calcActualLocation() { const { startLocation, endLocation, motionDuration } = this.seniorBarrageConfig; // 计算实际起始点的位置 // 计算 actualStartLocation let actualStartLocationX = (startLocation.type || 'PIXEL') === 'PIXEL' ? startLocation.x : startLocation.x * this.canvasSize.width; let actualStartLocationY = (startLocation.type || 'PIXEL') === 'PIXEL' ? startLocation.y : startLocation.y * this.canvasSize.height; if (startLocation.offsetX) actualStartLocationX += startLocation.offsetX; if (startLocation.offsetY) actualStartLocationY += startLocation.offsetY; this.actualStartLocation = { x: actualStartLocationX, y: actualStartLocationY }; // 计算 actualEndLocation let actualEndLocationX = (endLocation.type || 'PIXEL') === 'PIXEL' ? endLocation.x : endLocation.x * this.canvasSize.width; let actualEndLocationY = (endLocation.type || 'PIXEL') === 'PIXEL' ? endLocation.y : endLocation.y * this.canvasSize.height; if (endLocation.offsetX) actualEndLocationX += endLocation.offsetX; if (endLocation.offsetY) actualEndLocationY += endLocation.offsetY; this.actualEndLocation = { x: actualEndLocationX, y: actualEndLocationY }; // 根据实际起始点的位置,计算 vx 和 vy this.vx = (this.actualEndLocation.x - this.actualStartLocation.x) / motionDuration; this.vy = (this.actualEndLocation.y - this.actualStartLocation.y) / motionDuration; } }