This is part of a much larger code, but for some reason I cannot control the color of the drawn arc. When I create the arc and then call the draw function, the arc appears but it is filled in black. (not the expected blue)
class calnote {
constructor() {
this.radius = calnoteradius;
this.positionx = Math.floor(Math.random() * 700);
this.positiony = Math.floor(Math.random() * 500);
}
change_position() {
this.positionx = Math.floor(Math.random() * 700);
this.positiony = Math.floor(Math.random() * 500);
}
draw(ctx) {
ctx.beginPath();
ctx.arc(this.positionx, this.positiony, this.radius,0, 2 * Math.PI);
ctx.fillstyle = "#0000FF";
ctx.fill();
ctx.closePath();
}
}
However later I call this function
function drawScore() {
ctx.beginPath();
ctx.font = "16px Arial";
ctx.fillStyle = "#FF0000";
ctx.fillText("Combo: "+ combo, 8, 40);
ctx.fillText("Score: "+ score, 8, 20);
ctx.closePath();
}
and now the score is written in red, and the arcs are filled in red when drawn. (if I change this color it changes the score and all later arcs, so I can make them blue, green, etc.).
Finally, I know the draw function is updating because:
If anyone has any ideas please let me know.
Looks like just a typo on your ctx.fillstyle = "#0000FF";
the S
should be uppercase
class calnote { constructor() { this.positionx = Math.floor(Math.random() * 100); this.positiony = Math.floor(Math.random() * 100); } change_position() { this.positionx = Math.floor(Math.random() * 100); this.positiony = Math.floor(Math.random() * 100); } draw(ctx) { ctx.beginPath(); ctx.arc(this.positionx, this.positiony, 10, 0, 2 * Math.PI); ctx.fillStyle = "#0000FF"; ctx.fill(); } } function drawScore() { ctx.beginPath(); ctx.font = "16px Arial"; ctx.fillStyle = "#FF0000"; ctx.fillText("Combo: ", 8, 40); ctx.fillText("Score: ", 8, 20); } function draw() { ctx.clearRect(0, 0, canvas.width, canvas.height) drawScore() note.change_position() note.draw(ctx) } var canvas = document.getElementById('c'); var ctx = canvas.getContext('2d'); note = new calnote() setInterval(draw, 200);
<canvas height="160" width="300" id="c"></canvas>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.