在画布上绘制时,图像会丢失 webkit 转换

发布于 2024-12-25 09:01:09 字数 583 浏览 3 评论 0原文

我正在对 img 应用 webkit 转换,然后我想将该图像绘制到画布上。当我应用代码时,图像显示了转换,但画布显示的图像就像转换之前一样......提前感谢任何想法......

          var canvas = document.getElementById("myCanvas");
          var context = canvas.getContext("2d"); 
          var wrkimg = document.getElementById("workingimg");

          wrkimg.onload = function()
                {
                   $(this).css('-webkit-transform', 'rotate(162deg)  scale(1,1) scaleX(1)');    
                   context.drawImage(wrkimg, 50, 50);                
                 }

          wrkimg.src = 'Appimages/Head1.png';

I am applying a webkit transformation to an img then i want to draw that image onto a canvas. when i apply the code the image shows the transformations, but the canvas show the image like it was before the transformations... any ideas thanks in advance..

          var canvas = document.getElementById("myCanvas");
          var context = canvas.getContext("2d"); 
          var wrkimg = document.getElementById("workingimg");

          wrkimg.onload = function()
                {
                   $(this).css('-webkit-transform', 'rotate(162deg)  scale(1,1) scaleX(1)');    
                   context.drawImage(wrkimg, 50, 50);                
                 }

          wrkimg.src = 'Appimages/Head1.png';

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半透明的墙 2025-01-01 09:01:09

当您将图像绘制到 Canvas 时,您不会使用任何 webkit/CSS 转换来绘制图像。你只需绘制普通的旧图像即可。

但是,您可以将变换应用于 Canvas 上下文本身,然后绘制图像。

请注意,这与对画布应用任何类型的变换不同。只是画布上下文。

因此,您不需要执行 webkit 旋转转换,而是需要调用 context.rotate

context.rotate(162 * (Math.PI/180)); // works in radians so we gotta convert
context.drawImage(wrkimg, 50, 50);

请注意,这里缺少一个步骤。当您变换上下文时,您始终会根据上下文的左上角进行变换,因此通常您必须结合使用平移和旋转才能获得所需的效果。

以下是使用画布上下文围绕其中心旋转图像的示例: http://jsfiddle.net/Wyw8p/

When you draw an image to the Canvas you do not draw the image with any of its webkit/CSS transforms. You just get to draw the plain old image.

However you can apply transforms to the Canvas context itself and then draw the image.

Note that this is not the same as applying any kind of transform to the Canvas. Just the canvas context.

So instead of doing a webkit rotate transform you'll want to call context.rotate

context.rotate(162 * (Math.PI/180)); // works in radians so we gotta convert
context.drawImage(wrkimg, 50, 50);

Note that there's a step missing here. When you transform a context you are always transforming by the context's top-left corner, so typically you'll have to do a combination of translation and rotation to get what you want.

Here's an example of an image rotated about its center using the canvas context: http://jsfiddle.net/Wyw8p/

兔小萌 2025-01-01 09:01:09

我们找到了问题的解决方案。我的同事 Nikola Chakurov 发现了一个名为 Html2Canvas 的新版本的库。发布版本是 0.5.0-rc1,它非常适合与旋转相关的问题。这是解决我们问题的链接:

https://github.com/niklasvh/html2canvas/ tree/rc/build

我想提一下,Html2Canvas 0.5.0-rc1 版本的代码看起来非常干净漂亮。从我这里竖起大拇指。

We found solution of the problem. My colleague Nikola Chakurov found a new release of the library named Html2Canvas. The release version is 0.5.0-rc1 and it works perfect for the problem related with the rotation. This is the link which solve our problem:

https://github.com/niklasvh/html2canvas/tree/rc/build

I would like to mention that the code of the Html2Canvas version 0.5.0-rc1 it's look very clean and nice. Thumb up from me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文