IE9 Canvas现在支持虚线吗?目前,我正在执行与以下操作非常相似的操作:

var CP = window.CanvasRenderingContext2D && CanvasRenderingContext2D.prototype;
if (CP && CP.lineTo){
  CP.dashedLine = function(x,y,x2,y2,dashArray){
    if (!dashArray) dashArray=[10,5];
    if (dashLength==0) dashLength = 0.001; // Hack for Safari
    var dashCount = dashArray.length;
    this.moveTo(x, y);
    var dx = (x2-x), dy = (y2-y);
    var slope = dy/dx;
    var distRemaining = Math.sqrt( dx*dx + dy*dy );
    var dashIndex=0, draw=true;
    while (distRemaining>=0.1){
      var dashLength = dashArray[dashIndex++%dashCount];
      if (dashLength > distRemaining) dashLength = distRemaining;
      var xStep = Math.sqrt( dashLength*dashLength / (1 + slope*slope) );
      if (dx<0) xStep = -xStep;
      x += xStep
      y += slope*xStep;
      this[draw ? 'lineTo' : 'moveTo'](x,y);
      distRemaining -= dashLength;
      draw = !draw;
    }
  }
}


从以下位置找到:override line stroke in canvas for dashed/dotted lines

这在IE7,IE8,IE9兼容模式和FireFox中非常有效,但是,在IE9和Chrome中,为每条虚线绘制了实线。

为什么会发生这种情况的任何想法?

最佳答案

还有其他问题。在Chrome中可以正常使用:

ctx.beginPath();
ctx.dashedLine(10,10, 100, 100,[4, 4])
ctx.stroke();


http://jsfiddle.net/MPg5X/1/

07-28 11:24