使用fabricjs在旋转图像上未更新左上角坐标

编程入门 行业动态 更新时间:2024-10-24 18:27:58
本文介绍了使用fabricjs在旋转图像上未更新左上角坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当我在fabricjs中旋转图像时,旋转后不会更新左上角的坐标.而是,图像的左上角仍然指向旧点.我相信它应该根据图像的新位置重新计算左上角.有没有办法做到这一点?感谢您的帮助!

When I rotate an image in fabricjs, the top-left corner's coordinates are not updated after rotated. Instead, the top-left corner of the image still refers to the old point. I believe that it should recalculate the top-left corner based on the image's new position. Is there a way to achieve this? Any help is appreciated!

下面是图像旋转的代码:

Below is the code for image rotation:

function rotate(){ activeCanvas.forEachObject(function(obj){ if(obj instanceof fabric.Image){ curAngle = obj.getAngle(); obj.setAngle(curAngle-90); } }); activeCanvas.renderAll(); }

现在,在旋转之后,我想要新旋转的图像的左上角坐标,但是它仍然返回旧图像状态的左上角和左上角.

Now, after the rotation, I want top-left coordinates of the new rotated image but it still returns top and left from the old image state.

例如,假设图像的左上角最初位于(100,200),图像的尺寸为500x600.现在,如果我将图像旋转90度,则新尺寸为600x500,并且随着图像相对于其中心旋转,左上角也会发生变化.但是fabricjs图像仍然指向旧的左上角.像setCoords()一样,有什么方法可以将新的左上角点作为左上角?

For example, let say the image's top-left corner was originally at (100,200) and the image's dimensions are 500x600. Now, if I rotate the image in 90 degrees, the new dimensions are 600x500 and the top-left corner changes as well, as the image is rotated related to its center. But the fabricjs image still refers to the old top-left corner. Is there any method just like setCoords() to get the new upper left corner point as its top left?

推荐答案

如下面的代码片段所示,如果仅旋转对象,则只会更新边界框,您必须移动对象才能具有对象的位置已更新.

As you can see from the snippet below, if you only rotate your object, only the bounding box will be updated, you have to move your object to have the position of your object updated.

var canvas = this.__canvas = new fabric.Canvas('c'); var rect = new fabric.Rect({ left: 120, top: 30, width: 100, height: 100, fill: 'green', angle: 20 }); canvas.on("object:rotating", function() { var ao = canvas.getActiveObject(); if(ao){ console.log('top and left are the same after rotation'); console.log('top:' + ao.top); console.log('left:' + ao.left); console.log('but not the bounding box'); var bound = ao.getBoundingRect(); console.log('bounding box - top:' + bound.top); console.log('bounding box - left:' + bound.left); } }) canvas.add(rect); canvas.renderAll();

<script src="rawgit/kangax/fabric.js/master/dist/fabric.js"></script> <script src="seikichi.github.io/tmp/PDFJS.0.8.715/pdf.min.js"></script> <canvas id="c" style="border:1px solid black"></canvas>

更多推荐

使用fabricjs在旋转图像上未更新左上角坐标

本文发布于:2023-11-09 06:05:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1571585.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:左上角   坐标   图像   fabricjs

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!