Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fabricjs count objects

Is there any way to count how many objects are already in canvas using Fabric.js

    function addImage(imageName) {

    fabric.Image.fromURL('./image_path/' + imageName, function (image) {

        image.set({
            left: 10,
            top: 10,
            width: 100,
            height: 100,
            centeredScaling: true,
            lockUniScaling: true
        })


        canvas.add(image);
    });
};

and then you have jQuery:

    $('.click').on("click", function (e) {
    e.preventDefault;
    var imgId = $(this).attr('id');

    var number = $('canvas img').length;
    if (number == 5) {
        alert("You can add only 5 images");
    } else {
        addImage(imgId + ".png");
    }
});

Is there any way to count it?

like image 695
Digital Legend Avatar asked Nov 18 '25 08:11

Digital Legend


1 Answers

Here is fixed version how to count objects in fabricjs inside canvas

var count = canvas.getObjects().length - 1;
$('.a').on("click", function (e) {
    e.preventDefault;
    var imgId = $(this).attr('id');


    if (count > 40) {
        alert("You can add only 40 images");

    } else {
        addImage(imgId + ".png");
        count++;
    }

});
like image 189
Digital Legend Avatar answered Nov 20 '25 20:11

Digital Legend



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!