2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2019-01-15 16:20:22 +00:00
|
|
|
* @copyright 2019 Photon Storm Ltd.
|
2018-02-12 16:01:20 +00:00
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
2019-01-25 17:29:03 +00:00
|
|
|
var CanvasPool = require('../../display/canvas/CanvasPool');
|
|
|
|
var Color = require('../../display/color/Color');
|
|
|
|
var GetFastValue = require('../../utils/object/GetFastValue');
|
|
|
|
|
2018-02-09 13:46:04 +00:00
|
|
|
/**
|
2019-01-25 17:29:03 +00:00
|
|
|
* Takes a snapshot of an area from the current frame displayed by a canvas.
|
|
|
|
*
|
|
|
|
* This is then copied to an Image object. When this loads, the results are sent
|
|
|
|
* to the callback provided in the Snapshot Configuration object.
|
2018-02-09 13:46:04 +00:00
|
|
|
*
|
|
|
|
* @function Phaser.Renderer.Snapshot.Canvas
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2019-01-25 17:29:03 +00:00
|
|
|
* @param {HTMLCanvasElement} sourceCanvas - The canvas to take a snapshot of.
|
|
|
|
* @param {SnapshotState} config - The snapshot configuration object.
|
2018-02-09 13:46:04 +00:00
|
|
|
*/
|
2019-01-25 17:29:03 +00:00
|
|
|
var CanvasSnapshot = function (canvas, config)
|
2017-05-16 19:15:01 +00:00
|
|
|
{
|
2019-01-25 17:29:03 +00:00
|
|
|
var callback = GetFastValue(config, 'callback');
|
|
|
|
var type = GetFastValue(config, 'type', 'image/png');
|
|
|
|
var encoderOptions = GetFastValue(config, 'encoder', 0.92);
|
|
|
|
var x = Math.abs(Math.round(GetFastValue(config, 'x', 0)));
|
|
|
|
var y = Math.abs(Math.round(GetFastValue(config, 'y', 0)));
|
|
|
|
var width = GetFastValue(config, 'width', canvas.width);
|
|
|
|
var height = GetFastValue(config, 'height', canvas.height);
|
|
|
|
var getPixel = GetFastValue(config, 'getPixel', false);
|
|
|
|
|
|
|
|
if (getPixel)
|
|
|
|
{
|
|
|
|
var context = canvas.getContext('2d');
|
|
|
|
var imageData = context.getImageData(x, y, 1, 1);
|
|
|
|
var data = imageData.data;
|
|
|
|
|
|
|
|
callback.call(null, new Color(data[0], data[1], data[2], data[3] / 255));
|
|
|
|
}
|
|
|
|
else if (x !== 0 || y !== 0 || width !== canvas.width || height !== canvas.height)
|
|
|
|
{
|
|
|
|
// Area Grab
|
|
|
|
var copyCanvas = CanvasPool.createWebGL(this, width, height);
|
|
|
|
var ctx = copyCanvas.getContext('2d');
|
|
|
|
|
|
|
|
ctx.drawImage(canvas, x, y, width, height, 0, 0, width, height);
|
|
|
|
|
|
|
|
var image1 = new Image();
|
|
|
|
|
|
|
|
image1.onerror = function ()
|
|
|
|
{
|
|
|
|
callback.call(null);
|
|
|
|
|
|
|
|
CanvasPool.remove(copyCanvas);
|
|
|
|
};
|
|
|
|
|
|
|
|
image1.onload = function ()
|
|
|
|
{
|
|
|
|
callback.call(null, image1);
|
2018-01-07 13:27:07 +00:00
|
|
|
|
2019-01-25 17:29:03 +00:00
|
|
|
CanvasPool.remove(copyCanvas);
|
|
|
|
};
|
2017-07-04 12:10:26 +00:00
|
|
|
|
2019-01-25 17:29:03 +00:00
|
|
|
image1.src = copyCanvas.toDataURL(type, encoderOptions);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Full Grab
|
|
|
|
var image2 = new Image();
|
|
|
|
|
|
|
|
image2.onerror = function ()
|
|
|
|
{
|
|
|
|
callback.call(null);
|
|
|
|
};
|
2017-07-04 12:10:26 +00:00
|
|
|
|
2019-01-25 17:29:03 +00:00
|
|
|
image2.onload = function ()
|
|
|
|
{
|
|
|
|
callback.call(null, image2);
|
|
|
|
};
|
2017-07-04 12:10:26 +00:00
|
|
|
|
2019-01-25 17:29:03 +00:00
|
|
|
image2.src = canvas.toDataURL(type, encoderOptions);
|
|
|
|
}
|
2017-05-16 19:15:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = CanvasSnapshot;
|