https://github.com/svagco/lib
A library for drawing SVGs from JavaScript with a set of useful methods, such as to create elements and rounded corners.
https://github.com/svagco/lib
javascript macos mockup svg ui
Last synced: 6 months ago
JSON representation
A library for drawing SVGs from JavaScript with a set of useful methods, such as to create elements and rounded corners.
- Host: GitHub
- URL: https://github.com/svagco/lib
- Owner: svagco
- License: mit
- Created: 2018-09-07T19:07:17.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2018-09-17T17:52:20.000Z (about 7 years ago)
- Last Synced: 2025-03-18T06:32:20.415Z (7 months ago)
- Topics: javascript, macos, mockup, svg, ui
- Language: JavaScript
- Homepage: https://svag.co
- Size: 132 KB
- Stars: 2
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# @svag/lib
[](https://npmjs.org/package/@svag/lib)
`@svag/lib` is a library for drawing SVGs from JavaScript with a set of useful methods, such as to create elements and rounded corners. It is used in other `svag` packages, e.g., [`@svag/window`](https://github.com/svagjs/window)
```sh
yarn add -E @svag/lib
```## Table Of Contents
- [Table Of Contents](#table-of-contents)
- [Types](#types)
* [`Coordinate`](#coordinate)
* [`length`](#length)
* [`percentage`](#percentage)
- [API](#api)
* [`makeElement(name: string, options?: MakeElementOptions): string`](#makeelementname-stringoptions-makeelementoptions-string)
* [`MakeElementOptions`](#makeelementoptions)
* [`minify(svg: string): string`](#minifysvg-string-string)
* [`roundedCorner(from: Coordinate, to: Coordinate, anticlockwise?: boolean): string`](#roundedcornerfrom-coordinateto-coordinateanticlockwise-boolean-string)
* [Clockwise](#clockwise)
* [Anticlockwise](#anticlockwise)
- [Elements](#elements)
* [`svg(options: SVGOption): string`](#svgoptions-svgoption-string)
* [`SVGOptions`](#svgoptions)
* [`rect(attributes: RectAttributes): string`](#rectattributes-rectattributes-string)
* [`RectAttributes`](#rectattributes)
- [TODO](#todo)
- [Copyright](#copyright)## Types
There are some common types used by various methods. They are described in this section.
__`Coordinate`__: A coordinate used for drawing.
| Name | Type | Description | Default |
| ------ | -------- | ----------------------------------- | ------- |
| __x*__ | _number_ | The `x` position of the coordinate. | - |
| __y*__ | _number_ | The `y` position of the coordinate. | - |`string|number` __`length`__: A length is a distance measurement, given as a number along with a unit.
`string` __`percentage`__: Percentages are specified as a number followed by a `%` character.
## API
The package library exports a number of functions, including `makeElement` and `minify`.
```js
import { makeElement, minify } from '@svag/lib'
```### `makeElement(`
`name: string,`
`options?: MakeElementOptions,`
`): string`This function will create an element as a string given its name and the options. The attributes will be split by new lines whenever the line width reaches the length of 100 symbols, and each line of the content will be indented by 2 spaces as well.
__`MakeElementOptions`__: Options to make a new element.
| Name | Type | Description | Default |
| ---------- | ------------------ | ---------------------------------------------------------------------------------- | ------- |
| content | _string\|string[]_ | The content to write inside of the element, such as string or an array of strings. | - |
| attributes | _object_ | A map of attributes to add to the element. | - |```js
import { makeElement } from '@svag/lib'const circle = makeElement('circle', {
attributes: {
cx: 50,
cy: 50,
r: 25,
},
})
const rect = makeElement('rect', {
attributes: {
width: '100',
height: '100',
},
})
const g = makeElement('g', {
attributes: {
fill: 'green',
},
// 1. SET Single content attribute
content: rect,
})
const element = makeElement('g', {
name: 'g',
attributes: {
test: true,
'font-size': '12px',
},
// 2. SET Multiple content attributes
content: [circle, g],
})
``````svg
```
### `minify(`
`svg: string,`
`): string`Removes the whitespace between the elements in the input string.
```js
import { minify } from '@svag/lib'const svg = `
`
const minified = minify(svg)
``````svg
```
### `roundedCorner(`
`from: Coordinate,`
`to: Coordinate,`
`anticlockwise?: boolean,`
`): string`Create a `C` directive to include in a `path` element to create a rounded corner. If `anticlockwise` argument is passed, the path will follow the counter-clockwise movement.
Clockwise: The table below shows the corners drawn clockwise.
Direction
Usage
Output
Preview
Top Right
```js
import { roundedCorner } from '@svag/lib'
const C = roundedCorner({
x: 0,
y: 1,
}, {
x: 50,
y: 51,
})
```
```svg
C 25 1, 50 26, 50 51
```

Bottom Right
```js
import { roundedCorner } from '@svag/lib'const C = roundedCorner({
x: 60,
y: 0,
}, {
x: 10,
y: 50,
})
```
```svg
C 60 25, 35 50, 10 50
```

Bottom Left
```js
import { roundedCorner } from '@svag/lib'const C = roundedCorner({
x: 60,
y: 60,
}, {
x: 10,
y: 10,
})
```
```svg
C 35 60, 10 35, 10 10
```

Top Left
```js
import { roundedCorner } from '@svag/lib'const C = roundedCorner({
x: 1,
y: 60,
}, {
x: 51,
y: 10,
})
```
```svg
C 1 35, 26 10, 51 10
```

Anticlockwise: The table below shows the corners drawn anticlockwise.
Direction
Usage
Output
Preview
Top Right
```js
import { roundedCorner } from '@svag/lib'const C = roundedCorner({
x: 60,
y: 60,
}, {
x: 10,
y: 10,
}, true)
```
```svg
C 60 35, 35 10, 10 10
```

Top Left
```js
import { roundedCorner } from '@svag/lib'const C = roundedCorner({
x: 60,
y: 1,
}, {
x: 10,
y: 51,
}, true)
```
```svg
C 35 1, 10 26, 10 51
```

Bottom Left
```js
import { roundedCorner } from '@svag/lib'
const C = roundedCorner({
x: 1,
y: 0,
}, {
x: 51,
y: 50,
}, true)
```
```svg
C 1 25, 26 50, 51 50
```

Bottom Right
```js
import { roundedCorner } from '@svag/lib'const C = roundedCorner({
x: 1,
y: 60,
}, {
x: 51,
y: 10,
}, true)
```
```svg
C 26 60, 51 35, 51 10
```

## Elements
This section describes how to create individual elements.
### `svg(`
`options: SVGOption,`
`): string`Generate an `svg` element with given content and dimensions.
__`SVGOptions`__: An option for creating an svg.
| Name | Type | Description | Default |
| ------------ | --------- | ---------------------------------------------------------------------------------------------- | ------- |
| __width*__ | _number_ | The width of the `svg`. | - |
| __height*__ | _number_ | The height of the `svg`. | - |
| __content*__ | _string_ | The content to put inside of the `svg`. | - |
| stretch | _boolean_ | Expand the `svg` to the width of the container by not setting `width` and `height` attributes. | `true` |```js
import { svg } from '@svag/lib'const stretchedSvg = svg({
height: 100,
width: 100,
content: '',
})
``````svg
```
To generate an `svg` which will not adjust its size to the viewport, the `stretch` option needs to be set to `false`.
```js
import { svg } from '@svag/lib'const fixedSvg = svg({
height: 100,
width: 100,
content: '',
stretch: false,
})
``````svg
```
### `rect(`
`attributes: RectAttributes,`
`): string`Create a `` element.
__`RectAttributes`__: Non-global attributes for the element.
| Name | Type | Description | Default |
| ---------- | ------------------------------------------------------ | ------------------------------------------------------------------------- | ------- |
| x | _[length](#length)\|[percentage](#percentage)_ | This attribute determines the x coordinate of the rect. | `0` |
| y | _[length](#length)\|[percentage](#percentage)_ | This attribute determines the y coordinate of the rect. | `0` |
| width | _'auto'\|[length](#length)\|[percentage](#percentage)_ | This attribute determines the width of the rect. | `auto` |
| height | _'auto'\|[length](#length)\|[percentage](#percentage)_ | This attribute determines the height of the rect. | `auto` |
| rx | _'auto'\|[length](#length)\|[percentage](#percentage)_ | This attribute determines the horizontal corner radius of the rect. | `auto` |
| ry | _'auto'\|[length](#length)\|[percentage](#percentage)_ | This attribute determines the vertical corner radius of the rect. | `auto` |
| pathLength | _number_ | This attribute lets specify the total length for the path, in user units. | - |```js
import { rect } from '@svag/lib'const image = rect({
height: 100,
width: 100,
rx: '0.5em',
ry: '0.5em',
})
``````svg
```
## TODO
- [ ] Create an alias for each SVG element, e.g., `circle`, _etc_, and parse their documentation from `MDN`.
- [ ] Update `documentary` to make sure that types are linked to their description.
- [ ] Update `alamode` to be able to write XML syntax (JSX transform).
- [ ] Update `minify` to work correctly with attributes on new lines.## Copyright
Some of the element documentation, including `rect` was taken directly from [MDN](https://developer.mozilla.org/en-US/docs/).
(c) [SVaG][1] 2018
[1]: https://svag.co