2014-03-04 48 views
1

我知道<画布> </canvas>在必须添加形状的位置绘制空白区域。将空白画布修改为矩阵

在我的项目中,用户需要画一个矩阵,而不是空白。我怎么做?

+1

http://stackoverflow.com/questions/21117684/how-to-create -dynamic-grid/21119488 –

回答

0

这里是如何借鉴HTML画布的网格线:

演示:http://jsfiddle.net/m1erickson/DwdX6/

enter image description here

function drawGrid(lineCount){ 
    var xSpan=cw/lineCount; 
    var ySpan=cw/lineCount; 
    ctx.clearRect(0,0,cw,ch); 
    ctx.save(); 
    if(lineCount/2===parseInt(lineCount/2)){ 
     ctx.translate(.5,.5); 
    } 
    ctx.beginPath(); 
    for(var i=0;i<lineCount;i++){ 
     var x=(i+1)*xSpan; 
     var y=(i+1)*ySpan; 
     ctx.moveTo(x,0); 
     ctx.lineTo(x,ch); 
     ctx.moveTo(0,y); 
     ctx.lineTo(ch,y); 
    } 
    ctx.lineWidth=0.50; 
    ctx.stroke(); 
    ctx.restore(); 
} 
+0

非常感谢,这很有帮助,但只有在宽度=高度时才能完美工作。如果这些值不相等,我有一个没有填充行的特定区域(这些行被中断)。 –