xxxxxxxxxx
140
// define global variables
let l1, l2, l3, l4; // leaves
let box, stem, ribbon, quality, bestgift, scribble; // other images
let msX = 0;
let msY = 0;
let stem_pos;
let rot_speed;
let pivots = [];
let omega;
let lambda;
let t;
let max_rot_speed;
function setup() {
createCanvas(windowWidth, 800);
l1 = loadImage('newleaf01.png');
l2 = loadImage('newleaf02.png');
l3 = loadImage('newleaf03.png');
l4 = loadImage('newleaf04.png');
stem = loadImage('assets/stem.png');
ribbon = loadImage('assets/ribbon.png');
quality = loadImage('assets/qualitytime.png');
bestgift = loadImage('assets/isthebestgift.png');
scribble = loadImage('assets/pinkscribble.png');
box = loadImage('assets/box.png');
crsr = loadImage('assets/cursore.png');
imageMode(CENTER);
angleMode(DEGREES);
// damping
omega = 2*PI*10;
lambda = 0.05;
t = 0; // explicit time variable
msX = width/2;
msY = height/2;
}
function clip(pos, limit_plus, limit_minus)
{
if (pos > limit_plus)
{
pos = limit_plus;
}
else if (pos < limit_minus )
{
pos = limit_minus;
}
return pos
}
function draw_leaf(leaf, x,y, angle, leafscale, lr)
{
imageMode(CORNER);
offset_x = 0
if (lr=='left')
{
offset_x = leaf.width
offset_y = leaf.height
}
else if (lr=='right')
{
offset_y = leaf.height
}
translate(x + offset_x, y+offset_y);
strokeWeight(10); // Make the points 10 pixels in size
//point(0,0) //
strokeWeight(1); // Make the points 1 pixels in size
rotate(angle);
scale(leafscale);
//rect(-offset_x,-offset_y,leaf.width,leaf.height);
image(leaf, -offset_x, -offset_y);
}
function draw() {
background(255, 255, 255);
if (mouseIsPressed) {
rot_speed = abs(mouseY-msY);
if(rot_speed > 20)
rot_speed = 20;
msX = mouseX;
msY = mouseY;
t = 0;
}
// clip stem pos as anchor
stem_pos = clip(msY+stem.height/2, 920, 450)
image(stem, width/2, stem_pos);
image(ribbon, width/2+13, stem_pos-stem.height/2+40);
tip_pos = stem_pos-stem.height/2;
// leaf oscillation (different phases for each leaf)
osc_1 = 0.05*rot_speed*exp(-lambda*t)*cos(omega*t+PI/8)
osc_2 = 0.05*rot_speed*exp(-lambda*t)*cos(omega*t+PI/9)
osc_3 = 0.05*rot_speed*exp(-lambda*t)*cos(omega*t+PI/6)
osc_4 = 0.05*rot_speed*exp(-lambda*t)*cos(omega*t+PI/4)
// draw leaves
push();
draw_leaf(l1, width/2+5-l1.width, tip_pos+100, -5*osc_1*(height-msY)/height, 0.7*(height-msY)/height, 'left')
pop();
push();
draw_leaf(l2, width/2+5-l2.width, tip_pos+300, -10*osc_2*(height-msY)/height, 1.3*(height-msY)/height, 'left')
pop();
push();
draw_leaf(l3, width/2+5, tip_pos+400, 15*osc_3*(height-msY)/height, 1.5*(height-msY)/height, 'right')
pop();
push();
draw_leaf(l4, width/2+5, tip_pos+200, 20*osc_4*(height-msY)/height, 0.9*(height-msY)/height, 'right')
pop();
image(box, width/2, height-box.height/3.1);
image(crsr, mouseX, mouseY);
push();
if(msY < 300)
{
translate(200,200)
scale(clip((height-msY)/height,1,0));
image(quality, 0, 0);
scale(clip((height-msY)/height,1,0));
image(bestgift, 0, 50);
image(scribble, random()*2, random()+80);
}
pop();
t+=0.1
t = t%100
//rotate(msX/100, [msX, msY]);
// Displays the image at point (0, height/2) at half size
//image(l1, 0, height / 2, l1.width / 2, l1.height / 2);
}