使用html或javascript从精灵表中提取单个帧

时间:2014-06-04 11:46:05

标签: javascript html5 canvas html5-canvas sprite-sheet

我有一个包含12帧的精灵表。

enter image description here

我想从中提取每个单独的框架,并希望在不同的画布中显示它,如下所示。 enter image description here

我到目前为止所尝试的内容发布在下面

//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;

}

我只能获得一张图片,但我想让所有图片都显示在网格中。而且我想让图片显示在我想要的任何地方。 我还希望缩小大尺寸图像以在网格中显示,并在拍摄时我想显示原始图像。

注意:我不想动画我的帧,我只想在网格中显示。互联网上主要有精灵动画的例子。

1 个答案:

答案 0 :(得分:3)

你有正确版本的drawImage来剪贴spritesheet中的各个精灵,但是你必须为每个精灵改变drawImage中的值。

&#34;面孔&#34;您显示的示例spritesheet似乎具有相同大小的单个精灵(75px乘75px)。

假设你所有的精灵都是相同的大小,你会改变第二个&amp;第3个drawImage参数告诉画布左上角的x / y坐标开始剪贴在spritesheet上。

以下是示例代码和演示:http://jsfiddle.net/m1erickson/tVD2K/

<!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 spriteWidth=75;
    var spriteHeight=75;
    var spriteCols=4;
    var spriteRows=3;
    var y=20-sprightHeight;

    var img=new Image();
    img.onload=start;
    img.src="https://dl.dropboxusercontent.com/u/139992952/multple/spritesheet1.jpg";
    function start(){
        var canvasY=0;
        for(var col=0;col<spriteCols;col++){
        for(var row=0;row<spriteRows;row++){
            var sourceX=col*spriteWidth;
            var sourceY=row*spriteHeight;
            // testing: calc a random position to draw this sprite
            // on the canvas
            var canvasX=Math.random()*150+20;
            canvasY+=spriteHeight+5;
            // drawImage with changing source and canvas x/y positions
            ctx.drawImage(img,
                sourceX,sourceY,spriteWidth,spriteHeight,
                canvasX,canvasY,spriteWidth,spriteHeight
            );
        }}
    }

}); // end $(function(){});
</script>
</head>
<body>
    <h4>Draw individual sprites from a spritesheet</h4>
    <canvas id="canvas" width=300 height=1000></canvas>
</body>
</html>

enter image description here