diff --git a/v3/src/renderer/canvas/CanvasRenderer.js b/v3/src/renderer/canvas/CanvasRenderer.js index ec9dbc375..d1d069aa1 100644 --- a/v3/src/renderer/canvas/CanvasRenderer.js +++ b/v3/src/renderer/canvas/CanvasRenderer.js @@ -3,7 +3,7 @@ var DrawImage = require('./utils/DrawImage'); var BlitImage = require('./utils/BlitImage'); var GetBlendModes = require('./utils/GetBlendModes'); var GetContext = require('../../canvas/GetContext'); -var Snapshot = require('../../snapshot/Snapshot'); +var CanvasSnapshot = require('../snapshot/CanvasSnapshot'); var Smoothing = require('../../dom/Smoothing'); var ScaleModes = require('../ScaleModes'); @@ -257,7 +257,7 @@ CanvasRenderer.prototype = { if (this.snapshotCallback) { - this.snapshotCallback(Snapshot.CanvasSnapshot(this.gameCanvas)); + this.snapshotCallback(CanvasSnapshot(this.gameCanvas)); this.snapshotCallback = null; } diff --git a/v3/src/snapshot/CanvasSnapshot.js b/v3/src/renderer/snapshot/CanvasSnapshot.js similarity index 61% rename from v3/src/snapshot/CanvasSnapshot.js rename to v3/src/renderer/snapshot/CanvasSnapshot.js index baa44392b..6c4e6af82 100644 --- a/v3/src/snapshot/CanvasSnapshot.js +++ b/v3/src/renderer/snapshot/CanvasSnapshot.js @@ -1,8 +1,11 @@ -var CanvasSnapshot = function (canvas) +var CanvasSnapshot = function (canvas) { var src = canvas.toDataURL(); - var image = new Image(); + + var image = new Image(); + image.src = src; + return image; }; diff --git a/v3/src/renderer/snapshot/Snapshot.js b/v3/src/renderer/snapshot/Snapshot.js new file mode 100644 index 000000000..9d3b7ea5c --- /dev/null +++ b/v3/src/renderer/snapshot/Snapshot.js @@ -0,0 +1,8 @@ +// Phaser.Renderer.Snapshot + +module.exports = { + + Canvas: require('./CanvasSnapshot'), + WebGL: require('./WebGLSnapshot') + +}; diff --git a/v3/src/snapshot/WebGLSnapshot.js b/v3/src/renderer/snapshot/WebGLSnapshot.js similarity index 83% rename from v3/src/snapshot/WebGLSnapshot.js rename to v3/src/renderer/snapshot/WebGLSnapshot.js index c66777df5..2acbb7bce 100644 --- a/v3/src/snapshot/WebGLSnapshot.js +++ b/v3/src/renderer/snapshot/WebGLSnapshot.js @@ -1,18 +1,22 @@ -var WebGLSnapshot = function (canvas) +var WebGLSnapshot = function (sourceCanvas) { - var gl = canvas.getContext('experimental-webgl'); + var gl = sourceCanvas.getContext('experimental-webgl'); var pixels = new Uint8Array(gl.drawingBufferWidth * gl.drawingBufferHeight * 4); gl.readPixels(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight, gl.RGBA, gl.UNSIGNED_BYTE, pixels); + // CanvasPool? var canvas = document.createElement('canvas'); var ctx = canvas.getContext('2d'); var imageData; + canvas.width = gl.drawingBufferWidth; canvas.height = gl.drawingBufferHeight; imageData = ctx.getImageData(0, 0, canvas.width, canvas.height); + var data = imageData.data; - for (var y = 0; y < canvas.height; y += 1) + + for (var y = 0; y < canvas.height; y += 1) { for (var x = 0; x < canvas.width; x += 1) { @@ -24,10 +28,12 @@ var WebGLSnapshot = function (canvas) data[di + 3] = pixels[si + 3]; } } + ctx.putImageData(imageData, 0, 0); var src = canvas.toDataURL(); - var image = new Image(); + var image = new Image(); + image.src = src; return image; diff --git a/v3/src/renderer/webgl/WebGLRenderer.js b/v3/src/renderer/webgl/WebGLRenderer.js index 762caeb86..4c974003f 100644 --- a/v3/src/renderer/webgl/WebGLRenderer.js +++ b/v3/src/renderer/webgl/WebGLRenderer.js @@ -17,7 +17,7 @@ var BlendModes = require('../BlendModes'); var ScaleModes = require('../ScaleModes'); var ResourceManager = require('./ResourceManager'); var Resources = require('./resources'); -var Snapshot = require('../../snapshot/Snapshot'); +var WebGLSnapshot = require('../snapshot/WebGLSnapshot'); var WebGLRenderer = function (game) { @@ -414,7 +414,7 @@ WebGLRenderer.prototype = { if (this.snapshotCallback) { - this.snapshotCallback(Snapshot.WebGLSnapshot(this.view)); + this.snapshotCallback(WebGLSnapshot(this.view)); this.snapshotCallback = null; } @@ -423,12 +423,11 @@ WebGLRenderer.prototype = { // console.log('%c render end ', 'color: #ffffff; background: #ff0000;'); }, - snapshot: function (callback) + snapshot: function (callback) { this.snapshotCallback = callback; }, - destroy: function () { this.gl = null; diff --git a/v3/src/snapshot/Snapshot.js b/v3/src/snapshot/Snapshot.js deleted file mode 100644 index 8dceaad08..000000000 --- a/v3/src/snapshot/Snapshot.js +++ /dev/null @@ -1,4 +0,0 @@ -module.exports = { - CanvasSnapshot: require('./CanvasSnapshot'), - WebGLSnapshot: require('./WebGLSnapshot') -};