我听起来你想从中心缩放。
默认情况下,坐标系的原点位于左上角 0,0。
如果您想从图像的中心缩放,您首先需要在那里平移然后缩放。从中心缩放后,您需要平移回左上角进行渲染。
这是一个基本示例:
PGraphics frame;
float scale = 1.0;
void setup(){
size(640,480);
//fake a camera/video frame to test with
frame = createGraphics(width,height);
frame.beginDraw();
frame.textSize(700);
frame.text("X",0,480);
frame.text("Y",250,480);
frame.endDraw();
}
void draw(){
background(0);
//move "transformation point" from top left corner to centre
//by translating by half the frame size
translate(frame.width/2,frame.height/2);
//scale from centre
scale(scale);
//move coordinate system back to top left and draw the im
image(frame,-frame.width/2,-frame.height/2);
}
void keyPressed(){
if(keyCode == UP) scale += 0.1;
if(keyCode == DOWN) scale -= 0.1;
}
frame 变量是一个占位符,它将成为您的相机/电影帧,所以不要太担心setup() 中的内容。重要部分在draw() 中注释。使用向上/向下箭头键更改比例。
请注意,这将影响在此图像之后绘制的其他对象。如果您想仅针对图像将缩放与中心隔离,您可以将转换放在pushMatrix(),popMatrix() 调用中。这些将隔离一个独立于处理的全局坐标系的坐标系:
PGraphics frame;
float scale = 1.0;
void setup(){
size(640,480);
//fake a camera/video frame to test with
frame = createGraphics(width,height);
frame.beginDraw();
frame.textSize(700);
frame.text("X",0,480);
frame.text("Y",250,480);
frame.endDraw();
}
void draw(){
background(0);
//isolate coordinate system
pushMatrix();
//move "transformation point" from top left corner to centre
//by translating by half the frame size
translate(frame.width/2,frame.height/2);
//scale from centre
scale(scale);
//move coordinate system back to top left and draw the im
image(frame,-frame.width/2,-frame.height/2);
popMatrix();
}
void keyPressed(){
if(keyCode == UP) scale += 0.1;
if(keyCode == DOWN) scale -= 0.1;
}
欲了解更多详情,请务必查看2D Transformations tutorial
为了更好地了解这是如何工作的,这里有一个版本来说明每个坐标变换:
PGraphics frame;
float scale = 1.0;
void setup(){
size(640,480);
strokeWeight(3);
//fake a camera/video frame to test with
frame = createGraphics(width,height);
frame.beginDraw();
frame.textSize(700);
frame.text("X",0,480);
frame.text("Y",250,480);
frame.endDraw();
}
void draw(){
background(0);
drawCoordinateSystem("Processing/global CS",100);
//isolate coordinate system
pushMatrix();
//move "transformation point" from top left corner to centre
//by translating by half the frame size
translate(frame.width * .5,frame.height * .5);
drawCoordinateSystem("translated to image centre",100);
//scale from centre
scale(scale);
drawCoordinateSystem("scaled from image centre",100);
//move coordinate system back to top left and draw the im
pushMatrix();
translate(-frame.width * .5,-frame.height * .5);
drawCoordinateSystem("scaled back to image top/left",100);
image(frame,0,0);
popMatrix();
popMatrix();
}
void drawCoordinateSystem(String label,float size){
text(label,15,15);
stroke(192,0,0);
line(0,0,size,0);
stroke(0,192,0);
line(0,0,0,size);
}
void keyPressed(){
if(keyCode == UP) scale += 0.1;
if(keyCode == DOWN) scale -= 0.1;
}