npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

linemate

v0.1.6

Published

Render canvas lines between DOM nodes with ease.

Downloads

25

Readme

: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.

| Option | Description | Default |-----------------|-------------------------------|--------------------------- | cap | CanvasRenderingContext2D.lineCap | 'round' | color | CanvasRenderingContext2D.strokeStyle | '#000' | dashed | CanvasRenderingContext2D.setLineDash() | false | dashOffset | CanvasRenderingContext2D.lineDashOffset | 0 | dashSegments | CanvasRenderingContext2D.setLineDash() segments | [5, 15] | entryPoint | The location at which each node is entered by an incoming line (valid values outlined below) | 'center' | exitPoint | The location at which each node is exited by an outgoing line (valid values outlined below) | 'center' | join | CanvasRenderingContext2D.lineJoin | 'round' | miterLimit | CanvasRenderingContext2D.miterLimit | 10 | path | The path that the canvas lines take between nodes (valid values outlined below) | 'shortest' | width | CanvasRenderingContext2D.lineWidth | 1 | zIndex | The 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.