热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在画布上多次翻转和镜像图像-Flippingandmirroringanimagemultipletimesonacanvas

Imtryingtomakeamosaicofandimagesclonethatisflippedandmirroredagainseachother.我正在尝

I'm trying to make a mosaic of and images clone that is flipped and mirrored agains each other.

我正在尝试制作一个马赛克和图像克隆,它们互相翻转和镜像。

But I can't get it to work properly. I have this JSFiddle: http://jsfiddle.net/arpo/yafLg7dc/

但我不能让它正常工作。我有这个JSFiddle:http://jsfiddle.net/arpo/yafLg7dc/

I want every second one flipped agains the first and so on.

我希望每一秒钟都会翻转第一个,依此类推。

But when I change to values to what I thought would do the trick the mosaic gets screwed up. Here's the values that I hoped should work but doesn't.

但是当我改变价值观时,我认为这样做会让马赛克搞砸了。这是我希望应该起作用但不起作用的价值观。

_drawImg(img, ctx, x1, y1, w, h, false, false);
_drawImg(img, ctx, x2, y1, w, h, true, false);
_drawImg(img, ctx, x1, y2, w, h, false, true);
_drawImg(img, ctx, x2, y2, w, h, true, true);

1 个解决方案

#1


Flipping an image horizontally (left to right) involves:

水平翻转图像(从左到右)涉及:

  • translate to the horizontal middle of the image. This sets the rotation point as the horizontal middle of the image. It does this by moving the canvas's [0,0] origin horizontally to the middle of the image.

    转换为图像的水平中间。这将旋转点设置为图像的水平中间。它通过将画布的[0,0]原点水平移动到图像的中间来实现。

    context.translate(x+img.width/2,0);
    
  • Use scale to flip the image horizontally. This causes the image to be drawn as a horizontal mirror of itself.

    使用比例水平翻转图像。这会导致图像被绘制为自身的水平镜像。

    context.scale(-1,1);
    
  • drawImage the image offset by half the image's width. This offset is necessary because context.translate has moved the [0,0] to the midpoint of the image so the image must be pulled leftward so that it's drawn at the desired X location.

    drawImage图像偏移图像宽度的一半。此偏移是必要的,因为context.translate已将[0,0]移动到图像的中点,因此必须向左拉动图像,以便在所需的X位置绘制图像。

    context.drawImage(img,-img.width/2,y);
    

body{ background-color: ivory; }
#canvas{border:1px solid red;}


推荐阅读
author-avatar
海纳百川2602902033
这个家伙很懒,什么也没留下!
Tags | 热门标签
RankList | 热门文章
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有