Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/franciscop/vector-graph

Generate high-quality geometry and vector graphs with HTML
https://github.com/franciscop/vector-graph

draw euclidean graph javascript plane vector visualization

Last synced: 6 days ago
JSON representation

Generate high-quality geometry and vector graphs with HTML

Awesome Lists containing this project

README

        

# Vector Graph [![gzip size](https://img.badgesize.io/franciscop/vector-graph/master/index.js.svg?compression=gzip)](https://github.com/franciscop/vector-graph/blob/master/index.js)

> ⚠️ This library is experimental [until 1.0.0 in npm](https://www.npmjs.com/vector-graph). For updates [follow me](https://twitter.com/fpresencia)!

Generate high-quality geometry and vector graphs with HTML:













Basic building blocks
Geometry and angles
Crank it up a notch

```html





```

> Note: none of the `id`s are necessary, but we add them because it's nice

## Getting started

To use this library as usual you'll need three things. First, import it from a CDN; put this line anywhere in your HTML:

```

```

Now let's draw a graph anywhere within your HTML:

![Plain Graph](./examples/plain.svg)

```html


```

Finally, please take a minute to understand the license of vector-graph. We are [dual-licensed](#license), GNU AGPL free license and a commercial license available:

\$19 BUY A LICENSE · AGPL LICENSE

## Documentation

The way you build images is with a parent `` that draws the image and few top-level configuration (like the axis, grid, etc) and then add elements inside.

Unless specified otherwise all units should be local units. So for example, the default plane axis sizes are defined as x=[0,10] and y=[0,10]. This means that if you want to put a point in 2,2, then you'd use those coordinates, never pixel values:

![Basic of units](./examples/basic-units.svg?1)

```html

```

The same applies to positioning of all other elements. There are some exceptions, like the `width` and `height` of ``, or the `width` for the `` which is also in pixels.

### \

Create a vector graph image. It controls the display of grids, axis, numbers, etc.

| attribute | default | description |
| --------- | ------- | ------------------------------------------------------ |
| `width` | `200` | The width of the containing SVG element (pixels) |
| `height` | `200` | The height of the containing SVG element (pixels) |
| `x` | `0,10` | The x-coordinates of the graph to fit into the SVG |
| `y` | `0,10` | The y-coordinates of the graph to fit into the SVG |
| `units` | `false` | Show the numbers on each of the axis |
| `grid` | `1` | The size of the grid, or `false` to hide it |
| `axis` | `x,y` | The X and Y axis labels, or `false` to hide the axis |
| `dark` | _auto_ | Dark theme (true), light theme (false) or auto (undef) |
| `pad` | `30` | The space around the content to avoid SVG clipping |

Control the grid size and position with `x`, `y` and `grid`:













Default grid 10x10
Shifted and scaled
No grid at all

```html

```

You can also hide the different parts of the grid; let's change the size, remove the `units` and disable the `axis` and `grid`:













1x1 grid of 0.2 steps
Hide the grid
Hide grid and axis

```html

```

### \

Draws a small circle in the graph that represents a point in space:

![Point example](./examples/point.svg?20)

```html





```

| attribute | default | description |
| --------- | ------- | ------------------------------------------------- |
| `x` | `0` | The horizontal coordinate where to draw the point |
| `y` | `0` | The vertical coordinate where to draw the point |
| `label` | _none_ | The text to draw on top of the point |
| `color` | `black` | The color of the point, it can be a name or hexa |
| `axis` | `false` | Draw the horizontal and vertical coordinate lines |

### \

Draws a segment between two given points:

![Line example](./examples/line.svg?17)

```html




```

| attribute | default | description |
| --------- | ------- | ------------------------------------------------ |
| `to` | _none_ | The point where the line/segment ends |
| `from` | `0,0` | The point where the line/segment starts |
| `label` | _none_ | Text to draw on the middle of the line |
| `color` | `black` | The color of the line, it can be a name or hexa |
| `width` | `1.75` | The stroke width of the line to draw |
| `dashed` | `false` | Show as dashes (true) or as a solid line (false) |

### \

Draw a hollow circle around the specified center:

![Circle example](./examples/circle.svg?26)

```html





```

| attribute | default | description |
| --------- | ------- | ------------------------------------------------ |
| `x` | `0` | The horizontal coordinate of the circle's center |
| `y` | `0` | The vertical coordinate of the circle's center |
| `radius` | `1` | The circle's radius, in coordinates size |
| `label` | _none_ | Text to draw on the middle of the line |
| `color` | `black` | The color of the line, it can be a name or hexa |
| `width` | `1.75` | The stroke width of the line to draw |

### \

| attribute | default | description |
| --------- | ------- | ------------------------------------------------- |
| `to` | _none_ | The point where the vector ends with an arrow |
| `from` | `0,0` | The point where the vector starts |
| `label` | _none_ | Text to draw on the middle of the line |
| `color` | `black` | The color of the line, it can be a name or hexa |
| `axis` | `false` | Draw the horizontal and vertical coordinate lines |

### \













Simple polygon
Different shapes
With angles and sides

```html

```

| attribute | default | description |
| --------- | ------- | ----------------------------------------------- |
| `points` | _none_ | All of the points of the domain |
| `color` | `black` | The color of the line, it can be a name or hexa |
| `angles` | `false` | Draw the angles in the internal vertices |

### \

Draws an arc representing the angle between two existing lines (the lines must be drawn separately).

![Angle example](./examples/angle.svg)

```html





```

| attribute | default | description |
| --------- | -------- | ------------------------------------------------ |
| `x` | `0` | The horizontal coordinate of the angle's center |
| `y` | `0` | The vertical coordinate of the angle's center |
| `from` | `0` | The angle (in degrees) to start drawing the arc |
| `to` | _none_ | The angle (in degrees) to finish drawing the arc |
| `radius` | `1` | The angle's arc radius, in coordinates size |
| `label` | _none_ | Text to draw on the middle of the line |
| `color` | `black` | The color of the line, it can be a name or hexa |
| `size` | `normal` | The label's size |
| `dashed` | `true` | Draw dashes instead of a solid line |

### \

| attribute | default | description |
| --------- | -------- | ---------------------------------------------------- |
| `text` | _none_ | The content that goes inside the label |
| `x` | _none_ | The horizontal coordinate where to draw the label |
| `y` | _none_ | The vertical coordinate where to draw the label |
| `color` | `black` | Color of text and border, it can be a name or hexa |
| `size` | `normal` | Define how big it is: "tiny, small, normal or large" |
| `width` | _auto_ | The width of the label, leave it empty |

### \

| attribute | default | description |
| --------- | -------- | ---------------------------------------------------- |
| `text` | _none_ | The content that goes inside the label |
| `x` | _none_ | The horizontal coordinate where to draw the label |
| `y` | _none_ | The vertical coordinate where to draw the label |
| `color` | `black` | Color of text and border, it can be a name or hexa |
| `size` | `normal` | Define how big it is: "tiny, small, normal or large" |
| `width` | _auto_ | The width of the label, tip: leave it empty |
| `height` | _auto_ | The height of the label, tip: leave it empty |

### \

![Simple Complex numbers](./examples/custom-function.svg?46)

| attribute | default | description |
| --------- | ------- | ----------------------------------------------- |
| `fn` | string | The function to draw (in Javascript) |
| `color` | `black` | Color of plotted line, it can be a name or hexa |
| `width` | _auto_ | The width of the line to draw |

```html



```

## Examples

![Simple Complex numbers](./examples/imaginary.svg)

```html


```

### Custom function

![Simple Complex numbers](./examples/custom-function.svg?46)

```html



```

### Full XY Plane

![Electromagnetism](./examples/electromagnetism.svg)

```html











```

## Environments

### Javascript

Follow the [Getting Started](#getting-started) guide and it'll all work great. You can include the script at the beginning or at the end of your HTML, it doesn't matter.

### React

### Vue

### Wordpress

### Node.js

You can run this on the Node.js side to generate static SVGs as well. To do so, you'll need to install `jsdom` on your own and then use it like this:

```js
import { JSDOM } from "jsdom";
import graph from "vector-graph";

// Make some of these variable accessible from anywhere
const dom = new JSDOM();
global.window = dom.window;
global.document = window.document;
global.DOMParser = window.DOMParser;

// Render the HTML to SVG
const svg = graph(`





`);

console.log(svg);
// ...
```

## About Vector Graph

I love Katex and I've written several tutorials in the past about mathematics, but I've felt that we are lacking on a way of describing mathematic vectors and equations easily on the web.

There is one project that does a similar thing, but it's way too large (680kb of JS!) and overly complex in my opinion. Instead, I just wanted a way of easily define a simple graph with plain HTML, something like this:

![About example](./examples/about.svg)

```html

```

So after some experimenting and seeing it was possible, I decided to create this library. I'm using SVG since it is scalable and its API is not terrible to work with. It's also not awesome to work with, hence a non-trivial part of the library internals are dedicated to work around some SVG issues.

Now, I've spent _a lot_ of hours creating this and I'm releasing it for everyone to use for free. My ask is that if you want to use my work, you either also release your work for others to use, or compensate me so I can keep building amazing things!

## License

This library is dual-licensed:

- **GNU Affero GPL** (AGPL) is the default license. This might be too strict and relaxed to _GPL_ or _LGPL_ on the release date, we'll see. I pick this for now since it's better to start strict and then relax the license than the other way around.
- **Commercial License** for \$19 [buy in here](https://franciscop.lemonsqueezy.com/checkout/buy/2db23884-26e2-4c97-833f-4dd266e48bfb?name=0&discount=0), which allows you to use in any proprietary codebase without following the AGPL. Ideal for business, education, etc.