Skip to content

Commit 8f016ef

Browse files
committed
Updating snapshot feature
1 parent 9142260 commit 8f016ef

2 files changed

Lines changed: 76 additions & 38 deletions

File tree

src/renderer/snapshot/WebGLSnapshot.js

Lines changed: 50 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
55
*/
66

7+
var GetFastValue = require('../../utils/object/GetFastValue');
8+
79
/**
810
* Takes a snapshot of the current frame displayed by a WebGL canvas.
911
*
@@ -16,48 +18,64 @@
1618
*
1719
* @return {HTMLImageElement} A new image which contains a snapshot of the canvas's contents.
1820
*/
19-
var WebGLSnapshot = function (sourceCanvas, type, encoderOptions)
21+
var WebGLSnapshot = function (sourceCanvas, snapshotConfig)
2022
{
21-
if (!type) { type = 'image/png'; }
22-
if (!encoderOptions) { encoderOptions = 0.92; }
23+
var type = GetFastValue(snapshotConfig, 'type', 'image/png');
24+
var encoderOptions = GetFastValue(snapshotConfig, 'encoder', 0.92);
25+
var x = GetFastValue(snapshotConfig, 'x', 0);
26+
var y = GetFastValue(snapshotConfig, 'y', 0);
27+
var getPixel = GetFastValue(snapshotConfig, 'getPixel', false);
2328

2429
var gl = sourceCanvas.getContext('experimental-webgl');
25-
var pixels = new Uint8Array(gl.drawingBufferWidth * gl.drawingBufferHeight * 4);
26-
gl.readPixels(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight, gl.RGBA, gl.UNSIGNED_BYTE, pixels);
27-
28-
// CanvasPool?
29-
var canvas = document.createElement('canvas');
30-
var ctx = canvas.getContext('2d');
31-
var imageData;
32-
33-
canvas.width = gl.drawingBufferWidth;
34-
canvas.height = gl.drawingBufferHeight;
3530

36-
imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
31+
if (getPixel)
32+
{
33+
var pixel = new Uint8Array(4);
3734

38-
var data = imageData.data;
35+
gl.readPixels(x, gl.drawingBufferHeight - y, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, pixel);
3936

40-
for (var y = 0; y < canvas.height; y += 1)
37+
return pixel;
38+
}
39+
else
4140
{
42-
for (var x = 0; x < canvas.width; x += 1)
41+
var pixels = new Uint8Array(gl.drawingBufferWidth * gl.drawingBufferHeight * 4);
42+
43+
gl.readPixels(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight, gl.RGBA, gl.UNSIGNED_BYTE, pixels);
44+
45+
// CanvasPool?
46+
var canvas = document.createElement('canvas');
47+
var ctx = canvas.getContext('2d');
48+
var imageData;
49+
50+
canvas.width = gl.drawingBufferWidth;
51+
canvas.height = gl.drawingBufferHeight;
52+
53+
imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
54+
55+
var data = imageData.data;
56+
57+
for (var y = 0; y < canvas.height; y += 1)
4358
{
44-
var si = ((canvas.height - y) * canvas.width + x) * 4;
45-
var di = (y * canvas.width + x) * 4;
46-
data[di + 0] = pixels[si + 0];
47-
data[di + 1] = pixels[si + 1];
48-
data[di + 2] = pixels[si + 2];
49-
data[di + 3] = pixels[si + 3];
59+
for (var x = 0; x < canvas.width; x += 1)
60+
{
61+
var si = ((canvas.height - y) * canvas.width + x) * 4;
62+
var di = (y * canvas.width + x) * 4;
63+
data[di + 0] = pixels[si + 0];
64+
data[di + 1] = pixels[si + 1];
65+
data[di + 2] = pixels[si + 2];
66+
data[di + 3] = pixels[si + 3];
67+
}
5068
}
69+
70+
ctx.putImageData(imageData, 0, 0);
71+
72+
var src = canvas.toDataURL(type, encoderOptions);
73+
var image = new Image();
74+
75+
image.src = src;
76+
77+
return image;
5178
}
52-
53-
ctx.putImageData(imageData, 0, 0);
54-
55-
var src = canvas.toDataURL(type, encoderOptions);
56-
var image = new Image();
57-
58-
image.src = src;
59-
60-
return image;
6179
};
6280

6381
module.exports = WebGLSnapshot;

src/renderer/webgl/WebGLRenderer.js

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,9 @@ var WebGLRenderer = new Class({
210210
* @since 3.0.0
211211
*/
212212
this.snapshotState = {
213+
x: 0,
214+
y: 0,
215+
getPixel: false,
213216
callback: null,
214217
type: null,
215218
encoder: null
@@ -1924,10 +1927,13 @@ var WebGLRenderer = new Class({
19241927

19251928
// Unbind custom framebuffer here
19261929

1927-
if (this.snapshotState.callback)
1930+
var state = this.snapshotState;
1931+
1932+
if (state.callback)
19281933
{
1929-
this.snapshotState.callback(WebGLSnapshot(this.canvas, this.snapshotState.type, this.snapshotState.encoder));
1930-
this.snapshotState.callback = null;
1934+
state.callback(WebGLSnapshot(this.canvas, state));
1935+
1936+
state.callback = null;
19311937
}
19321938

19331939
var pipelines = this.pipelines;
@@ -1952,9 +1958,23 @@ var WebGLRenderer = new Class({
19521958
*/
19531959
snapshot: function (callback, type, encoderOptions)
19541960
{
1955-
this.snapshotState.callback = callback;
1956-
this.snapshotState.type = type;
1957-
this.snapshotState.encoder = encoderOptions;
1961+
var state = this.snapshotState;
1962+
1963+
state.callback = callback;
1964+
state.type = type;
1965+
state.encoder = encoderOptions;
1966+
1967+
return this;
1968+
},
1969+
1970+
getPixel: function (x, y, callback)
1971+
{
1972+
var state = this.snapshotState;
1973+
1974+
state.x = x;
1975+
state.y = y;
1976+
state.getPixel = true;
1977+
state.callback = callback;
19581978

19591979
return this;
19601980
},

0 commit comments

Comments
 (0)