Adjusting X and Y coordinates for drawing program
in
Programming Questions
•
3 months ago
I have been working on a simple drawing program that lets you dynamically draw image masks. However, I want to scale the images and center them in the screen, no matter what the screen size is. This throws off my mouseX and mouseY drawing functionality.
- PShader maskShader;
- PImage srcImage;
- PGraphics maskImage;
- PGraphics displayImg;
- PImage bgImage;
- void setup() {
- size(640, 760, P2D);
- srcImage = loadImage("leaves.jpg");
- bgImage = loadImage("moonwalk.jpg");
- maskImage = createGraphics(srcImage.width,srcImage.height, P2D);
- maskImage.noSmooth();
- maskShader = loadShader("mask.glsl");
- maskShader.set("mask", maskImage);
- background(255);
- }
- void draw() {
- imageMode(CENTER);
- image(bgImage,width/2,height/2);
- maskImage.beginDraw();
- maskImage.background(0);
- if (mousePressed == true) {
- maskImage.noStroke();
- maskImage.fill(255, 0, 0);
- maskImage.ellipse(mouseX, mouseY, 50, 50);
- }
- maskImage.endDraw();
- shader(maskShader);
- image(srcImage, width/2, height/2);
- //scaleImage(srcImage);
- }
- void scaleImage(PImage imageToScale){
- imageMode(CENTER);
- int originalWidth=imageToScale.width;
- int originalHeight=imageToScale.height;
- int scaledWidth=originalWidth*(height/originalHeight);
- int scaledHeight=originalHeight*(width/originalWidth);
- if (imageToScale.width > imageToScale.height) {
- //if the image is wider than it is tall: make it as wide as the screen, and increase the height by the same factor
- imageToScale.width=width;//the width of the stage
- imageToScale.height=scaledHeight;
- }
- else {
- //same approach for tall images
- imageToScale.height=height;//the height of the stage
- imageToScale.width=scaledWidth;
- }
- }
I was thinking of different ways to get around this. I tried implementing a PGraphics that would do all the drawing, then move and manipulate the PGraphics positioning. However, it seems like I can't do that with the masking shader. beginDraw and endDraw have to go one after another, and can't be "nested" within each other (if I'm interpreting the errors I received correctly).
I'm just not really sure of what the best strategy is for accomplishing this. Automatically scale to screen, be able to position the "drawing stage" wherever I want, and have the X and Y coordinates translate from mouse position to position on the image. Not sure how to properly "encapsulate" everything in a way that I can understand.
Any help or advice would be greatly appreciated.
1