Skip to content

Commit

Permalink
feat: start, stop, render function code
Browse files Browse the repository at this point in the history
  • Loading branch information
vortesnail committed Mar 23, 2022
1 parent c9dabd6 commit c792851
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
11 changes: 10 additions & 1 deletion fixtures/qier-player-danmaku/index.js
@@ -1,8 +1,9 @@
window.onload = function () {
console.log(QierPlayer);
const { Player, EVENT } = QierPlayer;
const rootEle = document.querySelector('#app');

const player = new QierPlayer.Player({
const player = new Player({
src: 'http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerFun.mp4',
});

Expand All @@ -23,6 +24,14 @@ window.onload = function () {
// });
// }, 3000);

player.on(EVENT.PLAY, () => {
danmaku.start();
});

player.on(EVENT.PAUSE, () => {
danmaku.stop();
});

danmaku.add({
text: 'i am vortesnail',
color: '#1890ff',
Expand Down
30 changes: 29 additions & 1 deletion packages/qier-player-danmaku/src/main/danmaku.ts
Expand Up @@ -3,7 +3,8 @@ import { addDispose, dispose, Dispose } from './utils/dispose';
import { RollingCommander } from './commander';
import { getEle } from './utils/dom';
import strategy from './strategy';
import { Commander, DanmakuOptions, RawDanmu, CommanderMap, CommanderMapKey } from './types';
import Base from './commander/base';
import { Commander, DanmakuOptions, RawDanmu, RollingDanmu, CommanderMap, CommanderMapKey } from './types';

const defaultOpts: DanmakuOptions = {
tracksCnt: 4,
Expand All @@ -25,6 +26,8 @@ export class Danmaku extends EventEmitter implements Dispose {

commanderMap?: CommanderMap;

private rAFId: number | null = null;

constructor(container: HTMLElement | string, opts?: DanmakuOptionsInit) {
super();

Expand Down Expand Up @@ -55,6 +58,31 @@ export class Danmaku extends EventEmitter implements Dispose {
return fn(this, danmu, type);
}

start() {
if (this.rAFId) return;

this.rAFId = requestAnimationFrame(this.render.bind(this));
}

stop() {
if (!this.rAFId) return;

cancelAnimationFrame(this.rAFId);
this.rAFId = null;
}

private traversalManager(handler: (commander: Base<RollingDanmu>) => any) {
if (!this.commanderMap) return;
Object.keys(this.commanderMap).forEach((key) => handler.call(this, this.commanderMap![key as CommanderMapKey]));
}

private render() {
// 遍历每个 commander 执行它们各自的 render 方法
this.traversalManager((manager) => manager.render());

this.rAFId = requestAnimationFrame(this.render.bind(this));
}

dispose(): void {
this.el = null!;
}
Expand Down
8 changes: 4 additions & 4 deletions packages/qier-player-danmaku/src/main/strategy.ts
Expand Up @@ -4,7 +4,7 @@ import { RawDanmu, RollingDanmu, FixedDanmu, CommanderMapKey } from './types';
export interface Strategy {
clear(danmaku: Danmaku): void;
add(danmaku: Danmaku, danmu: RawDanmu, type: CommanderMapKey): void;
render(danmaku: Danmaku): void;
// render(danmaku: Danmaku): void;
}

const strategy: Strategy = {
Expand All @@ -31,9 +31,9 @@ const strategy: Strategy = {
console.log(1);
}
},
render(danmaku: Danmaku): void {
console.log(222);
},
// render(danmaku: Danmaku): void {
// console.log(222, danmaku);
// },
};

export default strategy;

0 comments on commit c792851

Please sign in to comment.