【发布时间】:2014-07-25 01:12:16
【问题描述】:
我有一个由 12 帧组成的精灵表。
我想从中提取每个单独的帧,并希望在不同的画布中显示它,如下所示。
到目前为止我尝试过的都发布在下面
//HTML code for define the canvas area .
<body onload="image_render();">
<div id="image_area"> <canvas id="image"></canvas></div>
<script src="sprite_demo.js"></script>
</body>
// javascript to slice the image and assign to the canvas
var canvasImage = new Image();
canvasImage.src = "image/sprite_xxdpi.jpg";
var can= document.getElementById("image");
can.width = 500;
can.height = 300;
function image_render()
{
coin.render();
}
var coin = sprite({
context: can.getContext("2d"),
width: 500,
height: 300,
image: coinImage
});
function sprite (options) {
var that = {};
that.context = options.context;
that.width = options.width;
that.height = options.height;
that.image = options.image;
that.render = function () {
// Draw the animation
that.context.drawImage(
that.image,
0, //X-axis starting position from where slicing begins
0, //y-axis starting position from where slicing begins
that.width, //width of slicing image
that.height,//height of slicing image
0, //X-axis starting position where image will be drawn
0, //y-axis starting position where image will be drawn
150, // width of the resulting image
150); //height of the resulting image
};
return that;
}
我只能得到一个图像,但我想让所有图像显示在一个网格中。而且我想让图像显示在我想要的任何地方。 我还想缩小大尺寸图像以显示在网格中,并且在点击它时我想显示原始图像。
注意:我不想为我的帧设置动画,我只想在网格中显示。互联网上大多是精灵动画的例子。
【问题讨论】:
标签: javascript html canvas html5-canvas sprite-sheet