简体   繁体   中英

How to detect mouse down event on fabric js animated objects

i was playing with Fabric.js and aim is to detect mouse down event on an animated object. Currently images are being animated from bottom to top, and when i press mouse down it just shows coordinate but does not detect clicking on Object.

I also noticed that, when images start their animation, only then i can click/select them, after that they become non-selectable

here is a jsfiddle demo of code. please check and suggest.

my html is :

 <h3>Hello Fabric</h3>
    <div class="mainCanvasContainer">
        <canvas class="mainCanvas" id="mycanvas" width="500px" height="400px"></canvas>
    </div>
    <div id='info'>

    </div>

now here is my Javascript:

 $(document).ready(function () {
            var canvas_id = 'mycanvas';

            function showInfo(text){
                $('#info').append('<div> '+text+' </div>');
            }


            (function () {
                var canvas = this.__canvas = new fabric.Canvas(canvas_id, {selection: false});
                fabric.Object.prototype.originX = fabric.Object.prototype.originY = 'center';

                canvas.on('mouse:down', function (options) {
                    if (options.target) {
                       showInfo('an object was clicked! ', options.target.type);
                    }
                    var cor=options.e.clientX+','+ options.e.clientY;
                     showInfo(cor);
                });


                setInterval(function () {
                    fabric.Image.fromURL('http://www.rashflash.com/fabricjs_animation/img.png', function (img) {
                        img.scale(0.2);
                        //img.set('left', fabric.util.getRandomInt(100, 400)).set('top', 450);
                        //img.movingLeft = !!Math.round(Math.random());

                        var text = new fabric.Text('hello', {
                            fontSize: 30,
                            originX: 'center',
                            originY: 'center'
                        });

                        var group = new fabric.Group([img, text]);
                        group.set('left', fabric.util.getRandomInt(100, 400)).set('top', 420);                           
                        canvas.add(group);
                    });

                }, 1000);

                (function animate() {
                    canvas.forEachObject(function (obj) {
                        if (!obj) {
                            return;
                        }
                        // obj.left += (obj.movingLeft ? -1 : 1);
                        obj.top -= 1;
                        if (obj.left > 500 || obj.top < 0) {
                            canvas.remove(obj);
                        }                           
                    });
                    canvas.renderAll();

                      fabric.util.requestAnimFrame(animate);
                })();                   
            })();


        });

RashFlash, you are missing setCoords() function when you animate.

Updated function animate:

(function animate() {
                        canvas.forEachObject(function (obj) {
                            if (!obj) {
                                return;
                            }
                            // obj.left += (obj.movingLeft ? -1 : 1);
                            obj.top -= 1;
                            if (obj.left > 500 || obj.top < 0) {
                                canvas.remove(obj);
                            }                 
                            obj.setCoords();
                        });
                        canvas.renderAll();

                          fabric.util.requestAnimFrame(animate);
                    })();                   
                })();

Also, updated fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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