@qier-player/danmaku
Version:
Powerful danmaku, support many features.
128 lines • 4.86 kB
JavaScript
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 { EventEmitter } from './utils/eventmitter';
import { dispose } from './utils/dispose';
import { RollingCommander, FixedBottomCommander } from './commander';
import { getEle } from './utils/dom';
import { requestAnimationFrame, cancelAnimationFrame } from './utils/common';
import strategy from './strategy';
import FixedTopCommander from './commander/fixed-top';
var defaultOpts = {
tracksCnt: 6,
trackHeight: 20 * 1.5,
fontSize: 20,
fontColor: '#fff',
duration: 5000,
zoom: 1,
eventProxyElement: undefined,
};
var Danmaku = /** @class */ (function (_super) {
__extends(Danmaku, _super);
function Danmaku(container, opts) {
var _this = _super.call(this) || this;
_this.rAFId = null;
_this.opts = __assign(__assign({}, defaultOpts), opts);
_this.el = getEle(container);
if (!_this.el) {
console.error('The container element you are currently passing in is not an HTML element.');
return _this;
}
// pointer-events 避免容器阻碍下层点击
_this.el.style.pointerEvents = 'none';
var commanderConfig = {
trackWidth: _this.el.offsetWidth,
};
_this.commanderMap = {
rolling: new RollingCommander(_this, _this.el, commanderConfig, _this.opts),
'fixed-bottom': new FixedBottomCommander(_this, _this.el, commanderConfig, _this.opts),
'fixed-top': new FixedTopCommander(_this, _this.el, commanderConfig, _this.opts),
};
_this.resize();
return _this;
}
Danmaku.prototype.resize = function (width) {
width = width || this.el.offsetWidth;
this.eachManager(function (manager) { return manager.resize(width); });
};
Danmaku.prototype.clear = function () {
var fn = strategy.clear;
return fn(this);
};
Danmaku.prototype.add = function (danmu, type) {
if (type === void 0) { type = 'rolling'; }
var fn = strategy.add;
return fn(this, danmu, type);
};
Danmaku.prototype.setOpacity = function (opacity) {
if (opacity === void 0) { opacity = 1; }
if (!this.el)
return;
this.el.style.opacity = "" + opacity;
};
Danmaku.prototype.setFontSize = function (zoom) {
if (zoom === void 0) { zoom = 1; }
this.opts.zoom = zoom;
};
Danmaku.prototype.setDuration = function (duration) {
this.opts.duration = duration;
};
Danmaku.prototype.setTrackCount = function (cnt) {
this.opts.tracksCnt = cnt;
};
Danmaku.prototype.start = function () {
if (this.rAFId)
return;
this.rAFId = requestAnimationFrame(this.render.bind(this));
};
Danmaku.prototype.stop = function () {
if (!this.rAFId)
return;
cancelAnimationFrame(this.rAFId);
this.rAFId = null;
};
Danmaku.prototype.eachManager = function (handler) {
var _this = this;
if (!this.commanderMap)
return;
Object.keys(this.commanderMap).forEach(function (key) { return handler.call(_this, _this.commanderMap[key]); });
};
Danmaku.prototype.render = function () {
// 遍历每个 commander 执行它们各自的 render 方法
this.eachManager(function (manager) { return manager.render(); });
this.rAFId = requestAnimationFrame(this.render.bind(this));
};
Danmaku.prototype.dispose = function () {
if (!this.el)
return;
this.el = null;
dispose(this);
this.removeAllListeners();
};
return Danmaku;
}(EventEmitter));
export { Danmaku };
//# sourceMappingURL=danmaku.js.map