【问题标题】:drawing lines with canvas用画布画线
【发布时间】:2013-02-08 00:54:54
【问题描述】:

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 中,每条虚线都会绘制一个实线。

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

【问题讨论】:

    标签: javascript canvas internet-explorer-9 html5-canvas


    【解决方案1】:

    还有其他问题。它在 Chrome 中运行良好:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-20
      • 1970-01-01
      • 2011-05-24
      • 2019-06-09
      • 2023-03-31
      • 2021-09-18
      • 2013-01-05
      相关资源
      最近更新 更多