2018-02-09 13:46:04 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @function Phaser.Renderer.Snapshot.WebGL
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {HTMLCanvasElement} sourceCanvas - [description]
|
|
|
|
* @param {string} [type='image/png'] - [description]
|
|
|
|
* @param {float} [encoderOptions=0.92] - [description]
|
|
|
|
*
|
|
|
|
* @return {Image} [description]
|
|
|
|
*/
|
2018-01-07 13:27:07 +00:00
|
|
|
var WebGLSnapshot = function (sourceCanvas, type, encoderOptions)
|
2017-05-16 19:15:01 +00:00
|
|
|
{
|
2018-01-20 04:05:56 +00:00
|
|
|
if (!type) { type = 'image/png'; }
|
|
|
|
if (!encoderOptions) { encoderOptions = 0.92; }
|
2018-01-07 13:27:07 +00:00
|
|
|
|
2017-07-04 12:10:26 +00:00
|
|
|
var gl = sourceCanvas.getContext('experimental-webgl');
|
2017-05-16 19:15:01 +00:00
|
|
|
var pixels = new Uint8Array(gl.drawingBufferWidth * gl.drawingBufferHeight * 4);
|
|
|
|
gl.readPixels(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight, gl.RGBA, gl.UNSIGNED_BYTE, pixels);
|
|
|
|
|
2017-07-04 12:10:26 +00:00
|
|
|
// CanvasPool?
|
2017-05-16 19:15:01 +00:00
|
|
|
var canvas = document.createElement('canvas');
|
|
|
|
var ctx = canvas.getContext('2d');
|
|
|
|
var imageData;
|
2017-07-04 12:10:26 +00:00
|
|
|
|
2017-05-16 19:15:01 +00:00
|
|
|
canvas.width = gl.drawingBufferWidth;
|
|
|
|
canvas.height = gl.drawingBufferHeight;
|
|
|
|
|
|
|
|
imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
|
2017-07-04 12:10:26 +00:00
|
|
|
|
2017-05-16 19:15:01 +00:00
|
|
|
var data = imageData.data;
|
2017-07-04 12:10:26 +00:00
|
|
|
|
|
|
|
for (var y = 0; y < canvas.height; y += 1)
|
2017-05-16 19:15:01 +00:00
|
|
|
{
|
|
|
|
for (var x = 0; x < canvas.width; x += 1)
|
|
|
|
{
|
|
|
|
var si = ((canvas.height - y) * canvas.width + x) * 4;
|
|
|
|
var di = (y * canvas.width + x) * 4;
|
|
|
|
data[di + 0] = pixels[si + 0];
|
|
|
|
data[di + 1] = pixels[si + 1];
|
|
|
|
data[di + 2] = pixels[si + 2];
|
|
|
|
data[di + 3] = pixels[si + 3];
|
|
|
|
}
|
|
|
|
}
|
2017-07-04 12:10:26 +00:00
|
|
|
|
2017-05-16 19:15:01 +00:00
|
|
|
ctx.putImageData(imageData, 0, 0);
|
|
|
|
|
2018-01-07 13:27:07 +00:00
|
|
|
var src = canvas.toDataURL(type, encoderOptions);
|
2017-07-04 12:10:26 +00:00
|
|
|
var image = new Image();
|
|
|
|
|
2017-05-16 19:15:01 +00:00
|
|
|
image.src = src;
|
|
|
|
|
|
|
|
return image;
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = WebGLSnapshot;
|