smart resize images in a HTML5 Canvas