```js
var art = new Artplayer({
container: '.artplayer-app',
url: '/assets/sample/video.mp4',
setting: true,
plugins: [
artplayerPluginDanmuku({
// Danmuku array
danmuku: [
{
text: '111', // Danmuku text
time: 1, // Send time, in seconds
color: '#fff', // Danmuku local color
border: false, // Whether to display outline
mode: 0, // Danmuku mode: 0 for scrolling, 1 for stationary
},
{
text: '222',
time: 2,
color: 'red',
border: true,
mode: 0,
},
{
text: '333',
time: 3,
color: 'green',
border: false,
mode: 1,
},
],
}),
],
});
```
## Using Danmuku XML
▶ Run Code
```js
var art = new Artplayer({
container: '.artplayer-app',
url: '/assets/sample/video.mp4',
plugins: [
artplayerPluginDanmuku({
// Danmuku XML file, consistent with the danmuku format of the Bilibili website
danmuku: '/assets/sample/danmuku.xml',
}),
],
});
```
## Using Asynchronous Call
▶ Run Code
```js
var art = new Artplayer({
container: '.artplayer-app',
url: '/assets/sample/video.mp4',
plugins: [
artplayerPluginDanmuku({
// Use Promise to return asynchronously
danmuku: function () {
return new Promise((resolve) => {
return resolve([
{
text: '111',
time: 1,
},
{
text: '222',
time: 2,
},
{
text: '333',
time: 3,
},
]);
});
},
}),
],
});
```
## `hide/show`
Hide or show the bullet comments with the methods `hide` and `show`.
▶ Run Code
```js
var art = new Artplayer({
container: '.artplayer-app',
url: '/assets/sample/video.mp4',
plugins: [
artplayerPluginDanmuku({
danmuku: '/assets/sample/danmuku.xml',
}),
],
controls: [
{
position: 'right',
html: 'Hide Danmuku',
click: function () {
art.plugins.artplayerPluginDanmuku.hide();
},
},
{
position: 'right',
html: 'Show Danmuku',
click: function () {
art.plugins.artplayerPluginDanmuku.show();
},
},
],
});
```
## `isHide`
Judge whether the current barrage is hidden or displayed with the `isHide` property.
▶ Run Code
```js
var art = new Artplayer({
container: '.artplayer-app',
url: '/assets/sample/video.mp4',
plugins: [
artplayerPluginDanmuku({
danmuku: '/assets/sample/danmuku.xml',
}),
],
controls: [
{
position: 'right',
html: 'Hide Barrage',
click: function (_, event) {
if (art.plugins.artplayerPluginDanmuku.isHide) {
art.plugins.artplayerPluginDanmuku.show();
event.target.innerText = 'Hide Barrage';
} else {
art.plugins.artplayerPluginDanmuku.hide();
event.target.innerText = 'Show Barrage';
}
},
},
],
});
```
## `emit`
Send a real-time danmaku message through the `emit` method
▶ Run Code
```js
var art = new Artplayer({
container: '.artplayer-app',
url: '/assets/sample/video.mp4',
plugins: [
artplayerPluginDanmuku({
danmuku: '/assets/sample/danmuku.xml',
}),
],
controls: [
{
position: 'right',
html: 'Send Danmaku',
click: function () {
var text = prompt('Please enter the danmaku text', 'Danmaku test text');
if (!text || !text.trim()) return;
var color = '#' + Math.floor(Math.random() * 0xffffff).toString(16);
art.plugins.artplayerPluginDanmuku.emit({
text: text,
color: color,
border: true,
});
},
},
],
});
```
## `config`
Change the barrage configuration in real-time with the `config` method, supported attributes include:
- `danmuku`
- `speed`
- `opacity`
- `fontSize`
- `color`
- `mode`
- `margin`
- `antiOverlap`
- `synchronousPlayback`
- `filter`
- `lockTime`
- `beforeEmit`
▶ Run Code
```js
var art = new Artplayer({
container: '.artplayer-app',
url: '/assets/sample/video.mp4',
plugins: [
artplayerPluginDanmuku({
danmuku: '/assets/sample/danmuku.xml',
}),
],
controls: [
{
position: 'right',
html: 'Danmaku size:',
style: {
display: 'flex',
alignItems: 'center',
},
mounted: function ($setting) {
const $range = $setting.querySelector('input[type=range]');
$range.addEventListener('change', () => {
art.plugins.artplayerPluginDanmuku.config({
fontSize: Number($range.value),
});
});
},
},
],
});
```
## `load`
The load method can be used to reload the danmu data source or switch to new danmu.
▶ Run Code
```js
var art = new Artplayer({
container: '.artplayer-app',
url: '/assets/sample/video.mp4',
plugins: [
artplayerPluginDanmuku({
danmuku: '/assets/sample/danmuku.xml',
}),
],
controls: [
{
position: 'right',
html: 'Reload Danmu',
click: function () {
art.plugins.artplayerPluginDanmuku.load();
},
},
{
position: 'right',
html: 'Switch Danmu',
click: function () {
art.plugins.artplayerPluginDanmuku.config({
danmuku: '/assets/sample/danmuku-v2.xml',
});
art.plugins.artplayerPluginDanmuku.load();
},
},
],
});
```
## `reset`
Used to clear the current displayed barrages
▶ Run Code
```js
var art = new Artplayer({
container: '.artplayer-app',
url: '/assets/sample/video.mp4',
setting: true,
plugins: [
artplayerPluginDanmuku({
danmuku: '/assets/sample/danmuku.xml',
}),
],
});
art.on('resize', () => {
art.plugins.artplayerPluginDanmuku.reset();
});
```
## `option`
Used to get the current barrage configuration