Socket
Socket
Sign inDemoInstall

curved-arrows

Package Overview
Dependencies
0
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    curved-arrows

Draw S-curved arrows between points or shapes


Version published
Weekly downloads
948
increased by84.08%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

Curved Arrows

version license

A set of functions for drawing S-curved arrows between points and shapes.

demo animation

👉 Demo | How it works

You may also want to see steveruizok's perfect-arrows, it's smarter but the start point and the end point are less predictable.

Installation

npm i curved-arrows

or

yarn add curved-arrows

Usage

The functions in this library has similar arguments and return values to steveruizok's perfect-arrows. Notable differences are options and the return values containing two control points, one for start point and one end point, instead of one, to represent an S-curve.

The return values provide only the information needed to draw an arrow. You'll need to draw the arrow yourself using your technology of choice. See below for an example using React and SVG.

getArrow(x0, y0, x1, y1, options)

The getArrow function accepts the position of two points and returns an array containing this information:

  • four points: start, end, and two control points (one for start, one for end)
  • two angles: end and start

You can use this information to draw an S-curve and arrow heads. You can use the options object to tweak the return values.

const arrowHeadSize = 9
const arrow = getArrow(0, 0, 100, 200, {
  padStart: 0,
  padEnd: arrowHeadSize,
})
const [sx, sy, c1x, c1y, c2x, c2y, ex, ey, ae, as] = arrow
Arguments
ArgumentTypeDescription
x0numberThe x position of the starting point.
y0numberThe y position of the starting point.
x1numberThe x position of the ending point.
y1numberThe y position of the ending point.
optionsobjectAn (optional) object containing one or more of the options described below.
Options
OptionTypeDefaultDescription
padStartnumber0How far the arrow's starting point should be from the provided starting point.
padEndnumber0How far the arrow's ending point should be from the provided ending point.
controlPointStretchnumber50Enforce a minimal distance from the arrow's starting/ending point to the control points making the curve. (demo, more explanation, code)
Returns
ArgumentTypeDescription
sxnumberThe x position of the (padded) starting point.
synumberThe y position of the (padded) starting point.
c1xnumberThe x position of the control point of the starting point.
c1ynumberThe y position of the control point of the starting point.
c2xnumberThe x position of the control point of the ending point.
c2ynumberThe y position of the control point of the ending point.
exnumberThe x position of the (padded) ending point.
eynumberThe y position of the (padded) ending point.
aenumberThe angle (in degree) for an ending arrowhead.
asnumberThe angle (in degree) for a starting arrowhead.

getBoxToBoxArrow(x0, y0, w0, h0, x1, y1, w1, h1, options)

The getBoxToBoxArrow function accepts the position and dimensions of two boxes (or rectangles) and returns an array containing this information:

  • four points: start, end, and two control points (one for start, one for end)
  • two angles: end and start

You can use this information to draw an S-curve and arrow heads. You can use the options object to tweak the return values.

Note: The options and values returned by getBoxToBoxArrow are in the same format as the options and values for getArrow.

const arrowHeadSize = 9
const arrow = getArrow(0, 0, 200, 100, 300, 50, 200, 100, {
  padStart: 0,
  padEnd: arrowHeadSize,
})
const [sx, sy, c1x, c1y, c2x, c2y, ex, ey, ae, as] = arrow
Arguments
ArgumentTypeDescription
x0numberThe x position of the first rectangle.
y0numberThe y position of the first rectangle.
w0numberThe width of the first rectangle.
h0numberThe height of the first rectangle.
x1numberThe x position of the second rectangle.
y1numberThe y position of the second rectangle.
w1numberThe width of the second rectangle.
h1numberThe height of the second rectangle.
optionsobjectAn (optional) object containing one or more of the options described below.
Options

See options in getArrow above. (Both functions use the same options object.)

Returns

See returns in getArrow above. (Both functions return the same set of values.)

Example: A React Arrow Component

import * as React from 'react'
import { getArrow } from 'curved-arrows'

export function Arrow() {
  const p1 = { x: 100, y: 100 }
  const p2 = { x: 300, y: 200 }
  const arrowHeadSize = 9
  const color = 'black'
  const [sx, sy, c1x, c1y, c2x, c2y, ex, ey, ae] = getArrow(p1.x, p1.y, p2.x, p2.y, {
    padEnd: arrowHeadSize,
  })

  return (
    <svg
      width="100%"
      height="100%"
      xmlns="http://www.w3.org/2000/svg">
      <path
        d={`M ${sx} ${sy} C ${c1x} ${c1y}, ${c2x} ${c2y}, ${ex} ${ey}`}
        stroke={color}
        strokeWidth={arrowHeadSize / 2}
        fill="none"
      />
      <polygon
        points={`0,${-arrowHeadSize} ${arrowHeadSize *
          2},0, 0,${arrowHeadSize}`}
        transform={`translate(${ex}, ${ey}) rotate(${ae})`}
        fill={color}
      />
    </svg>
  )
}

Author

@hialexwang

Keywords

FAQs

Last updated on 02 May 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc