Processing 在处理过程中相对于鼠标缩放和定位对象

Processing 在处理过程中相对于鼠标缩放和定位对象,processing,Processing,在此草图中,从固定点从左到右计算对象的位置。放大或缩小时,对象也将从左向右放大或缩小 如何计算需要添加到x的偏移量,以便在放大或缩小对象时,根据鼠标的x位置展开或缩小对象(以便mouseX将成为缩放的原点) 草图的代码(加载需要一段时间,因为图像是从internet获取的) PImage背景图像; 字体; int x=0; int xoffset=0; int deltax=5; 浮动比例=512/30; 浮点数deltascale=0.02; ArrayList对象列表; //字符串[]对象文

在此草图中,从固定点从左到右计算对象的位置。放大或缩小时,对象也将从左向右放大或缩小

如何计算需要添加到x的偏移量,以便在放大或缩小对象时,根据鼠标的x位置展开或缩小对象(以便mouseX将成为缩放的原点)

草图的代码(加载需要一段时间,因为图像是从internet获取的)

PImage背景图像;
字体;
int x=0;
int xoffset=0;
int deltax=5;
浮动比例=512/30;
浮点数deltascale=0.02;
ArrayList对象列表;
//字符串[]对象文件;
//字符串[]配置文件;
//int savetimer=0;
类对象{
字符串名;
浮子直径;
弦单元;
弦精灵;
皮马杰;
int wid;
英平;
int XPO;
对象(字符串Oname、浮点Odiameter、字符串Ounit、字符串Oimg){
name=Oname;
直径=直径计;
单位=盎司;
img=载荷图像(Oimg);
雪碧=Oimg;
//img=载荷图像(Oimg);
wid=img.宽度;
hei=img.高度;
}
}
无效设置(){
尺寸(1500800);
帧率(200);
//objectfile=loadStrings(“objects.txt”);
//configfile=loadStrings(“config.ini”);
//backgroundImage=loadImage(“background.png”);
//font=createFont(“Franklin Gottic Book Regular.ttf”,32);
填充(255255);
文本大小(32);
textAlign(居中,居中);
文本(“正在加载图像(这可能需要一段时间)”,700400;
ObjectList=新的ArrayList();
readObjects();
//readConfig();
//loadOffset();
}
void addObject(字符串Aname、float Adiameter、字符串Aunit、字符串Aimg){
//如果(目标等于(“空”)){
//添加(新对象(Aname,Adiameter,Aunit,“images/”+Aname+“.png”);
//}否则{
添加(新对象(Aname、Adiameter、Aunit、Aimg));
//}
}
void readObjects(){
//for(int i=0;i=0&&x 0&&drawHeight>0){
图像(drawObject.img,x,400-(drawHeight/2),drawWidth,drawHeight);
//计算相对于对象当前大小的字体大小
int文本大小;
//检查名称或直径是否较长,然后使用较长的名称或直径进行计算
填充(255255);
if(drawObject.name.length()0){
textSize(textSize);
浮动文本Y=418+(图纸高度/2);
如果(文本<(418+(图纸高度/2))){
texty=418;
}
text(drawObject.name,x+(drawWidth/2),texty);
text(drawObject.diameter+drawObject.unit,x+(drawWidth/2),texty+textSize);
}
}
}
drawObject.xpos=x;
x+=(图纸宽度*0.08+图纸宽度);
//显示deltax
文本大小(32);
文本(str(deltax),1450,10);
文本(str(deltascale),1450,50);
//显示保存的
//如果(保存计时器>0){
//文本(“已保存”,1450,90);
////文本(str(savetimer),1450130);
//}
//如果(保存计时器>0){
////savetimer-=1;
//}
println(str(量表));
}
}
作废鼠标滚轮(鼠标事件){
float e=event.getCount();
if(e<0){
比例*=(1+三角洲比例);
}如果(e>0),则为else{
比例*=(1-三角洲比例);
}
}
按下void键(){
如果(键==编码){
if(keyCode==左){
xoffset+=代尔税;
}else if(keyCode==右){
xoffset-=deltax;
}else if(keyCode==UP){
deltax+=5;
}else if(keyCode==向下){
deltax-=5;
}
}
如果(键=='r'){
//重载对象();
}else if(键=='q'){
deltascale+=0.02;
}否则如果(键=='a'){
deltascale-=0.02;
}
如果(deltax<0){
deltax=0;
}否则
//a position to scale from
PVector mouse = new PVector();
//a scale value
float scale = 1.0;
//drawing dimensions - this depends on your actual content
float w = 400;
float h = 400;

void setup(){
  size(400,400);
  rectMode(CENTER);
}
void draw(){
  background(255);
  //move everything to centre
  translate(width * .5, height * .5);
  //isolate coordinate space
  pushMatrix();
  //move in the opposite direction by half of the drawing size => transform from drawing centre
  translate(-w * .5,-h * .5);
  //move to where the transformation centre should be (this can be mixed with the above, but I left it on two lines so it's easier to understand)
  translate(mouse.x,mouse.y);
  //transform from set position
  scale(scale);
  //move the transformation back
  translate(-mouse.x,-mouse.y);
  //draw what you need to draw
  drawStuff();
  //return to global coordinate space
  popMatrix();

  //draw a preview of the transformation centre
  if(mousePressed){
    fill(192,0,0);
    ellipse(mouse.x-width*.5,mouse.y-height*.5,35,35);
  }
}
//just a bunch of boxes as a placeholder
void drawStuff(){
  for(int i = 0 ; i < 400; i++){
    int x = i % 20;
    int y = i / 20;
    pushMatrix();
    translate(x * 20, y * 20);
    rotate(radians(i));
    scale(sin(radians(i)) + 1.1);
    fill(i % 400);
    rect(0,0,15,15);
    popMatrix();
  }
}
//set the transformation centre
void mousePressed(){
  mouse.set(mouseX,mouseY);
}
//change scale by moving mouse on X axis
void mouseDragged(){
  scale = map(mouseX,0,width,0.25,2.0);
}