最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - fabricjs: how to show object size after scaling - Stack Overflow

programmeradmin1浏览0评论

I am using wordpress plugin called "Woomerce Product Designer" which includes a JavaScript library called fabicjs in version 1.4.9.

I need a way to show the width and height of an object in fabric after (or while) I re-scale them.

I made some adjustments directly in file fabric-all.min.js:

_beforeTransform: function (e, t) {
    var myHeight = Math.floor(t.currentHeight);
    var myWidth = Math.floor(t.currentWidth);
    if(!jQuery("#size").length){
        jQuery('<p id="size"></p>').insertBefore(".upper-canvas");
    }
    else{
        jQuery("#size").html("<span>size: </span>"+myWidth +" x "+myHeight +" cm"+"<br>");
    }                                                  
    var n;
    this.stateful && t.saveState(), (n = t._findTargetCorner(this.getPointer(e))) && this.onBeforeScaleRotate(t), t !== this.getActiveGroup() && t !== this.getActiveObject() && (this.deactivateAll(),     this.setActiveObject(t, e))
},

It works but it shows values from previous transform right before current transform starts. In this way I need another action (for example simple click on object) to update to current values.

So I need either call some function which simulate click on object to update values or move my code to a different function which is called during (or after) transform and which has access to information about current object size.

I am using wordpress plugin called "Woomerce Product Designer" which includes a JavaScript library called fabicjs in version 1.4.9.

I need a way to show the width and height of an object in fabric after (or while) I re-scale them.

I made some adjustments directly in file fabric-all.min.js:

_beforeTransform: function (e, t) {
    var myHeight = Math.floor(t.currentHeight);
    var myWidth = Math.floor(t.currentWidth);
    if(!jQuery("#size").length){
        jQuery('<p id="size"></p>').insertBefore(".upper-canvas");
    }
    else{
        jQuery("#size").html("<span>size: </span>"+myWidth +" x "+myHeight +" cm"+"<br>");
    }                                                  
    var n;
    this.stateful && t.saveState(), (n = t._findTargetCorner(this.getPointer(e))) && this.onBeforeScaleRotate(t), t !== this.getActiveGroup() && t !== this.getActiveObject() && (this.deactivateAll(),     this.setActiveObject(t, e))
},

It works but it shows values from previous transform right before current transform starts. In this way I need another action (for example simple click on object) to update to current values.

So I need either call some function which simulate click on object to update values or move my code to a different function which is called during (or after) transform and which has access to information about current object size.

Share Improve this question edited Jun 8, 2015 at 21:31 T.C. 138k17 gold badges300 silver badges433 bronze badges asked Jun 8, 2015 at 21:21 Pavel NěmecPavel Němec 2721 gold badge6 silver badges18 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

I don't think it is a good idea to change the fabricJs source. It would be better to extend it or make some functions. I made you jsFiddle with how to get the width and height of the object while it is scaling.

canvas.on('object:scaling', function(){
    var obj = canvas.getActiveObject();
    $('#rect-width').val(Math.floor(obj.getWidth()));
    $('#rect-height').val(Math.floor(obj.getHeight()));
});
发布评论

评论列表(0)

  1. 暂无评论