Search code examples
kineticjs

KineticJS: set a maximum number for cloned objects


How do I set a maximum number to limit the cloned objects? The project requires putting this kind of conditions to make sure to have a reasonable number of items (equipments, doors, sensors in the house, etc).

Any suggestions, tips, ideas?


Solution

  • A Demo: http://jsfiddle.net/m1erickson/8A9sP/

    You can attach a maxClones property to your original object.

    var circle1 = new Kinetic.Circle({
        x:50,
        y:75,
        radius: 30,
        fill: 'red',
        stroke: 'black',
        strokeWidth: 4,
        draggable: true
    });
    
    // add a property defining the max # clones available from this original object
    
    circle1.maxClones=3;
    

    Then when cloning you can use this property to control the max number of clones made:

    if(circle1.maxClones>0){
        layer.add(circle1.clone({x:circle1.maxClones*50+100}));
        layer.draw();
        circle1.maxClones--;
    }else{
        alert("Cloning Unavailable: max clone count has been reached.");
    }