Somewhere in the universe, at the setting of one of its suns. Birds fly in the sky to the sound of insects, while the wind caresses the herbs ...
function t(t,e){return t+Math.random()*(e-t)}function e(){setTimeout(function(){var t,i,o;x+=h,z+=p,x>s&&(x=-u,z=0);for(t in M){for(g=parseInt(t),r.fillStyle="#F8"+g.toString(16),o=u/d,r.fillRect(0,t*o,s,(t+1)*o),t%2==0&&(c=l*c),v=c*T*Math.sin(z),r.fillStyle="#000",r.beginPath(),r.moveTo(M[t][0]+x,M[t][1]-v),i=2;9>i;i+=2)r.lineTo(M[t][i]+x,M[t][i+1]);r.closePath(),r.fill()}for(o in m)m[o].o=20*Math.abs(Math.sin(z)),px=m[o].x,r.beginPath(),r.moveTo(y+px,u),r.quadraticCurveTo(10+px,100,y+px+m[o].o,200),r.stroke();e()},1e3/n)}var o,n,r=window.c,a=new AudioContext,c=1,l=-1,s=1920,u=s/2,x=-u,v=z=0,h=3,p=.2,T=17,d=15,m=[],P=240,y=20,M=[];for(_P=[18,5,31,5,41,0,5,5],M[0]=_P,i=1;i<=P;i++){if(m[i-1]={x:t(0,s),o:t(0,1.5)},i<=d)for(cstx=t(90,u/3),M[i]=[],o=0;8>o;o++)M[i][o]=M[0][o]+cstx;i<3&&setInterval(function(){var t,e;for(f=[0,4e3,.2,.3,12e3,.6,.6,1e4,.8],t=0;7>t;t+=3)e=a.createOscillator(),e.frequency.value=f[t+1],e.type="square",e.connect(a.destination),e.start(f[t]),e.stop(a.currentTime+f[t+2])},2300*i)}n=30,e();
ZnVuY3Rpb24gdCh0LGUpe3JldHVybiB0K01hdGgucmFuZG9tKCkqKGUtdCl9ZnVuY3Rpb24gZSgpe3NldFRpbWVvdXQoZnVuY3Rpb24oKXt2YXIgdCxpLG87eCs9aCx6Kz1wLHg+cyYmKHg9LXUsej0wKTtmb3IodCBpbiBNKXtmb3IoZz1wYXJzZUludCh0KSxyLmZpbGxTdHlsZT0iI0Y4IitnLnRvU3RyaW5nKDE2KSxvPXUvZCxyLmZpbGxSZWN0KDA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AwKmkpfW49MzAsZSgpOw==
/**
* @file javascript into shim.html
* @version 2017-02-27-09-36-00
* @date 2017/02/27
* @author Patrice CHASSAING
* @copyright Me
* @brief JS1k 2017 demo contest
*/
//(Supposed) Random function
function rA(n, x) {
return (n + (Math.random() * (x - n)))
}
var ctx = window.c, //get the canvas context
s = new(AudioContext), //prepare sound context
d = 1, ds = -1, //use to create inverted variables
cW = 1920, //canvas Weight
cH = cW / 2, //canvas Height
x = -cH, //first bird position
wY = z = 0, //wings bird position and angle
sX = 3, //horizontal bird speed
sz = .2, //wing & blades of grass angle speed
wM = 17, //wing Max amplitude
nB = 15, //birds number
_BoGrass = [], //blades of grass
lM = 240, //number of blades of grass
v = 20; //a point to draw a blade of grass
//x1 = 18, //x1,y1 is the moving point of wing and x1 = (x2+x3)/2 also 31+5/2
//y1 = 5, //y1 = (y2+y3)/2 also 5+5/2
//x4=41,y4=0
var _Birds = []; //birds array
_P = [18, 5, 31, 5, 41, 0, 5, 5]; //points to draw birds
_Birds[0] = _P;
//Need that lM>nB , remove one "for loop" line
for (i = 1; i <= lM; i++) {
//Create differents blades of grass positions
_BoGrass[i - 1] = {
x : rA(0, cW), //Random to create differents blades of grass horizontal positions
o : rA(0, 1.5) //Random to create differents blades of grass curve positions
};
if (i <= nB) { //Used to remove one "for loop" line
cstx = rA(90, cH / 3); //Random to create differents blades of grass vertical positions
_Birds[i] = [];
for (var U = 0; U < 8; U++) {
_Birds[i][U] = _Birds[0][U] + cstx;
} //Create differents birds positions
}
//Crickets Sound generator
if (i < 3) { //Used to remove one "for loop" line
setInterval(function () {
f = [0, 4E3, .2, .3, 12E3, .6, .6, 10E3, .8]//.2,0,.7,
for (var I = 0; I < 7; I += 3) {
var o = s.createOscillator(); //Test with http://p.chas.free.fr/demo/jsaudioapi/index.html
o.frequency.value = f[I + 1]; //3-->8KHz, it's the cricket sound ... they think : http://www.scholarpedia.org/article/Cricket_acoustic_communication
//But get a .wav on net where f is a sin between 4-->12KHz
o.type = 'square'; //But what's the type of oscillation ? https://developer.mozilla.org/fr/docs/Web/API/OscillatorNode/type
o.connect(s.destination);
o.start(f[I]); //Start to play crickets sound
o.stop(s.currentTime + f[I + 2]);//Stop to play in above 2 seconds
}
}, i * 2300);
}
}
var fps = 30;
function drawc() {
setTimeout(function() {
x = x + sX; //Change x bird position
z = z + sz; //Change O wings bird angle
if (x > cW) { //If bird quit the screen by the right
x = -cH; //Go to the left ...
z = 0 //Reset wings bird angle
}
for (var i in _Birds) { //= 0; i < nB; i++) {
g = parseInt(i); //Must convert, you know the cause
ctx.fillStyle = '#F8' + (g.toString(16)); //Select a color to draw between F80-->F8F //use http://p.chas.free.fr/demo/shorthandcolors/index-h.html
l = cH / nB; //Cut canvas Height in nB parts
ctx.fillRect(0, (i) * l, cW, (i + 1) * l); //So draw a gradient effect
if (i % 2 == 0)
d = ds * d; //d to have differents wings positions of birds
wY = d * wM * Math.sin(z); //Sinusoidal function wM-->0-->-wM
ctx.fillStyle = '#000'; //Select black to draw
ctx.beginPath(); //Start to draw a bird
ctx.moveTo(_Birds[i][0] + x, _Birds[i][1] - wY);
for (var j = 2; j < 9; j += 2) {
ctx.lineTo(_Birds[i][j] + x, _Birds[i][j + 1]);
}
ctx.closePath();
ctx.fill(); //Finish to draw a bird
}
//Grass moving in the wind
for (var l in _BoGrass) {
_BoGrass[l].o = 20 * Math.abs(Math.sin(z)); //Sinusoidal function 0-->20
px = _BoGrass[l].x; //Blade of grass movement
ctx.beginPath(); //Start to draw a blade of grass
ctx.moveTo(v + px, cH);
ctx.quadraticCurveTo(10 + px, 100, v + px + _BoGrass[l].o, 200);
ctx.stroke(); //Finish to draw a blade of grass
}
//requestAnimationFrame(drawc); //Synchronize with the framerate
drawc();
}, 1000 / fps);
}
drawc(); //There is always a begin