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);
};
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] 删除。
我来说两句