All Projects → weizhenye → Danmaku

weizhenye / Danmaku

Licence: mit
A high-performance JavaScript danmaku engine. 高性能弹幕引擎库

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Danmaku

Kikoplay
KikoPlay - NOT ONLY A Full-Featured Danmu Player 不仅仅是全功能弹幕播放器
Stars: ✭ 313 (-29.82%)
Mutual labels:  danmaku, danmu
Danmu.server
一个开源的弹幕后端
Stars: ✭ 92 (-79.37%)
Mutual labels:  danmaku, danmu
Ocbarrage
iOS 弹幕库 OCBarrage, 同时渲染5000条弹幕也不卡, 轻量, 可拓展, 高度自定义动画, 超高性能, 简单易上手; A barrage render-engine with high performance for iOS. At the same time, rendering 5000 barrages is also very smooth, lightweight, scalable, highly custom animation, ultra high performance, simple and easy to use!
Stars: ✭ 589 (+32.06%)
Mutual labels:  danmaku, danmu
Danmu.js
HTML5 danmu (danmaku) plugin for any DOM element
Stars: ✭ 130 (-70.85%)
Mutual labels:  danmaku, danmu
awesome-danmaku
一款轻量、适用于 H5 弹幕场景的 JS lib。🚀🚀
Stars: ✭ 35 (-92.15%)
Mutual labels:  danmaku, danmu
acfundanmu
AcFun直播API
Stars: ✭ 27 (-93.95%)
Mutual labels:  danmaku, danmu
Danmaku
live video comments protocol and platform api
Stars: ✭ 70 (-84.3%)
Mutual labels:  danmaku, danmu
Commentcorelibrary
Javascript Live Comment (Danmaku) Engine Implementation. JS弹幕模块核心,提供从基本骨架到高级弹幕的支持。
Stars: ✭ 1,724 (+286.55%)
Mutual labels:  danmaku, danmu
Danmu Client
A cross-platforms danmaku client that supports transparency which based on canvas + WebSocket. 多用跨平台透明弹幕客户端,支持图文弹幕,基于canvas + WebSocket。
Stars: ✭ 151 (-66.14%)
Mutual labels:  danmaku, danmu
Danmu Server
A danmaku server based on WebSocket. 弹幕服务器,基于WebSocket。
Stars: ✭ 169 (-62.11%)
Mutual labels:  danmaku, danmu
Ocbarrage
iOS 弹幕库 OCBarrage, 同时渲染5000条弹幕也不卡, 轻量, 可拓展, 高度自定义动画, 超高性能, 简单易上手; A barrage render-engine with high performance for iOS. At the same time, rendering 5000 barrages is also very smooth, lightweight, scalable, highly custom animation, ultra high performance, simple and easy to use!
Stars: ✭ 294 (-34.08%)
Mutual labels:  danmaku, danmu
ksdanmu
快手直播弹幕
Stars: ✭ 18 (-95.96%)
Mutual labels:  danmu
DanmakuChicken
一个在屏幕上显示弹幕的小程序
Stars: ✭ 17 (-96.19%)
Mutual labels:  danmaku
Rc Bullets
🌈基于CSS3 Animation,使用React构建的弹幕组件
Stars: ✭ 391 (-12.33%)
Mutual labels:  danmu
hello-muiplayer
💡 一款优秀的 HTML5 视频播放器框架
Stars: ✭ 268 (-39.91%)
Mutual labels:  danmaku
Bilibili Block List
基于正则表达式的Bilibili弹幕屏蔽规则
Stars: ✭ 304 (-31.84%)
Mutual labels:  danmaku
blrec
Bilibili Live Streaming Recorder 哔哩哔哩直播录制
Stars: ✭ 124 (-72.2%)
Mutual labels:  danmaku
QLivePlayer
Tools to play live stream with danmaku.
Stars: ✭ 158 (-64.57%)
Mutual labels:  danmaku
acfunlive-backend
AcFun直播通用后端
Stars: ✭ 19 (-95.74%)
Mutual labels:  danmaku
Taisei
A free and open-source Touhou Project fangame
Stars: ✭ 428 (-4.04%)
Mutual labels:  danmaku

Danmaku

GitHub Action Coverage Dependencies NPM version License File size jsDelivr

Browser compatibility

Danmaku is a JavaScript library to display flying comments on HTML media elements (video and audio). It can also display comments to your container in real time without timeline.

Demo

中文文档

Installation

You can install it with npm:

npm install danmaku
// Full version
import Danmaku from 'danmaku';
// DOM engine only
import Danmaku from 'danmaku/dist/esm/danmaku.dom.js';
// Canvas engine only
import Danmaku from 'danmaku/dist/esm/danmaku.canvas.js';

Or use CDN (jsDelivr, unpkg):

Full DOM engine only Canvas engine only
UMD
UMD minified
ESM
ESM minified

Usage

Media mode

<div id="my-video-container" style="width:640px;height:360px;position:relative;">
  <video id="my-video" src="./example.mp4" style="position:absolute;"></video>
</div>

<div id="my-audio-container" style="width:640px;height:360px;position:relative;"></div>
<audio id="my-audio" src="./example.mp3"></audio>

<script src="path/to/danmaku.min.js"></script>
<script>
  var danmaku1 = new Danmaku({
    container: document.getElementById('my-video-container'),
    media: document.getElementById('my-video'),
    comments: []
  });
  var danmaku2 = new Danmaku({
    container: document.getElementById('my-audio-container'),
    media: document.getElementById('my-audio'),
    comments: []
  });
</script>

Live mode

To display comments in real time, you need to set up server and use something like Socket.IO. Danmaku is just receiving comments data and display them to container.

Here is a simple example using with Socket.IO and Node.js.

Server:

const app = require('http').createServer(handler);
const io = require('socket.io')(app);
app.listen(80);
function handler(req, res) {
  // your handler...
}
io.on('connection', socket => {
  socket.on('danmaku', comment => {
    socket.broadcast.emit('danmaku', comment);
  });
});

Client:

<div id="my-container" style="width:640px;height:360px;"></div>
<button id="send-button">Send</button>

<script src="path/to/socket.io.js"></script>
<script src="path/to/danmaku.min.js"></script>
<script>
  var danmaku = new Danmaku({
    container: document.getElementById('my-container')
  });
  var socket = io();
  socket.on('danmaku', function(comment) {
    danmaku.emit(comment)
  });
  var btn = document.getElementById('send-button');
  btn.addEventListener('click', function() {
    var comment = {
      text: 'bla bla',
      style: {
        fontSize: '20px',
        color: '#ffffff'
      },
    };
    danmaku.emit(comment);
    socket.emit('danmaku', comment);
  });
</script>

API

Initialization

var danmaku = new Danmaku({
  // REQUIRED. The stage to display comments will be appended to container.
  container: document.getElementById('my-container'),

  // media can be <video> or <audio> element,
  // if it's not provided, Danmaku will be in live mode
  media: document.getElementById('my-media'),

  // Array of comment, used in media mode,
  // you can find its format in `danmaku.emit` API.
  comments: [],

  // You can use DOM engine or canvas engine to render comments.
  // Canvas engine may more efficient than DOM however it costs more memory.
  // 'DOM' by default in full version.
  engine: 'canvas',

  // You can also set speed by using `danmaku.speed` API.
  speed: 144
});

Emit a comment

danmaku.emit({
  text: 'example',

  // 'rtl'(right to left) by default, available mode: 'ltr', 'rtl', 'top', 'bottom'.
  mode: 'rtl',

  // Specified in seconds, if not provided when using with media,
  // it will be set to `media.currentTime`. Not required in live mode.
  time: 233.3,

  // When using DOM engine, Danmaku will create a <div> node for each comment,
  // the style object will be set to `node.style` directly, just write with CSS rules.
  // For example:
  style: {
    fontSize: '20px',
    color: '#ffffff',
    border: '1px solid #337ab7',
    textShadow: '-1px -1px #000, -1px 1px #000, 1px -1px #000, 1px 1px #000'
  },

  // When using canvas engine, Danmaku will create a <canvas> object for each comment,
  // you should pass in a CanvasRenderingContext2D object.
  // For example:
  style: {
    font: '10px sans-serif',
    textAlign: 'start',
    // Note that 'bottom' is the default
    textBaseline: 'bottom',
    direction: 'inherit',
    fillStyle: '#000',
    strokeStyle: '#000',
    lineWidth: 1.0,
    // ...
  },

  // A custom render to draw comment.
  // when `render` exist, `text` and `style` will be ignored.

  // When using DOM engine, you should return an HTMLElement.
  render: function() {
    var $div = document.createElement('div');
    var $img = document.createElement('img');
    $img.src = '/path/to/xxx.png';
    $div.appendChild($img);
    return $div;
  },
  // When using canvas engine, you should return an HTMLCanvasElement.
  render: function() {
    var canvas = document.createElement('canvas');
    canvas.width = 320;
    canvas.height = 180;
    var ctx = canvas.getContext('2d');
    ctx.beginPath();
    ctx.arc(75, 75, 50, 0, 2 * Math.PI);
    ctx.stroke();
    return canvas;
  }
});

More details about CanvasRenderingContext2D.

Tips:

  • With DOM engine, you may want to change line spacing by set line-height to each comment, a better way is set line-height to the container.
  • With canvas engine, line height is 1.2 by default, you can set it with style.font.
  • With canvas engine, style.font uses the same syntax as the CSS font specifier. However you can only use px, %, em, rem units, I'm sure you don't need others.
  • There is a hitbox for each comment, which height is determined by its line height. With canvas engine, when style.textBaseline is top or hanging, the baseline is set to top of the hitbox; when it's middle, baseline is middle of the hitbox; otherwise baseline is bottom of the hitbox. So if you set style.textBaseline to alphabetic or hanging, the comment's head or foot may out of the hitbox and be invisible.
  • With canvas engine, style.filter is supported in Chrome 52 and Firefox 49.

Resize

Do it when you resize container.

danmaku.resize();

Show

danmaku.show();

Hide

If you set display: none; to the container directly when using DOM engine, you should also do danmaku.hide() otherwise the typesetting will be broken when it's showed.

danmaku.hide();

Clear

Clear current stage.

danmaku.clear();

Speed

There is a property duration for all comments, which means how long will a comment be shown to the stage. duration is calculated by stage.width / danmaku.speed, and danmaku.speed is a standard for all comments, because the actually speed for each comment is then calculated by (comment.width + stage.width) / duration. The default value is 144.

danmaku.speed = 144;

Destroy

Destroy danmaku instance and release memory.

danmaku.destroy();
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].