javascript - Interactive drawing with Fabric.js - Stack Overflow

I am trying to do an interactive drawing using Fabric.js. Now I can draw a rectangle by using mouse. Bu

I am trying to do an interactive drawing using Fabric.js. Now I can draw a rectangle by using mouse. But after I finish drawing the rectangle, I can not select it unless I resize it once by using the left top controller after drawing. I wonder what break the event system.

Here is my code: /

var canvas = new fabric.Canvas('canvas');

    var rect = new fabric.Rect({
        top : 100,
        left : 100,
        width : 60,
        height : 70,
        fill : 'red'
    });

    canvas.add(rect);

    canvas.on('mouse:down', function (option) {
        console.log(option);
        if (typeof option.target != "undefined") {
            return;
        } else {
            var startY = option.e.offsetY,
                startX = option.e.offsetX;

            console.log(startX, startY);

            var rect2 = new fabric.Rect({
                top : startY,
                left : startX,
                width : 0,
                height : 0,
                fill : 'transparent',
                stroke: 'red',
                strokewidth: 4
            });

            canvas.add(rect2);

            console.log("added");
            canvas.on('mouse:move', function (option) {
                var e = option.e;
                rect2.set('width', e.offsetX - startX);
                rect2.set('height', e.offsetY - startY);
                rect2.saveState();
            });


        }
    });

    canvas.on('mouse:up', function () {
        canvas.off('mouse:move');
    });

I am trying to do an interactive drawing using Fabric.js. Now I can draw a rectangle by using mouse. But after I finish drawing the rectangle, I can not select it unless I resize it once by using the left top controller after drawing. I wonder what break the event system.

Here is my code: http://jsfiddle/rmFgX/1/

var canvas = new fabric.Canvas('canvas');

    var rect = new fabric.Rect({
        top : 100,
        left : 100,
        width : 60,
        height : 70,
        fill : 'red'
    });

    canvas.add(rect);

    canvas.on('mouse:down', function (option) {
        console.log(option);
        if (typeof option.target != "undefined") {
            return;
        } else {
            var startY = option.e.offsetY,
                startX = option.e.offsetX;

            console.log(startX, startY);

            var rect2 = new fabric.Rect({
                top : startY,
                left : startX,
                width : 0,
                height : 0,
                fill : 'transparent',
                stroke: 'red',
                strokewidth: 4
            });

            canvas.add(rect2);

            console.log("added");
            canvas.on('mouse:move', function (option) {
                var e = option.e;
                rect2.set('width', e.offsetX - startX);
                rect2.set('height', e.offsetY - startY);
                rect2.saveState();
            });


        }
    });

    canvas.on('mouse:up', function () {
        canvas.off('mouse:move');
    });
Share Improve this question edited Mar 8, 2014 at 12:59 kangax 39.2k13 gold badges100 silver badges135 bronze badges asked Mar 4, 2014 at 8:32 TomTom 7949 silver badges19 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

updated the fiddle http://jsfiddle/rmFgX/5/

For an object when changing position/dimension -related properties (left, top, scale, angle, etc.) set does not update position of object's borders/controls.

If you need to update those, call:

setCoords().

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744679224a4587514.html

相关推荐

  • javascript - Interactive drawing with Fabric.js - Stack Overflow

    I am trying to do an interactive drawing using Fabric.js. Now I can draw a rectangle by using mouse. Bu

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信