Search code examples
javascriptarrayskineticjs

Adding an array of shapes into a layer in kineticjs


I'm trying to attach an array of shapes to layer in kineticJS, so far this is what I've done:

elementContainer = {
    start: buildCircle(75, 75),
    activity: buildActivity(150, 50),
    end: buildCircle(250+25, 50+25)
 };

when I try to add the array like this

linesLayer.add(elementContainer);

I get an exception with the following message:

Uncaught TypeError: Object # has no method 'getType' kinetic.min.js:4 Kinetic.Util.addMethods._validateAdd kinetic.min.js:4 Kinetic.Util.addMethods.add kinetic.min.js:3 (anonymous function) (index):163 jQuery.event.dispatch jquery-2.1.0.js:4371 elemData.handle

this is the JSFiddle


Solution

  • As lavrton said, each element must be added individually, but you may want to use a loop in case you ever need to change the number or names of elements in elementsContainer instead of hard coding them.

    for (var key in elementContainer) {
        layer.add(elementContainer[key]);
    }
    

    http://jsfiddle.net/ze6nj/