Fabricjs计算对象 [英] Fabricjs count objects

查看:121
本文介绍了Fabricjs计算对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法计算使用Fabric.js已在画布中有多少个对象

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);
    });
};

然后你有jQuery:

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");
    }
});

有没有办法计算它?

推荐答案

尝试

var count = 0;//initial count
$('.click').on("click", function (e) {
    e.preventDefault;
    var imgId = $(this).attr('id');
    if (count > 5) { //check count of images added
        alert("You can add only 5 images");
    } else {
        addImage(imgId + ".png");
        count++;//increase count 
    }
});

这篇关于Fabricjs计算对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆