首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >动画/移动putImageData放置的画布图像

动画/移动putImageData放置的画布图像
EN

Stack Overflow用户
提问于 2015-02-27 14:35:59
回答 2查看 780关注 0票数 0

我有一个已经解码的jpeg图像,我正在用putImageData编写它到画布上。是否有可能将这张图片四处移动?我在上面找不到任何文件。

我的想法是用dirtyX和dirtyY裁剪解码图像的某一部分,现在我想要裁剪图像的另一部分。我用的是解码后的图像,就像一个喷纸。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-02-27 16:12:22

drawImage 使用的剪裁版本将您的各个精灵绘制到画布

(不要使用getImageData和putImageData来裁剪,因为drawImage更容易、更快。)

如果要将解码后的图像作为spritesheet使用,则可以使用画布的裁剪版本context.drawImage来剪辑spritesheet的特定子部分并将其绘制到画布上。

下面是关于drawImage剪裁版本的前一篇文章。

HTML / Java Script Canvas - how to draw an image between source and destination points?

下面是示例代码和演示:

请注意,在画布上“移动”精灵的方法是清除画布,并在画布的新位置重新绘制所需的精灵。(不能“移动”画布上现有的绘图)。

代码语言:javascript
复制
$(window).load(function(){

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

  // animation related variables
  var lastFlap,lastMove;

  // define a bird object
  // x,y are the position of the bird on the canvas
  // spriteX,spriteY is the position of the first desired
  //      sprite image on the spritesheet
  // width,height is the size of 1 sprite image
  // currentFrame is the index of which of the sprite images to display
  // currentDirection.  The sprite plays forward and then backward to
  //      accomplish 1 flap.  This determines if the next frame index will
  //      be increased (play forward) or decreased (play backward)
  var bird={
    x:30,
    y:30,
    spriteX:0,
    spriteY:52,
    width:51,
    height:51,
    frames:4,
    currentFrame:0,
    currentDirection:1
  }

  // load the spritesheet and start the animation
  var spritesheet=new Image();
  spritesheet.onload=start;
  spritesheet.src="https://dl.dropboxusercontent.com/u/139992952/multple/birdSpritesheet.png";
  function start(){
    requestAnimationFrame(animate);
  }

  function animate(time){

    // request another animation frame

    if(bird.x<canvas.width){
      requestAnimationFrame(animate);
    }

    // if the lastFlap or lastMove times don't aren't set, then set them

    if(!lastFlap){lastFlap=time;}
    if(!lastMove){lastMove=time;}

    // calculate the elapsed times since the last flap and the last move

    var elapsedFlap=time-lastFlap;
    var elapsedMove=time-lastMove;

    // if 50ms have elapsed, advance to the next image in this sprite

    if(elapsedFlap>50){

      // advance to next sprite on the spritesheet (flap)

      bird.currentFrame+=bird.currentDirection;

      // clamp bird.currentFrame between 0-3  (0,1,2,3)
      // (because there are 4 images that make up the whole bird sprite)

      if(bird.currentFrame<0 || bird.currentFrame>bird.frames-1){
        bird.currentDirection*=-1;
        bird.currentFrame+=bird.currentDirection;
      }

      // reset the flap timer

      lastFlap=time;
    }

    // locate the current sprite from the spritesheet

    var sx=bird.spriteX+bird.currentFrame*bird.width;
    var sy=bird.spriteY;

    // if 100ms have elapsed, move the bird across the canvas

    if(elapsedMove>100){
      bird.x+=3;
      lastMove=time;
    }

    // clear the whole canvas

    ctx.clearRect(0,0,canvas.width,canvas.height);

    // draw the current part of the bird sprite at the current bird.x

    ctx.drawImage(spritesheet,
                  sx,sy,bird.width,bird.height,
                  bird.x,bird.y,bird.width,bird.height
                 );

  }

}); // end $(function(){});
代码语言:javascript
复制
body{ background-color: white; }
canvas{border:1px solid red;}
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<h4>The canvas animating a clipped sprite</h4>
<canvas id="canvas" width=300 height=100></canvas>
<br>
<h4>The spritesheet</h4>
<img id=spritesheet src='https://dl.dropboxusercontent.com/u/139992952/multple/birdSpritesheet.png'>

票数 1
EN

Stack Overflow用户

发布于 2015-02-27 14:56:20

是和否:-)由于画布以即时模式绘制,它不知道您已经在画布上绘制了图像。所以,你不能一次在画布上画它,然后把画出来的图像到处移动。您将不得不手动重新绘制在每个动画帧中的完整图像在一个新的位置。正如Jonas解释的那样,使用requestAnimationFrame()可以做到这一点。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28767535

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档