Which object was modified in fabric.js

Progrower

I'm adding objects to a canvas and keeping track of their type and count number. When I add them to the canvas everything works great but when I'm modifying them I'm struggling with which object was modified in my fabric.js canvas but only if it's a cardtype. I want to get it's coordinates the cardtype and the cardcount. I made a fiddle that shows everything working except that I get the info for all objects even when I only modify one object. How do I only get the actual activeObject and it's info?

Here's the js in the fiddle that I've been trying to work out.

        //**********When card is moved************  

    canvas.on('object:modified', onObjectModified);

          function onObjectModified(e) {    

          var activeObject = e.target;

          alert(activeObject.get('left')+' '+ activeObject.get('top')+' '+cardtype+' '+cardcount);                  

       };
Konrad 'Zegis'

To get selected canvas object with fabric.js you need to use methood getActiveObject() it returns exactly what you need. Like:

alert('Modified object x,y: ' + this.getActiveObject().get('left') + 
  ' ' + this.getActiveObject().get('left') + 
  ' it\'s id is: ' + this.getActiveObject()._objects[0].id);

Here's fiddle.

And remember to check your brackets. Now you add onObiectModified as an eventhandler for each yellowcard, and I'll be called for each. Just move canvas.on('object:modified', onObjectModified); elsewhere, for example after var declarations.

Oh and if you use test enviroments like jsFiddle with fabric.js, please use as link https://rawgithub.com/kangax/fabric.js/master/dist/fabric.js instead of https://raw.github.com/kangax/fabric.js/master/dist/fabric.js, (note removing of dot!) or else it won't work on Chrome (see this question)

Hope it helps.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何以编程方式触发fabric.js“ object:modified”事件?

来自分类Dev

Fabric.js-将object:modified事件同步到另一个客户端

来自分类Dev

Fabric.js-将object:modified事件同步到另一个客户端

来自分类Dev

fabric.js:我的fabric.Object子类的_render方法永远不会被调用

来自分类Dev

Fabric.js:如何观察object:scaling事件并动态更新属性?

来自分类Dev

Fabric.js:如何观察object:scaling事件并动态更新属性?

来自分类Dev

Fabric JS父子对象

来自分类Dev

Fabric JS中的BringForward

来自分类Dev

Fabric JS 对象缩放

来自分类Dev

画布旋转 - Fabric js

来自分类Dev

Javascript - Return modified object passed by reference

来自分类Dev

Fabric.js的几何形状

来自分类Dev

如何设置Fabric.js?

来自分类Dev

改善Fabric.js性能

来自分类Dev

Fabric.js计数对象

来自分类Dev

图像数据fabric.js

来自分类Dev

Fabric.js drawImage替代

来自分类Dev

图像数据fabric.js

来自分类Dev

Fabric.js drawImage替代

来自分类Dev

右键单击pan fabric js

来自分类Dev

Fabric.js-重建和商业使用

来自分类Dev

Fabric.js画布图像抗锯齿

来自分类Dev

如何激活Fabric js IText的光标?

来自分类Dev

Fabric.js动态添加/删除元素

来自分类Dev

在Fabric.js中全屏显示画布

来自分类Dev

Fabric JS:如何防止扩展活动组

来自分类Dev

fabric.js中的圆形控制旋钮

来自分类Dev

在fabric.js中编写乳胶配方

来自分类Dev

缩放fabric.js画布对象