xxxxxxxxxx
348
var tableMonth;
var currentMonth = -1; //for change of month
var previousMonth = -1;
var change = 0;
var appleTrees=[];
let treeNum;
var colR, colG, colB;
var mouse = 0;
var earthAng;
var sound = [];
//background gradient
const Y_AXIS = 1;
const X_AXIS = 2;
let c1, c2;
function preload(){
tableMonth = loadTable("month.csv","csv","header");
sound[0] = loadSound('05/0.wav');
sound[1] = loadSound('05/1.wav');
sound[2] = loadSound('05/2.wav');
sound[3] = loadSound('05/3.wav');
sound[4] = loadSound('05/4.wav');
sound[5] = loadSound('05/5.wav');
sound[6] = loadSound('611/6.wav');
sound[7] = loadSound('611/7.wav');
sound[8] = loadSound('611/8.wav');
sound[9] = loadSound('611/9.wav');
sound[10] = loadSound('611/10.wav');
sound[11] = loadSound('611/11.wav');
}
function setup(){
createCanvas(windowWidth, windowHeight);
treeNum = 8;
for(var n=0; n<treeNum; n++){
appleTrees.push(new Tree());
}
colR = tableMonth.getColumn('r');
colG = tableMonth.getColumn('g');
colB = tableMonth.getColumn('b');
}
function draw(){
background(50);
c1 = color(204,102,0,100);
c2 = color(0,102,153,100);
setGradient(0,0,width,height,c2,c1,Y_AXIS);
push();
if(currentMonth>=0){
//drawGround();
for(var n=0; n<treeNum; n++){
appleTrees[n].ground();
}
for(var t=0; t<treeNum; t++){
appleTrees[t].show();
}
drawLable();
}
pop();
if(mouse==0){
Start();
}
if(mouse==1){
Indicator();
if(currentMonth>=0){
earthIndicator();
}
}
}
function mouseClicked(){
mouse = 1;
}
function Start(){
textAlign(CENTER);
fill(250);
textStyle(BOLD);
textSize(height*0.09);
text("Years and Seasons",width*0.5,height*0.32);
textStyle(NORMAL);
textSize(height*0.06);
text("The Revolution of the Earth",width*0.5,height*0.45);
textStyle(ITALIC);
textSize(height*0.04);
text("Tap the screen to start.",width*0.5,height*0.6);
}
function Indicator(){
//draw arrow
push();
fill(250);
noStroke();
rect(width*0.03,height*0.07,width*0.92,height*0.003);
translate(0,height*0.0015);
triangle(width*0.94,height*0.065,width*0.94,height*0.075,width*0.97,height*0.07);
pop();
//draw Indicator
push();
fill(0,230);
noStroke();
translate(width-height*0.25,height-height*0.25);
ellipse(0,0,height*0.3);
//sun
fill(255,50,0);
ellipse(0,0,height*0.05);
pop();
//draw trajactory
push();
noFill();
stroke(255,100);
translate(width-height*0.25,height-height*0.25);
ellipse(0,0,height*0.25);
pop();
}
function earthIndicator(){
push();
translate(width-height*0.25,height-height*0.25);
var dir = p5.Vector.fromAngle(earthAng,height*0.25/2);
translate(dir.x,dir.y);
fill(0,200,255);
noStroke();
ellipse(0,0,20);
pop();
}
function keyPressed(){
if (key == 'q'){currentMonth = 0; earthAng=-PI/6; sound[0].play();}
if (key == 'w'){currentMonth = 1; earthAng=-PI/3; sound[1].play();}
if (key == 'e'){currentMonth = 2; earthAng=-3*PI/6; sound[2].play();}
if (key == 'r'){currentMonth = 3; earthAng=-2*PI/3; sound[3].play();}
if (key == 't'){currentMonth = 4; earthAng=-5*PI/6; sound[4].play();}
if (key == 'y'){currentMonth = 5; earthAng=-PI; sound[5].play();}
if (key == 'h'){currentMonth = 6; earthAng=-7*PI/6; sound[6].play();}
if (key == 'g'){currentMonth = 7; earthAng=-4*PI/3; sound[7].play();}
if (key == 'f'){currentMonth = 8; earthAng=-3*PI/2; sound[8].play();}
if (key == 'd'){currentMonth = 9; earthAng=-5*PI/3; sound[9].play();}
if (key == 's'){currentMonth = 10; earthAng=-11*PI/6; sound[10].play();}
if (key == 'a'){currentMonth = 11; earthAng=0; sound[11].play();}
}
function Tree(){
var branchNum = tableMonth.getColumn('branchNum');
this.len = height*random(0.15,0.25);
this.thick = this.len*0.1;
this.pos = createVector(random(width),random(height*0.65,height*1))
this.colR = random(100,110);
this.colG = 80+random(-10,10);
this.colB = 50;
this.ang1 = random(PI/50,PI/3.5);
this.ang2 = random(-PI/3.5,-PI/50);
this.ang3 = random(PI/50,PI/3.5);
this.ang4 = random(-PI/3.5,-PI/50);
this.show = function(){
push();
translate(this.pos);
stroke(this.colR,this.colG,this.colB,230);
Branch1(this.len,branchNum[currentMonth],this.thick,this.ang1,this.ang2);
Branch2(this.len,branchNum[currentMonth],this.thick,this.ang3,this.ang4);
pop();
}
this.ground = function(){
noStroke();
fill(colR[currentMonth],colG[currentMonth],colB[currentMonth],80);
ellipse(this.pos.x,this.pos.y+height*0.03,height*0.55,height*0.25);
}
}
function Leave(){
var leaveColor = color(colR[currentMonth],colG[currentMonth],colB[currentMonth]);
var leaveWidth = tableMonth.getColumn('leaveWidth');
var leaveHeight = tableMonth.getColumn('leaveHeight');
var currentLeaveWidth = leaveWidth[currentMonth];
var currentLeaveHeight = leaveHeight[currentMonth];
push();
fill(leaveColor);
ellipse(0,0,height*currentLeaveWidth,height*currentLeaveHeight);
pop();
}
function Snow(){
var snowWidth = tableMonth.getColumn('snowWidth');
var snowHeight = tableMonth.getColumn('snowHeight');
var currentSnowWidth = snowWidth[currentMonth];
var currentSnowHeight = snowHeight[currentMonth];
push();
fill(250,150);
ellipse(0,0,height*currentSnowWidth,height*currentSnowHeight);
pop();
}
function Branch1(len,num,thick,ang1,ang2){
strokeWeight(thick);
line(0,0,0,-len);
push();
translate(0,-len);
if (num>0){
len = len*0.65;
thick = thick*0.65;
num--;
push();
rotate(ang1+noise(currentMonth*10)*0.2);
Branch1(len,num,thick,ang1,ang2);
//add leaves
if(num<2){
push();
translate(0,-len);
noStroke();
Leave();
pop();
}
//add snow
if(currentMonth == 11 || currentMonth == 0 || currentMonth == 1){
if(num<3){
push();
translate(0,-0.5*len);
noStroke();
Snow();
pop();
}
}
pop();
push();
rotate(ang2-noise(currentMonth*20)*0.2);
Branch1(len,num,thick,ang1,ang2);
if(num<2){
push();
translate(0,-len);
noStroke();
Leave();
pop();
}
pop();
}
pop();
}
function Branch2(len,num,thick,ang3,ang4){
strokeWeight(thick);
line(0,0,0,-len);
push()
translate(0,-len);
if (num>0){
len = len*0.65;
thick = thick*0.65;
num--;
push();
rotate(ang3+noise(currentMonth*10)*0.2);
Branch2(len,num,thick,ang3,ang4);
if(num<2){
push();
translate(0,-len);
noStroke();
Leave();
pop();
}
pop();
push();
rotate(ang4-noise(currentMonth*20)*0.2);
Branch2(len,num,thick,ang3,ang4);
if(num<2){
push();
translate(0,-len);
noStroke();
Leave();
pop();
}
pop();
}
pop();
}
function drawLable(){
textAlign(CENTER);
var monthLable = tableMonth.getColumn('monthLable');
fill(255);
textSize(height*0.045);
text(monthLable[currentMonth],width*0.5,height*0.05);
fill(130);
push();
for (var j=currentMonth+1; j<=20; j++){
textSize(height*0.025);
translate(height*0.25,0);
var a;
if(j<12){a=j}else{a=j-12}
text(monthLable[a],width*0.5,height*0.045);
}
pop();
for (var k=currentMonth-1; k>=-8; k--){
textSize(height*0.025);
translate(-height*0.25,0);
var b;
if(k>=0){b=k}else{b=k+12}
text(monthLable[b],width*0.5,height*0.045);
}
}
function setGradient(x, y, w, h, c1, c2, axis) {
noFill();
if (axis === Y_AXIS) {
// Top to bottom gradient
for (let i = y; i <= y + h; i++) {
let inter = map(i, y, y + h, 0, 1);
let c = lerpColor(c1, c2, inter);
stroke(c);
line(x, i, x + w, i);
}
} else if (axis === X_AXIS) {
// Left to right gradient
for (let i = x; i <= x + w; i++) {
let inter = map(i, x, x + w, 0, 1);
let c = lerpColor(c1, c2, inter);
stroke(c);
line(i, y, i, y + h);
}
}
}