Danmaku
![File size](https://img.shields.io/bundlephobia/minzip/danmaku)
・
Online Demo
・
中文文档
・
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.
Installation
![](https://img.shields.io/badge/unpkg-555?logo=unpkg)
npm install danmaku
import Danmaku from 'danmaku';
import Danmaku from 'danmaku/dist/esm/danmaku.dom.js';
import Danmaku from 'danmaku/dist/esm/danmaku.canvas.js';
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) {
}
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({
container: document.getElementById('my-container'),
media: document.getElementById('my-media'),
comments: [],
engine: 'canvas',
speed: 144
});
danmaku.emit({
text: 'example',
mode: 'rtl',
time: 233.3,
style: {
fontSize: '20px',
color: '#ffffff',
border: '1px solid #337ab7',
textShadow: '-1px -1px #000, -1px 1px #000, 1px -1px #000, 1px 1px #000'
},
style: {
font: '10px sans-serif',
textAlign: 'start',
textBaseline: 'bottom',
direction: 'inherit',
fillStyle: '#000',
strokeStyle: '#000',
lineWidth: 1.0,
},
render: function() {
var $div = document.createElement('div');
var $img = document.createElement('img');
$img.src = '/path/to/xxx.png';
$div.appendChild($img);
return $div;
},
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();