0.1.6 • Published 9 years ago

linemate v0.1.6

Weekly downloads
16
License
MIT
Repository
github
Last release
9 years ago

:dancers: linemate.js

Render canvas lines between DOM nodes with ease.

Installation

Install linemate with npm:

npm install --save linemate

No npm? Grab linemate.min.js from dist/ and include it in your project. No dependencies are needed, and linemate is made global.

Usage

Use linemate to render canvas lines between DOM nodes.

View Demo

Example

Connect three div elements with red, dashed lines. (Note that the options object is—as one might guess—optional. Defaults are detailed below.)

Provide elements to connect:

<div id="a" class="node"></div>
<div id="b" class="node"></div>
<div id="c" class="node"></div>

Use linemate.connect to add lines between the elements.

window.addEventListener('load', function(e) {
  // Render some canvas lines!
  linemate.connect('.node', {
    color: '#ff3300',
    dashed: true,
    width: 10
  });
});

linemate.connect

Connect nodes such that the last node is not connected back to the first node.

/*
 * Connect two or more DOM nodes without completeness.
 *
 * @param {Array[string]|string} nodes - Array of two or more selectors or a selector
 * @param {object} opts - An options object
 */
function connect(nodes, opts = {}) {
  // ...
}

linemate.complete

Connect nodes such that the last node is connected back to the first node.

/*
 * Connect two or more DOM nodes with completeness.
 *
 * @param {Array[string]|string} nodes - Array of two or more selectors or a selector
 * @param {object} opts - An options object
 */
function complete(nodes, opts = {}) {
  // ...
}

linemate.custom

Connect nodes with your own custom algorithm.

/*
 * Connect two or more DOM nodes with a custom
 * stroke algorithm.
 *
 * @param {Array[string]|string} nodes - Array of two or more selectors or a selector
 * @param {object} opts - An options object
 * @param {function} doStrokes - Custom stroke algorithm callback
 */
function custom(nodes, opts = {}, doStrokes) {
  // ...
}
Custom Example

In the custom stroke algorithm callback, we have access to the canvas context, the pnodes collection, and the options object. The pnodes collection consists of objects with an entryPoint and exitPoint—these are canvas coordinates for each node derived from the entryPoint and exitPoint options. Each point has an x and a y property.

In this example, we simply draw a wide pink line from the top left of the canvas (0, 0) to each node entry point.

linemate.custom('.node', {
  color: 'pink',
  width: 30
}, function(context, pnodes, options) {
  context.moveTo(0, 0);

  for(var i = 0, l = pnodes.length; i < l; i++) {
    var pnode = pnodes[i];

    context.lineTo(pnode.entryPoint.x, pnode.entryPoint.y);
    context.moveTo(0, 0);
  }
});

linemate.confine

By default, linemate will append the canvas to document.body. If the nodes that you wish to connect are positioned absolutely within some parent element other than document.body, you'll want to confine linemate to that parent node.

/*
 * Confine linemate to a common parent node
 * other than 'document.body'
 *
 * @param {string|Node} node - A selector or DOM node
 */
function confine(node) {
  // ...
}
Confine Example

If we have an absolutely positioned #container node, we can confine our canvas to this parent node with linemate.confine.

Child nodes within parent node:

<div id="container">
  <div id="a" class="node"></div>
  <div id="b" class="node"></div>
  <div id="c" class="node"></div>
</div>

Confine canvas to parent node:

linemate.confine('#container-1');
linemate.connect('.node');

linemate.clear

Remove canvas nodes inserted by linemate.

/*
 * Clear linemate canvases
 */
function clear() {
  // ...
}

linemate.defaults

Set custom default options.

/*
 * Set custom default options
 *
 * @param {object} custom - Custom linemate defaults
 */
function defaults(custom = {}) {
  // ...
}

Options

The options object can be used to modify canvas line styles, node entry and exit points, etc.

OptionDescriptionDefault
capCanvasRenderingContext2D.lineCap'round'
colorCanvasRenderingContext2D.strokeStyle'#000'
dashedCanvasRenderingContext2D.setLineDash()false
dashOffsetCanvasRenderingContext2D.lineDashOffset0
dashSegmentsCanvasRenderingContext2D.setLineDash() segments5, 15
entryPointThe location at which each node is entered by an incoming line (valid values outlined below)'center'
exitPointThe location at which each node is exited by an outgoing line (valid values outlined below)'center'
joinCanvasRenderingContext2D.lineJoin'round'
miterLimitCanvasRenderingContext2D.miterLimit10
pathThe path that the canvas lines take between nodes (valid values outlined below)'shortest'
widthCanvasRenderingContext2D.lineWidth1
zIndexThe z-index of the inserted canvas element-1

Valid values for the path option include the following:

  • 'shortest' (a straight line between an exitPoint and entryPoint)
  • 'square-v' (a vertical-first elbow between an exitPoint and entryPoint)
  • 'square-h' (a horizontal-first elbow between an exitPoint and entryPoint)

Valid values for entryPoint and exitPoint options include the following:

  • 'center'
  • 'topLeft'
  • 'top'
  • 'topRight'
  • 'right'
  • 'bottomRight'
  • 'bottom'
  • 'bottomLeft'
  • 'left'

Testing

Ensure that testing dependencies are installed:

npm install

Run tape tests with tape-run and faucet output.

npm run test

License

Copyright (c) 2015 Travis Loncar.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

0.1.6

9 years ago

0.1.5

9 years ago

0.1.4

9 years ago

0.1.3

9 years ago

0.1.2

9 years ago

0.1.1

9 years ago

0.1.0

9 years ago

0.0.1

9 years ago