HTML and CSS Reference
In-Depth Information
Figure 19-3. Randomly placed dots
Not too bad. But if you crowd them in a bit more by making more dots (300) and reducing the area to 100
by 100, you notice something odd starting to happen, which you can see in Figure 19-4. Here's the code
for example 04-random-2.html :
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Random 2</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<canvas id="canvas" width="400" height="400"></canvas>
<script>
window.onload = function () {
var canvas = document.getElementById('canvas'),
context = canvas.getContext('2d'),
numDots = 300;
while (numDots--) {
var x = canvas.width / 2 + Math.random() * 100 - 50,
y = canvas.height / 2 + Math.random() * 100 - 50;
context.fillStyle = "#000000";
context.beginPath();
context.arc(x, y, 2, 0, (Math.PI * 2), true);
context.closePath();
context.fill();
}
};
</script>
</body>
</html>
Search WWH ::




Custom Search