Code Monkey home page Code Monkey logo

svg-line's Introduction

svg-line Flattr this!experimental

Generate SVG paths for a line, d3-style.

Usage

svg-line

line = svgLine()

Creates a new line generator.

line(points)

Given an array of points, return an SVG path string that you can then use when setting a <path>'s d attribute.

By default, each point is assumed to be an object with an x and a y coordinate, e.g. to create a 1x1 square:

var line = require('svg-line')()
var svg = 'http://www.w3.org/2000/svg'
var d = line([
    { x: 0, y: 0 }
  , { x: 1, y: 0 }
  , { x: 1, y: 1 }
  , { x: 0, y: 1 }
])

var square = document.createElementNS(svg, 'path')

square.setAttribute('d', d)

line.x(getX)

Pass in a new function responsible for getting a point's x coordinate from a single element in the array.

The function is passed (d, i), where d is the element and i is its index in the array.

For example:

var svg  = 'http://www.w3.org/2000/svg'
var line = require('svg-line')()
  .x(function(d) { return d[0] })
  .y(function(d) { return d[1] })

var d = line([
    [0, 0]
  , [1, 0]
  , [1, 1]
  , [0, 1]
])

var square = document.createElementNS(svg, 'path')

square.setAttribute('d', d)

line.y(getY)

Much like line.x, pass in a new function responsible for getting a point's y corrdinate from a single element in the array.

License

MIT. See LICENSE.md for details.

svg-line's People

Contributors

hughsk avatar

Stargazers

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

Watchers

 avatar  avatar  avatar

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.