是否可以使用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:
$('.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");
}
});有办法数数吗?
发布于 2014-02-21 15:28:13
试一试
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
}
});发布于 2014-03-07 14:28:29
下面是固定版本,如何在画布中计算对象
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++;
}
});https://stackoverflow.com/questions/21938358
复制相似问题