HTML and CSS Reference
In-Depth Information
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Random 1</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 = 50;
while (numDots--) {
var x = canvas.width / 2 + Math.random() * 200 - 100,
y = canvas.height / 2 + Math.random() * 200 - 100;
//draw circle...
context.fillStyle = "#000000";
context.beginPath();
context.arc(x, y, 2, 0, (Math.PI * 2), true);
context.closePath();
context.fill();
}
};
</script>
</body>
</html>
This creates a random number from -100 to +100 and adds it to the center point of the canvas, so all of the
dots are no farther than 100 pixels on either axis from the center. Figure 19-3 shows the result.
Search WWH ::




Custom Search