werty37
January 26, 2018, 3:31pm
#1
Hi
I am a newbie in Openframeworks. I was wondering if there is a way to automatically preserve aspect ratio of an Image?
void ofApp::draw(){
image.draw(ofGetWidth()/2 - image.getWidth()/2, ofGetHeight()/2 - image.getHeight()/2, 100, auto?);
}
I did search for topics on this is the forum but didn’t find anything. Sorry if this is such a noob question.
Thanks
hamoid
January 26, 2018, 5:33pm
#2
Maybe
void ofApp::draw(){
image.draw(ofGetWidth() / 2 - image.getWidth() / 2,
ofGetHeight()/ 2 - image.getHeight()/ 2,
100,
100 * image.getHeight() / image.getWidth());
}
?
Sometimes it’s convenient to use ofRectangle , because it has many methods to query corners, scale, fit, cover… Not needed if you are just drawing one image, but if you want to draw other items next to the image it can be useful.
//.h
ofRectangle imgArea;
//setup
ofRectangle fitArea;
fitArea.setFromCenter(ofGetWidth()/2, ofGetHeight()/2, 100, 100);
imgArea.scaleTo(fitArea, OF_SCALEMODE_FIT);
//draw
image.draw(imgArea);
2 Likes