简体   繁体   English

JavaScript画布上的免费变换矩形

[英]Free Transform Rectangle on JavaScript Canvas

I am new to the JavaScript canvas element. 我是JavaScript canvas元素的新手。 I actually just started a few hours ago. 我实际上是几个小时前才开始的。 I'm working on a free transform of a rectangular section of an image. 我正在对图像的矩形截面进行自由变换。 (For an example of what I am calling a "free transform," see this: http://www.html5.jp/test/perspective_canvas/demo1_en.html . Please note that I do not want to be able to allow the user to transform the image with handles as this example features.) (有关我所谓的“自由转换”的示例,请参见: http : //www.html5.jp/test/perspective_canvas/demo1_en.html 。请注意,我不想允许用户使用此示例功能处理带有手柄的图像。)

I know a little bit of how to transform a large image, but I want to transform only the left half (for example) of an image. 我知道如何变换大图像,但是我只想变换图像的左半部分。 How can I single out a section and free transform it? 如何选出一部分并进行自由变换? I'm guessing I should start by loading the image. 我猜我应该从加载图像开始。 I'm also betting the solution will involve marking the left side with paths. 我还押注解决方案将涉及用路径标记左侧。 But setTransform() doesn't given me the control I need. 但是setTransform()没有给我我所需的控件。 I would like to have a function similar to this: 我想要一个类似的功能:

function freeTransform(canvas, image,
  startX1, startY1, startX2, startY2, startX3, startY3, startX4, startY4,
  endX1, endY1, endX2, endY2, endX3, endY3, endX4, endY4)

But the canvas's context does not have this function (as far as I know). 但是画布的上下文没有此功能(据我所知)。 Would someone give me some direction? 有人可以给我一些指示吗? Thank you. 谢谢。

You are correct. 你是对的。

The html canvas cannot transform 1 corner of a rectangle using its common 2d context. html canvas无法使用其公共2d上下文变换矩形的1个角。

You can use "triangulation" to deform an image in canvas 2d to give it perspective: 您可以使用“三角剖分”使画布2d中的图像变形以使其具有透视图:

http://tulrich.com/geekstuff/canvas/perspective.html http://tulrich.com/geekstuff/canvas/perspective.html

You can get closer to a perspective transform using canvas's 3d context (webGL). 您可以使用画布的3d上下文(webGL)更加接近透视变换。

You can use non-parallel transforms to deform an image in webGL to give it perspective: 您可以使用非平行变换在webGL中变形图像以使其具有透视图:

http://games.greggman.com/game/webgl-3d-perspective/ http://games.greggman.com/game/webgl-3d-perspective/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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