将Webglearth画布的输出绘制到2D画布上

时间:2018-10-16 08:55:12

标签: javascript html canvas webgl webgl-earth

我正在尝试将运行webglearth(http://www.webglearth.org/)的画布的输出复制到标准2d画布。我遵循了this问题的答案,但是我只能看到画布的背景色。

我的代码是:

<html>
<body>
    <div id="earthDiv" style="background-color:rgba(255,0,255,0.5); width:500px; height: 500px;"></div>
    <canvas id="canvas" style="background-color:rgba(0,255,0,0.5); width:500px; height: 500px;"></canvas>

    <script src="http://www.webglearth.com/v2/api.js"></script>

    <script type="text/javascript">
        window.onload = function () {

            var earth = new WE.map("earthDiv");
            var earthCanvas = earth.canvas;
            var earthContext = earthCanvas.getContext("webgl", {
                preserveDrawingBuffer: true,
            });

            WE.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {}).addTo(earth);

            var canvas = document.getElementById("canvas");
            var context = canvas.getContext("2d");
            setInterval(function () {
                context.drawImage(earthContext.canvas, 0, 0);
            }, 1000);
        };
    </script>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

原来这是一个问题: https://github.com/webglearth/webglearth2/issues/104,其中画布的上下文在首次获取时需要“ preserveDrawingBuffer:true”。

可以通过更改来解决

 Cesium.Scene({
            canvas: this.canvas,
            contextOptions: { webgl: { alpha: !0 !== c.sky } }
        });

http://www.webglearth.com/v2/api.js

Cesium.Scene({
            canvas: this.canvas,
            contextOptions: { webgl: { alpha: !0 !== c.sky, preserveDrawingBuffer: true } }
        });

(然后将其保存并用作本地文件)。

可以通过使用

获取png图像来解决
earth.getScreenshot(function(dataUrl) {...}))

(请参阅 https://github.com/webglearth/webglearth2/issues/60

但是png方法的效果并不理想。