trianglify README

The project's README from github.com/qrohlf/trianglify. As one markdown file: README.md.

Trianglify

Trianglify is a library that I wrote to generate nice SVG background images like this one:

Contents

📦 Getting Trianglify
🏎 Quickstart
⚖️ Licensing
📖 API
🎨 Configuration

📦 Getting Trianglify

You can grab Trianglify with npm/yarn (recommended):

npm install --save trianglify

Include it in your application via the unpkg CDN:

<script src='https://unpkg.com/trianglify@^4/dist/trianglify.bundle.js'></script>

Or download a .zip from the releases page.

🏎 Quickstart

Browsers

<script src='https://unpkg.com/trianglify@^4/dist/trianglify.bundle.js'></script>
<script>
  const pattern = trianglify({
    width: window.innerWidth,
    height: window.innerHeight
  })
  document.body.appendChild(pattern.toCanvas())
</script>

Node

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/ 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). 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 [email protected] 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. 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.

// 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:

[
  [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:

// {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:

const svgOpts = {
  // Include or exclude the xmlns='http://www.w3.org/2000/svg' attribute on
  // the root <svg> tag. See https://github.com/qrohlf/trianglify/issues/41
  // for additional details on why this is sometimes important
  includeNamespace: true,
  // Controls how many decimals to round coordinate values to.
  // You can set this to -1 to disable rounding. Default is 1.
  coordinateDecimals: 1
}

pattern.toSVGTree(svgOpts?)

Alternate rendering function for SVG. Returns a lightweight node tree structure that can be seralized to a valid SVG string using the toString() function. In node environments, this is an alias for pattern.toSVG().

pattern.toCanvas(destCanvas?, canvasOpts?)

Rendering function for canvas. In browser and browser-like environments, returns a Canvas HTMLElement node. In node environments, this will return a node-canvas object which follows a superset of the Web Canvas API.

If an existing canvas element is passed as the destCanvas, this function will render the pattern to the pre-existing element instead of creating a new one.

To use this in a node.js environment, the optional dependency node-canvas needs to be installed as a dependency of your project npm install -save canvas.

The canvasOpts option allows for some canvas-specific customizations to the output:

const canvasOpts = {
  // determines how the canvas is rendered on high-DPI (aka "retina") devices.
  // - 'auto' will automatically render the canvas at the appropriate scale ratio
  //   for pixel-perfect display.
  // - a numeric value will render the canvas at that specific scale factor
  //   for example, 2.0 will render it at 2x resolution, wheras 0.5 will render
  //   at half resolution
  // - 'false' will disable scaling, and the canvas will be rendered at the 
  //   exact resolution specified by `width, height`
  scaling: 'auto',
  // if the canvas is rendered at a different resolution than the {width, height}
  // trianglify will apply some inline style attributes to scale it back to
  // the requested {width, height} options. Set applyCssScaling to false to 
  // disable this behavior.
  applyCssScaling: true
}

🎨 Configuration

Trianglify is configured by an options object passed in as the only argument. The following option keys are supported, see below for a complete description of what each option does.

const defaultOptions = {
  width: 600,
  height: 400,
  cellSize: 75,
  variance: 0.75,
  seed: null,
  xColors: 'random',
  yColors: 'match',
  fill: true,
  palette: trianglify.colorbrewer,
  colorSpace: 'lab',
  colorFunction: trianglify.colorFunctions.interpolateLinear(0.5),
  strokeWidth: 0,
  points: null
}

width

Integer, defaults to 600. Specify the width in pixels of the pattern to generate.

height

Integer, defaults to 400. Specify the height in pixels of the pattern to generate.

cellSize

Integer, defaults to 75. Specify the size in pixels of the mesh used to generate triangles. Larger values will result in coarser patterns, smaller values will result in finer patterns. Note that very small values may dramatically increase the runtime of Trianglify.

variance

Decimal value between 0 and 1 (inclusive), defaults to 0.75. Specify the amount of randomness used when generating triangles. You may set this higher than 1, but doing so may result in patterns that include "gaps" at the edges.

seed

String, defaults to null. Seeds the random number generator to create repeatable patterns. When set to null, the RNG will be seeded with random values from the environment. An example usage would be passing in blog post titles as the seed to generate unique but consistient trianglify patterns for every post on a blog site.

xColors

False, string, or array of CSS-formatted colors, default is 'random'. Specify the color gradient used on the x axis.

Valid string values are 'random', or the name of a colorbrewer palette (i.e. 'YlGnBu' or 'RdBu'). When set to 'random', a gradient will be randomly selected from the colorbrewer library.

Valid array values should specify the color stops in any CSS format (i.e. ['#000000', '#4CAFE8', '#FFFFFF']).

yColors

False, string or array of CSS-formatted colors, default is 'match'. When set to 'match' the x-axis color gradient will be used on both axes. Otherwise, accepts the same options as xColors.

palette

The array of color combinations to pick from when using random for the xColors or yColors. See src/utils/colorbrewer.js for the format of this data.

colorSpace

String, defaults to 'lab'. Set the color space used for generating gradients. Supported values are rgb, hsv, hsl, hsi, lab and hcl. See this blog post for some background on why this matters.

colorFunction

Specify a custom function for coloring triangles, defaults to null. Accepts a function to override the standard gradient coloring, which is passed a variety of data about the pattern and each polygon and must return a Chroma.js color object.

See examples/color-function-example.html and src/utils/colorFunctions.js for more information about the built-in color functions, and how to write custom color functions.

fill

Boolean, defaults to true. Specifies whether the polygons generated by Trianglify should be filled in.

strokeWidth

Number, defaults to 0. Specify the width of the strokes used to outline the polygons. This can be used in conjunction with fill: false to generate weblike patterns.

points

Array of points ([x, y]) to triangulate, defaults to null. When not specified an array randomised points is generated filling the space. Points must be within the coordinate space defined by width and height. See examples/custom-points-example.html for a demonstration of how this option can be used to generate circular trianglify patterns.

trianglify (qrohlf/trianglify)

Searching Database…

这是做什么的?Trianglify 是一个 JavaScript 库,能通过算法生成彩色三角形网格背景图片,输出为 SVG —— 就是那种可以放进网页里的装饰性低多边形风格图案。
它是一个要在代码里调用的库,还是更像一个设计工具?它是一个库:你把一个配置对象传给 trianglify 函数,会得到一个 Pattern 对象,然后再把它渲染成 SVG 或 canvas。
我怎么把它加到项目里?推荐用 npm 或 yarn 安装,也可以从 unpkg CDN 引入,或者从 releases 页面下载 .zip 文件。
运行它还需要安装别的东西吗?不需要 —— 它在浏览器和 Node 里开箱即用。唯一的可选依赖是 node-canvas 包,只有当你想在 Node 环境里做 canvas 渲染时才需要。
我可以在闭源项目里使用 Trianglify 吗?不买商业授权就不行:按 GPLv3 的要求,任何引入了 Trianglify 库本身的网站或应用,都必须以兼容的开源许可证发布。作者通过电子邮件出售商业授权。
Trianglify 生成的图片归我所有吗?归你 —— 你对自己生成的输出图片文件拥有版权,就像用 GIMP 做出来的图片一样,所以在任何项目里使用生成的图片都不会触发 GPL 的限制。
图案可以渲染成什么 —— SVG 还是 canvas?都可以,都是通过 Pattern 对象:在浏览器里 toSVG() 返回一个 SVG DOM 节点,在 Node 里返回一棵可序列化的节点树;toCanvas() 则渲染到一个 canvas 元素。其他格式都可以从这两种结果再派生出来。
不写代码能先预览图案效果吗?可以 —— 仓库里自带一个图形界面,你可以随意调整各种配置参数,实时看到它们对生成结果的影响,是找到自己喜欢配置的好办法。
能让每次都生成同一个图案吗?给 seed 选项传一个字符串,随机数生成器就会变成确定性的,生成的图案可以复现。README 还建议用博客文章标题作为 seed,这样每篇文章都能得到独一无二又保持一致的图案。
颜色怎么控制?通过 xColors 选项设置渐变:可以是 'random'、ColorBrewer 调色板名称(比如 'YlGnBu' 或 'RdBu'),或者你自己指定的 CSS 颜色节点。yColors 默认与它保持一致,渐变还会在可选的色彩空间里混合(默认是 lab)。
能控制三角形本身的样子吗?可以。cellSize(默认 75)控制网格的粗细 —— 数值越小图案越细腻,但生成速度也越慢;variance(0 到 1,默认 0.75)控制随机程度。你还可以传入自己的 points 数组,比如用来生成圆形图案。
Creator
qrohlf
Size
4.1 MB
Stars
10.1K
Updated
CommitMay 12
Age
12y
Commits · 90 Days