When I tried to create an array of randomly generated circles (stars) in my first code, I encountered a TypeError on this line:
stars[i].x = Math.floor(Math.random() * w)
Even though stars is defined in the code, the issue persisted.
$(document).ready(function() {
//Canvas
var canvas = $("#canvas")[0];
var ctx = canvas.getContext("2d");
var w = $("#canvas").width();
var h = $("#canvas").height();
var stars = [];
The remaining code seemed correct, but including it here might highlight any other mistakes that slipped past me.
$(document).ready(function() {
//Canvas
var canvas = $("#canvas")[0];
var ctx = canvas.getContext("2d");
var w = $("#canvas").width();
var h = $("#canvas").height();
var stars = [];
function init() {
createStars();
drawStars();
}
init();
function createStars() {
for (var i=0; i<=4; i++) {
stars[i].x = Math.floor(Math.random() * w);
stars[i].y = Math.floor(Math.random() * h);
}
}
function drawStars() {
for (var i=0; i <= 4; i++) {
ctx.beginPath();
ctx.arc(stars[i].x, stars[i].y, 10, 0, 2 * Math.PI);
ctx.stroke();
}
}
});
This being my initial attempt at programming, troubleshooting is not yet my strong suit. Thank you for any assistance provided.