【问题标题】:CanvasRenderingContext2D flip transformationCanvasRenderingContext2D翻转变换
【发布时间】:2015-11-17 13:49:54
【问题描述】:

我找到了this CanvasRenderingContext2D,我用它玩了一下。我能够使用此上下文缩放和旋转我的图像:

crop: function () {
    var canvas = document.createElement("canvas");
    var context = canvas.getContext("2d");

    canvas.width = this.options.width / this.scale;
    canvas.height = this.options.height / this.scale;

    var currWidth = this.imgWidth * this.scale;
    var currHeight = this.imgHeight * this.scale;
    var correctX = (currWidth - this.imgWidth) / 2;
    var correctY = (currHeight - this.imgHeight) / 2;
    var sourceX = (this.posX - correctX) / this.scale;
    var sourceY = (this.posY - correctY) / this.scale;

    context.translate(sourceX, sourceY);
    context.translate(this.imgWidth / 2, this.imgHeight / 2);
    context.rotate(this.rotate * Math.PI / 180);
    context.drawImage(this.imgFull, this.imgWidth / 2 * -1, this.imgHeight / 2 * -1);

    this.options.modal.remove();
    this.promise.resolve(canvas);
},

不幸的是,我找不到任何垂直或水平翻转画布的功能。在代码中,我认为我可以执行以下操作:

if(self.flipV) {
    context.rotateY(180);
}

if(self.flipH) {
    context.rotateX(180);
}

但我找不到任何在 y 轴或 x 轴上旋转的方法。 有什么办法可以在这里进行翻转转换吗?

【问题讨论】:

  • 虽然不是很明显,但是可以使用context.scale方法进行翻转。使用context.scale(-1,1) 水平翻转,使用context.scale(1,-1) 垂直翻转。就像旋转一样,您必须在执行context.scale 之前使用context.translate 设置旋转点。 :-)
  • 哇。这是我的情况的完美解决方案。谢谢。你不想从你的评论中得到答案吗?

标签: canvas 2d transformation


【解决方案1】:

虽然不是很明显,但可以使用context.scale 方法进行翻转。

使用context.scale(-1,1) 水平翻转,使用context.scale(1,-1) 垂直翻转。

就像旋转一样,您必须在执行 context.scale 之前使用 context.translate 设置旋转点。

这是示例代码和演示:

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

var img=new Image();
img.onload=start;
img.src="https://dl.dropboxusercontent.com/u/139992952/multple/marioRunningRight.png";
function start(){

  // original
  ctx.drawImage(img,200,200);

  // horizontal flip
  ctx.translate(200,0);
  ctx.scale(-1,1);
  ctx.drawImage(img,0,200);
  ctx.setTransform(1,0,0,1,0,0);

  // vertical flip
  ctx.translate(0,200);
  ctx.scale(1,-1);
  ctx.drawImage(img,200,0);
  ctx.setTransform(1,0,0,1,0,0);

  // labels
  ctx.font='18px verdana';
  ctx.textAlign='center';
  ctx.fillText('Original',275,375);
  ctx.fillText('scale(-1,1)',125,375);
  ctx.fillText('Horizontal Flip',125,395);
  ctx.fillText('scale(1,-1)',275,20);
  ctx.fillText('Vertical Flip',275,40);
}
<canvas id="canvas" width=400 height=425></canvas>

  

【讨论】:

    【解决方案2】:

    通过 CanvasRenderingContext2D 镜像图像

    这是一个简单的实用功能,可以水平、垂直或同时镜像图像。

    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

    【讨论】:

      【解决方案3】:

      我不知道这是否是您要查找的内容,但您可以使用 CSS 来反映图像。我的一个项目中有这个:

      <style>
      .image-reflect {
          transform: scaleX(-1);
      }
      </style>
      .
      .
      .
      <span class="fa fa-share-alt image-reflect"></span>
      

      将 FontAwesome share icon 显示为“合并”图标。

      实际上,如果你查看“CSS 变换”,你会发现它可以旋转、缩放、移动、倾斜等。

      【讨论】:

      • 我试过这个,但我实际上不知道如何在我的 javascript 代码中对我的画布对象执行该 css。画布仅在我的 javascript 中创建,因为它是动态图像裁剪视图。我对 html/css 和 javascript 不是很熟悉。我缺乏经验。
      • @Mulgard,你使用 jQuery 之类的东西来处理你的 DOM 吗?如果是这样,将样式动态应用于您的元素真的很容易。例如,这不是我的想法,你可以做类似$("#my-canvas").css("transform", "scaleX(-1)");
      • 看看developer.mozilla.org/en-US/docs/Web/API/Element ...您应该能够在不使用jQuery的情况下执行canvas.id = "my-canvas";之类的操作来设置ID。您还应该能够通过canvas.className = "image-reflect"; 设置课程。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多