I try to draw various products from an array on the canvas. Now I succeed to a certain extent. But if I want to output the variable "articleNumber", the product images "oImg" change, but not the text. This text will probably be overwritten with every new drawing and at the end all texts are identical with the last one in the array. It is probably related to the "var text". Can any of you help me to solve this problem easily? I'm relatively unfamiliar with the subject and therefore don't really know what to look for.
for (var i = 0; i < allproducts.length; i++) {
// Product image
var articleNumber = allproducts[i];
fabric.Image.fromURL('img/products/' + articleNumber + '.png', function(oImg) {
oImg.scaleToHeight(fullHeight/10)
.set('originX', 'center')
.set('left', Math.floor(Math.random() * fullWidth))
.set('top', Math.floor(Math.random() * (fullHeight - 200)))
// Article Number
var text = new fabric.Text(' '+ allproducts[i] + ' ', {
left: oImg.left,
top: oImg.top + fullHeight/10 + 2,
originX: 'center',
fontSize: 9,
fontFamily: 'Helvetica',
textAlign: 'center',
backgroundColor: 'white'
});
// Grouped Article number and Product Image
var product = new fabric.Group([oImg, text], {
hasControls: false,
});
// Add Product to Canvas
canvas.add(product);
});
}
Regards, Reden G.