在 PhaserJS 中将像素艺术图下载为 .png
Download pixel art drawing as .png in PhaserJS
我需要通过 FilesSaver.js 从这个 Phaser example 下载创建的像素绘图作为 .png 图片,但是 canvas returns空。
错误:
Uncaught TypeError: Cannot read properties of null (reading 'toBlob')
这是保存函数:
function save() {
var canvasX = document.getElementById("canvas");
canvasX.toBlob(function(blob) { saveAs(blob, "image.png"); }); }
绘图区域:(PhaserJS 2)
function createDrawingArea() {
game.create.grid('drawingGrid', 16 * canvasZoom, 16 * canvasZoom, canvasZoom, canvasZoom, 'rgba(0,191,243,0.8)');
canvas = game.make.bitmapData(spriteWidth * canvasZoom, spriteHeight * canvasZoom);
canvasBG = game.make.bitmapData(canvas.width + 2, canvas.height + 2);
canvasBG.rect(0, 0, canvasBG.width, canvasBG.height, '#fff');
canvasBG.rect(1, 1, canvasBG.width - 2, canvasBG.height - 2, '#3f5c67');
var x = 10;
var y = 64;
canvasBG.addToWorld(x, y);
canvasSprite = canvas.addToWorld(x + 1, y + 1);
canvasGrid = game.add.sprite(x + 1, y + 1, 'drawingGrid');
canvasGrid.crop(new Phaser.Rectangle(0, 0, spriteWidth * canvasZoom, spriteHeight * canvasZoom));
}
如何获取绘图的数据以从中创建 .png?
嗯,我认为 canvas 没有 ID canvas
。这就是为什么,我认为这就是 null
错误的原因。
无论如何,我都将 原始示例代码 作为此工作解决方案的基础。
免责声明: 此代码只会从“绘制图像”创建图像,而不是整个 UI.
主要思想,On Save
:
- 新建一个
canvas
- 将目标区域绘制到新的canvas
- 创建图像,
filesave.js
信息: 我从全局定义的变量 canvasGrid
和 canvas
得到 information/values,如果您的代码不包含它们,此代码将不起作用。
希望对您有所帮助。
function saveImage() {
// I assume there will be only one canvas on the page
let realCanvas = document.querySelector('canvas');
let ouputCanvas = document.createElement('canvas');
let ctx = ouputCanvas.getContext('2d');
// Get the target area (Details are from example code)
let xOfGrid = canvasGrid.x - 1; // Info from Linie 267 from example
let yOfGrid = canvasGrid.y - 1; // Info from Linie 267 from example
// Info: this "canvas" is not a HTML Canvas Element
let width = canvas.width; // Info from Linie 256 from example
let height = canvas.height; // Info from Linie 256 from example
// Set initial Canvas Size
ouputCanvas.width = width;
ouputCanvas.height = height;
// draw Image onto new Canvas
ctx.drawImage(realCanvas, xOfGrid, yOfGrid, width, height, 0, 0, width, height);
// Output Image, with filesaver.js
ouputCanvas.toBlob(function onDone(blob) {
saveAs(blob, "image.png");
});
}
// An extra "Save Button", for testing
window.addEventListener('DOMContentLoaded', function(){
let btn = document.createElement('button');
btn.innerText = 'SAVE FILE';
btn.addEventListener('click', saveImage);
document.body.prepend( btn );
});
我需要通过 FilesSaver.js 从这个 Phaser example 下载创建的像素绘图作为 .png 图片,但是 canvas returns空。
错误:
Uncaught TypeError: Cannot read properties of null (reading 'toBlob')
这是保存函数:
function save() {
var canvasX = document.getElementById("canvas");
canvasX.toBlob(function(blob) { saveAs(blob, "image.png"); }); }
绘图区域:(PhaserJS 2)
function createDrawingArea() {
game.create.grid('drawingGrid', 16 * canvasZoom, 16 * canvasZoom, canvasZoom, canvasZoom, 'rgba(0,191,243,0.8)');
canvas = game.make.bitmapData(spriteWidth * canvasZoom, spriteHeight * canvasZoom);
canvasBG = game.make.bitmapData(canvas.width + 2, canvas.height + 2);
canvasBG.rect(0, 0, canvasBG.width, canvasBG.height, '#fff');
canvasBG.rect(1, 1, canvasBG.width - 2, canvasBG.height - 2, '#3f5c67');
var x = 10;
var y = 64;
canvasBG.addToWorld(x, y);
canvasSprite = canvas.addToWorld(x + 1, y + 1);
canvasGrid = game.add.sprite(x + 1, y + 1, 'drawingGrid');
canvasGrid.crop(new Phaser.Rectangle(0, 0, spriteWidth * canvasZoom, spriteHeight * canvasZoom));
}
如何获取绘图的数据以从中创建 .png?
嗯,我认为 canvas 没有 ID canvas
。这就是为什么,我认为这就是 null
错误的原因。
无论如何,我都将 原始示例代码 作为此工作解决方案的基础。
免责声明: 此代码只会从“绘制图像”创建图像,而不是整个 UI.
主要思想,On Save
:
- 新建一个
canvas
- 将目标区域绘制到新的canvas
- 创建图像,
filesave.js
信息: 我从全局定义的变量 canvasGrid
和 canvas
得到 information/values,如果您的代码不包含它们,此代码将不起作用。
希望对您有所帮助。
function saveImage() {
// I assume there will be only one canvas on the page
let realCanvas = document.querySelector('canvas');
let ouputCanvas = document.createElement('canvas');
let ctx = ouputCanvas.getContext('2d');
// Get the target area (Details are from example code)
let xOfGrid = canvasGrid.x - 1; // Info from Linie 267 from example
let yOfGrid = canvasGrid.y - 1; // Info from Linie 267 from example
// Info: this "canvas" is not a HTML Canvas Element
let width = canvas.width; // Info from Linie 256 from example
let height = canvas.height; // Info from Linie 256 from example
// Set initial Canvas Size
ouputCanvas.width = width;
ouputCanvas.height = height;
// draw Image onto new Canvas
ctx.drawImage(realCanvas, xOfGrid, yOfGrid, width, height, 0, 0, width, height);
// Output Image, with filesaver.js
ouputCanvas.toBlob(function onDone(blob) {
saveAs(blob, "image.png");
});
}
// An extra "Save Button", for testing
window.addEventListener('DOMContentLoaded', function(){
let btn = document.createElement('button');
btn.innerText = 'SAVE FILE';
btn.addEventListener('click', saveImage);
document.body.prepend( btn );
});