Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance issues using images with arbor.js

I've been working on adapting arbor.js to use images.

However, being a relative JS noob what I have is totally un-optimised.

As far as I can tell, the way I've set it up is recreating the image object for every image and every frame, resulting in tons of flicker.

Can anyone suggest a way to move the new Image() stuff out of the redraw function into the initiation? As far as I know this is a basic OOP issue, but totally stuck.

Thanks!

Pastebin of where I'm up to on the output script

Current status.

like image 232
kimadactyl Avatar asked Mar 22 '12 16:03

kimadactyl


1 Answers

Apologies all! There's a few steps. I'll highlight the key stages, the rest is from the tutorial.

First, add the relevant information to your JSON, for example:

nodes:{
    innovation:{    'color':colour.darkblue, 
                    'shape':'dot', 
                    'radius':30, 
                    'image': 'innovation.png',
                    'image_w':130,
                    'image_h':24,
                    'alpha':1 },
    participation:{ 'color':colour.purple, 
                    'shape':'dot',
                    'radius':40, 
                    'image':'participation.png',
                    'image_w':130,
                    'image_h':24,
                    'alpha':1 },
   ...

Cache all your images when the thing loads.

init:function(system){

    // Normal initialisation
    particleSystem = system
    particleSystem.screenSize(canvas.width, canvas.height)
    particleSystem.screenPadding(25, 50)
    that.initMouseHandling()

    // Preload all images into the node object
    particleSystem.eachNode(function(node, pt) {
        if(node.data.image) {
            node.data.imageob = new Image()
            node.data.imageob.src = imagepath + node.data.image
        }
    })
 ...

Then, for moving the images themselves...

particleSystem.eachNode(function(node, pt){
    ...       
    // Image info from JSON
    var imageob = node.data.imageob
    var imageH = node.data.image_h
    var imageW = node.data.image_w
    ...
    // Draw the object        
    if (node.data.shape=='dot'){
        // Check if it's a dot
        gfx.oval(pt.x-w/2, pt.y-w/2, w,w, {fill:ctx.fillStyle, alpha:node.data.alpha})
        nodeBoxes[node.name] = [pt.x-w/2, pt.y-w/2, w,w]
        // Does it have an image?      
        if (imageob){
            // Images are drawn from cache
            ctx.drawImage(imageob, pt.x-(imageW/2), pt.y+radius/2, imageW, imageH)
        }
    }else {
        // If none of the above, draw a rectangle
        gfx.rect(pt.x-w/2, pt.y-10, w,20, 4, {fill:ctx.fillStyle, alpha:node.data.alpha})
        nodeBoxes[node.name] = [pt.x-w/2, pt.y-11, w, 22]
    }
    ...
like image 68
kimadactyl Avatar answered Oct 22 '22 04:10

kimadactyl