UNPKG

@qier-player/danmaku

Version:

Powerful danmaku, support many features.

92 lines 3.62 kB
var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; import { createDanmu } from '../helper'; import { isEmptyArray } from '../utils/is'; import BaseRolling from './base-rolling'; var BaseFixed = /** @class */ (function (_super) { __extends(BaseFixed, _super); function BaseFixed(danmaku, el, config, options) { var _this = _super.call(this, danmaku, el, config, options) || this; _this.danmaku = danmaku; _this.elHeight = el.offsetHeight; return _this; } BaseFixed.prototype.add = function (danmu) { var trackId = this.findTrack(); if (trackId === -1) { return false; } var text = danmu.text, size = danmu.size, color = danmu.color, offset = danmu.offset; var danmuDom = createDanmu(text, color, size, this.trackHeight, offset); this.el.appendChild(danmuDom); var width = danmuDom.offsetWidth; // 计算位置 var track = this.tracks[trackId]; var trackWidth = this.trackWidth; // 居中 var danmuOffset = (trackWidth - width) / 2; var normalizedDanmu = __assign(__assign({}, danmu), { offset: danmuOffset, duration: this.duration, width: width }); this.objToElm.set(normalizedDanmu, danmuDom); this.elmToObj.set(danmuDom, normalizedDanmu); track.push(normalizedDanmu); return true; }; BaseFixed.prototype.findTrack = function () { var idx = -1; for (var i = 0; i < this.tracks.length; ++i) { if (isEmptyArray(this.tracks[i].danmus)) { idx = i; break; } } return idx; }; BaseFixed.prototype.extractDanmu = function () { var isAdded; for (var i = 0; i < this.waitingQueue.length;) { isAdded = this.add(this.waitingQueue[i]); // 若有一次无法添加成功,说明无轨道可用,终止剩余弹幕的 add 尝试 if (!isAdded) { break; } this.waitingQueue.shift(); } }; BaseFixed.prototype.removeElementFromTrack = function (track, removedIdx) { var danmu = track.danmus[removedIdx]; if (!danmu) { return; } var danmuDom = this.objToElm.get(danmu); this.objToElm.delete(danmu); this.elmToObj.delete(danmuDom); this.removeElement(danmuDom); }; return BaseFixed; }(BaseRolling)); export default BaseFixed; //# sourceMappingURL=base-fixed.js.map