简体   繁体   English

如何通过phantomJS实时渲染JavaScript?

[英]How to render javascript in realtime by phantomJS?

I render the javascript behaviour of a page by phantomJS with a code like 我用phantomJS用类似的代码渲染页面的javascript行为

var page = require('webpage').create();
page.viewportSize = { width: 1920, height: 1080 };
page.open('index.html', function(){
    (function snap(i){
        if(i < 5000){
            window.setTimeout(function(){
                page.render('i+'.png');
                snap(++i);
            }, 20);
        }
        else{
            phantom.exit();
        }
    })(0);
});

The problem is when rendering at high resolution, the CPU is heavily used and thus the javascript performance becomes slower. 问题是当以高分辨率渲染时,CPU被大量使用,因此javascript性能会变慢。 For example, if a javascript action takes 100s, it will take 500s under a heavy load. 例如,如果一个javascript操作花费100秒,那么在重负载下将花费500秒。 Thus, the captured images are only for 1/5 of the javascript scenario. 因此,捕获的图像仅适用于javascript场景的1/5。

Is it possible to guarantee that the javascript events is finished in the original timeframe? 是否可以保证javascript事件在原始时间范围内完成? Or render the javascript according to the original timeframe rather than the occuring timeframe? 还是根据原始时间范围而不是发生的时间范围呈现JavaScript?

Is it possible to guarantee that the javascript events is finished in the original timeframe? 是否可以保证javascript事件在原始时间范围内完成?

No, that's not possible because there is no concept of "original timeframe". 不,这是不可能的,因为没有“原始时间表”的概念。

All you need to do instead is speed up your capability for capturing. 相反,您需要做的就是加快捕获能力。 You'll have to abandon PhantomJS for this. 您必须为此放弃PhantomJS。 Fortunately, Chromium itself has a very nice tab capture API which can capture video in realtime. 幸运的是,Chromium本身具有一个非常不错的标签捕获API,可以实时捕获视频。

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

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