简体   繁体   English

将 html5 视频与 highchart.js 折线图同步

[英]Sync an html5 video with an highchart.js line chart

I have a one minute long video of a train moving and some data series about his position, onboard accelerometer etc.我有一段一分钟长的火车行驶视频和一些关于他的 position、车载加速度计等的数据系列。

My goal is to make line charts (with Highcharts.js) of those measurements that are dynamically drawn with the video at a rate of 20points per second.我的目标是制作折线图(使用 Highcharts.js)以每秒 20 点的速度随视频动态绘制的那些测量值。 The charts have to move with the video, so that if the user go back also the charts go at the same frame and so on.图表必须随视频移动,因此如果用户 go 返回同一帧的图表 go,依此类推。

I was wondering if there's a way to attach an event to the video progress bar and redraw the chart every x milliseconds and/or every time that the user play/stop the video我想知道是否有办法将事件附加到视频进度条并每 x 毫秒和/或每次用户播放/停止视频时重新绘制图表

Connecting timeupdate event for a video element with setData method for a Highcharts series should be enough.使用 Highcharts 系列的setData方法连接视频元素的timeupdate事件应该就足够了。

Example:例子:

let currentDataIndex = 0;
const video = document.getElementById("video1");

const chart = Highcharts.chart('container', {
    series: [{
        // Highcharts mutate original data array, so use a copy
        data: data[currentDataIndex].slice()
    }]
});

const updateChartData = index => {
    chart.series[0].setData(data[index].slice());
    currentDataIndex = index;
};

video.addEventListener('timeupdate', e => {
    const second = Math.floor(video.currentTime);

    if (second !== currentDataIndex) {
        updateChartData(second);
    }
});

Live demo: http://jsfiddle.net/BlackLabel/8a6yvjs5/现场演示: http://jsfiddle.net/BlackLabel/8a6yvjs5/

API Reference: https://api.highcharts.com/class-reference/Highcharts.Series#setData API参考: https://api.highcharts.com/class-reference/Highcharts.Series#setData

Docs:文档:

https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/currentTime https://developer.mozilla.org/zh-CN/docs/Web/API/HTMLMediaElement/currentTime

https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/timeupdate_event https://developer.mozilla.org/zh-CN/docs/Web/API/HTMLMediaElement/timeupdate_event

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM