ofImage get position

Hello everyone,

I’m dragging an ofImage, when I realized the mouse I need to know the new position of the image, but I can’t have access to this data. When I drag a rectangle I have this information, so, who can I get access to the new position of a ofImage?


I think you should keep the position of an image in a separate variable, like an ofVec2, two int of float.

I presume you are trying to drag an Image. I’ll try to rapidly explain it if that’s the case (otherwise just ignore).

Declare two variables, one por storing the position where to draw the image, and one to calculate the distante of the mouse when pressed and the same image.
In the header (ofApp.h)

ofImage  image;
ofVec2f	pos, ofset;

And then

void ofApp::setup(){
//... Load image and other stuff
pos = ofVec2f(0, 0);

void ofApp::mouseDragged(int x, int y, int button){
	pos = ofVec2f(x, y) - ofset;

void ofApp::mousePressed(int x, int y, int button){
	ofset = ofVec2f(x, y) - pos;

void ofApp::draw(){

The position of the image is the same thing as the position of the mouse when you release it. To get the position of the mouse simply ofGetMouseX() and ofGetMouseY().

More easily, you could add it to the “mouse release event” in your ofApp.cpp

void ofApp::mouseReleased(int x, int y, int button){
    cout << ofToString(x) << endl;
    cout << ofToString(y) << endl;