|
10 | 10 | 'use strict';
|
11 | 11 |
|
12 | 12 | var d3 = require('d3');
|
13 |
| -var isNumeric = require('fast-isnumeric'); |
14 | 13 |
|
15 | 14 | var Color = require('../color');
|
16 | 15 | var Drawing = require('../drawing');
|
17 | 16 |
|
18 | 17 | var ARROWPATHS = require('./arrow_paths');
|
19 | 18 |
|
20 |
| -// add arrowhead(s) to a path or line d3 element el3 |
21 |
| -// style: 1-6, first 5 are pointers, 6 is circle, 7 is square, 8 is none |
22 |
| -// ends is 'start', 'end' (default), 'start+end' |
23 |
| -// mag is magnification vs. default (default 1) |
24 |
| - |
25 |
| -module.exports = function drawArrowHead(el3, style, ends, mag, standoff) { |
26 |
| - if(!isNumeric(mag)) mag = 1; |
| 19 | +/** |
| 20 | + * Add arrowhead(s) to a path or line element |
| 21 | + * |
| 22 | + * @param {d3.selection} el3: a d3-selected line or path element |
| 23 | + * |
| 24 | + * @param {string} ends: 'start', 'end', or 'start+end' for which ends get arrowheads |
| 25 | + * |
| 26 | + * @param {object} options: style information. Must have all the following: |
| 27 | + * @param {number} options.arrowhead: head style - see ./arrow_paths |
| 28 | + * @param {number} options.arrowsize: relative size of the head vs line width |
| 29 | + * @param {number} options.standoff: distance in px to move the arrow point from its target |
| 30 | + */ |
| 31 | +module.exports = function drawArrowHead(el3, ends, options) { |
27 | 32 | var el = el3.node(),
|
28 |
| - headStyle = ARROWPATHS[style||0]; |
29 |
| - |
30 |
| - if(typeof ends !== 'string' || !ends) ends = 'end'; |
| 33 | + headStyle = ARROWPATHS[options.arrowhead || 0]; |
31 | 34 |
|
32 |
| - var scale = (Drawing.getPx(el3, 'stroke-width') || 1) * mag, |
| 35 | + var scale = (Drawing.getPx(el3, 'stroke-width') || 1) * options.arrowsize, |
33 | 36 | stroke = el3.style('stroke') || Color.defaultLine,
|
34 | 37 | opacity = el3.style('stroke-opacity') || 1,
|
35 | 38 | doStart = ends.indexOf('start') >= 0,
|
36 | 39 | doEnd = ends.indexOf('end') >= 0,
|
37 |
| - backOff = headStyle.backoff * scale + standoff, |
| 40 | + backOff = headStyle.backoff * scale + options.standoff, |
38 | 41 | start,
|
39 | 42 | end,
|
40 | 43 | startRot,
|
@@ -110,7 +113,7 @@ module.exports = function drawArrowHead(el3, style, ends, mag, standoff) {
|
110 | 113 |
|
111 | 114 | function drawhead(p, rot) {
|
112 | 115 | if(!headStyle.path) return;
|
113 |
| - if(style > 5) rot = 0; // don't rotate square or circle |
| 116 | + if(options.arrowhead > 5) rot = 0; // don't rotate square or circle |
114 | 117 | d3.select(el.parentNode).append('path')
|
115 | 118 | .attr({
|
116 | 119 | 'class': el3.attr('class'),
|
|
0 commit comments