Skip to content

Commit

Permalink
Merge pull request #19 from slatejack/feature/resize-bullets-screen
Browse files Browse the repository at this point in the history
Feature/resize bullets screen
  • Loading branch information
slatejack authored Feb 2, 2024
2 parents b1ba009 + 223cbdf commit a3fb5d1
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 16 deletions.
2 changes: 1 addition & 1 deletion demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"rc-bullets-ts": "^1.3.0",
"rc-bullets-ts": "^1.3.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
Expand Down
30 changes: 19 additions & 11 deletions demo/src/App.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import React, {useEffect, useRef} from 'react';
import './App.css';
import BulletScreen, {StyledBullet} from 'rc-bullets-ts';
import './App.css';

function App() {
const isPause = useRef(false);
const barrageScreen = useRef(null);
useEffect(() => {
// 给页面中某个元素初始化弹幕屏幕,一般为一个大区块。此处的配置项全局生效
barrageScreen.current = new BulletScreen('.screen-container', {duration: 10});
document.addEventListener('visibilitychange', function () {
if (!document.hidden) {
console.log('page is visible');
barrageScreen.current.resize();
} else {
console.log('page is invisible');
}
});
}, []);

/**
Expand All @@ -16,16 +24,16 @@ function App() {
*/
const handleSend = (barrageInfo) => {
if (barrageInfo) {
barrageScreen.current.push(
<StyledBullet
head={barrageInfo.img}
msg={barrageInfo.msg}
backgroundColor="rgba(0,0,0,0.4)"
/>,
{
bottom: '9px', // 指定所有弹幕距离容器底部距离为9px
}
);
setInterval(() => {
barrageScreen.current.push(
<StyledBullet
head={barrageInfo.img}
msg={barrageInfo.msg}
backgroundColor="rgba(0,0,0,0.4)"
/>,
{}
);
}, 500);
}
};

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rc-bullets-ts",
"version": "1.3.2",
"version": "1.4.0",
"description": "一个简单的react弹幕库",
"main": "dist/main.js",
"module": "dist/main.js",
Expand Down
20 changes: 18 additions & 2 deletions src/bulletScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ class BulletScreen {
style.sheet?.insertRule(`@keyframes RightToLeft { ${from} ${to} }`, 0);
}

/**
* 发送弹幕
* @param item
* @param opts
*/
push(item: pushItem, opts: Partial<ScreenOpsTypes>) {
const options = {...this.options, ...opts};
const {onStart, onEnd, top, bottom} = options;
Expand Down Expand Up @@ -101,6 +106,10 @@ class BulletScreen {
return bulletContainer.id;
}

/**
* 获取需要渲染的dom样式
* @param Item
*/
getRenderDom(Item: pushItem) {
if (React.isValidElement(Item)) {
return Item;
Expand Down Expand Up @@ -169,6 +178,15 @@ class BulletScreen {
this.bullets = [];
}

/**
* 样式重置
*/
resize() {
const {trackHeight} = this.options;
this.initBulletTrack(trackHeight);
this.initBulletAnimate(this.target);
}

/**
* 获取播放轨道
* @returns
Expand Down Expand Up @@ -226,8 +244,6 @@ class BulletScreen {
const observer = new IntersectionObserver(enteries => {
for (const entry of enteries) {
const {intersectionRatio, target, isIntersecting} = entry;
console.log('bullet id', target.id, intersectionRatio, isIntersecting);
console.log('resTarget', this.target, entry);
if (intersectionRatio >= 1) {
const curTaget = target as HTMLElement;
const trackIdx = typeof (curTaget.dataset.track) === 'undefined' ? undefined : +curTaget.dataset.track;
Expand Down
13 changes: 13 additions & 0 deletions typings/bulletScreen.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,27 @@ declare class BulletScreen {
* @param screen
*/
initBulletAnimate(screen: HTMLElement): void;
/**
* 发送弹幕
* @param item
* @param opts
*/
push(item: pushItem, opts: Partial<ScreenOpsTypes>): string;
/**
* 获取需要渲染的dom样式
* @param Item
*/
getRenderDom(Item: pushItem): JSX.Element;
_toggleAnimateStatus: (id: string | null, status?: string) => void;
pause(id?: string | null): void;
resume(id?: string | null): void;
hide(): void;
show(): void;
clear(id?: null): void;
/**
* 样式重置
*/
resize(): void;
/**
* 获取播放轨道
* @returns
Expand Down
13 changes: 13 additions & 0 deletions typings/src/bulletScreen.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,27 @@ declare class BulletScreen {
* @param screen
*/
initBulletAnimate(screen: HTMLElement): void;
/**
* 发送弹幕
* @param item
* @param opts
*/
push(item: pushItem, opts: Partial<ScreenOpsTypes>): string;
/**
* 获取需要渲染的dom样式
* @param Item
*/
getRenderDom(Item: pushItem): JSX.Element;
_toggleAnimateStatus: (id: string | null, status?: string) => void;
pause(id?: string | null): void;
resume(id?: string | null): void;
hide(): void;
show(): void;
clear(id?: null): void;
/**
* 样式重置
*/
resize(): void;
/**
* 获取播放轨道
* @returns
Expand Down

0 comments on commit a3fb5d1

Please sign in to comment.