如何使用纯 javascript 从精灵表中提取精灵
How to extract sprites from a spritesheet with pure javascript
所以我有这个 spritesheet,其中包含塞尔达传说中的所有不同图块,有没有一种方法可以让我输入像 getTile(2,1) 这样的函数,它会 return base64骑士的编码?
到目前为止我已经试过了
function getTiles() {
var sprites = new Image();
sprites.src = 'https://cdn.glitch.me/b3505ee1-a321-48ea-baf5-d84841324af1%2Fd3b13ce6-f5ed-4122-9bd9-37f74d13d470.image.png?v=1634242985542';
allOverworldTiles = []
var i = 0;
for (var top = 0; top < 8 * 17; top += 17) {
for (var left = 0; left < 20 * 17; left += 17) {
var canvas = document.createElement("canvas");
var ctx = canvas.getContext("2d");
canvas.width = 16;
canvas.height = 16;
ctx.drawImage(sprites, left + 1, top + 1, 16, 16, 0, 0, 16, 16)
}
}
var dataURL = canvas.toDataURL("image/png");
var newTab = window.open('about:blank', 'image from canvas');
newTab.document.write("<img src='" + dataURL + "' alt='from canvas'/>");
}
如果答案是原版的,我会更喜欢javascript
您的代码存在一些问题...
- 你需要一个 onload 事件来确保图像已经加载,然后你才能做任何事情
- 你需要
sprites.crossOrigin="anonymous"
否则你会得到一个 Tainted canvas 错误
- 你的逻辑不清楚,你有一个双循环但循环外只有一个toDataURL
这是一个工作原型
var canvas = document.createElement("canvas");
var ctx = canvas.getContext("2d");
canvas.width = canvas.height = 16;
var sprites = new Image();
sprites.crossOrigin="anonymous"
sprites.src = 'https://cdn.glitch.me/b3505ee1-a321-48ea-baf5-d84841324af1%2Fd3b13ce6-f5ed-4122-9bd9-37f74d13d470.image.png?v=1634242985542';
sprites.onload = getTiles
function getTile(x, y) {
ctx.drawImage(sprites, x * 17 + 1, y * 17 + 1, 16, 16, 0, 0, 16, 16)
return canvas.toDataURL("image/png");
}
function getTiles() {
var knight1 = getTile(2, 1)
document.write("<img src='" + knight1 + "'/>");
var knight2 = getTile(8, 1)
document.write("<img src='" + knight2 + "'/>");
var knight3 = getTile(14, 1)
document.write("<img src='" + knight3 + "'/>");
}
所以我有这个 spritesheet,其中包含塞尔达传说中的所有不同图块,有没有一种方法可以让我输入像 getTile(2,1) 这样的函数,它会 return base64骑士的编码?
到目前为止我已经试过了
function getTiles() {
var sprites = new Image();
sprites.src = 'https://cdn.glitch.me/b3505ee1-a321-48ea-baf5-d84841324af1%2Fd3b13ce6-f5ed-4122-9bd9-37f74d13d470.image.png?v=1634242985542';
allOverworldTiles = []
var i = 0;
for (var top = 0; top < 8 * 17; top += 17) {
for (var left = 0; left < 20 * 17; left += 17) {
var canvas = document.createElement("canvas");
var ctx = canvas.getContext("2d");
canvas.width = 16;
canvas.height = 16;
ctx.drawImage(sprites, left + 1, top + 1, 16, 16, 0, 0, 16, 16)
}
}
var dataURL = canvas.toDataURL("image/png");
var newTab = window.open('about:blank', 'image from canvas');
newTab.document.write("<img src='" + dataURL + "' alt='from canvas'/>");
}
如果答案是原版的,我会更喜欢javascript
您的代码存在一些问题...
- 你需要一个 onload 事件来确保图像已经加载,然后你才能做任何事情
- 你需要
sprites.crossOrigin="anonymous"
否则你会得到一个 Tainted canvas 错误 - 你的逻辑不清楚,你有一个双循环但循环外只有一个toDataURL
这是一个工作原型
var canvas = document.createElement("canvas");
var ctx = canvas.getContext("2d");
canvas.width = canvas.height = 16;
var sprites = new Image();
sprites.crossOrigin="anonymous"
sprites.src = 'https://cdn.glitch.me/b3505ee1-a321-48ea-baf5-d84841324af1%2Fd3b13ce6-f5ed-4122-9bd9-37f74d13d470.image.png?v=1634242985542';
sprites.onload = getTiles
function getTile(x, y) {
ctx.drawImage(sprites, x * 17 + 1, y * 17 + 1, 16, 16, 0, 0, 16, 16)
return canvas.toDataURL("image/png");
}
function getTiles() {
var knight1 = getTile(2, 1)
document.write("<img src='" + knight1 + "'/>");
var knight2 = getTile(8, 1)
document.write("<img src='" + knight2 + "'/>");
var knight3 = getTile(14, 1)
document.write("<img src='" + knight3 + "'/>");
}