简体   繁体   English

HTML5 Canvas Tileset绘图

[英]HTML5 Canvas Tileset drawing

Something like I am have tileset - http://flashpunk.net/img/tut/swordguy.png 像我这样的东西 - http://flashpunk.net/img/tut/swordguy.png

And my drawing code is - http://jsfiddle.net/WnjB6/ 我的绘图代码是 - http://jsfiddle.net/WnjB6/

But how to do something like - drawTile(x, y, tile, width, height); 但是怎么做 - drawTile(x,y,tile,width,height);

Now need to set tileX and tileY on tile, but how to do only one tile need to set and draw tiles from all tileset? 现在需要在tile上设置tileX和tileY,但是如何只需要设置一个tile并从所有tileset中绘制tile?

Something like now need tileX = 3, tileY = 1, but I am need - tile = 8 and draw same tile. 像现在这样的东西需要tileX = 3,tileY = 1,但我需要 - tile = 8并绘制相同的tile。 'http://flashpunk.net/img/tut/swordguyframes.png “http://flashpunk.net/img/tut/swordguyframes.png

How to do something like? 怎么办?

Thanks for help and sorry for my bad English language. 感谢您的帮助,抱歉我的英语不好。

You know there are 6 tiles in a row, so you can do it like this: 你知道连续有6个瓷砖,所以你可以这样做:

var drawTile = function(x, y, tile, width, height) {
    context.drawImage(image, (tile % 6) * width, Math.floor(tile / 6) * height, width, height, x, y, width, height);
};

So passing in 6 will go to the first tile in the second row, etc. 因此,传入6将转到第二行中的第一个图块,等等。

Here's an example, it cycles through all the tiles: 这是一个例子,它遍历所有瓷砖:

http://jsfiddle.net/Jrjyq/ http://jsfiddle.net/Jrjyq/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM