Line 1: | Line 1: | ||
<html> | <html> | ||
− | |||
− | |||
<script> | <script> | ||
Line 10: | Line 8: | ||
/** | /** | ||
Original Author: Lucas Mota Ribeiro | Original Author: Lucas Mota Ribeiro | ||
+ | Edited by iGem 2021 Düsseldorf | ||
This project creates an adaptive array of leafs and animate them. | This project creates an adaptive array of leafs and animate them. | ||
*/ | */ | ||
Line 21: | Line 20: | ||
var MID_HEIGHT = 2; // The height of the middle of the leaf. | var MID_HEIGHT = 2; // The height of the middle of the leaf. | ||
− | var HEIGHT = | + | var HEIGHT = 72; // Leaf Height. |
− | var WIDTH = | + | var WIDTH = 6; // Leaf width. |
− | var distance = | + | var distance = 6; // Distance between leafs. |
var ANGLE = Math.PI / 2; // The angle tha the leaf starts. | var ANGLE = Math.PI / 2; // The angle tha the leaf starts. | ||
− | var RANDOMNESS = | + | var RANDOMNESS = 42; // How random these parameters are the leaf's array. |
var time = 0; // Time counter. | var time = 0; // Time counter. | ||
var leafColor = ['#3AAA35', '#006633', '#8BCB60']; // Color of the leaf. | var leafColor = ['#3AAA35', '#006633', '#8BCB60']; // Color of the leaf. | ||
var FPS = 30; | var FPS = 30; | ||
− | var SPEED = 1 | + | var SPEED = 1; |
− | var LAMBDA = 0. | + | var LAMBDA = 0.005; |
/*! | /*! | ||
Line 41: | Line 40: | ||
*/ | */ | ||
var grass = []; | var grass = []; | ||
+ | var colors = []; | ||
/*! | /*! | ||
Line 87: | Line 87: | ||
* Create leafs. | * Create leafs. | ||
*/ | */ | ||
− | function | + | |
− | + | function setLeafParams (leaf, dist, paralAng){ | |
− | + | ||
− | + | ||
− | + | ||
leaf.rand0 = nextRand(); | leaf.rand0 = nextRand(); | ||
leaf.rand1 = nextRand(); | leaf.rand1 = nextRand(); | ||
leaf.rand2 = nextRand(); | leaf.rand2 = nextRand(); | ||
− | |||
leaf.Y0 = canvas.height + 5; | leaf.Y0 = canvas.height + 5; | ||
leaf.X0 = dist; | leaf.X0 = dist; | ||
Line 110: | Line 107: | ||
leaf.path1 = new Path2D('M '+leaf.X0+' '+leaf.Y0+' q '+leaf.X3+' -'+leaf.Y3+' '+leaf.X1+' -'+leaf.Y1); | leaf.path1 = new Path2D('M '+leaf.X0+' '+leaf.Y0+' q '+leaf.X3+' -'+leaf.Y3+' '+leaf.X1+' -'+leaf.Y1); | ||
leaf.path2 = new Path2D('M '+leaf.X0+' '+leaf.Y0+' q '+leaf.X4+' -'+leaf.Y4+' '+leaf.X1+' -'+leaf.Y1); | leaf.path2 = new Path2D('M '+leaf.X0+' '+leaf.Y0+' q '+leaf.X4+' -'+leaf.Y4+' '+leaf.X1+' -'+leaf.Y1); | ||
+ | return leaf; | ||
+ | } | ||
+ | function createLeafs(){ | ||
+ | randCounter = 0; | ||
+ | let paralAng=ANGLE-Math.PI/2; | ||
+ | for (var dist = 0; dist < canvas.width + 50; dist += distance) { | ||
+ | colors.push(leafColor[getRandomInt(3)]); | ||
+ | |||
+ | var leaf={} | ||
+ | leaf = setLeafParams(leaf, dist, paralAng); | ||
+ | |||
grass.push(leaf); // Add lead to array. | grass.push(leaf); // Add lead to array. | ||
} | } | ||
Line 126: | Line 134: | ||
var paralAng = ANGLE-Math.PI/2+time_ang; | var paralAng = ANGLE-Math.PI/2+time_ang; | ||
var aux; | var aux; | ||
+ | |||
leaf.rand0 = nextRand(); | leaf.rand0 = nextRand(); | ||
leaf.rand1 = nextRand(); | leaf.rand1 = nextRand(); | ||
Line 131: | Line 140: | ||
aux = ((leaf.rand0 - 5) * HEIGHT * RANDOMNESS / 500); | aux = ((leaf.rand0 - 5) * HEIGHT * RANDOMNESS / 500); | ||
tHeight =aux+HEIGHT; | tHeight =aux+HEIGHT; | ||
− | + | ||
− | + | ||
− | + | ||
tMheight = ((((leaf.rand1-5)/5)*MID_HEIGHT*RANDOMNESS/100)+MID_HEIGHT); | tMheight = ((((leaf.rand1-5)/5)*MID_HEIGHT*RANDOMNESS/100)+MID_HEIGHT); | ||
tWidth = ((((leaf.rand2-5)/5)*WIDTH*RANDOMNESS/100)+WIDTH); | tWidth = ((((leaf.rand2-5)/5)*WIDTH*RANDOMNESS/100)+WIDTH); | ||
− | |||
− | |||
leaf.Y0 = canvas.height+5; | leaf.Y0 = canvas.height+5; | ||
leaf.X0 = dist; | leaf.X0 = dist; | ||
Line 152: | Line 157: | ||
leaf.path1 =new Path2D('M '+leaf.X0+' '+leaf.Y0+' q '+leaf.X3+' '+(-leaf.Y3)+' '+leaf.X1+' '+(-leaf.Y1)); | leaf.path1 =new Path2D('M '+leaf.X0+' '+leaf.Y0+' q '+leaf.X3+' '+(-leaf.Y3)+' '+leaf.X1+' '+(-leaf.Y1)); | ||
leaf.path2 =new Path2D('M '+leaf.X0+' '+leaf.Y0+' q '+leaf.X4+' '+(-leaf.Y4)+' '+leaf.X1+' '+(-leaf.Y1)); | leaf.path2 =new Path2D('M '+leaf.X0+' '+leaf.Y0+' q '+leaf.X4+' '+(-leaf.Y4)+' '+leaf.X1+' '+(-leaf.Y1)); | ||
+ | |||
+ | aux = ((leaf.rand0 - 5) * HEIGHT * RANDOMNESS / 500); | ||
+ | |||
+ | tHeight =aux+HEIGHT; | ||
+ | tMheight = ((((leaf.rand1-5)/5)*MID_HEIGHT*RANDOMNESS/100)+MID_HEIGHT); | ||
+ | tWidth = ((((leaf.rand2-5)/5)*WIDTH*RANDOMNESS/100)+WIDTH); | ||
+ | |||
+ | |||
grass.push(leaf); | grass.push(leaf); | ||
} | } | ||
− | + | ctx.lineWidth = 1; | |
− | + | ||
− | ctx.lineWidth = | + | |
for(var i = 0; i < grass.length; i++){ | for(var i = 0; i < grass.length; i++){ | ||
− | ctx.fillStyle = | + | let color = colors[i]; |
+ | ctx.fillStyle = color; | ||
var leaf = grass[i]; | var leaf = grass[i]; | ||
ctx.fill(leaf.path1); | ctx.fill(leaf.path1); | ||
Line 165: | Line 177: | ||
//ctx.strokeStyle = "rgba(1,62,4,0.5)"; | //ctx.strokeStyle = "rgba(1,62,4,0.5)"; | ||
− | + | ctx.strokeStyle = '#eeffee'; | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
ctx.beginPath(); | ctx.beginPath(); | ||
Line 178: | Line 183: | ||
ctx.lineTo(leaf.X0+leaf.X1, leaf.Y0-leaf.Y1); | ctx.lineTo(leaf.X0+leaf.X1, leaf.Y0-leaf.Y1); | ||
ctx.stroke(); | ctx.stroke(); | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
} | } | ||
} | } | ||
Line 212: | Line 204: | ||
createLeafs(); | createLeafs(); | ||
− | |||
// Updates canvas for a certain FPS. | // Updates canvas for a certain FPS. | ||
+ | var timeDir = 1; | ||
+ | var timeDirSteps = 0; | ||
window.setInterval(function () { | window.setInterval(function () { | ||
+ | SPEED = 0.5 | ||
resizeCanvas(); | resizeCanvas(); | ||
drawGrass(time); | drawGrass(time); | ||
− | time = (time<= | + | prevTime = time; |
+ | time = (time<=260) ? time + (timeDir) : 0; | ||
+ | if(time <= 260 && time >= 0){ | ||
+ | time = time + (timeDir); | ||
+ | }else{ | ||
+ | time = 0; | ||
+ | } | ||
+ | if(timeDirSteps >= 30){ | ||
+ | if(getRandomInt(2) === 0){ | ||
+ | timeDir *= -1; | ||
+ | timeDirSteps = 0; | ||
+ | } | ||
+ | } | ||
+ | timeDirSteps++; | ||
+ | |||
+ | |||
}, 1000/FPS); | }, 1000/FPS); | ||
} | } |
Revision as of 10:29, 6 October 2021