Home Web Front-end CSS Tutorial How to efficiently generate high-definition video or GIF animation based on ECharts charts using JavaScript?

How to efficiently generate high-definition video or GIF animation based on ECharts charts using JavaScript?

Apr 05, 2025 pm 04:12 PM
css Solution

Methods for generating high-definition videos based on echarts charts

This article will explore how to convert dynamic charts generated based on echarts, such as histograms that support dynamic sorting, into high-definition videos. Direct use of screen recording software often causes insufficient video clarity issues, while echarts uses canvas or svg rendering, which provides us with an opportunity to improve clarity.

The problem is how to directly generate videos using echarts' canvas rendering features instead of relying on screen recording. The solution is to use the javascript library to implement the video recording function.

One way is to use the recordrtc.js plugin. This plugin combines html2canvas to record canvas content into video. The two plugins, recordrtc.js and html2canvas.js, need to be introduced. The following code snippet shows how to record an echarts chart into an mp4 video using recordrtc.js:

 



    <meta charset="utf-8">
    <meta http-equiv="x-ua-compatible" content="ie=edge,chrome=1">
    <meta name="renderer" content="webkit">
    <title>echarts to video</title>
    <style>
        html,body,#mycanvas{
            height: 100%;
            width: 100%;
            padding: 0;
            margin: 0;
        }
    </style>



    <div id="mycanvas"></div>
    <script type="text/javascript" src="https://cdn.bootcss.com/echarts/4.7.0/echarts-en.min.js"></script>
    <script src="https://cdn.bootcss.com/html2canvas/0.5.0-beta4/html2canvas.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/recordrtc/5.5.9/recordrtc.js"></script>
    <script src="https://cdn.bootcss.com/filesaver.js/1.3.8/filesaver.min.js"></script>
    <script>
        let $parent = document.getelementbyid(&#39;mycanvas&#39;)
        let mychart = echarts.init($parent);
        //The option here is the option in the echrts case. There are too many codes, so I won’t post mychart.setoption(option);
        
        settimeout(function(){
            //Start recording exporttovideo(5000)
        },500)
        
        function exporttovideo(time){
            //time is the recording time milliseconds time = time || 0
            
            let $canvas = document.queryselector(&#39;#mycanvas canvas&#39;)
            
            var recordrtc = recordrtc($canvas, {
                type: &#39;canvas&#39;
            });
            //Start recording recordrtc.startrecording();
            
            settimeout(function(){
                //Record end recordrtc.stoprecording(function(videourl) {
                    console.log(videourl)
                
                    var recordedblob = recordrtc.getblob();
                    //recordrtc.getdataurl(function(dataurl) { });
                    saveas(recordedblob, "test.mp4");
                });
            }, time)
        }
    </script>


Copy after login

If you need to generate gif animations, you can use the gif.js plugin. Need to introduce the gif.js plugin. The code snippet is as follows:

 



    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="renderer" content="webkit">
    <title>echarts to gif</title>
    <style>
        html,body,#myCanvas{
            height: 100%;
            width: 100%;
            padding: 0;
            margin: 0;
        }
    </style>



    <div id="myCanvas"></div>
    <script type="text/javascript" src="https://cdn.bootcss.com/echarts/4.7.0/echarts-en.min.js"></script>
    <script type="text/javascript" src="https://cdn.bootcss.com/gif.js/0.2.0/gif.js"></script>
    <script src="https://cdn.bootcss.com/FileSaver.js/1.3.8/FileSaver.min.js"></script>
    <script>
        let $parent = document.getElementById(&#39;myCanvas&#39;)
        let myChart = echarts.init($parent);

        //The option here is the option in the echrts case. There are too many codes, so I won’t post myChart.setOption( option);
        
        setTimeout(function(){
            //Start recording gif
            exportToGif(5000)
        }, 20)
        
        function exportToGif(time){
            var start = Date.now()
            //time is the recording time milliseconds time = time || 0
            
            let $canvas = document.querySelector(&#39;#myCanvas canvas&#39;)
            
            function loop(){
                window.requestAnimationFrame(function(){
                    gif.addFrame($canvas, {delay: 100});
                    if(Date.now() - start >= time){
                        gif.render();
                    }else{
                        loop()
                    }
                })
            }
            
            var gif = new GIF({
              workers: 2,
              Quality: 10
            });
            
            gif.on(&#39;finished&#39;, function(blob) {
                  saveAs(blob, "test.gif");
            });
            
            loop()
        }
    </script>


Copy after login

These snippets show how to use the corresponding javascript library to record videos or gifs, thus avoiding the clarity issues caused by screen recording. Remember, you need to adjust the recording time and the frame rate and quality parameters of the gif according to the actual situation.

The above is the detailed content of How to efficiently generate high-definition video or GIF animation based on ECharts charts using JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

HTML: The Structure, CSS: The Style, JavaScript: The Behavior HTML: The Structure, CSS: The Style, JavaScript: The Behavior Apr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

Can vs code run in Windows 8 Can vs code run in Windows 8 Apr 15, 2025 pm 07:24 PM

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

Solve caching issues in Craft CMS: Using wiejeben/craft-laravel-mix plug-in Solve caching issues in Craft CMS: Using wiejeben/craft-laravel-mix plug-in Apr 18, 2025 am 09:24 AM

When developing websites using CraftCMS, you often encounter resource file caching problems, especially when you frequently update CSS and JavaScript files, old versions of files may still be cached by the browser, causing users to not see the latest changes in time. This problem not only affects the user experience, but also increases the difficulty of development and debugging. Recently, I encountered similar troubles in my project, and after some exploration, I found the plugin wiejeben/craft-laravel-mix, which perfectly solved my caching problem.

Can visual studio code be used in python Can visual studio code be used in python Apr 15, 2025 pm 08:18 PM

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

Can vs code run python Can vs code run python Apr 15, 2025 pm 08:21 PM

Yes, VS Code can run Python code. To run Python efficiently in VS Code, complete the following steps: Install the Python interpreter and configure environment variables. Install the Python extension in VS Code. Run Python code in VS Code's terminal via the command line. Use VS Code's debugging capabilities and code formatting to improve development efficiency. Adopt good programming habits and use performance analysis tools to optimize code performance.

Can vscode compare two files Can vscode compare two files Apr 15, 2025 pm 08:15 PM

Yes, VS Code supports file comparison, providing multiple methods, including using context menus, shortcut keys, and support for advanced operations such as comparing different branches or remote files.

How to generate html by sublime How to generate html by sublime Apr 16, 2025 am 09:03 AM

There are two ways to generate HTML code in Sublime Text: Using the Emmet plugin, you can generate HTML elements by entering an abbreviation and pressing the Tab key, or use a predefined HTML file template that provides basic HTML structure and other features such as code snippets, autocomplete functionality, and Emmet Snippets.

See all articles