summaryrefslogtreecommitdiffstats
path: root/examples/script/context2d/scripts/plasma.js
blob: 1aa9294dfb3344e0772a4032d80e180747aa4e75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
var counter = 0;

var PIXEL_SIZE = 4;

var temp_1 = 0;

function init()
{
    setInterval('render()',50);
}

function dist(a, b, c, d)
{
    return Math.sqrt((a - c) * (a - c) + (b - d) * (b - d));
}


function render()
{
    var canvas = document.getElementById('tutorial');
    canvas.resize(128, 128);
    var ctx = canvas.getContext('2d');
    ctx.save();

    var time = counter * 5;


    for( y = 0; y < 128; y+=PIXEL_SIZE) {
	for( x = 0 ; x < 128; x+=PIXEL_SIZE) {
	    
	    
	    var temp_val = Math.floor(Math.sin(dist(x + time, y, 128.0, 128.0) / 8.0)
				      + Math.sin(dist(x, y, 64.0, 64.0) / 8.0)
				      + Math.sin(dist(x, y + time / 7, 192.0, 64) / 7.0)
				      + Math.sin(dist(x, y, 192.0, 100.0) / 8.0));
	    
	    
	    
	    
	    var temp_col = Math.floor((2 + temp_val) * 50);
	    
	    var rand_red = temp_col * 3;
	    var rand_green = temp_col  ;
	    var rand_blue = 128 - temp_col;

	    ctx.fillStyle = "rgb("+rand_red+","+rand_green+","+rand_blue+")";

	    ctx.fillRect(x,y,PIXEL_SIZE,PIXEL_SIZE);
	}
    }


    ctx.restore();
    counter++;

}

init();