I want to perform some actions when I click on image in Openframeworks(C++). How to do this?
I am using ofImage. Help me to resolve this.
When you render the image in draw()
you might use a position (x,y
) and size (width,height
) to display on screen.
You can use the same position and dimensions values in the mouseReleased() event to check if the mouse coordinates (x,y
) are within the rendered image's bounding box.
Here's a a bit of code to illustrate this, assuming you are already have the rendered image's x,y,width,height variables declared and updated:
void ofApp::mouseReleased(int x, int y, int button){
if((x >= imageX && x <= imageX + imageWidth) &&
(y >= imageY && y <= imageY + imageHeight)){
std::cout << "image clicked" << std::endl;
}
}