Giter VIP home page Giter VIP logo

leader-line's Introduction

LeaderLine

npm GitHub issues dependencies license

Draw a leader line in your web page.

Document and Examples https://anseki.github.io/leader-line/

<div id="start">start</div>
<div id="end">end</div>
// Add new leader line from `start` to `end` (HTML/SVG element, basically).
new LeaderLine(
  document.getElementById('start'),
  document.getElementById('end')
);

ex-010

It supports options to customize.

ex-020

Basically, it can indicate HTML/SVG element such as <div>, <button>, <ul>, <td>, <circle>, <text>, etc.

ex-021

It can indicate a part of an element also instead of the element.

ex-030

ex-031

Also, it can indicate an element of another library.
For example, the following uses LeaderLine with D3.js. Move the mouse on the list.

ex-040

Usage

Load LeaderLine into your web page.

<script src="leader-line.min.js"></script>

Pass two HTML/SVG elements to LeaderLine constructor. Then a leader line is drawn between those elements.

new LeaderLine(
  document.getElementById('element-1'),
  document.getElementById('element-2')
);

ex-050

Any element that has bounding-box is accepted. For example, <div>, <button>, <ul>, <td>, <circle>, <text>, and also, elements in another window (i.e. <iframe>). (See start and end option.)

And, the constructor accepts options.

var startElement = document.getElementById('element-1'),
  endElement = document.getElementById('element-2');

// New leader line has red color and size 8.
new LeaderLine(startElement, endElement, {color: 'red', size: 8});

ex-060

Also, the options can be accessed via properties of the instance (readable and writable).

var line = new LeaderLine(startElement, endElement);
line.color = 'red'; // Change the color to red.
line.size++; // Up size.
console.log(line.size); // Output current size.

You can change the style of the leader line via color, size, outlineColor, and more options.

new LeaderLine(startElement, endElement, {
  color: '#fff',
  outline: true,
  endPlugOutline: true,
  endPlugSize: 1.5
});

ex-070

You can add effects to the leader line via some options.

new LeaderLine(element1, element2, {
  startPlugColor: '#1a6be0',
  endPlugColor: '#1efdaa',
  gradient: true
});
new LeaderLine(element2, element3, {dash: {animation: true}});
new LeaderLine(element4, element5, {dropShadow: true});
new LeaderLine(element5, element6, {dash: true});

ex-080

You can change symbols that are shown at the end of the leader line via startPlug and endPlug options.

new LeaderLine(startElement, endElement, {
  startPlug: 'square',
  endPlug: 'hand'
});

ex-090

You can indicate a point or area of an element instead of the element via pointAnchor or areaAnchor attachment. You can indicate a point or area of the document also.

You can specify additional labels via startLabel, middleLabel and endLabel options. Also, captionLabel and pathLabel attachments can be specified as labels.

new LeaderLine(
  startElement1,
  LeaderLine.pointAnchor(endElement, {
    x: 60,
    y: 20
  }),
  {endLabel: LeaderLine.pathLabel('This is additional label')}
);

new LeaderLine(
  startElement2,
  LeaderLine.areaAnchor(endElement, {
    x: 80,
    y: 60,
    width: 50,
    height: 80,
  }),
  {endLabel: 'This is additional label'}
);

ex-100

You can show and hide the leader line with effects by show and hide methods.
mouseHoverAnchor attachment allows it to implement showing and hiding with mouse moving, easily.

new LeaderLine(LeaderLine.mouseHoverAnchor(startElement), endElement);

ex-110

For more details, refer to the following.

Constructor

line = new LeaderLine(options)

Or

line = new LeaderLine(start, end[, options])

The options argument is an Object that can have properties as options. hide option also can be contained.

The start and end arguments are shortcuts to options.start and options.end. The following two codes work same.

new LeaderLine({start: element1, end: element2});
new LeaderLine({start: element3, end: element4, color: 'red'});
new LeaderLine(element1, element2);
new LeaderLine(element3, element4, {color: 'red'});

The instance has properties that have the same name as each option to get or set those values (other than hide option).

var line = new LeaderLine(startElement, endElement);

upButton.addEventListener('click', function() {
  if (line.size < 20) { line.size++; }
}, false);

downButton.addEventListener('click', function() {
  if (line.size > 4) { line.size--; }
}, false);

If you want to set multiple options after it was constructed, using setOptions method instead of the properties may give better performance.

When you will do something about HTML document regardless of the LeaderLine, you typically do that after the HTML document is ready (i.e. the HTML document has been loaded and parsed by web browser).
For example:

// Wait for HTML document to get ready
window.addEventListener('load', function() { // NOT `DOMContentLoaded`
  // Do something about HTML document
  var line = new LeaderLine(
    document.getElementById('start'),
    document.getElementById('end')
  );
});

If you don't wait for HTML document to be ready, you might not be able to get a target element yet, or problems with incomplete layout may occur. Also, you should do so asynchronous like the above for the performance because synchronous code blocks parsing HTML.

hide option

Only the constructor accepts hide option. That is, the instance doesn't have hide property. (Note that the instance has hide method.)
If true is specified, the leader line is not shown, it is shown by show method.
This is used to hide it without using hide method, it is not shown at all until show method is called.

// The leader line is never shown until the button is clicked.
var line = new LeaderLine(startElement, endElement, {hide: true});
button.addEventListener('click', function() { line.show(); });

Methods

setOptions

self = line.setOptions(options)

Set one or more options.
The options argument is an Object that can have properties as options.

Since this method updates a view only once after it sets all specified options, it may give better performance than setting options via the properties when multiple options are set to the instance that already exists.

show, hide

self = line.show([showEffectName[, animOptions]])
self = line.hide([showEffectName[, animOptions]])

Show or hide the leader line.

var line = new LeaderLine(startElement, endElement, {hide: true});
showButton.addEventListener('click', function() { line.show(); }, false);
hideButton.addEventListener('click', function() { line.hide(); }, false);

showEffectName

Type: string
Default: Value that was specified last time, or fade at first time

One of the following keywords as effect:

  • none
  • fade
    Default animOptions: {duration: 300, timing: 'linear'}
  • draw
    Default animOptions: {duration: 500, timing: [0.58, 0, 0.42, 1]}

animOptions

Type: Object
Default: See above

An Object that can have properties as Animation Options.

position

self = line.position()

Re-position the leader line with current position and size of the elements as start or end option.
By default, the position of each leader line is fixed automatically when the window that loads LeaderLine was resized. You should call position method if your web page moved or resized the elements without resizing the window. For example, animation, a box that was scrolled or <iframe> that was resized.

scrollableBox.addEventListener('scroll', AnimEvent.add(function() {
  line.position();
}), false);

(The code above uses AnimEvent for a better performance.)

If you want to disable the fixing the position automatically, set LeaderLine.positionByWindowResize to false.

remove

line.remove()

Remove the leader line from the web page. It can't be used anymore.

Options

The following options are specified by constructor or setOptions method. And also, those are accessed via each property of instance.

start, end

Type: HTML/SVG element or Attachment

The leader line is drawn from the start element to the end element.

line.end = document.getElementById('end-element');

Any element that has bounding-box is accepted. For example, <div>, <button>, <ul>, <td>, <circle>, <text>, and also, elements in another window (i.e. <iframe>).

Note: if you want to handle elements in another window regardless of LeaderLine, you should understand about security.

Or you can specify an attachment instead of HTML/SVG element to indicate something.

color

Type: string
Default: 'coral'

A color (see Color Value) of the leader line.

line.color = 'rgba(30, 130, 250, 0.5)';

size

Type: number
Default: 4

The width of the leader line, in pixels.

line.size = 20;

path

Type: string
Default: 'fluid'

One of the following keywords to indicate how to draw the line:

  • straight
  • arc
  • fluid
  • magnet
  • grid

ex-180

startSocket, endSocket

Type: string
Default: 'auto'

The string to indicate which side of the element the leader line connects. It can be 'top', 'right', 'bottom', 'left' or 'auto'.

line.setOptions({startSocket: 'bottom', endSocket: 'top'});

If 'auto' (default) is specified, the closest side is chosen automatically.

startSocketGravity, endSocketGravity

Type: number, Array or string
Default: 'auto'

The force of gravity at a socket.

If a number is specified, the leader line is pulled in the direction of the socket. The number is pull strength.

line.startSocketGravity = 400;

If an Array that is coordinates [x, y] is specified, the leader line is pulled in the direction of the coordinates. The distance between the coordinates and [0, 0] is pull strength.
For example, if [50, -100] is specified, it is pulled in the direction of the rightward and upward (The strength in the Y axis direction is larger than the X axis direction). If [-50, 0] is specified, it is pulled in the direction of the leftward (no strength in the Y axis direction).

For example, parabola:

line.setOptions({
  startSocketGravity: [192, -172],
  endSocketGravity: [-192, -172]
});

If 'auto' (default) is specified, it is adjusted to gravity suitable for current path option automatically.

startPlug, endPlug

Type: string
Default: startPlug: 'behind' | endPlug: 'arrow1'

One of the following keywords to indicate type of plug (symbol that is shown at the end of the leader line):

ex-220

startPlugColor, endPlugColor

Type: string
Default: 'auto'

Each option for when a plug that accepts this option is specified for startPlug/endPlug option.

A color (see Color Value) of a plug.
It is painted separately from the line (i.e. Those don't overlap each other). Therefore one of color and startPlugColor/endPlugColor or both options can have an alpha channel.

lineA.setOptions({ // element-1, element-2
  color: 'rgba(30, 130, 250, 0.5)', // translucent
  startPlugColor: 'rgb(241, 76, 129)',
  endPlugColor: 'rgba(241, 76, 129, 0.5)' // translucent
});

lineB.setOptions({ // element-3, element-4
  color: 'rgb(30, 130, 250)',
  startPlugColor: 'rgb(241, 76, 129)',
  endPlugColor: 'rgba(241, 76, 129, 0.5)' // translucent
});

If 'auto' (default) is specified, a value of color option is set synchronously (i.e. it is changed when color was changed).

startPlugSize, endPlugSize

Type: number
Default: 1

Each option for when a value other than behind is specified for startPlug/endPlug option.

A multiplying factor of the size of a plug.
The plugs are resized synchronously, with the following options that contain size:

Plug Size: size * [default-plug-scale] * [startPlugSize or endPlugSize]

new LeaderLine(element1, element2, {
  startPlug: 'arrow1',
  size: 4,
  startPlugSize: 1,
  endPlugSize: 2
});

new LeaderLine(element3, element4, {
  startPlug: 'arrow1',
  size: 8,
  startPlugSize: 1,
  endPlugSize: 2
});

outline

Type: boolean
Default: false

If true is specified, an outline of the leader line is enabled.

line.outline = true;

outlineColor

Type: string
Default: 'indianred'

An option for when true is specified for outline option.

A color (see Color Value) of an outline of the leader line.
It is painted separately from inside of the line (i.e. Those don't overlap each other). Therefore one of color and outlineColor or both options can have an alpha channel.

lineA.setOptions({ // element-1, element-2
  color: 'rgb(248, 205, 30)',
  outlineColor: 'rgb(30, 130, 250)'
});

lineB.setOptions({ // element-3, element-4
  color: 'rgba(248, 205, 30, 0.5)', // translucent
  outlineColor: 'rgba(30, 130, 250, 0.5)' // translucent
});

lineC.setOptions({ // element-5, element-6
  color: 'rgba(248, 205, 30, 0.5)', // translucent
  outlineColor: 'rgb(30, 130, 250)'
});

lineD.setOptions({ // element-7, element-8
  color: 'rgb(248, 205, 30)',
  outlineColor: 'rgba(30, 130, 250, 0.5)' // translucent
});

outlineSize

Type: number
Default: 0.25

An option for when true is specified for outline option.

A multiplying factor of the size of an outline of the leader line, it is greater than 0 and is less than or equal to 0.48.
The outline is resized synchronously, with the following options that contain size:

Outline Size: size * outlineSize

lineA.setOptions({ // element-1, element-2
  size: 12,
  outlineSize: 0.4
});

lineB.setOptions({ // element-3, element-4
  size: 24,
  outlineSize: 0.08
});

startPlugOutline, endPlugOutline

Type: boolean
Default: false

Each option for when a plug that accepts this option is specified for startPlug/endPlug option.

If true is specified, an outline of the plug is enabled.

line.endPlugOutline = true;

startPlugOutlineColor, endPlugOutlineColor

Type: string
Default: 'auto'

Each option for when true is specified for startPlugOutline/endPlugOutline option.

A color (see Color Value) of an outline of the plug.
It is painted separately from inside of the plug (i.e. Those don't overlap each other). Therefore one of startPlugColor/endPlugColor and startPlugOutlineColor/endPlugOutlineColor or both options can have an alpha channel.

lineA.setOptions({ // element-1, element-2
  startPlugColor: 'rgb(248, 205, 30)',
  startPlugOutlineColor: 'rgb(30, 130, 250)',
  endPlugColor: 'rgba(248, 205, 30, 0.5)', // translucent
  endPlugOutlineColor: 'rgb(30, 130, 250)'
});

lineB.setOptions({ // element-3, element-4
  startPlugColor: 'rgb(248, 205, 30)',
  startPlugOutlineColor: 'rgba(30, 130, 250, 0.5)', // translucent
  endPlugColor: 'rgba(248, 205, 30, 0.5)', // translucent
  endPlugOutlineColor: 'rgba(30, 130, 250, 0.5)' // translucent
});

If 'auto' (default) is specified, a value of outlineColor option is set synchronously (i.e. it is changed when outlineColor was changed).

startPlugOutlineSize, endPlugOutlineSize

Type: number
Default: 1

Each option for when true is specified for startPlugOutline/endPlugOutline option.

A multiplying factor of the size of an outline of the plug, it is greater than or equal to 1 and is less than or equal to outlineMax that is shown in startPlug/endPlug option.
The outline is resized synchronously, with the following options that contain size:

Plug Outline Size: size * [default-plug-scale] * [startPlugSize or endPlugSize] * [default-plug-outline-scale] * [startPlugOutlineSize or endPlugOutlineSize]

lineA.setOptions({ // element-1, element-2
  size: 4,
  startPlugSize: 1.5,
  startPlugOutlineSize: 2.5,
  endPlugSize: 3,
  endPlugOutlineSize: 1
});

lineB.setOptions({ // element-3, element-4
  size: 10,
  startPlugSize: 1.5,
  startPlugOutlineSize: 1,
  endPlugSize: 3,
  endPlugOutlineSize: 2.5
});

startLabel, middleLabel, endLabel

Type: string or Attachment
Default: ''

An additional label that is shown on the leader line.

new LeaderLine(startElement, endElement, {
  startLabel: 'START',
  middleLabel: 'MIDDLE',
  endLabel: 'END'
});

Or you can specify an attachment instead of a string.

dash (effect)

Type: boolean or Object
Default: false

Enable the effect with specified Object that can have properties as the following options.
Or true to enable it with all default options.

new LeaderLine(startElement, endElement, {dash: true});

len, gap

Type: number or string
Default: 'auto'

The size of parts of the dashed line, in pixels.
len is length of drawn lines, gap is gap between drawn lines.

If 'auto' (default) is specified, the following each value is set synchronously (i.e. it is changed when size was changed).

len: size * 2 gap: size

new LeaderLine(element1, element2, {
  dash: {len: 4, gap: 24}
});

new LeaderLine(element3, element4, {
  size: 8,
  dash: true // len: 16, gap: 8
});

animation

Type: boolean or Object
Default: false

An Object that can have properties as Animation Options to animate the effect.
Or true to animate it with the following default options.

Default Animation Options: {duration: 1000, timing: 'linear'}

new LeaderLine(startElement, endElement, {dash: {animation: true}});

gradient (effect)

Type: boolean or Object
Default: false

Enable the effect with specified Object that can have properties as the following options.
Or true to enable it with all default options.

new LeaderLine(startElement, endElement, {startPlugColor: '#a6f41d', gradient: true});

startColor, endColor

Type: string
Default: 'auto'

The start color (see Color Value) and end color of the gradient.

If 'auto' (default) is specified, each value of startPlugColor and endPlugColor is set synchronously (i.e. it is changed when startPlugColor/endPlugColor was changed).

lineA.setOptions({ // element-1, element-2
  gradient: {
    startColor: '#2e17c3',
    endColor: '#1df3f9'
  }
});

lineB.setOptions({ // element-3, element-4
  gradient: {
    startColor: 'rgba(17, 148, 51, 0.1)',
    endColor: 'rgb(17, 148, 51)'
  }
});

Since the gradient is made from only two colors, it might be not beautiful.

dropShadow (effect)

Type: boolean or Object
Default: false

Enable the effect with specified Object that can have properties as the following options.
Or true to enable it with all default options.

new LeaderLine(startElement, endElement, {dropShadow: true});

dx, dy

Type: number
Default: dx: 2 | dy: 4

The offset X and offset Y of the drop shadow, in pixels.

line.setOptions({
  color: '#f7f5ee',
  dropShadow: {dx: 0, dy: 3}
});

blur

Type: number
Default: 3

The standard deviation for the blur operation in the drop shadow.

line.setOptions({
  dropShadow: {
    dx: 6,
    dy: 8,
    blur: 0.2
  }
});

color

Type: string
Default: '#000'

A color (see Color Value) of the drop shadow.
An alpha channel can be contained but opacity option should be used instead.

new LeaderLine(startElement, endElement, {dropShadow: {color: 'blue', dx: 0, dy: 0}});

opacity

Type: number
Default: 0.8

A number ranging from 0 to 1 to indicate the transparency of the drop shadow.

Attachments

Attachments are passed to the leader line via some options, and those make that option have special behavior.

You can get new attachment instance by individual static methods of LeaderLine (not instance methods).
For example, LeaderLine.pointAnchor method makes new pointAnchor attachment instance. It is attached to the leader line via start or end option of the leader line.
The following code passes a new pointAnchor attachment instance to LeaderLine constructor, via second argument as end option.

new LeaderLine(startElement, LeaderLine.pointAnchor(endElement));

In the case of the plan to use the attachment afterward.

var attachment = LeaderLine.pointAnchor(endElement);

function attach() {
  line.end = attachment;
}

pointAnchor

attachment = LeaderLine.pointAnchor(options)

Or

attachment = LeaderLine.pointAnchor(element[, options])

An attachment that is specified instead of an element for the start or end option of the leader line, for indicating a point instead of the element.

new LeaderLine(startElement, LeaderLine.pointAnchor(endElement));

The options argument is an Object that can have properties as options that are described later.

The element argument is shortcut to options.element. The following two codes work same.

attachment1 = LeaderLine.pointAnchor({element: element1});
attachment2 = LeaderLine.pointAnchor({element: element2, x: 16, y: 32});
attachment1 = LeaderLine.pointAnchor(element1);
attachment2 = LeaderLine.pointAnchor(element2, {x: 16, y: 32});

This attachment can be shared between multiple leader lines.

// A new attachment instance is shared between `line1` and `line2`.
line1.end = line2.end = LeaderLine.pointAnchor(endElement);
line1.end = LeaderLine.pointAnchor(endElement);

function share() {
  // The `line1`'s attachment instance is shared with `line2`.
  line2.end = line1.end;
}

After the attachment was attached by start or end option of the leader line, when something else is specified for that option, the leader line is detached from the attachment. When the last leader line is detached, the attachment is removed from the web page automatically, and it can't be used anymore.

element

Type: HTML/SVG element

An element that is a base of the point. See x and y options.
You can specify a <body> element also. That is, you can make the leader line indicate anywhere in the document.

x, y

Type: number or string
Default: '50%'

The X and Y coordinates of the point, in pixels, relative to the top-left corner of the specified element for element option.
Each value can be a percentage of the element's width or height. For example, {x: '50%', y: '50%'} (default) indicates the center of the element, {x: '100%', y: 0} indicates the top-right corner.
And also, each value can be a negative value or a value over the element's width or height, it indicates the outside of the element.

new LeaderLine(element1, LeaderLine.pointAnchor(element3, {x: 10, y: 30}));
new LeaderLine(element2, LeaderLine.pointAnchor(element3, {x: '100%', y: 0}));

areaAnchor

attachment = LeaderLine.areaAnchor(options)

Or

attachment = LeaderLine.areaAnchor(element[, shape][, options])

An attachment that is specified instead of an element for the start or end option of the leader line, for indicating an area instead of the element.

new LeaderLine(startElement, LeaderLine.areaAnchor(endElement));

The options argument is an Object that can have properties as options that are described later.

The element and shape arguments are shortcuts to options.element and options.shape. The following two codes work same.

attachment1 = LeaderLine.areaAnchor({element: element1});
attachment2 = LeaderLine.areaAnchor({element: element2, color: 'red'});
attachment3 = LeaderLine.areaAnchor({element: element3, shape: 'circle'});
attachment4 = LeaderLine.areaAnchor({element: element4, shape: 'circle', color: 'red'});
attachment1 = LeaderLine.areaAnchor(element1);
attachment2 = LeaderLine.areaAnchor(element2, {color: 'red'});
attachment3 = LeaderLine.areaAnchor(element3, 'circle');
attachment4 = LeaderLine.areaAnchor(element4, 'circle', {color: 'red'});

This attachment can be shared between multiple leader lines. See pointAnchor attachment for the sharing and the life cycle.

element

Type: HTML/SVG element

An element that is a base of the area. See x, y, width and height options.
You can specify a <body> element also. That is, any area in the document can be indicated.

shape

Type: string
Default: 'rect'

One of the following keywords to indicate the shape of the area:

  • rect
  • circle
  • polygon

ex-450

x, y

Type: number or string
Default: '-5%'

An option for when rect or circle is specified for shape option.

The X and Y coordinates for the top-left corner of the area, in pixels, relative to the top-left corner of the specified element for element option.
Each value can be a percentage of the element's width or height. For example, {x: '50%', y: '50%'} indicates the center of the element, {x: '100%', y: 0} indicates the top-right corner.
And also, each value can be a negative value or a value over the element's width or height, it indicates the outside of the element.

width, height

Type: number or string
Default: '110%'

An option for when rect or circle is specified for shape option.

The width and height of the area, in pixels.
Each value can be a percentage of the element's width or height. For example, {x: '50%', y: 0, width: '50%', height: '100%'} indicates the right half of the element.
And also, each value can be a value over the element's width or height.

radius

Type: number
Default: 0

An option for when rect is specified for shape option.

The radius to round corners of the area, in pixels.

new LeaderLine(startElement,
  LeaderLine.areaAnchor(endElement,
    {x: '20%', y: '20%', width: '60%', height: '60%', radius: 10}));

points

Type: Array

An option for when polygon is specified for shape option.

An Array that contains three or more points of the polygon. Each item that is a point is an Array that contains the X and Y coordinates for the point. That is, it is Array that contains Array, like [[x1, y1], [x2, y2], ...].
The X and Y coordinates are handled as same as x and y options.

new LeaderLine(startElement,
  LeaderLine.areaAnchor(endElement,
    {shape: 'polygon', points: [[10, 15], ['90%', '70%'], [10, '80%']]}));

color

Type: string
Default: color of current first attached leader line (synchronously)

A color (see Color Value) of the border of the area.
By default, a value of color option of the first leader line in current attached leader lines is set synchronously (i.e. it is changed when color of the leader line was changed).

fillColor

Type: string
Default: ''

A fill-color (see Color Value) of the area.
If it is not specified (default), the area is not painted. It is better than specifying 'rgba(0, 0, 0, 0)'.

new LeaderLine(startElement,
  LeaderLine.areaAnchor(endElement,
    {x: 14, y: 20, width: 42, height: 60, radius: 10, fillColor: '#f8cd1e'}));

size

Type: number
Default: size of current first attached leader line (synchronously)

The width of the border of the area, in pixels.
If 0 is specified, the border is not drawn.

new LeaderLine(startElement,
  LeaderLine.areaAnchor(endElement,
    {shape: 'polygon', points: [[10, 15], [63, 70], [10, 80]],
      fillColor: '#f8cd1e', size: 0}));

dash

Type: boolean or Object
Default: false

Enable "dashed line" effect to the border of the area with specified Object that can have properties as the following options.
Or true to enable it with all default options.

new LeaderLine(startElement,
  LeaderLine.areaAnchor(endElement,
    {x: 14, y: 20, width: 42, height: 60, radius: 8, dash: true}));
len, gap

Type: number
Default: len: size * 2 (synchronously) | gap: size (synchronously)

The size of parts of the dashed line, in pixels.
len is length of drawn lines, gap is gap between drawn lines.

mouseHoverAnchor

attachment = LeaderLine.mouseHoverAnchor(options)

Or

attachment = LeaderLine.mouseHoverAnchor(element[, showEffectName][, options])

An attachment that is specified instead of an element for the start or end option of the leader line, for showing and hiding the leader line by the mouse hovering.
This is a convenient way to call show method when a mouse enters the element, and call hide method when a mouse leaves the element. Also, a small icon and some style are added to the element.
And also, it includes a polyfill for mouseenter and mouseleave events that are not supported by some web browsers.

new LeaderLine(LeaderLine.mouseHoverAnchor(startElement), endElement);

This is an attachment to provide a convenient way to do the behavior above. If you want more style or more custom behavior, you will use show/hide methods and your CSS code instead of this attachment.

The options argument is an Object that can have properties as options that are described later.

The element and showEffectName arguments are shortcuts to options.element and options.showEffectName. The following two codes work same.

attachment1 = LeaderLine.mouseHoverAnchor({element: element1});
attachment2 = LeaderLine.mouseHoverAnchor({element: element2, style: {color: 'red'}});
attachment3 = LeaderLine.mouseHoverAnchor({element: element3, showEffectName: 'draw'});
attachment4 = LeaderLine.mouseHoverAnchor(
  {element: element4, showEffectName: 'draw', style: {color: 'red'}});
attachment1 = LeaderLine.mouseHoverAnchor(element1);
attachment2 = LeaderLine.mouseHoverAnchor(element2, {style: {color: 'red'}});
attachment3 = LeaderLine.mouseHoverAnchor(element3, 'draw');
attachment4 = LeaderLine.mouseHoverAnchor(element4, 'draw', {style: {color: 'red'}});

This attachment can be shared between multiple leader lines. See pointAnchor attachment for the sharing and the life cycle.

element

Type: HTML element

An element that is a trigger for showing and hiding the leader line.

showEffectName

Type: string
Default: Value that was specified last time, or fade at first time

A value that is passed to show/hide methods as its showEffectName argument.

new LeaderLine(LeaderLine.mouseHoverAnchor(startElement, 'draw'), endElement);

animOptions

Type: Object
Default: See showEffectName of show/hide methods

A value that is passed to show/hide methods as its animOptions argument.

style

Type: Object
Default: undefined

An Object that has additional style properties for the element.
You can specify null as a property to disable adding the style property. Note that it doesn't disable the style property. For example, if {backgroundColor: null} is specified, the attachment doesn't change current backgroundColor style property of the element.

hoverStyle

Type: Object
Default: undefined

This works same to style option except that these style properties are added when a mouse enters the element.

onSwitch

Type: function
Default: undefined

A function that is called after show/hide method, with an event argument.

captionLabel

attachment = LeaderLine.captionLabel(options)

Or

attachment = LeaderLine.captionLabel(text[, options])

An attachment that is specified instead of a string for the startLabel, middleLabel or endLabel option of the leader line, for showing a custom label on the leader line.

new LeaderLine(startElement, endElement, {
  startLabel: LeaderLine.captionLabel('START'),
  middleLabel: LeaderLine.captionLabel('MIDDLE'),
  endLabel: LeaderLine.captionLabel('END')
});

The options argument is an Object that can have properties as options that are described later.

The text argument is shortcut to options.text. The following two codes work same.

attachment1 = LeaderLine.captionLabel({text: 'LABEL-1'});
attachment2 = LeaderLine.captionLabel({text: 'LABEL-2', color: 'red'});
attachment1 = LeaderLine.captionLabel('LABEL-1');
attachment2 = LeaderLine.captionLabel('LABEL-2', {color: 'red'});

This attachment can not be shared between multiple leader lines.
When the attachment that was already attached is attached to another leader line, then, the former leader line is detached automatically. That is, the attachment moves from the leader line to another leader line.

// A new attachment instance is attached to `line1`.
line1.endLabel = LeaderLine.captionLabel('LABEL-1');

// The attachment is attached to `line2`, then, `line1` is detached.
line2.endLabel = line1.endLabel;

Also, it can move between labels of the same leader line.

// The attachment is moved from `endLabel` to `startLabel`.
line1.startLabel = line1.endLabel;

When the leader line is detached from the attachment and any leader line is not attached, the attachment is removed from the web page automatically, and it can't be used anymore.

text

Type: string

A string that is shown as a label.

offset

Type: Array
Default: Calculated suitable position

By default, a captionLabel attachment that is attached as startLabel is positioned near the socket (i.e. connecting point) that is decided by startSocket option of the leader line. In like manner, attached one as endLabel is positioned near the socket that is decided by endSocket option. Those are calculated with the size of the leader line, the font size of the label, etc.

If an Array that is [x, y] in pixels is specified for offset option, the attachment is positioned at the specified coordinates relative to the decided socket.

new LeaderLine(startElement, endElement, {
  startLabel: LeaderLine.captionLabel('START', {color: 'blue', offset: [-20, 0]})
});

lineOffset

Type: number
Default: 0

By default, a captionLabel attachment that is attached as middleLabel is positioned at the middle point of the path of the leader line.
If a length in pixels is specified for lineOffset option, the attachment is positioned at the offset point from the middle point of the path. The length is distance along the path, a negative value makes it become close to the element as start option.

color

Type: string
Default: color of current attached leader line (synchronously)

A color (see Color Value) of the text.
By default, a value of color option of the current attached leader line is set synchronously (i.e. it is changed when color of the leader line was changed).

outlineColor

Type: string
Default: '#fff'

A color (see Color Value) of an outline of the text.
The outline makes the text avoid seeming to blend with a background.
If '' is specified, the outline is not drawn. It is better than specifying 'rgba(0, 0, 0, 0)'.

Other Style Properties

You can specify the following CSS properties also:

  • fontFamily
  • fontStyle
  • fontVariant
  • fontWeight
  • fontStretch
  • fontSize
  • fontSizeAdjust
  • kerning
  • letterSpacing
  • wordSpacing
  • textDecoration

Note that some properties might not be supported by some web browsers, LeaderLine doesn't care for those.

pathLabel

attachment = LeaderLine.pathLabel(options)

Or

attachment = LeaderLine.pathLabel(text[, options])

An attachment that is specified instead of a string for the startLabel, middleLabel or endLabel option of the leader line, for showing a label along the path of the leader line.

new LeaderLine(startElement, endElement, {
  startLabel: LeaderLine.pathLabel('START'),
  middleLabel: LeaderLine.pathLabel('MIDDLE'),
  endLabel: LeaderLine.pathLabel('END')
});

The options argument is an Object that can have properties as options that are described later.

The text argument is shortcut to options.text. The following two codes work same.

attachment1 = LeaderLine.pathLabel({text: 'LABEL-1'});
attachment2 = LeaderLine.pathLabel({text: 'LABEL-2', color: 'red'});
attachment1 = LeaderLine.pathLabel('LABEL-1');
attachment2 = LeaderLine.pathLabel('LABEL-2', {color: 'red'});

This attachment can not be shared between multiple leader lines. See captionLabel attachment for the sharing and the life cycle.

Note that the characters are put along the path of the leader line from the start element toward the end element even if the path curves sharply or it is drawn toward the left. If you have to avoid those cases for important text, use captionLabel instead.

text

Type: string

A string that is shown as a label.

lineOffset

Type: number
Default: 0

By default, a pathLabel attachment that is attached as startLabel is positioned near the element as start option. In like manner, attached one as endLabel is positioned near the element as end option. And attached one as middleLabel is positioned at the middle point of the path of the leader line.
If a length in pixels is specified for lineOffset option, the attachment is positioned at the offset point from the position above. The length is distance along the path, a negative value makes it become close to the element as start option.

color

Type: string
Default: color of current attached leader line (synchronously)

A color (see Color Value) of the text.
By default, a value of color option of the current attached leader line is set synchronously (i.e. it is changed when color of the leader line was changed).

outlineColor

Type: string
Default: '#fff'

A color (see Color Value) of an outline of the text.
The outline makes the text avoid seeming to blend with a background.
If '' is specified, the outline is not drawn. It is better than specifying 'rgba(0, 0, 0, 0)'.

Other Style Properties

See the option of captionLabel.

Animation Options

duration

Type: number

A number determining how long (milliseconds) the animation will run.

new LeaderLine(
  LeaderLine.mouseHoverAnchor(startElement, 'draw', {
    animOptions: {
      duration: 3000
    }
  }),
  endElement
);

timing

Type: Array or string

An Array that is [x1, y1, x2, y2] as a "timing function" that indicates how to change the speed. It works same as that of CSS animation.

new LeaderLine(
  LeaderLine.mouseHoverAnchor(startElement, 'draw', {
    animOptions: {
      duration: 3000,
      timing: [0.5, 0, 1, 0.42]
    }
  }),
  endElement
);

You can specify one of the following keywords also. These values mean keywords for common timing functions.

  • ease
  • linear
  • ease-in
  • ease-out
  • ease-in-out

Color Value

CSS color notations are accepted. A value might contain an alpha channel that specifies the transparency.
For example, hsl(200, 70%, 58%), rgba(73, 172, 223, 0.5), #49acdf, skyblue, etc. Some web browsers support hwb(), device-cmyk() and gray() also.


Thanks for images: Brain & Storm, Michael Gaida, CGvector

leader-line's People

Contributors

anseki avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

leader-line's Issues

Import in create-react-app

Is there a way to use this library with create-react-app ?
The only way I was able to use it was including it in a script tag inside index.html
and then inside of my component I had to use window.LeaderLine
This seems very hacky

Events for line

Is there events such as hover, click for a line?

I want to show 'delete icon' when hover on a line and remove a line when click on a line.

Unable to draw lines on a popup window

Hi,

First of all this is an amazing library, but i noticed that we are not able to draw lines on a popup window. The line is drawn below it , is there a workaround for this ?.

Thanks in advance.

Zoom Help

I am using a zoom function on my app. It is updating the Leader Line with the position method, but when you're zooming out things turn funny... see image.

I think a fix would be to constrain Leader Line to the div that is zooming so that they zoom out with the rest of the page, is this possible? Any thoughts?

Screenshot 2019-03-22 at 11 52 26

Cant get unminified JS

Hi Can you please provide the Unminified Js file.

I need it so I can attach a parent element to the leaderline

Targeting line intersections

I love your leader-line tool and have implemented it successfully. However, do have the ability to target intersections between lines? Like when these two things join you can call out another arrow to that point in time.

startSocketGravity and endSocketGravity limitations

leaderline

Hi, I am trying to achieve something like above(The connection from right of top box to left of bottom box) with startSocketGravity and endSocketGravity. But All I could achieve is this.

Screenshot 2019-03-20 at 6 43 38 PM

If there is an option to add radius to grid path its also ok for me. After a lot of tweaking, I decided to ask for help from you guys. Could you please guide me? Thanks in advance.

can not import leader-line into vue-cli project

Hi anseki,
I use npm install --save leader-line to install the leader-line into my vue-cli project, but I can not import LeaderLine into the code, the LeaderLine is an empty object.
However, I also try to solve this issue according #8, but those two solution also does not work. I can not get the window.LeaderLine object, but I can get the window.LeaderLineAttachment object. What is the usage of it? And can I use it to get the LeaderLine object?
Looking forward to your help.
Thanks and Best Regards,
West.

Panzoom help.

Hi Anseki,

This library is excellent no issues no bugs, just perfect.

I need a help for my project.
I'm using 'panzoom' library to zoom and drag elements in side a div.
i have to draw a line between two elements, using your excellent library i drew line.
but the problem is after i zoom out or drag the elements aside, the drawn line is still stuck there.
i know issue is not with leaderline library.
but would need your help to fix the issue.

Thank You

How to move an object along a path?

thanks for his lib. it's great in what it does.

I'd like to leave a line in place for a while once drawn and move an object along it. I think I can do this pretty easy if I can get the path back from this library for a line created. Any advice?

Package for use with bundlers

Thanks for the great library!

It would be nice to be able to import this into a larger app being bundled by webpack/rollup. Right now the global variable that's defined requires a legacy library plugin to get it in.

Leader line in VueJS?

I installed leader line via npm, then imported and used it in my main.js file; however, when I try to then draw a line between two elements after the app has been mounted I receive this error:
[Vue warn]: Error in mounted hook: "TypeError: __WEBPACK_IMPORTED_MODULE_1__assets_leaderLine_js__.default is not a constructor"

main.js file has:

import LeaderLine from 'leader-line'
Vue.use(LeaderLine)

How to use `pointAnchor` attachment (and `containment` of PlainDraggable)

Hi again Anseki!

One more time, thanks and congratulations for creating and sharing with us this amazing tool. I am sorry to bother you again with doubts and questions.
I was wondering if would be possible to use the lines with another draggable library. I found one called Displace.js that is very simple to use and lightweight, and it was easier to get for example my draggable element to go outside its div. I was trying to do the same with PlainDraggable and didn't get, unfortunately.

I need to build something like this:
https://codepen.io/A8-XPs/pen/JwWLOd

They need to be interactive for the user, which will be able to hold a circle and drag wherever he wants on the website, and the lines need to follow the circle, creating a deformation in the shape (this is intended). I can't find a way to make the circles to go outside their parent div (this is the first difficult). Also, the circles must be still draggable after we bring it outside of its parent div.

I apologize again to disturb you and would like to kindly request the possibility of help with samples.
I have those shapes in SVG but also considered to make them with span elements in absolute position, I planned to make the lines (paths) to be created with the leader-line and of course the visual result would be similar but not exactly the same (I can define the svg paths to be transparent).

I had difficult to create shared draggable elements, as the example that I sent you some of the circles are the start point for multiple items. Or, in the oval shape example the start point of a line is the end point to another.

Thank you very much!

How to permanently delete lines from DOM?

Hi,

I am trying to delete all leader-lines before unmounting a component in React.
After successfully invoking line.remove() and unmounting the component the lines are actually gone (they have been removed from the DOM).

But on screen resize the console gets full of:

A disconnected element was passed. index.js:2178
TypeError: n is null. leader-line.js:4:12474

As I understand, the lines (which supposedly do not exist anymore) are trying to find out the position of their respective start and end anchors (which of course do not exist anymore, since the component containing them has been unmounted).

Why could this be happening?
Isn't the method remove() the correct way to get rid of the lines?

Thanks a lot in advance!

Allowing for arrows to be draggable?

Is there any way for these arrows to be draggable? So I'd love to be able to drag the arrow pointer from one div to the next. I am creating a mapping tool of source columns to destination columns, so this would be very useful.

leadline does not stick to main endpoint

Hi,

I have implemented your great leader-line tool. but when i am using it and i suddenly change the position of the connecting endpoint the line doesn't move but instead it draws a new line. please could you help so if the endpoint position moves by and element getting smaller or any other element change size the leadline sticks to the endpoint

Thank you
Mand

Custom data to svg dom

could I add custom data like data-user property to the svg dom like this?

const leaderLineOptions = {
  "data-user": 'my_user_id'
};
new LeaderLine(start, end, leaderLineOptions);

Does not work in Firefox for me

Demo does not in FF, throws this error:
TypeError: Value being assigned to SVGRect.x is not a finite floating-point value

Can you define where in the DOM the line is added?

I'm having some issues with z-index because the lines are added to the body element and I have a div that needs to pop up over everything on the page. I can't get it to work as the lines will always be on top. I could hide the lines but the div needs to be semi-transparent and I'd prefer if they could appear through the background.

If I could append the lines to a certain element that would solve the problem but I can't seem to find anywhere to do this.

Multiple lines positioning.

Hello

I really like this plugin as its perfect for a small project i'm working on. Does 95% of the job very well except for 1 little issue. I have a fixed menu on the left which can be expanded / collapsed as desired. When this menu expands / collapses, the lines become disconnected with their elements. If it were 1 line i would have used the position method to update its position. but i will have 10s of lines between different elements. How can i update position of all these at once when menu is opened / closed? I have the screen shots attached to show before and after menu opens and closes.

Also, i want to add some extra classes to the SVG elements (add to leader-line class). How can i do this?

Thanks for your help and support.

issue-1
issue-2

Set z-index for the plugs an lines separately

Hi, This is more of a question than an issue. Is there an option to set z-index for the line and plugs separately? Please check the image below. I had to set z-index of the leaderline higher than the rectangle html element in order for the plugs to appear above the element. But I dont want the line to appear over the element. Can anyone please help me? Thanks in advance.

Screenshot 2019-03-27 at 7 16 43 PM

Using Leader-line and PlainDraggable together

Hi!

First, I'd like to say that this library is pretty cool and the documentation is great as well. The only aspect I am missing is about working examples showing Leader-line and Plaindraggable working together. I applied both on my project and when I move the draggable object (SVG circle) the lines are staying fixed on their initial position. I don't understand this behavior, would you mind to help with that?

I tried to find coding examples everywhere but did't have success.

Thank you!

Viewport

There is no option to specify where to append the line SVG. It would be good to be able to specify a specific element / viewport to append the SVG.

This would fix the issue if you draw the lines inside a scrollable element. Currently if it is inside a scrollable element we have to run the position method while scrolling which isn't very performant and the line is drawn on top which makes it able to go outside the element instead of disappearing underneath while it is scrolled.

Typescript typings

Hey! Super cool library! :) I couldn't find any TypeScript typings, is there a possibility you're gonna include them in the npm package?

z-index of leader line

Hi,

More than an issue, I m finding a method to cntrol z-index of leader lines. In my case I have a custom developed modal appearing in front of page content, leader lines (while attached elements are behind the modal) are appearing in front of modal,

Can this be controlled with CSS ?

Best Regards

Indubuk

Offset plug from edge of element?

There may be an easy way to do this; if so, my apologies.

I'd like to find a way to offset a plug from its target element by an arbitrary amount of pixels. I see that you can set the x,y coords relative to element but without knowing which side or point the plug will be at there's not a way to find which direction the offset should be in.

Draw arrow from coming from and going to the same element

Is there any easy way to do this? Been playing around and by disabling the check I can get it to render an SVG but the it has no size, obviously due to the x/y coords being the same as it's the same element. Any painless ways to change the style to have an arch that returns to the element it came from?

Wrong lines

Wrong lines are drawn. This is corrected by resizing the window.

jQuery(window).scroll(function() {
   var hT = jQuery('#n-1').offset().top,
       hH = jQuery('n-1').outerHeight(),
       wH = jQuery(window).height(),
       wS = jQuery(this).scrollTop();
   if (wS > (hT+hH-wH)){
       
       line.show('draw', {
        animOptions: {
          duration: 3000,
          timing: [0.5, 0, 1, 0.42]
        }
       }
       ); 
       if (typeof(Event) === 'function') {
  window.dispatchEvent(new Event('resize'));
} else {

  var evt = window.document.createEvent('UIEvents'); 
  evt.initUIEvent('resize', true, false, window, 0); 
  window.dispatchEvent(evt);
}
   }
});

But it doesn't work on Safari.

Using this within a modal that's scrollable

I tried using this within a scrollable bootstrap modal, but since the svg's are seemingly attached to the body of the document, it does not scroll with the rest of the page. Is there a way around this?

Animation speed & stroke-linecap="round"

Hi,

I have two questions:

  • is there a way to manage the animation speed?
  • when setting dash to true, is it possible to set the stroke-linecap to round instead of square?

Thanks a lot in advance!

Custom plug style?

Is it possible to define a custom svg element to use as a plug? For instance, I have a sketchy-looking arrowhead I'd like to use.

Arrows don't respond to page scroll when pointed at static/sticky object

If you have something like a sticky footer and have an arrow pointing from element X on the page to element Y in the sticky footer, when the user scrolls down the page the arrow will stay bound to element X however the length will not change to accommodate the now greater distance between X and Y, therefore resulting in the arrow moving further and further away from Y the more you scroll.

Using with Webpack

您好,看了之前的Issues,#2,但是还是不是很清楚如何引用,你说的用skeleton-loader这个的作用是什么?不是很明白,我是直接npm安装,然后直接import,这样在vue created中使用是不可以的。还望指点,谢谢!

Browsersupport?

Which browsers does this lib support? I can't find anything in the readme?

Option for smoothing out grid paths

Hi, this is more a question than issue, but I'm curious if there's a straightforward way of making the 'bends' in the lines for the 'grid' paths more fluid? Otherwise I'm really enjoying this library, thanks!

basic example issue - start/end missing

Hi Team,
I am just trying this basic code and using http-server(https://www.npmjs.com/package/http-server) for hosting my app and facing below issue in console:

leader-line.min.js:2 Uncaught Error: `start` and `end` are required.
    at Ze (leader-line.min.js:2)
    at Ye.setOptions (leader-line.min.js:2)
    at new Ye (leader-line.min.js:2)
    at index.html:17

Here is my basic test code:

<html>
<head>
    <script src="leader-line.min.js"></script>
    <script>
        var startElement = document.getElementById('start'),
            endElement = document.getElementById('end');
        new LeaderLine(startElement, endElement, { color: 'red', size: 8 });
    </script>
</head>
<body>
    <div id="start">start</div>
    <div id="end">end</div>
</body>
</html>

Possibility of attaching events to leaderlines

Hi,

I went through the documentation and didnt come across about attaching events to lines. I mean events like right click, mouse over etc.

Are there event handling support added already or

is there a possibility to add 'id' attributes to lines, so then it is possible to bind events to individual lines.

Best Regards

Indunil

Tooltips

Hi!

Could you add support for tooltips? It would be much like the labels but would only show when hovering over the leader line.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.