I am having some serious performance issues with kineticjs as the number of shapes on the canvas increases. I have two functions, one that initializes a path, a text and renders them on a stage. The second function gets called every 5 seconds and it updates any changes on the path and text drawn earlier. Here's a sample snippet of what i am trying,
/**
* Function that inits the shapes
*/
init = function(params) {
//Create the path object
var path = new Kinetic.Path({
data : params.geom,
fill : params.fill,
stroke : params.stroke.fill,
strokeEnabled : false,
scale : 1
});
path.setId(params.id + '.path');
var simpleText = new Kinetic.Text({
x : params.x,
y : params.y + (params.height /2),
text : params.displayText,
fontSize : 12,
fontFamily : params.family,
fontStyle : params.style,
fill : params.fill
});
simpleText.setId(params.id + '.text');
var layer = new Kinetic.Layer();
layer.setId(params.id);
layer.add(path);
layer.add(simpleText);
stage.add(layer);
}
/**
* Update every 5 seconds based on new data
*/
update = function(params) {
var layer = null;
if (Kinetic.Global.ids[params.id] != undefined) {
layer = Kinetic.Global.ids[params.id];
//Set text
var textItem = Kinetic.Global.ids[params.id + '.text'];
textItem.setText(params.displayText);
//center the text
textItem.setAttr('x', params.x + (params.width / 2) - (textItem.textWidth / 2));
textItem.setAttr('y', params.y + (params.height / 2) - textItem.getAttr('fontSize'));
// set center style
textItem.setAlign('center');
//Set path color
var pathItem = Kinetic.Global.ids[params.id + '.path'];
pathItem.setFill(params.fill);
pathItem.setOpacity(params.alpha);
layer.clear();
layer.draw();
}
}
This code will get sluggish and consume high memory as the number of shapes to draw increases. There is probably many areas in the code above to improve and I am looking for those pointers. Specifically I am not convinced if Id look-up and update is the best way to update an existing shape.