如何水平翻转图像

时间:2016-03-13 17:35:28

标签: javascript html canvas

我怎样才能只画出两张反转的图像我不知道如何反转。请帮助。

    var canvas = document.createElement('canvas');
    canvas.width = 16 * scale;
    canvas.height = 32 * scale;
    //we assign the same classname the image has, for CSS purposes
    canvas.setAttribute('class', skinImage.getAttribute('class'));
    canvas.setAttribute('title', skinImage.getAttribute('title'));
    var context = canvas.getContext("2d"),
    s = scale;
    context.imageSmoothingEnabled=!1,
    context.mozImageSmoothingEnabled=!1,
    context.oImageSmoothingEnabled=!1,
    CanvasRenderingContext2D.webkitImageSmoothingEnabled=!1,
    context.clearRect(0,0,canvas.width,canvas.height);

    //back draw the head
    context.drawImage(skinImage, 24,  8,  8, 8,  4*s,  0*s,  8*s, 8*s);
    //back draw the body
    context.drawImage(skinImage, 32, 20, 8, 12, 4*s,  8*s,  8*s, 12*s);
    //back draw the right leg
    context.drawImage(skinImage, 52, 20, 4, 12, 12*s, 8*s,  4*s, 12*s);
    //back draw the right arm
    context.drawImage(skinImage, 12, 20, 4, 12, 8*s,  20*s, 4*s, 12*s);

    //back draw the left arm
    context.drawImage(skinImage, 12, 20, 4, 12, 4*s,  20*s, 4*s, 12*s);
    context.drawImage(skinImage, 52, 20, 4, 12, 0*s,  8*s,  4*s, 12*s);

3 个答案:

答案 0 :(得分:4)

首先,要删除2个不需要的图像,只需清除画布并重绘所需的图像即可。您可以使用context.clearRect(0,0,canvas.width,canvas.height)清除画布。

水平翻转图像

enter image description here

如何水平翻转图片

  1. 将画布原点移动(平移)到所需的X坐标加上图像宽度:context.translate(x+img.width,y);添加img.width是必要的,因为我们抓住图像的左边缘并向左翻转。如果不添加img.width,img将被绘制到所需x坐标的左侧。

  2. 使用context.scale(-1,1);

  3. 水平翻转
  4. 绘制图像:`context.drawImage(img,0,0);

  5. 通过将转换重置为默认值进行清理:context.setTransform(1,0,0,1,0,0);

  6. 带注释的代码和演示:

    var canvas=document.getElementById("canvas");
    var ctx=canvas.getContext("2d");
    var cw=canvas.width;
    var ch=canvas.height;
    
    var img=new Image();
    img.onload=start;
    img.src="https://dl.dropboxusercontent.com/u/139992952/multple/sillouette2.png";
    function start(){
    
        ctx.fillText('original',10,30);
        ctx.drawImage(img,10,30);
    
        ctx.fillText('flipped',150,30);
        flipHorizontally(img,150,30);
    
    }
    
    function flipHorizontally(img,x,y){
        // move to x + img's width
        ctx.translate(x+img.width,y);
    
        // scaleX by -1; this "trick" flips horizontally
        ctx.scale(-1,1);
        
        // draw the img
        // no need for x,y since we've already translated
        ctx.drawImage(img,0,0);
        
        // always clean up -- reset transformations to default
        ctx.setTransform(1,0,0,1,0,0);
    }
    #canvas{border:1px solid red; margin:0 auto; }
    <canvas id="canvas" width=300 height=300></canvas>

    [添加:从spritesheet翻转精灵]

    要翻转spritesheet中包含的精灵,您可以使用drawImage的裁剪形式。裁剪drawImage将通过指定精灵的spriteX, spriteY, spriteWidth & spriteHeight从精灵表中裁剪出所需的精灵。相同的裁剪drawImage还会通过指定所需的画布canvasX, canvasY, spriteWidth & spriteHeight将裁剪的子图像绘制到画布上。

    以下是显示如何从spritesheet翻转精灵的代码

    var canvas=document.getElementById("canvas");
    var ctx=canvas.getContext("2d");
    var cw=canvas.width;
    var ch=canvas.height;
    
    var img=new Image();
    img.onload=start;
    img.src="https://dl.dropboxusercontent.com/u/139992952/multple/cars.jpg";
    function start(){
    
      var spriteX=92;
      var spriteY=63;
      var spriteW=80;
      var spriteH=40;
    
      ctx.fillText('original',10,30);
      ctx.drawImage(img,spriteX,spriteY,spriteW,spriteH,10,30,spriteW,spriteH);
    
      ctx.fillText('flipped',150,30);
      flipSpriteHorizontally(img,150,30, spriteX,spriteY,spriteW,spriteH);
    
    }
    
    function flipSpriteHorizontally(img,x,y,spriteX,spriteY,spriteW,spriteH){
      // move to x + img's width
      // adding img.width is necessary because we're flipping from
      //     the right side of the img so after flipping it's still
      //     at [x,y]
      ctx.translate(x+spriteW,y);
    
      // scaleX by -1; this "trick" flips horizontally
      ctx.scale(-1,1);
    
      // draw the img
      // no need for x,y since we've already translated
      ctx.drawImage(img,
                    spriteX,spriteY,spriteW,spriteH,0,0,spriteW,spriteH
                   );
    
      // always clean up -- reset transformations to default
      ctx.setTransform(1,0,0,1,0,0);
    }
    #canvas{border:1px solid red; margin:0 auto; }
    <h4>Drawing & horizontally flipping a sprite</h4>
    <canvas id="canvas" width=300 height=100></canvas>
    <h4>Spritesheet:</h4>
    <img src='https://dl.dropboxusercontent.com/u/139992952/multple/cars.jpg'>

答案 1 :(得分:2)

镜像图像

这是一个简单的效用函数,可以水平,垂直或同时镜像图像。

// arguments
// ctx : the context on which to draw the mirrored image
// image : the image to mirror
// x,y : the top left of the rendered image
// horizontal : boolean if true mirror along X
// vertical : boolean if true mirror along y
function mirrorImage(ctx, image, x = 0, y = 0, horizontal = false, vertical = false){
    ctx.save();  // save the current canvas state
    ctx.setTransform(
        horizontal ? -1 : 1, 0, // set the direction of x axis
        0, vertical ? -1 : 1,   // set the direction of y axis
        x + horizontal ? image.width : 0, // set the x origin
        y + vertical ? image.height : 0   // set the y origin
    );
    ctx.drawImage(image,0,0);
    ctx.restore(); // restore the state as it was when this function was called
}

用法

mirrorImage(ctx, image, 0, 0, true, false); // horizontal mirror
mirrorImage(ctx, image, 0, 0, false, true); // vertical mirror
mirrorImage(ctx, image, 0, 0, true, true);  // horizontal and vertical mirror

更多镜子

如果您希望能够沿任意一行进行镜像,请参阅答案Mirror along line

答案 2 :(得分:0)

要镜像图像,您可以使用负比例setTransform

context.setTransform(-1, 0, 0, 1, 0, 0);    //Now all images will be horizontally mirrored
context.drawImage(<myimage>, srcx, srcy, srcw, srch, dstx, dsty, dstw, dsth);

完成后不要忘记恢复变换。

context.setTransform(1, 0, 0, 1, 0, 0);