-
Notifications
You must be signed in to change notification settings - Fork 1
/
bilinear2.html
103 lines (81 loc) · 1.84 KB
/
bilinear2.html
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
var canvas;
var ctx;
//
// Ejemplo de bilinear sampling
//
var diff_map = [];
function CreateTexture()
{
var dt = 32;
for(var i=0;i<dt;++i)
for(var j=0;j<dt;++j)
{
var x = i-16;
var y = j-16;
var fx = Math.sqrt(x*x + y*y);
if( Math.abs(fx - 6)<1)
diff_map[i+dt*j] = {r:255 , g:255, b:255};
else
diff_map[i+dt*j] = {r:(i/dt*255)|0 , g:(j/dt*255)|0, b:0};
}
}
function tex2d(u, v)
{
var du = 32;
var dv = 32;
u -= 0.5/du;
v -= 0.5/dv;
var r = u*du;
var s = v*dv;
var i = (r<0 ? du-1 : r)|0
var j = (s<0 ? dv-1 : s)|0
var ri = r-i;
var rj = s-j;
var i1 = i<du-1?i+1:0;
var j1 = j<dv-1?j+1:0;
if(ri<0)
ri+=du;
if(rj<0)
rj+=dv;
var c00 = diff_map[i+dv*j];
var c01 = diff_map[i+dv*j1];
var c11 = diff_map[i1+dv*j1];
var c10 = diff_map[i1+dv*j];
var red = (c00.r * (1-ri)*(1-rj) + c10.r*ri*(1-rj) + c11.r*ri*rj + c01.r*(1-ri)*rj) | 0;
var green = (c00.g * (1-ri)*(1-rj) + c10.g*ri*(1-rj) + c11.g*ri*rj + c01.g*(1-ri)*rj) | 0;
var blue = (c00.b * (1-ri)*(1-rj) + c10.b*ri*(1-rj) + c11.b*ri*rj + c01.b*(1-ri)*rj) | 0;
return 'rgba(' + red.toString() + ',' + green.toString() + ',' + blue.toString() + ',255)';
}
function draw()
{
if (canvas.getContext)
{
ctx.fillStyle = 'rgba(240,240,240,255)';
ctx.fillRect(0,0,1000,800);
var dt = 42;
var k = 5;
for(var i=0;i<=dt;++i)
for(var j=0;j<=dt;++j)
{
ctx.fillStyle = tex2d(i/dt,j/dt);
ctx.fillRect(10+i*k,10+j*k,k,k);
}
}
}
function animate()
{
canvas = document.getElementById('mycanvas');
ctx = canvas.getContext('2d');
CreateTexture();
draw();
}
</script>
</head>
<body onload="animate();">
<canvas id="mycanvas" width="1000" height="700"></canvas>
</body>
</html>