Drawing smoothness

Hello Everybody …

I’m working on a drawing application … while handling the touch issue to dynamically load sprites … while moving on the screen it lags and create white spaces between each sprite and the other …

here is the code I’m using …

    onLoad: function () {
    this.node.on(cc.Node.EventType.TOUCH_START, this.drawCircle, this , true);
    this.node.on(cc.Node.EventType.TOUCH_MOVE, this.drawCircle, this, false);
    this.node.on(cc.Node.EventType.TOUCH_END, this.drawCircle, this , true);}

   drawCircle: function (event) {

    var touchPos = event.touch.getLocation();
    var localPos = this.node.convertToNodeSpaceAR(touchPos);
    
    var node = new cc.Node("New Sprite");
    node.setPosition(localPos.x, localPos.y);
    var sprite = node.addComponent(cc.Sprite);
    node.parent = this.node;
    sprite.spriteFrame = this.textureProp1; 
    node.setScale(0.1,0.1);

}

how can i reduce the lag and reduce the white spaces … ??

Thanks
Regards
Abulbisht

Hi @Abulbisht, you can try implementing cc.NodePool instead of always creating a new node instance. By the way, when will these node be destroyed?

Creating new sprite nodes is a very crappy way for a drawing app, it would lag as hell on any system eventually. You need to use some canvas-like technique, having a single raster buffer for you image being drawn. Check the piece of code I’ve posted here:


It’s a very crude implementation that only works in web build, but I hope you’ll get the idea.

P.S. Also a on move event you should draw a line of strokes along a brezenheim path corresponding to a move event instead of a single sprite at the end point

Did you see the MakeSchool tutorial about creating a drawing app?
This could be exactly what you need:

Hi @jake72 … from the description of the nodePool in the Api … I’ve understood that you mean adding new node pool with the properties the sprite and default value … then instantiating an object by the methods of the nodePool ??

What I meant was implement a node recycle system which is a node pool in cocos case. I’m assuming all your node with a sprite component is going to be removed from the scene at a point. So instead of destroying them and recreate when needed, you recycle them. This is going to help reduce the CPU/GPU load in exchange caching data in the memory.

By the way, as @persy mentioned above. Creating lots of sprite for a drawing app is not really an elegant solution. Avoid doing this at all cost. You might want to look in the cc.Graphics api to suit your use case.