简体   繁体   中英

Extract individual frame from a sprite sheet using html or javascript

I have a sprite sheet consisting of 12 frames.

在此处输入图片说明

I want to extract each individual frame from it and want to show it in different canvas like below. 在此处输入图片说明

what i have tried so far is posted below

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

}

I am only able to get a single image ,But I want to get all the images to show in a grid.And also i want to get the image to show any where I want. I also want to scale down big size images to show in a grid and while taping on it I want to show the original image.

Note: I don't want to animate my frames, I just want to show in grid. There are mostly examples of sprite animation available on internet.

You have the correct version of drawImage to clip individual sprites from the spritesheet, but you must alter the values in drawImage for each sprite.

The "faces" example spritesheet you show appear to have equal sized individual sprites (75px by 75px).

Assuming all your sprites are the same size, you would alter the 2nd & 3rd drawImage parameters which tell canvas the top-left x/y coordinate to begin clipping on the spritesheet.

Here's example code and a Demo: 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>

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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