gpt4 book ai didi

javascript - 使用 HTML5 Canvas 将图像解析为 tileset

转载 作者:可可西里 更新时间:2023-11-01 13:13:32 26 4
gpt4 key购买 nike

我正在尝试将 tileset 图像(类型为 .png)解析为数组。我有一个测试 Canvas ,我先用它来绘制它,然后我从中提取图像信息。当我运行这段代码时,它会抛出“Uncaught TypeError: Type error”。我能够记录 this.mainTiles 是空的。有任何想法吗?我还不知道 Canvas 的所有微妙之处,所以我被困住了。谢谢您的帮助! (另外,你可以忽略最后一行,我只是用它来测试——但我想说明它不起作用)。

function TileSet() {
this.tileSheets = [];
this.mainTiles = [];
this.tileHeight = 32;
this.tileWidth = 32;




this.addSpriteSheet = function (spriteSheetLoc, name) {

var tileSheet = new Image();
try {
tileSheet.src = spriteSheetLoc;
}
catch(err) {
dMode.Log("Invalid TileSheet Src ( TileSet.setSpriteSheet() Failed ); ERR: " + err);
}


tempContext.drawImage(tileSheet, 0, 0);


var tilesX = tempContext.width / this.tileWidth;
var tilesY = tempContext.height / this.tileHeight;

for(var i=0; i<tilesY; i++) {
for(var j=0; j<tilesX; j++) {
// Store the image data of each tile in the array.
this.mainTiles.push(tempContext.getImageData(j*this.tileWidth, i*this.tileHeight, this.tileWidth, this.tileHeight));
}
}


context.putImageData(this.mainTiles[0], 5, 5);



}

编辑:这里是 Canvas 等的定义方式:

var tempCanvas = document.getElementById("tempCanvas");
var tempContext = tempCanvas.getContext("2d");

var canvas = document.getElementById("gameCanvas");
var context = canvas.getContext("2d");


var Tiles = new TileSet;
//this is the line it gets stuck at


Tiles.addSpriteSheet("resources/tiles/tilea2.png");

编辑 2:在 markE 的回答之后,这是最新的更新。仍然感觉好像我缺少有关 .onload 的基本属性。

function TileSet() {
this.Tiles = [];
this.tileHeight = 32;
this.tileWidth = 32;
this.tileCount = 4;




this.addSpriteSheet = function (spriteSheetLoc, name) {

var tileSheet = new Image();
tileSheet.onload = function() {
tempCanvas.width = tileSheet.width;
tempCanvas.height = tileSheet.height;
tempContext.drawImage(tileSheet, 0, 0);



for (var t=0;t<this.tileCount;t++) {
this.Tiles[t]=tempContext.getImageData(t*this.tileWidth,t*32,this.tileWidth,tileSheet.height);
dMode.Log(this.Tiles);
}


context.putImageData(this.Tiles[this.Tiles.length-1],0,0);
dMode.Log(this.Tiles);
}

tileSheet.src = spriteSheetLoc;
}

最佳答案

以下是将 spritesheet 解析为独立 Canvas 图像数据数组的方法

我有一些工作代码可以执行您想要执行的操作。

我没有你的“resources/tiles/tilea2.png”,所以我使用了我自己的“monstersArun.png”,它是一个 10 跨 64x64 tiles spritesheet。

您可以修改此代码以适合您的 spritesheet 布局(行 x 列和图 block 大小)。

代码如下:

<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css -->
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>

<style>
body{ background-color: ivory; }
canvas{border:1px solid red;}
</style>

<script>
$(function(){

var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");

var tileCount=10;
var tiles=new Array();
var tileWidth;
var t;
var img=new Image();
img.onload=function(){
canvas.width=img.width;
canvas.height=img.height;
tileWidth=img.width/tileCount;
ctx.drawImage(img,0,0);

for(var t=0;t<tileCount;t++){
tiles[t]=ctx.getImageData(t*tileWidth,0,tileWidth,img.height);
}

// just a test
// draw the last tiles[] into another canvas
var canvasTile=document.getElementById("canvasTile");
var ctxTile=canvasTile.getContext("2d");
canvasTile.width=tileWidth;
canvasTile.height=canvas.height;
ctxTile.putImageData(tiles[tiles.length-1],0,0);
}
img.src="monsterarun.png";

}); // end $(function(){});
</script>

</head>

<body>

<canvas id="canvas" width=300 height=300></canvas><br/>
<canvas id="canvasTile" width=64 height=64></canvas>

</body>
</html>

[编辑——包括 Juan Mendes 在编码问题上提供帮助的好主意]

此外,当我查看您的代码时...此处:

tileSheet.src = spriteSheetLoc;

这会导致您的图片加载。加载需要时间,因此 javascript 开始加载图像,但它也会立即继续您的下一行代码。因此,您下面的代码会在图像可用之前尝试使用它——这不好!

因此,在处理其余代码之前,您应该让 javascript 有机会完全加载您的图像。您可以像这样使用 Image 的 onload 方法来执行此操作:

var tileSheet = new Image();
tileSheet.onload=function(){

// put ALL you code that depends on the image being fully loaded here

}
tileSheet.src = spriteSheetLoc;

注意如何将 tileSheet.src 放在 onload 函数之后。实际上,javascript 执行 tilesheet.src 然后返回执行 onload block 中的所有代码!

[再次编辑--完整代码]

<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css -->
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>

<style>
body{ background-color: ivory; }
canvas{border:1px solid red;}
</style>

<script>
$(function(){

var tempCanvas = document.getElementById("tempCanvas");
var tempContext = tempCanvas.getContext("2d");

var canvas = document.getElementById("gameCanvas");
var context = canvas.getContext("2d");


// create a new TileSet
var Tiles = new TileSet();

// parse a spritesheet into tiles
//Tiles.addSpriteSheet("resources/tiles/tilea2.png","anyName");
Tiles.addSpriteSheet("houseIcon.png","anyName");


function TileSet() {
this.Tiles = [];
this.tileHeight = 32;
this.tileWidth = 32;
this.tileCount = 4;

this.addSpriteSheet = function (spriteSheetLoc, name) {

var me=this; // me==this==TileSet

var tileSheet = new Image();
tileSheet.onload = function() {

// calculate the rows/cols in the spritesheet
// tilesX=rows, tilesY=cols
var tilesX = tileSheet.width / me.tileWidth;
var tilesY = tileSheet.height / me.tileHeight;

// set the spritesheet canvas to spritesheet.png size
// then draw spritesheet.png into the canvas
tempCanvas.width = tileSheet.width;
tempCanvas.height = tileSheet.height;
tempContext.drawImage(tileSheet, 0, 0);
for(var i=0; i<tilesY; i++) {

for(var j=0; j<tilesX; j++) {

// Store the image data of each tile in the array.
me.Tiles.push(tempContext.getImageData(j*me.tileWidth, i*me.tileHeight, me.tileWidth, me.tileHeight));
}
}

// this is just a test
// display the last tile in a canvas
context.putImageData(me.Tiles[me.Tiles.length-1],0,0);

}
// load the spritesheet .png into tileSheet Image()
tileSheet.src = spriteSheetLoc;
}
}

}); // end $(function(){});
</script>

</head>

<body>
<canvas id="tempCanvas" width=300 height=300></canvas><br/>
<canvas id="gameCanvas" width=300 height=300></canvas>
</body>
</html>

关于javascript - 使用 HTML5 Canvas 将图像解析为 tileset,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15444987/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com