# trianglify
**Repository Path**: mirrors/trianglify
## Basic Information
- **Project Name**: trianglify
- **Description**: Trianglify 是 SVG 背景图片生成器,生成的 SVG 图片非常漂亮
- **Primary Language**: JavaScript
- **License**: GPL-3.0
- **Default Branch**: master
- **Homepage**: https://www.oschina.net/p/trianglify
- **GVP Project**: No
## Statistics
- **Stars**: 1
- **Forks**: 2
- **Created**: 2020-12-17
- **Last Updated**: 2025-09-27
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# Trianglify
Trianglify is a library that I wrote to generate nice SVG background images like this one:

# Contents
[📦 Getting Trianglify](#-getting-trianglify)
[🏎 Quickstart](#-quickstart)
[⚖️ Licensing](#%EF%B8%8F-licensing)
[📖 API](#-api)
[🎨 Configuration](#-configuration)
# 📦 Getting Trianglify
You can grab Trianglify with npm/yarn (recommended):
```
npm install --save trianglify
```
Include it in your application via the unpkg CDN:
```
```
Or download a .zip from the [**releases page**](https://github.com/qrohlf/trianglify/releases).
# 🏎 Quickstart
**Browsers**
```html
```
**Node**
```js
const trianglify = require('trianglify')
const fs = require('fs')
const canvas = trianglify({
width: 1920,
height: 1080
}).toCanvas()
const file = fs.createWriteStream('trianglify.png')
canvas.createPNGStream().pipe(file)
```
You can see the [`examples/`](./examples) folder for more usage examples.
The https://trianglify.io/ GUI is a good place to play around with the various configuration parameters and see their effect on the generated output, live.
# ⚖️ Licensing
The source code of Trianglify is licensed under version 3 of the GNU General Public License ([GPLv3](https://www.gnu.org/licenses/gpl-3.0.html)). This means that any websites, apps, or other projects that include the Trianglify javascript library need to be released under a compatible open-source license. If you are interested in using Trianglify in a closed-source project, please email qr@qrohlf.com to purchase a commercial license.
**However**, it's worth noting that you own the copyright to the output image files which you create using Trianglify, just like you own the copyright to an image created using something like [GIMP](https://www.gimp.org/). If you just want to use an image file that was generated using Trianglify in your project, and do not plan to distribute the Trianglify source code or compiled versions of it, you do not need to worry about the license restrictions described above.
# 📖 API
Trianglify is primarily used by calling the `trianglify` function, which returns a `trianglify.Pattern` object.
```js
// load the library, either via a window global (browsers) or require call (node)
// in es-module environments, you can `import trianglify from 'trianglify'` as well
const trianglify = window.trianglify || require('trianglify')
const options = { height: 400, width: 600 }
const pattern = trianglify(options)
console.log(pattern instanceof trianglify.Pattern) // true
```
## pattern
This object holds the generated geometry and colors, and exposes a number of methods for rendering this geometry to the DOM or a Canvas.
**`pattern.opts`**
Object containing the options used to generate the pattern.
**`pattern.points`**
The pseudo-random point grid used for the pattern geometry, in the following format:
```js
[
[x, y],
[x, y],
[x, y],
// and so on...
]
```
**`pattern.polys`**
The array of colored polygons that make up the pattern, in the following format:
```js
// {x, y} center of the first polygon in the pattern
pattern.polys[0].centroid
// [i, i, i] three indexes into the pattern.points array,
// defining the shape corners
pattern.polys[0].vertexIndices
// Chroma.js color object defining the color of the polygon
pattern.polys[0].color
```
**`pattern.toSVG(destSVG?, svgOpts?)`**
Rendering function for SVG. In browser or browser-like (e.g. JSDOM) environments, this will return a SVGElement DOM node. In node environments, this will return a lightweight node tree structure that can be serialized to a valid SVG string using the `toString()` function.
If an existing svg element is passed as the `destSVG`, this function will render the pattern to the pre-existing element instead of creating a new one.
The `svgOpts` option allows for some svg-specific customizations to the output:
```js
const svgOpts = {
// Include or exclude the xmlns='http://www.w3.org/2000/svg' attribute on
// the root