update heatmap.js

This commit is contained in:
Harvey Zack committed 2023-05-05 22:56:00 +08:00
1 parent 7cd91b8375
commit e095e45791
6 files changed
+49 -29

No files matched your search

@@ -63,6 +63,7 @@ export default class Danmuku {
mount: undefined,
theme: 'dark',
heatmap: false,
points: [],
beforeEmit: () => true,
};
}
@@ -87,6 +88,7 @@ export default class Danmuku {
mount: 'undefined|htmldivelement',
theme: 'string',
heatmap: 'object|boolean',
points: 'array',
beforeEmit: 'function',
};
}
@@ -44,12 +44,12 @@ export default function heatmap(art, danmuku, option) {
let $start = null;
let $stop = null;
function update() {
function update(arg = []) {
$start = null;
$stop = null;
$heatmap.innerHTML = '';
if (!danmuku.danmus.length || !art.duration) return;
if (!art.duration || art.option.isLive) return;
const svg = {
w: $heatmap.offsetWidth,
@@ -73,15 +73,22 @@ export default function heatmap(art, danmuku, option) {
Object.assign(options, option);
}
const points = [];
const gap = art.duration / svg.w;
for (let x = 0; x <= svg.w; x += options.sampling) {
const y = danmuku.danmus.filter(
({ time }) => time > x * gap && time <= (x + options.sampling) * gap,
).length;
points.push([x, y]);
let points = [];
if (Array.isArray(arg) && arg.length) {
points = [...arg];
} else {
const gap = art.duration / svg.w;
for (let x = 0; x <= svg.w; x += options.sampling) {
const y = danmuku.danmus.filter(
({ time }) => time > x * gap && time <= (x + options.sampling) * gap,
).length;
points.push([x, y]);
}
}
if (points.length === 0) return;
const lastPoint = points[points.length - 1];
const lastX = lastPoint[0];
const lastY = lastPoint[1];
@@ -167,9 +174,10 @@ export default function heatmap(art, danmuku, option) {
}
});
art.on('ready', update);
art.on('resize', update);
art.on('artplayerPluginDanmuku:loaded', update);
art.on('ready', () => update());
art.on('resize', () => update());
art.on('artplayerPluginDanmuku:loaded', () => update());
art.on('artplayerPluginDanmuku:points', (points) => update(points));
},
});
}
@@ -18,7 +18,7 @@ export default function artplayerPluginDanmuku(option) {
checkVersion(art);
const danmuku = new Danmuku(art, option);
setting(art, danmuku);
if (option.heatmap && !art.option.isLive) {
if (option.heatmap) {
heatmap(art, danmuku, option.heatmap);
}
return {