Edit in JSFiddle

var N_STARS = 100;
var STAR_SIZE = 2;

var canvas = document.getElementById('canvas');
if (canvas.getContext) {
    var ctx = canvas.getContext('2d');
    ctx.fillStyle = "white";
    var i, x, y;
    for(i = 0; i<N_STARS; i++){
        x = Math.random() * canvas.width;
        y = Math.random() * canvas.height;
        ctx.fillRect(x, y, STAR_SIZE, STAR_SIZE);
    }
}
<body>
    <canvas id="canvas" width="400" height="300"></canvas>
</body>
#canvas {
    background-color:black
}