Skip to content

Commit

Permalink
feat: add fixed top danmu
Browse files Browse the repository at this point in the history
  • Loading branch information
vortesnail committed May 4, 2022
1 parent 8a94923 commit 902a155
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
2 changes: 1 addition & 1 deletion fixtures/qier-player-danmaku/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ window.onload = function () {
const genDanmu = (time, danmu) => {
if (!danmu) return;
setTimeout(() => {
danmaku.add(danmu, 'fixed-bottom');
danmaku.add(danmu, 'fixed-top');
}, time);
};

Expand Down
39 changes: 39 additions & 0 deletions packages/qier-player-danmaku/src/main/commander/fixed-top.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { TIME_PER_FRAME } from '../constants';
import { Danmaku } from '../danmaku';
import type { Commander, DanmakuOptionsInit } from '../types';
import BaseFixed from './base-fixed';

class FixedTopCommander extends BaseFixed {
constructor(readonly danmaku: Danmaku, el: HTMLElement, config: Commander, options: DanmakuOptionsInit) {
super(danmaku, el, config, options);
}

render(): void {
this.extractDanmu();
const objToElm = this.objToElm;
const trackHeight = this.trackHeight;
this.each((track, trackIdx) => {
const danmu = track.danmus[0];
if (!danmu) {
return;
}
const danmuDom = objToElm.get(danmu);
if (!danmuDom) {
return;
}
if (danmu.static) {
return;
}
const { offset } = danmu;
const y = trackIdx * trackHeight;
danmuDom.style.transform = `translate(${offset}px, ${y}px)`;
danmu.duration -= TIME_PER_FRAME;
if (danmu.duration <= 0) {
this.removeElementFromTrack(track, 0);
track.remove(0);
}
});
}
}

export default FixedTopCommander;
2 changes: 2 additions & 0 deletions packages/qier-player-danmaku/src/main/danmaku.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type {
CommanderMapKey,
FixedDanmu,
} from './types';
import FixedTopCommander from './commander/fixed-top';

const defaultOpts: DanmakuOptions = {
tracksCnt: 6,
Expand Down Expand Up @@ -57,6 +58,7 @@ export class Danmaku extends EventEmitter implements Dispose {
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();
Expand Down
2 changes: 1 addition & 1 deletion packages/qier-player-danmaku/src/main/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export interface Commander {

export interface CommanderMap {
rolling: Base<RollingDanmu>;
// 'fixed-top': Base<FixedBarrageObejct>
'fixed-top': Base<FixedDanmu>;
'fixed-bottom': Base<FixedDanmu>;
}

Expand Down

0 comments on commit 902a155

Please sign in to comment.