We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOther Libraries › Simple form tweening
Page Index Toggle Pages: 1
Simple form tweening (Read 913 times)
Simple form tweening
Dec 2nd, 2008, 8:46pm
 
Hello!
Is there a way to do a simple form tween / morph / transformation? For example, transforming a rectangle to a circle? I took a look at the shapetween library, but it only handles color tweens, motion and easing.

Anyone knows help??

Cheers
Re: Simple form tweening
Reply #1 - Dec 2nd, 2008, 10:40pm
 
I don't know a generic morphing library for Processing, but you can do something manually.
For example, recycling a circle function I have shown earlier:
Code:
void DrawCircle(float x, float y, float w, float h, float ratio)
{
 float l = 1 - ratio;
 float hw = w / 2, hh = h / 2;
 float lx = l * hw, ly = l * hh;
 // Assume here default CENTER mode
 x -= hw; y -= hh;

 beginShape();
 vertex(x, y + hh); // Left
 bezierVertex(x, y + ly,
     x + lx, y,
     x + hw, y); // Top
 bezierVertex(x + w - lx, y,
     x + w, y + ly,
     x + w, y + hh); // Right
 bezierVertex(x + w, y + h - ly,
     x + w - lx, y + h,
     x + hw, y + h); // Bottom
 bezierVertex(x + lx, y + h,
     x, y + h - ly,
     x, y + hh); // Back to left
 endShape();
}

float ratio = 0.0;
int direction = 1;
float angle = 0.0;
int hw, hh;
boolean bWholeAnimation = false; // Change for goodies!

void setup()
{
 size(500, 500);
 hw = width / 2;
 hh = height / 2;
 smooth();
}

void draw()
{
 background(222);
 
 noStroke();
 if (bWholeAnimation)
 {
   pushMatrix();
   translate(hw, hh);
   rotate(angle);
   fill(0x33995500);
   rect(100 - hw, 100 - hh, width - 200, height - 200);
   pushMatrix();
   rotate(angle);
   fill(0x660000EE);
   DrawCircle(-hw/2, -hh/2, 200, 200, 3.0 - ratio);
   DrawCircle(hw/2, hh/2, 200, 200, 1.0 - ratio);
   popMatrix();
   rotate(HALF_PI + angle);
   DrawCircle(-hw/2, -hh/2, 200, 200, 2.0 - ratio);
   DrawCircle(hw/2, hh/2, 200, 200, -ratio);
   angle += PI/180;
   popMatrix();
 }
 
 fill(0x8800EE00);
 stroke(#2288FF);
 DrawCircle(hw, hh, 400, 400, ratio);
 ratio += 0.01 * direction;
 if (ratio > 2.0 || ratio < -2.0) direction *= -1;
}
Re: Simple form tweening
Reply #2 - Dec 3rd, 2008, 10:04am
 
Thanks for your reply!
This was what i was looking for!

Greets
Page Index Toggle Pages: 1