Canvas Fit Height and Fit Width in Code

I want to achieve the following property of Canvas Fit Width and Fit Height in Code.
I have a set of conditions and based on that I have to set it in cocos creator. Any help in right direction would be appreciated.

I don’t know what you really want, but there are properties:

Canvas.fitHeight
Canvas.fitWidth
doc: http://cocos2d-x.org/docs/api-ref/creator/v1.5/classes/Canvas.html

I hope it can be of any help to you.