function createImageFromTexture(gl, texture, width, height) {
// Create a framebuffer backed by the texture
var framebuffer = gl.createFramebuffer();
gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
// Read the contents of the framebuffer
var data = new Uint8Array(width * height * 4);
gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, data);
gl.deleteFramebuffer(framebuffer);
// Create a 2D canvas to store the result
var canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
var context = canvas.getContext('2d');
// Copy the pixels to a 2D canvas
var imageData = context.createImageData(width, height);
imageData.data.set(data);
context.putImageData(imageData, 0, 0);
var img = new Image();
img.src = canvas.toDataURL();
return img;
}
gl 可以通过webgl纹理的变量 _context._gl 获得
//var pgl = element.readyImagery.textureWebMercator._context._gl;
要想直接查看 图片:
可以将img.src 字符串直接复制到浏览器搜索栏查看
img.src = canvas.toDataURL();