Coder Social home page Coder Social logo

danmu.js's Introduction

概述

运用 danmu.js 可以方便地使用弹幕功能,满足任意 Web DOM 元素使用(包括 video)。弹幕实现了顶部居中、底部居中、彩色弹幕、无遮挡滚动(具体算法参考前端算法之弹幕设计),以及其它常用的弹幕使用方式。

起步

  1. 安装

    $ npm install danmu.js
    
  2. 使用

  • Step 1:

    <div id="vs"></div>
    <video id="mse" controls>
      <source src="demo.mp4" type="video/mp4" />
    </video>
  • Step 2:

    import DanmuJs from 'danmu.js'
    
    let danmujs = new DanmuJs({
      container: document.getElementById('vs'), //弹幕容器,该容器发生尺寸变化时会自动调整弹幕行为
      containerStyle: {
        //弹幕容器样式
        zIndex: 100
      },
      player: document.getElementsByTagName('video')[0], //配合音视频元素(video或audio)同步使用时需提供该项
      comments: [
        //弹幕预存数组,配合音视频元素(video或audio)同步使用时需提供该项
        {
          duration: 20000, //弹幕持续显示时间,毫秒(最低为5000毫秒)
          //moveV: 100, //弹幕匀速移动速度(单位: px/秒),设置该项时duration无效
          id: '1', //弹幕id,需唯一
          start: 2000, //弹幕出现时间(毫秒)
          prior: true, //该条弹幕优先显示,默认false
          color: true, //该条弹幕为彩色弹幕,默认false
          txt: '长弹幕长弹幕长弹幕长弹幕长弹幕', //弹幕文字内容
          style: {
            //弹幕自定义样式
            color: '#ff9500',
            fontSize: '20px',
            padding: '2px 11px'
          },
          mode: 'top', //显示模式,top顶部居中,bottom底部居中,scroll滚动,默认为scroll
          like: {
            // 点赞相关参数
            el: likeDOM, // el 仅支持传入 dom
            style: {
              // el 绑定样式
              paddingLeft: '10px',
              color: '#ff0000'
            }
          }
          // el: DOM //直接传入一个自定义的DOM元素作为弹幕,使用该项的话会忽略所提供的txt和style
        }
      ],
      area: {
        //弹幕显示区域
        start: 0, //区域顶部到播放器顶部所占播放器高度的比例
        end: 1, //区域底部到播放器顶部所占播放器高度的比例
        lines: undefined // 弹幕虚拟轨道显示行数。当指定行数时,显示范围 start/end 不生效;当弹幕字体大小超过所需要的总虚拟轨道数时,弹幕也不会出现在轨道上,因此请设置好弹幕fontSize及影响弹幕高度的其他样式,让弹幕和轨道高度匹配
      },
      channelSize: 40, // 轨道大小
      mouseControl: true, // 打开鼠标控制, 打开后可监听到 bullet_hover 事件。danmu.on('bullet_hover', function (data) {})
      mouseControlPause: false, // 鼠标触摸暂停。mouseControl: true 生效
      //bOffset: 1000, // 可调节弹幕横向间隔(毫秒)
      defaultOff: true, // 开启此项后弹幕不会初始化,默认初始化弹幕
      chaseEffect: true // 开启滚动弹幕追逐效果, 默认为true
    })

这是 danmu.js 的 npm 使用方法,cdn 使用可以参考示例。DEMO

Properties

danmu.status // 主进程运行状态 'idle' | 'paused' | 'playing' | 'closed'
danmu.state : {
    status: danmu.status,
    comments: Array<CommentData>, // 弹幕数据池
    bullets: Array<Bullet>, // 在轨弹幕数据
    displayArea: { width: number, height: number } // 弹幕显示区域状态
}

API

danmu.start() //弹幕初始化并播放(内部默认已调用)
danmu.pause() //弹幕暂停
danmu.play() //弹幕继续播放
danmu.stop() //弹幕停止并消失

// 单条更新弹幕数据,适合在用户发送弹幕时调用
danmu.sendComment({
  //发送弹幕
  duration: 15000,
  id: 'id',
  start: 3000, //不提供该项则立即发送
  txt: '弹幕内容',
  style: {
    color: '#ff9500',
    fontSize: '20px',
    border: 'solid 1px #ff9500',
    borderRadius: '50px',
    padding: '5px 11px',
    backgroundColor: 'rgba(255, 255, 255, 0.1)'
  }
})

// 批量更新弹幕数据,适合在从接口中获取到批量数据时调用
danmu.updateComments([
  {
    duration: 15000,
    id: 'id',
    start: 3000, //不提供该项则立即发送
    txt: '弹幕内容',
    style: {
      color: '#ff9500',
      fontSize: '20px',
      border: 'solid 1px #ff9500',
      borderRadius: '50px',
      padding: '5px 11px',
      backgroundColor: 'rgba(255, 255, 255, 0.1)'
    }
  },
  ...
])

danmu.setCommentDuration(id, duration) //按照id改变某一个弹幕的持续显示时间
danmu.setAllDuration(mode, duration) // 改变所有弹幕的持续显示时间,包括已加入队列弹幕
danmu.setPlayRate(mode, 1) // 设置弹幕播放速率,在弹幕播放速度上乘以一个系数,控制速度的变化。支持有不同显示时长弹幕的需求
danmu.setCommentID(oldID, newID) //改变某一个弹幕的id
danmu.hide(mode) //屏蔽某一类弹幕(参数可选值 scroll | top | bottom | color)
danmu.show(mode) //显示某一类弹幕(参数可选值 scroll | top | bottom | color)
danmu.setArea(area) // 修改弹幕显示区域, 参考上方config中area的配置
danmu.setOpacity(opacity) // 设置透明度
danmu.setFontSize(size, channelSize) // 设置样式 size 为字体大小 channelSize 如果不需要修改轨道大小则无需传入 channelSize
danmu.setCommentLike(id, {
  el: likeDOM,
  style: {
    paddingLeft: '10px',
    color: '#ff0000'
  }
}) // 这是点赞样式,id 为 commentid

Dev

我们为开发者提供了示例,使用方式如下:

$ git clone [email protected]:bytedance/danmu.js.git
$ cd danmu.js
$ yarn
$ yarn build
$ yarn serve

访问 http://localhost:3000/demo/index.html

License

MIT

danmu.js's People

Contributors

bethe-light avatar dependabot[bot] avatar divawth avatar gemxx avatar hongqx avatar jiuyuetianjiuyuetian avatar llftt avatar zhangxin92 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

danmu.js's Issues

发送弹幕,在style中设置fontSize属性无效

danmujs.sendComment({
duration: 15000,
id: generateUUID(),
txt: '弹幕内容',
style: {
color: '#ff9500',
fontSize: '200px',
border: 'solid 1px #ff9500',
borderRadius: '50px',
padding: '5px 11px',
backgroundColor: 'rgba(255, 255, 255, 0.1)'
}
})

style中除了fontSize属性不生效,其他都可以。改变fontSize,需要使用setFontSize API才行。

期待回复,谢谢。

Errors occurred while running "npm run build"

Issue with danmu.js build process

Dear authors,

I am writing to report an issue that I encountered while trying to build danmu.js. I am using the latest version of the code from the GitHub repository, and I followed the instructions in the README.md file to install the dependencies and run the build process.

However, when I run the "npm run build" command, I receive an error message that prevents the build process from completing successfully. The error message is as follows:

npm run build

[email protected] build
node ./src/version.js && webpack --progress --display-chunks -p

10% [0] building 0/1 modules 1 active ...abel-loader/lib/index.js??ref--4!/home/user0/danmu.js/src/index.js/home/user0/danmu.js/node_modules/babel-loader/lib/cache.js:22
const findCacheDirP = import("find-cache-dir");
^

TypeError: Invalid host defined options
at Object. (/home/user0/danmu.js/node_modules/babel-loader/lib/cache.js:22:23)
at Module._compile (/home/user0/danmu.js/node_modules/v8-compile-cache/v8-compile-cache.js:192:30)
at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
at Module.load (node:internal/modules/cjs/loader:1119:32)
at Module._load (node:internal/modules/cjs/loader:960:12)
at Module.require (node:internal/modules/cjs/loader:1143:19)
at require (/home/user0/danmu.js/node_modules/v8-compile-cache/v8-compile-cache.js:159:20)
at Object. (/home/user0/danmu.js/node_modules/babel-loader/lib/index.js:19:15)
at Module._compile (/home/user0/danmu.js/node_modules/v8-compile-cache/v8-compile-cache.js:192:30)
at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
at Module.load (node:internal/modules/cjs/loader:1119:32)
at Module._load (node:internal/modules/cjs/loader:960:12)
at Module.require (node:internal/modules/cjs/loader:1143:19)
at require (/home/user0/danmu.js/node_modules/v8-compile-cache/v8-compile-cache.js:159:20)
at loadLoader (/home/user0/danmu.js/node_modules/loader-runner/lib/loadLoader.js:18:17)
at iteratePitchingLoaders (/home/user0/danmu.js/node_modules/loader-runner/lib/LoaderRunner.js:169:2)
at runLoaders (/home/user0/danmu.js/node_modules/loader-runner/lib/LoaderRunner.js:365:2)
at NormalModule.doBuild (/home/user0/danmu.js/node_modules/webpack/lib/NormalModule.js:295:3)
at NormalModule.build (/home/user0/danmu.js/node_modules/webpack/lib/NormalModule.js:446:15)
at Compilation.buildModule (/home/user0/danmu.js/node_modules/webpack/lib/Compilation.js:739:10)
at /home/user0/danmu.js/node_modules/webpack/lib/Compilation.js:1111:12
at /home/user0/danmu.js/node_modules/webpack/lib/NormalModuleFactory.js:409:6
at /home/user0/danmu.js/node_modules/webpack/lib/NormalModuleFactory.js:155:13
at AsyncSeriesWaterfallHook.eval [as callAsync] (eval at create (/home/user0/danmu.js/node_modules/tapable/lib/HookCodeFactory.js:33:10), :6:1)
at AsyncSeriesWaterfallHook.lazyCompileHook (/home/user0/danmu.js/node_modules/tapable/lib/Hook.js:154:20)
at /home/user0/danmu.js/node_modules/webpack/lib/NormalModuleFactory.js:138:29
at /home/user0/danmu.js/node_modules/webpack/lib/NormalModuleFactory.js:346:9
at process.processTicksAndRejections (node:internal/process/task_queues:77:11)

Node.js v18.17.1

I have tried several solutions to resolve this issue, including checking my Node.js and OpenSSL versions, verifying my dependencies, and reviewing my webpack and babel-loader configurations. However, none of these solutions have worked.

I would appreciate any assistance you can provide in resolving this issue. Please let me know if you need any additional information or if there is anything else I can do to help diagnose the problem.

Thank you for your time and attention.

Sincerely,

弹幕消失bug及一些疑问

弹幕暂停顶部底部弹幕持续时间结束后,弹幕会自动消失。

弹幕时间轴跳转后,只显示跳转时间后的弹幕,不显示弹幕出现时间+持续时间>跳转后时间的弹幕,以后版本是否会支持显示。

以后版本是否会支持弹幕轨道允许重叠显示所有弹幕。

Does it work with vue ?

I have run the demo in an old-fashioned frontend project, it works well. However, when I migrated the demo code into vue project, it didn't work.

  • I have checked the danmu object created by new DanmuJS({...}), it seems to be correct.
  • There is no DOM element created during video playing.

Has anyone used the pkg in vue ? Thanks in advance for letting me know if u have a solution.

failed to "npm install"

os: fedora, node.js: v18.17.0.1
os: win10 , node.js: v18.17.0

Accoring to the README.md guide, I ran "npm install" after cloning the repository, but it failed at the same place every time. Can you provide some assistance in resolving this issue? The error message is posted below.

npm install
npm ERR! code ENOTFOUND
npm ERR! syscall getaddrinfo
npm ERR! errno ENOTFOUND
npm ERR! network request to http://bnpm.byted.org/@byted-lint/eslint-plugin-meta/-/eslint-plugin-meta-0.0.20.tgz failed, reason: getaddrinfo ENOTFOUND bnpm.byted.org
npm ERR! network This is a problem related to network connectivity.
npm ERR! network In most cases you are behind a proxy or have bad network settings.
npm ERR! network
npm ERR! network If you are behind a proxy, please make sure that the
npm ERR! network 'proxy' config is set properly. See: 'npm help config'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.