-
Notifications
You must be signed in to change notification settings - Fork 18
/
bingo-balls.html
149 lines (113 loc) · 3.6 KB
/
bingo-balls.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<html>
<head>
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<script src="three.min.js"></script>
<link rel="stylesheet" href="../etudes.css">
</head>
<body>
<h1 class="white">Bingo balls <a href="https://boytchev.github.io/etudes/">←</a></h1>
<script>
function randomBingoTexture()
{
// two random decimal digits
function rDec()
{
return (Math.random()+'').slice(-2);
}
// two random hex digits
function rHex()
{
var r = Math.random()*3;
if (r<1) return '00';
if (r<2) return '80';
return 'ff';
}
// creating 2D image
var cnv = document.createElement('canvas');
cnv.width = 512;
cnv.height = 512;
var ctx = cnv.getContext('2d');
// background
ctx.fillStyle = '#'+rHex()+rHex()+rHex();
ctx.fillRect(0, 0, 512, 512);
// two thick stripes
ctx.fillStyle = '#'+rHex()+rHex()+rHex();
ctx.fillRect(0, 100, 512, 50);
ctx.fillRect(0, 362, 512, 50);
// two thin stripes
ctx.fillStyle = '#'+rHex()+rHex()+rHex();
ctx.fillRect(0, 160, 512, 20);
ctx.fillRect(0, 332, 512, 20);
// two white circles with black borders
ctx.fillStyle = '#ffffff';
ctx.strokeStyle = '#000000';
ctx.beginPath();
ctx.arc(127,255,80,0,2*Math.PI);
ctx.fill();
ctx.stroke();
ctx.beginPath();
ctx.arc(385,255,80,0,2*Math.PI);
ctx.fill();
ctx.stroke();
// two bold numbers
ctx.fillStyle = '#000000';
ctx.font = '10em Arial Black';
ctx.textAlign="center";
var num=rDec();
ctx.fillText(num,127,290);
ctx.fillText(num,385,290);
// texture generation
var texture = new THREE.CanvasTexture( cnv );
texture.repeat.set( 2, 1 );
texture.wrapS = THREE.RepeatWrapping;
texture.anosotropy = renderer.capabilities.getMaxAnisotropy();
return texture;
}
// create the scene
var renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setAnimationLoop( animate );
document.body.appendChild( renderer.domElement );
document.body.style.margin = 0;
document.body.style.overflow = 'hidden';
var scene = new THREE.Scene();
scene.background = new THREE.Color( 'black' );
var camera = new THREE.PerspectiveCamera( 30, window.innerWidth/window.innerHeight, 1, 1000 );
camera.position.set( 0, 0, 120 );
camera.lookAt( scene.position );
var light = new THREE.PointLight();
light.position.set( 0, 150, 300 );
scene.add( light );
var clock = new THREE.Clock( true );
window.addEventListener( 'resize', onWindowResize, false );
onWindowResize();
function onWindowResize( event )
{
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight, true );
}
// create the balls
var geometry = new THREE.IcosahedronBufferGeometry( 5, 3 );
var balls = [];
for( var x = -44; x<=44; x+= 11 )
for( var y = -22; y<=22; y+= 11 )
{
var material = new THREE.MeshPhongMaterial( {map: randomBingoTexture()} ),
ball = new THREE.Mesh( geometry, material );
ball.position.set( x, y, 0 );
balls.push( ball );
}
scene.add( ...balls );
function animate()
{
var time = clock.getElapsedTime();
for( var i=0; i<balls.length; i++ )
{
balls[i].rotation.set( time+i, time/2-i, time/3+i/2 );
}
renderer.render( scene, camera );
}
</script>
</body>
</html>