npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@ironkinoko/danmaku

v1.4.1

Published

Display danmaku (flying comments) on HTML5 video.

Downloads

203

Readme

Danmaku

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.

npm bundle size npm jsDelivr hits (npm scoped)

Installation

You can install it with npm:

npm install @ironkinoko/danmaku
import Danmaku from '@ironkinoko/danmaku'

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 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,

  // 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',
  },

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

  // 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
  },
})

Tips:

  • You may want to change line spacing by set line-height to each comment, a better way is set line-height to the container.

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()

Thanks

fork from Zhenye Wei