I have an issue drawing on <canvas>
wherein this segment of my code, which draws musical staves on canvas, always makes the last line drawn lighter and almost fading away, no matter what browser is used.
window.onload = draw;
function draw() {
var canvas = document.getElementById('sheetmusic');
var c = canvas.getContext('2d');
var whitespace = 0; // pixel space between staves
var ycoordinate = 10; // y-plot of beginning of each staff
//draw the staves 'x' number of times requested
for (var x = 1; x <= 10; x++) {
// draw the staff
for (var i = 1; i <= 5; i++){
c.strokeStyle = 'black';
c.moveTo(0,ycoordinate);
c.lineTo(canvas.width,ycoordinate);
c.stroke();
ycoordinate = ycoordinate + 10;
}
//draw white space beneath each staff
c.fillStyle = 'white';
c.fillRect(0,whitespace + 52,canvas.width,45);
whitespace = whitespace + 100;
ycoordinate = ycoordinate + 50;
}
}
The solution I have, since I used a loop to draw the lines is to redraw each line once, when in fact all I really need is draw the very last line one more time.
window.onload = draw;
function draw() {
var canvas = document.getElementById('sheetmusic');
var c = canvas.getContext('2d');
var whitespace = 0; // pixel space between staves
var ycoordinate = 10; // y-plot of beginning of each staff
//draw the staves 'x' number of times requested
for (var x = 1; x <= 10; x++) {
// draw the staff
for (var i = 1; i <= 5; i++){
c.strokeStyle = 'black';
c.moveTo(0,ycoordinate);
c.lineTo(canvas.width,ycoordinate);
c.stroke();
// this code repeats to alleviate bug with last drawn line not being defined enough
c.moveTo(0,ycoordinate);
c.lineTo(canvas.width,ycoordinate);
c.stroke();
ycoordinate = ycoordinate + 10;
}
//draw white space beneath each staff
c.fillStyle = 'white';
c.fillRect(0,whitespace + 52,canvas.width,45);
whitespace = whitespace + 100;
ycoordinate = ycoordinate + 50;
}
}
Any ideas on how to redraw the last line once more?
Keep in mind, when the rest of this web app is implemented my x
variable will need to be changed via clicked option, therefore letting the amount of staves be as little or much as necessary.