• Stars
    star
    585
  • Rank 76,419 (Top 2 %)
  • Language
    JavaScript
  • Created almost 5 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🌈基于CSS3 Animation,使用React构建的弹幕组件

React 弹幕

🌈 基于 CSS3 Animation,使用 React 构建,可扩展,高性能。

NPM NPM downloads twitter

演示地址

👉zerosoul.github.io/rc-bullets/

demo gif

安装

npm:

npm install --save rc-bullets

yarn:

yarn add rc-bullets

初始化一个简单的弹幕场景

import React, { useEffect, useState } from 'react';
import BulletScreen, { StyledBullet } from 'rc-bullets';

const headUrl='https://zerosoul.github.io/rc-bullets/assets/img/heads/girl.jpg';
export default function Demo() {
  // 弹幕屏幕
  const [screen, setScreen] = useState(null);
  // 弹幕内容
  const [bullet, setBullet] = useState('');
  useEffect(() => {
    // 给页面中某个元素初始化弹幕屏幕,一般为一个大区块。此处的配置项全局生效
    let s = new BulletScreen('.screen',{duration:20});
    // or
    // let s=new BulletScreen(document.querySelector('.screen));
    setScreen(s);
  }, []);
  // 弹幕内容输入事件处理
  const handleChange = ({ target: { value } }) => {
    setBullet(value);
  };
  // 发送弹幕
  const handleSend = () => {
    if (bullet) {
      // push 纯文本
      screen.push(bullet);
      // or 使用 StyledBullet

      screen.push(
        <StyledBullet
          head={headUrl}
          msg={bullet}
          backgroundColor={'#fff'}
          size='large'
        />
      );
      // or 还可以这样使用,效果等同使用 StyledBullet 组件
      screen.push({msg:bullet,head:headUrl,color:"#eee" size="large" backgroundColor:"rgba(2,2,2,.3)"})
    }
  };
  return (
    <main>
      <div className="screen" style={{ width: '100vw', height: '80vh' }}></div>
      <input value={bullet} onChange={handleChange} />
      <button onClick={handleSend}>发送</button>
    </main>
  );
}

特性

  • 支持传入 React 组件,灵活控制弹幕内容和 UI,并提供一个默认样式组件:<StyledBullet/>
  • 弹幕屏幕管理:清屏,暂停,隐藏(后续可能会加入针对单个弹幕的控制)
  • 弹幕动画参数化:运动函数(匀速/ease/步进/cubic-bezier)、时长(秒)、循环次数、延迟等
  • 鼠标悬浮弹幕暂停

常用 API

  • 初始化弹幕屏幕:const screen = new BulletScreen(<queryString>|<HTMLElement>,[<option>]),此处的option和下面的一致,偏向全局初始化,没有则使用默认值,每次发送弹幕不传则使用默认或全局设置,传了则该条弹幕覆盖全局设置。
  • 发送弹幕:const bulletId = screen.push(<string>|<ReactElement>,[<option>])

option

选项 含义 值类型 默认值 备注
top 弹幕位置 string undefined 自已强制定制距离顶部的高度,格式同 CSS 中的 top
trackHeight 轨道高度 number 50 均分轨道的高度
onStart 自定义动画开始函数 function null e.g.(bulletId,screen)=>{//do something}可以自定义一些动作,比如播放某个音效,在特定时间暂停该弹幕:screen.pause(bulletId)
onEnd 自定义动画结束函数 function null e.g.(bulletId,screen)=>{//do something}可以自定义一些动作,比如播放某个音效
pauseOnClick 鼠标点击暂停 boolean false 再次点击继续
pauseOnHover 鼠标悬停暂停 boolean true 鼠标进入暂停,离开继续
loopCount 循环次数 number/string 1 值为‘infinite’时,表示无限循环
duration 滚动时长 number/string 10 数字则单位为‘秒’,字符串则支持'10s'和'300ms'两种单位
delay 延迟 number/string 0 数字则单位为‘秒’,字符串则支持'10s'和'300ms'两种单位
direction 动画方向 string normal animation-direction支持的所有值
animateTimeFun 动画函数 string linear:匀速 animation-timing-function支持的所有值
  • 弹幕清屏:screen.clear([<bulletId>]),无参则清除全部
  • 暂停弹幕:screen.pause([<bulletId>]),无参则暂停全部
  • 弹幕继续:screen.resume([<bulletId>]),无参则继续全部
  • 隐藏弹幕(滚动继续):screen.hide([<bulletId>]),无参则隐藏全部
  • 显示弹幕:screen.show([<bulletId>]),无参则显示全部
  • 自带的一个弹幕样式组件:<StyledBullet msg="<弹幕内容>" head="<头像地址>" color="<字体颜色>" backgroundColor="<背景色>" size="<尺寸:small|normal|large|huge|自定义大小,基于em机制,默认normal>">

TO DO

  • 弹幕过多时,防重叠处理
  • react hooks 版本:useBulletScreen

自己动手,丰衣足食

克隆项目

git clone https://github.com/zerosoul/rc-bullets.git && cd rc-bullets

本地类库构建

npm i && npm run start

本地 demo

cd example && npm i && npm run start

支持

赞赏码

License

MIT © zerosoul

More Repositories

1

chinese-colors

🇨🇳🎨Chinese traditional color cheatsheet online
JavaScript
2,351
star
2

honeyed-words-generator

在线生成土味情话
JavaScript
666
star
3

image-compress-without-backend

Pure frontend solution to compress images
JavaScript
372
star
4

breathe-relaxer

A relaxing breathing app with a visual director to tell you when to breathe in, hold and breathe out
JavaScript
120
star
5

github-star-stats

awesome GitHub repo star statistics generator
JavaScript
114
star
6

tech-logo-memo-game

🖱️🖱️🖕🖕🤯🤯🤯technology logo memory game, including frontend and backend
JavaScript
53
star
7

tristan-wedding

给自己做的婚礼页面
JavaScript
50
star
8

github-social-image-generator

🔨generate github social image online!
JavaScript
41
star
9

react-starter

react webapp development scaffold for mobile and pc, with or without redux/router.
JavaScript
29
star
10

PIW

Pick Image as Wallpaper
JavaScript
27
star
11

life-progress

🚼->🚶->👻
JavaScript
23
star
12

ohminesweeper.online

A retro-style minesweeper game built with Next.js, and Tailwind CSS.
TypeScript
20
star
13

chinese-word-chaos

科学究研明表,汉字序顺并不一定影阅响读
JavaScript
15
star
14

blog.yangerxiao.com

personal blog power by React.js and Gatsby.js
JavaScript
15
star
15

static-site-data-management

manage the static site data
JavaScript
14
star
16

yangerxiao.com

personal site landing page
JavaScript
14
star
17

strong-password-generator

create strong passwords online
JavaScript
14
star
18

F2E.codeguide

front-end engineer code guide
CSS
13
star
19

oh-my-goal

keeping fit to get the lady
JavaScript
12
star
20

html-color-cheatsheet

HTML color cheatsheet
JavaScript
7
star
21

css-arrow-generator

CSS arrow generator online
JavaScript
7
star
22

react-meeting-h5

meeting h5 with react
JavaScript
6
star
23

do-the-five

Do the five to stop coronavirus
JavaScript
5
star
24

mini-stopwatch

mini stopwatch
JavaScript
5
star
25

gatsby-plugin-baidu-analytics

biadu analytics for gatsby site
JavaScript
5
star
26

swordman-music-list

🎵🎵🎵歌曲《沧海一声笑》各种版本...
JavaScript
5
star
27

react-h5

react swiper h5 demo
JavaScript
4
star
28

fake-system-update

fake system update screen
JavaScript
4
star
29

what_to_eat

解决每天都会遇到的一个难题:吃什么?!
JavaScript
3
star
30

self-cultivate

FH21 share
JavaScript
2
star
31

impress-resume

My resume powered by impress.js
JavaScript
2
star
32

react-antd-starter

boilerplate based antd
JavaScript
2
star
33

weekly-report

weekly report
JavaScript
2
star
34

WeeklyReport

Created by javascript(jQuery)&HTML&CSS&Email API
JavaScript
2
star
35

gatsby-site-tpl

template for static site
2
star
36

gameRanking

赌王争霸赛
CSS
2
star
37

one-stop-nav

demo
JavaScript
1
star
38

wechat-head-generator

Just for Chun Hua
JavaScript
1
star
39

zerosoul

profile
1
star
40

react-graphql-express-mongodb-starter

react graphql express mongodb starter
JavaScript
1
star
41

http-status-code-as-emoji

http status code as emoji
1
star
42

zerosoul.github.io

Take time to smell roses
HTML
1
star
43

border-it

adding image border
JavaScript
1
star
44

gitbuff

speed up github link in China
JavaScript
1
star
45

rc-video-player

react video component
1
star
46

weekly-report-generator

online weekly report generator
JavaScript
1
star
47

tiny-idea-lab

奇思妙想实验室
1
star