Skip to content

Instantly share code, notes, and snippets.

@gamehelp16
Created March 16, 2016 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gamehelp16/d4f7260c31d98261ccbb to your computer and use it in GitHub Desktop.
Save gamehelp16/d4f7260c31d98261ccbb to your computer and use it in GitHub Desktop.
The 'original' source of my 2016 JS1k entry: 'Elemental Forest'
/*
Story time, after submitting an update to the demo I planned to make another
update for the demo (which I didn't submit in the end) so I edited the code
to do some edits etc etc. And maybe you've guessed it, stupid me didn't make
a copy of the original source of the demo and just modified the code straight
away. Therefore, I have to reverse the changes in the code but I can't seem to
restore the changes completely. So, here's the almost original source of the
demo. It's not exactly the same as the one submitted, but it's almost the same
and the differences between this and the original code should be just very
minor things.
TL;DR this is not exactly the original source of the demo I submitted.
*/
/*
a, b, c = shim
e = theme color h
f = frame number
g = theme color l
h = canvas height
i, k, K = looping variable
r() = generate seeded random number
R() = render
s = random number seed
S = random number
t[] = road data
T() = draw tree
w = canvas width
x, y, z = function variable
X, Y, Z = placeholder variable
*/
S = Math.random(),
s = f = 0,
t = [],
e = S * 360;
for(k=200;k--;)t.push([Math.sin(k*(Math.PI/180))*500-250,150+Math.cos(k/30)*50,180-k,Math.random(),k]); //[x,y,z,seed,tree type]
R()
function r(x,y,z) {
s = (s * 9301 + 49297) % 233280;
return s / 233280
}
function R(x,y,z) {
s = S;
c.fillStyle="hsl("+[e+=.1,"90%",g=s*20+50]+"%)";
c.fillRect(0,0,w = a.width, h = a.height);
for(k=2;k--;) {
c.save();
c.fillStyle = "rgba(0,0,0,.1)";
c.translate(0,k*h/5+h/10-Math.cos(f/30)*20);
c.beginPath();
c.moveTo(w,h);
c.lineTo(w,0);
for(i=15;i--;)c.lineTo(w/15*i+r()*20, 30*r());
c.lineTo(-w,h);
c.fill();
c.restore();
}
for(K=199;K--;) {
k=(K+(f%200))%200;
c.save();
p = 30 / (30 + t[k][2]--), s = t[k][3];
Z=(h/2>230)?230:h/2;
c.translate(w/2+500*Math.sin(Math.PI/180*f)*-p,h-Z-20*Math.cos(f/30));
c.scale(p,p);
r();
c.fillStyle = "hsl("+[e,"90%",g-30+t[k][2]/13]+"%)";
c.fillRect(-h*15,t[k][1],h*30,10);
c.fillStyle = "hsl("+[e,"90%",g-25+t[k][2]/13]+"%)";
c.fillRect(t[k][0]-50*r(),t[k][1],500,20);
c.fillStyle = c.strokeStyle = "hsla("+[e,"90%",t[k][2]/8+15+r()*3+"%",1-(t[k][2]-150)/30]+")";
if(r()<.5)T(t[k][0]-150-2500*r(),t[k][1],t[k][4]);
if(r()<.5)T(t[k][0]+2500*r()+650,t[k][1],t[k][4]);
c.restore();
if(f%200==k)t[k]=[Math.sin((f+181)*(Math.PI/180))*500-250,150+Math.cos(k/30)*50,180,Math.random(),200+f];
}
f++;
requestAnimationFrame(R)
}
function T(x,y,z) {
if(z%1e3<500) {
c.lineWidth = r()*12+28;
c.beginPath();
c.moveTo(x,y);
X = x-20 + r()*40;
Y = y - r()*40 - 280;
c.lineTo(X,Y);
X = x-20 + r()*40;
Y = Y - r()*40 - 280;
c.lineTo(X,Y);
c.stroke();
c.beginPath();
for(i=0;i<360;i+=r()*15+50) {
if(i<1)c.moveTo(X + Math.cos(i*(Math.PI/180)) * (180 + r()*70), Y + Math.sin(i*(Math.PI/180)) * (180 + r()*40));
else c.lineTo(X + Math.cos(i*(Math.PI/180)) * (180 + r()*70), Y + Math.sin(i*(Math.PI/180)) * (180 + r()*40));
}
c.fill()
}
else {
c.beginPath();
c.moveTo(x,y - r()*80 - 600);
X = 99 + r()*50;
c.lineTo(-X+x,y);
c.lineTo(X+x,y);
c.fill()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment