import {path} from 'd3-path';

var pi = Math.PI;

export default function() {
  var x,
      y,
      size,
      defined,
      context = null,
      ready, x1, y1, r1;

  function point(x2, y2, w2) {
    var r2 = w2 / 2;

    if (ready) {
      // get normal vector
      var ux = y1 - y2,
          uy = x2 - x1,
          ud = Math.sqrt(ux * ux + uy * uy),
          rx = (ux /= ud) * r1,
          ry = (uy /= ud) * r1,
          t = Math.atan2(uy, ux);

      // draw segment
      context.moveTo(x1 - rx, y1 - ry);
      context.lineTo(x2 - ux * r2, y2 - uy * r2);
      context.arc(x2, y2, r2, t - pi, t);
      context.lineTo(x1 + rx, y1 + ry);
      context.arc(x1, y1, r1, t, t + pi);
      context.closePath();
    } else {
      ready = 1;
    }
    x1 = x2, y1 = y2, r1 = r2;
  }

  function trail(data) {
    var i,
        n = data.length,
        d,
        defined0 = false,
        buffer;

    if (context == null) context = buffer = path();

    for (i = 0; i <= n; ++i) {
      if (!(i < n && defined(d = data[i], i, data)) === defined0) {
        if (defined0 = !defined0) ready = 0;
      }
      if (defined0) point(+x(d, i, data), +y(d, i, data), +size(d, i, data));
    }

    if (buffer) return context = null, buffer + '' || null;
  }

  trail.x = function(_) {
    return arguments.length ? (x = _, trail) : x;
  };

  trail.y = function(_) {
    return arguments.length ? (y = _, trail) : y;
  };

  trail.size = function(_) {
    return arguments.length ? (size = _, trail) : size;
  };

  trail.defined = function(_) {
    return arguments.length ? (defined = _, trail) : defined;
  };

  trail.context = function(_) {
    return arguments.length ? (_ == null ? context = null : context = _, trail) : context;
  };

  return trail;
}
@@ -238,35 +238,8 @@
 ) %7B%0A
-      // get normal vector%0A
     
@@ -280,27 +280,81 @@
  x2 - x1
-,%0A         
+;%0A%0A      if (ux %7C%7C uy) %7B%0A        // get normal vector%0A        var
  ud = Ma
@@ -391,16 +391,18 @@
         
+  
 rx = (ux
@@ -407,32 +407,34 @@
 ux /= ud) * r1,%0A
+  
           ry = (
@@ -441,32 +441,34 @@
 uy /= ud) * r1,%0A
+  
           t = Ma
@@ -492,16 +492,18 @@
 %0A%0A      
+  
 // draw 
@@ -510,16 +510,18 @@
 segment%0A
+  
       co
@@ -550,32 +550,34 @@
 y1 - ry);%0A      
+ 
+ 
 context.lineTo(x
@@ -602,32 +602,34 @@
 uy * r2);%0A      
+  
 context.arc(x2, 
@@ -648,16 +648,18 @@
 pi, t);%0A
+  
       co
@@ -688,32 +688,34 @@
 y1 + ry);%0A      
+  
 context.arc(x1, 
@@ -734,16 +734,81 @@
  + pi);%0A
+      %7D else %7B%0A        context.arc(x2, y2, r2, 0, 2*pi);%0A      %7D%0A
       co

Fix handling of duplicate point in trail mark. (#40)