// Define this once and before it will be called, hence at the top of this file function DrawRefimg(fig, img, canvas, orig_face ) { context=canvas.getContext('2d') // another call to this func will occur on load, so skip this one if( img.width == 0 ) return // only set canvas.width once we have valid img dimensions canvas.width=img.width/(img.height/canvas.height) // actually draw the pixel images to the canvas at the right size context.drawImage(img, 0, 0, img.width/(img.height/canvas.height), canvas.height); fig.width(canvas.width) }