{"id":13528302,"url":"https://github.com/steveruizok/perfect-arrows","last_synced_at":"2025-05-14T10:11:29.450Z","repository":{"id":38195843,"uuid":"280519332","full_name":"steveruizok/perfect-arrows","owner":"steveruizok","description":"Draw perfect arrows between points and shapes.","archived":false,"fork":false,"pushed_at":"2023-01-07T20:12:38.000Z","size":5440,"stargazers_count":2781,"open_issues_count":12,"forks_count":59,"subscribers_count":22,"default_branch":"master","last_synced_at":"2025-04-03T20:06:22.452Z","etag":null,"topics":["angles","arc","arrow-heads","draw","perfect-arrows"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/steveruizok.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":["steveruizok"]}},"created_at":"2020-07-17T20:29:56.000Z","updated_at":"2025-03-28T21:23:30.000Z","dependencies_parsed_at":"2023-02-08T00:30:56.123Z","dependency_job_id":null,"html_url":"https://github.com/steveruizok/perfect-arrows","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/steveruizok%2Fperfect-arrows","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/steveruizok%2Fperfect-arrows/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/steveruizok%2Fperfect-arrows/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/steveruizok%2Fperfect-arrows/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/steveruizok","download_url":"https://codeload.github.com/steveruizok/perfect-arrows/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248338026,"owners_count":21087150,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["angles","arc","arrow-heads","draw","perfect-arrows"],"created_at":"2024-08-01T06:02:24.554Z","updated_at":"2025-04-11T03:36:38.519Z","avatar_url":"https://github.com/steveruizok.png","language":"TypeScript","funding_links":["https://github.com/sponsors/steveruizok"],"categories":["TypeScript","Utilities","📦 Legacy \u0026 Inactive Projects"],"sub_categories":["React Components"],"readme":"# Perfect Arrows\n\nA set of functions for drawing perfect arrows between points and shapes.\n\n- [`getArrow`](#getarrowx0-y0-x1-y1-options) - For point-to-point arrows.\n- [`getBoxToBoxArrow`](#getboxtoboxarrowx0-y0-w0-h0-x1-y1-w1-h1-options) - For rectangle-to-rectangle arrows.\n\n![Example](/example.gif)\n\n👉 [Demo](https://perfect-arrows.now.sh/)\n\n[![Edit example](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/github/steveruizok/perfect-arrows/tree/master/example?fontsize=14\u0026hidenavigation=1\u0026theme=dark)\n\n\u003cdiv align=\"center\"\u003e\n\u003cp\u003eOther languages\u003c/p\u003e\n\u003cp\u003e\u003ca href=\".github/README_pt_BR.md\"\u003ePortuguês (pt-BR)\u003c/a\u003e\u003c/p\u003e\u003c/div\u003e\n\n## Installation\n\n```\nnpm i perfect-arrows\n```\n\n_or_\n\n```\nyarn add perfect-arrows\n```\n\n## Usage\n\nThe functions in this library 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 React component with SVG.\n\n### `getArrow(x0, y0, x1, y1, options)`\n\nThe `getArrow` function accepts the position of two points and returns an array containing information for:\n\n- three points: a start, end, and control point\n- three angles: an end, start, and center\n\nYou can use this information to draw an arc and arrow-heads. You can use the options object to tweak the return values.\n\n```js\nconst arrow = getArrow(0, 0, 100, 200, {\n  bow: 0,\n  stretch: 0.5,\n  stretchMin: 0,\n  stretchMax: 420,\n  padStart: 0,\n  padEnd: 0,\n  flip: false,\n  straights: true,\n})\n\nconst [sx, sy, cx, cy, ex, ey, ae, as, sc] = arrow\n```\n\n#### Arguments\n\n| Argument | Type   | Description                                                                 |\n| -------- | ------ | --------------------------------------------------------------------------- |\n| `x0`     | number | The x position of the starting point.                                       |\n| `y0`     | number | The y position of the starting point.                                       |\n| `x1`     | number | The x position of the ending point.                                         |\n| `y1`     | number | The y position of the ending point.                                         |\n| options  | object | An (optional) object containing one or more of the options described below. |\n\n#### Options\n\n| Option       | Type    | Default | Description                                                                                                                                                    |\n| ------------ | ------- | ------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- |\n| `bow`        | number  | 0       | A value representing the natural bow of the arrow. At `0`, all lines will be straight.                                                                         |\n| `stretch`    | number  | .5      | The effect that the arrow's length will have, relative to its `minStretch` and `maxStretch`, on the bow of the arrow. At `0`, the stretch will have no effect. |\n| `minStretch` | number  | 0       | The length of the arrow where the line should be most stretched. Shorter distances than this will have no additional effect on the bow of the arrow.           |\n| `maxStretch` | number  | 420     | The length of the arrow at which the stretch should have no effect.                                                                                            |\n| `padStart`   | number  | 0       | How far the arrow's starting point should be from the provided start point.                                                                                    |\n| `padEnd`     | number  | 0       | How far the arrow's ending point should be from the provided end point.                                                                                        |\n| `flip`       | boolean | false   | Whether to reflect the arrow's bow angle.                                                                                                                      |\n| `straights`  | boolean | true    | Whether to use straight lines at 45 degree angles.                                                                                                             |\n\n#### Returns\n\n| Argument | Type   | Description                                      |\n| -------- | ------ | ------------------------------------------------ |\n| `x0`     | number | The x position of the (padded) starting point.   |\n| `y0`     | number | The y position of the (padded) starting point.   |\n| `x1`     | number | The x position of the (padded) ending point.     |\n| `y1`     | number | The y position of the (padded) ending point.     |\n| `ae`     | number | The angle (in radians) for an ending arrowhead.  |\n| `as`     | number | The angle (in radians) for a starting arrowhead. |\n| `ac`     | number | The angle (in radians) for a center arrowhead.   |\n\n## Example: A React Arrow Component\n\n```jsx\nimport * as React from \"react\"\nimport { getArrow } from \"perfect-arrows\"\n\nexport function PerfectArrow() {\n  const p1 = { x: 64, y: 64 }\n  const p2 = { x: 128, y: 96 }\n\n  const arrow = getArrow(p1.x, p1.y, p2.x, p2.y, {\n    padEnd: 20,\n  })\n\n  const [sx, sy, cx, cy, ex, ey, ae, as, ec] = arrow\n\n  const endAngleAsDegrees = ae * (180 / Math.PI)\n\n  return (\n    \u003csvg\n      viewBox=\"0 0 720 480\"\n      style={{ width: 720, height: 480 }}\n      stroke=\"#000\"\n      fill=\"#000\"\n      strokeWidth={3}\n    \u003e\n      \u003ccircle cx={sx} cy={sy} r={4} /\u003e\n      \u003cpath d={`M${sx},${sy} Q${cx},${cy} ${ex},${ey}`} fill=\"none\" /\u003e\n      \u003cpolygon\n        points=\"0,-6 12,0, 0,6\"\n        transform={`translate(${ex},${ey}) rotate(${endAngleAsDegrees})`}\n      /\u003e\n    \u003c/svg\u003e\n  )\n}\n```\n\n---\n\n### `getBoxToBoxArrow(x0, y0, w0, h0, x1, y1, w1, h1, options)`\n\nThe `getBoxToBoxArrow` function accepts the position and dimensions of two boxes (or rectangles) and returns an array containing information for:\n\n- three points: a start, end, and control point\n- three angles: an end, start, and center\n\nYou can use this information to draw an arc and arrow-heads. You can use the options object to tweak the return values.\n\n**Note:** The options and values returned by `getBoxToBoxArrow` are in the same format as the options and values for `getArrow`.\n\n```js\nconst arrow = getBoxToBoxArrow(0, 0, 96, 128, 400, 200, 128, 96, {\n  bow: 0,\n  stretch: 0.5,\n  stretchMin: 0,\n  stretchMax: 420,\n  padStart: 0,\n  padEnd: 0,\n  flip: false,\n  straights: true,\n})\n\nconst [sx, sy, cx, cy, ex, ey, ae, as, sc] = arrow\n```\n\n#### Arguments\n\n| Argument | Type   | Description                                                                 |\n| -------- | ------ | --------------------------------------------------------------------------- |\n| `x0`     | number | The x position of the first rectangle.                                      |\n| `y0`     | number | The y position of the first rectangle.                                      |\n| `w0`     | number | The width of the first rectangle.                                           |\n| `h0`     | number | The height of the first rectangle.                                          |\n| `x1`     | number | The x position of the second rectangle.                                     |\n| `y1`     | number | The y position of the second rectangle.                                     |\n| `w1`     | number | The width of the second rectangle.                                          |\n| `h1`     | number | The height of the second rectangle.                                         |\n| options  | object | An (optional) object containing one or more of the options described below. |\n\n#### Options\n\nSee options in `getArrow` above. (Both functions use the same options object.)\n\n#### Returns\n\nSee returns in `getArrow` above. (Both functions return the same set of values.)\n\n## Example: A React Box-to-box Arrow Component\n\n```jsx\nimport * as React from \"react\"\nimport { getBoxToBoxArrow } from \"perfect-arrows\"\n\nexport function PerfectArrow() {\n  const p1 = { x: 64, y: 64, w: 64, h: 64 }\n  const p2 = { x: 128, y: 96, w: 64, h: 64 }\n\n  const arrow = getBoxToBoxArrow(\n    p1.x,\n    p1.y,\n    p1.w,\n    p1.h,\n    p2.x,\n    p2.y,\n    p2.w,\n    p2.h,\n    {\n      bow: 0.2,\n      stretch: 0.5,\n      stretchMin: 40,\n      stretchMax: 420,\n      padStart: 0,\n      padEnd: 20,\n      flip: false,\n      straights: true,\n    }\n  )\n\n  const [sx, sy, cx, cy, ex, ey, ae, as, ec] = arrow\n\n  const endAngleAsDegrees = ae * (180 / Math.PI)\n\n  return (\n    \u003csvg\n      viewBox=\"0 0 1280 720\"\n      style={{ width: 1280, height: 720 }}\n      stroke=\"#000\"\n      fill=\"#000\"\n      strokeWidth={3}\n    \u003e\n      \u003ccircle cx={sx} cy={sy} r={4} /\u003e\n      \u003cpath d={`M${sx},${sy} Q${cx},${cy} ${ex},${ey}`} fill=\"none\" /\u003e\n      \u003cpolygon\n        points=\"0,-6 12,0, 0,6\"\n        transform={`translate(${ex},${ey}) rotate(${endAngleAsDegrees})`}\n      /\u003e\n    \u003c/svg\u003e\n  )\n}\n```\n\n## Author\n\n[@steveruizok](https://twitter.com/steveruizok)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsteveruizok%2Fperfect-arrows","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsteveruizok%2Fperfect-arrows","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsteveruizok%2Fperfect-arrows/lists"}