-
Notifications
You must be signed in to change notification settings - Fork 102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature] export racing bar animation as video #724
Comments
I think this is a great idea! ! Because Racing Bar is also a very interesting feature. Hi~ @lhbvvvvv , can you give it a try? |
Ok, I can try it. |
Hi @lhbvvvvv, Do you have any ideas to share with us regarding the implementation of this feature? |
I thought about using RecordRTC.js to do this, I did a simple test in my own html file, and now I'm wondering if I can use this in hypercrx. |
If the library can help you to implement the feature, then there you go! A draft PR is also welcomed. Hi @lhbvvvvv, I have also some information generated by GPT for you. Hope it helps :) Me:I use ECharts to implement a racing bar chart, now I want to export the animation as a video and I hope the export process entirely happens in the browser, How can I achieve this?
myChart.on('finished', function() {
// Export the chart animation as a video
});
const canvas = document.querySelector('canvas');
const stream = canvas.captureStream();
const chunks = [];
const mediaRecorder = new MediaRecorder(stream, {
mimeType: 'video/webm; codecs=vp9'
});
mediaRecorder.ondataavailable = function(event) {
chunks.push(event.data);
};
mediaRecorder.onstop = function() {
const blob = new Blob(chunks, { type: 'video/webm' });
const url = URL.createObjectURL(blob);
// Create a video element and set the source to the recorded video
const video = document.createElement('video');
video.src = url;
// Download the video
const a = document.createElement('a');
a.download = 'chart_animation.webm';
a.href = url;
a.click();
// Clean up
URL.revokeObjectURL(url);
};
mediaRecorder.start();
myChart.on('finished', function() {
mediaRecorder.start();
});
// Stop the media recorder after desired duration
setTimeout(function() {
mediaRecorder.stop();
}, 5000); // Set the duration as desired or based on the chart animation time With these steps, the racing bar chart animation will be captured and exported as a video file within the browser. Me:Why start the record process after the animation finished? I do want to record the animation! // Create the racing bar chart using ECharts and configure the animation as desired
const myChart = echarts.init(document.getElementById('chart'));
// ...
// Capture the canvas element
const canvas = document.querySelector('canvas');
const stream = canvas.captureStream();
// Start the media recorder
const chunks = [];
const mediaRecorder = new MediaRecorder(stream, {
mimeType: 'video/webm; codecs=vp9'
});
mediaRecorder.ondataavailable = function(event) {
chunks.push(event.data);
};
// Start recording immediately
mediaRecorder.start();
// Stop the media recorder after desired duration or when the animation is complete
setTimeout(function() {
mediaRecorder.stop();
}, 5000); // Set the duration as desired or based on the chart animation time
// Handle the stop event
mediaRecorder.onstop = function() {
const blob = new Blob(chunks, { type: 'video/webm' });
const url = URL.createObjectURL(blob);
// Create a video element and set the source to the recorded video
const video = document.createElement('video');
video.src = url;
// Download the video
const a = document.createElement('a');
a.download = 'chart_animation.webm';
a.href = url;
a.click();
// Clean up
URL.revokeObjectURL(url);
}; With this modification, the recording process will start immediately upon rendering the chart, capturing the animation frames until the desired duration or when manually stopped. Me:I want to start the recording process after clicking a start button and finish the record process after the echarts animation finished.
<button id="startButton">Start Recording</button>
const startButton = document.getElementById('startButton');
// Create a flag to track if recording has started
let recordingStarted = false;
// Event listener for the start button
startButton.addEventListener('click', function() {
// Start the recording only if it has not already started
if (!recordingStarted) {
recordingStarted = true;
// Capture the canvas element
const canvas = document.querySelector('canvas');
const stream = canvas.captureStream();
// Start the media recorder
const chunks = [];
const mediaRecorder = new MediaRecorder(stream, {
mimeType: 'video/webm; codecs=vp9'
});
mediaRecorder.ondataavailable = function(event) {
chunks.push(event.data);
};
// Start recording
mediaRecorder.start();
// Stop the media recorder after the animation finishes
myChart.on('finished', function() {
if (recordingStarted) {
recordingStarted = false;
mediaRecorder.stop();
}
});
// Handle the stop event
mediaRecorder.onstop = function() {
const blob = new Blob(chunks, { type: 'video/webm' });
const url = URL.createObjectURL(blob);
// Create a video element and set the source to the recorded video
const video = document.createElement('video');
video.src = url;
// Download the video
const a = document.createElement('a');
a.download = 'chart_animation.webm';
a.href = url;
a.click();
// Clean up
URL.revokeObjectURL(url);
};
}
}); With this code, the recording process will start only when the start button is clicked. The recording will continue until the ECharts animation finishes or until the stop button is clicked. Me:The demo code looks good, please also give the explanation of the API used in the code
I hope this explanation clarifies the usage of the APIs in the code. Let me know if you have any further questions! |
Thanks @tyn1998 for your help, it was very useful for me. I have a question how to get the 'finished' status of the chart. I also asked the gpt but didn't get a useful answer. // Stop the media recorder after the animation finishes
myChart.on('finished', function() {
if (recordingStarted) {
recordingStarted = false;
mediaRecorder.stop();
}
}); |
@lhbvvvvv you may refer to the docs of ECharts API: https://echarts.apache.org/zh/api.html#events.finished I guess the event will be fired after each animation triggered by each |
Seems like a trouble, could you create a draft PR for further discussion on implementation? |
I encountered some network problems and couldn't push the code. Then I will tweak the code and submit the pr draft. |
@lhbvvvvv If you are using Mac, this might help: |
Description
Users want to share Contributors Activity Racing Bar in the form of video. It is a good idea and also a chance to spread Hypercrx, OpenDigger and X-lab. We should support this feature :)
Watermark: "Powered by OpenDigger and Hypercrx" or "Powered by X-lab"
The text was updated successfully, but these errors were encountered: