Improved canvas resize function.