phaser/src/renderer/snapshot/CanvasSnapshot.js

88 lines
2.6 KiB
JavaScript
Raw Normal View History

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}
*/
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
/**
* 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
*
* @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
*/
var CanvasSnapshot = function (canvas, config)
2017-05-16 19:15:01 +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);
CanvasPool.remove(copyCanvas);
};
image1.src = copyCanvas.toDataURL(type, encoderOptions);
}
else
{
// Full Grab
var image2 = new Image();
image2.onerror = function ()
{
callback.call(null);
};
image2.onload = function ()
{
callback.call(null, image2);
};
image2.src = canvas.toDataURL(type, encoderOptions);
}
2017-05-16 19:15:01 +00:00
};
module.exports = CanvasSnapshot;