简体   繁体   English

Fabric js在将图像加载到fabric.Image.fromURL后旋转图像

[英]Fabric js rotate a image after it has been loaded with fabric.Image.fromURL

I have an image loaded using fabrics fabric.Image.fromURL 我有使用面料fabric.Image.fromURL加载的图像

 fabric.Image.fromURL($scope.image, function(oImg)
            {
                oImg.set({width: $scope.imageWidth, height: $scope.imageHeight, originX:  'left', originY: 'top', selectable: false});
                canvas.add(oImg);
                canvas.centerObject(oImg);
                canvas.renderAll();
                oImg.sendToBack();
            });

What I want to do now is I have a rotation button on the page where they can rotate this image. 我现在想做的是页面上有一个旋转按钮,他们可以在其中旋转此图像。 however I cannot modify the image object after it's been loaded already. 但是,在已加载图像对象之后,我无法对其进行修改。 I've tried: 我试过了:

 oImg.rotate(90) 

but oImg is undefined now. 但是oImg现在尚未定义。 Has anyone had any luck with this? 有人有运气吗?

First off, I'd suggest using the .setAngle method as opposed to the .rotate. 首先,我建议使用.setAngle方法而不是.rotate方法。

But the root of your issue is how do you target that specific object. 但是,问题的根源是如何针对特定对象。

Your oImg variable is localized to the function that is creating the object on the canvas. 您的oImg变量已本地化到在画布上创建对象的函数。 So simply setting oImg to a global variable allows you to target that object via a variable name. 因此,只需将oImg设置为全局变量,就可以通过变量名称定位该对象。

var rotateThisImage;  /* Set this on a global scope outside of a function */
...
rotateThisImage = oImg;  /* Set oImg to your new global variable */
...
rotateThisImage.setAngle(curAngle+15);  /* Elsewhere in your code, on button click, set angle */

Here is an example of this: http://jsfiddle.net/PromInc/h9kL5bs0/ 这是一个示例: http : //jsfiddle.net/PromInc/h9kL5bs0/

Another approach is to rotate only the active object on the canvas. 另一种方法是仅旋转画布上的活动对象。 For this to work though, the object would need to be selectable and you'd have to remove your selectable:false attribute. 为了使它起作用,该对象将需要是可选的,并且您必须删除selectable:false属性。

canvas._activeObject

Canvas is the variable name for the canvas it self, and _activeObject is whatever object is selected on the canvas. Canvas是其自身画布的变量名,_activeObject是在画布上选择的任何对象。

Here is an example of that - note that you have to select the object before it'll rotate. 这是一个示例-请注意,您必须先选择对象才能旋转它。 http://jsfiddle.net/PromInc/ckqk2Lzs/ http://jsfiddle.net/PromInc/ckqk2Lzs/

Yet another approach that would allow you to keep the selectable:false attribute is to select the object by it's object id on the canvas. 允许您保留selectable:false属性的另一种方法是通过画布上的对象ID选择对象。

canvas.item(0)

Item 0 is the first item on the canvas, which is the lowest in the layering order. 项目0是画布上的第一项,在分层顺序中是最低的。 If you had 5 objects on the canvas, the top most object would be item 4 as the indexing starts at 0. 如果您在画布上有5个对象,则最上面的对象将是项4,因为索引从0开始。

Here is an example of this method: http://jsfiddle.net/PromInc/3efe2x9j/ 这是此方法的示例: http : //jsfiddle.net/PromInc/3efe2x9j/

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

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