Created
January 30, 2019 22:47
-
-
Save kylemcdonald/b523f47b07208d57b4d24d6d0db18470 to your computer and use it in GitHub Desktop.
2D Zoomable Region for openFrameworks. Scroll to zoom. Left click and drag to translate.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Camera2d : public ofCamera { | |
private: | |
ofVec2f mouseStart; | |
ofVec2f startPosition; | |
float zoom; | |
float maxZoom = .01; | |
float minZoom = 1; | |
float zoomSpeed = 1. / 500; | |
public: | |
void setup() { | |
setupPerspective(true); | |
ofVec3f position = getPosition(); | |
ofCamera::setPosition(0, 0, position.z); | |
zoom = 1; | |
ofAddListener(ofEvents().mouseDragged, this, &Camera2d::mouseDragged); | |
ofAddListener(ofEvents().mousePressed, this, &Camera2d::mousePressed); | |
ofAddListener(ofEvents().mouseScrolled, this, &Camera2d::mouseScrolled); | |
} | |
void mouseDragged(ofMouseEventArgs& e) { | |
ofVec3f position = getPosition(); | |
position.x = startPosition.x - (e.x - mouseStart.x) * zoom; | |
position.y = startPosition.y - (e.y - mouseStart.y) * zoom; | |
ofCamera::setPosition(position); | |
} | |
void mousePressed(ofMouseEventArgs& e) { | |
mouseStart = e; | |
startPosition = getPosition(); | |
} | |
void mouseScrolled(ofMouseEventArgs& e) { | |
zoom += -e.scrollY * zoomSpeed; | |
zoom = ofClamp(zoom, maxZoom, minZoom); | |
setFov(60 * zoom); | |
} | |
void setPosition(float x, float y) { | |
ofVec3f position = getPosition(); | |
ofCamera::setPosition(x, y, position.z); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment