我有一个问题,<canvas>
其中我的这段代码在画布上绘制五线谱,无论使用什么浏览器,总是使最后一行绘制得更轻并且几乎消失。
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;
}
}
我拥有的解决方案,因为我使用循环来绘制线条是重绘每条线一次,而实际上我真正需要的只是再绘制最后一条线。
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;
}
}
关于如何再次重绘最后一行的任何想法?
请记住,当实现此 Web 应用程序的其余部分时,我的x
变量将需要通过单击选项进行更改,因此让五线谱的数量尽可能少或多。