|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | + <head> |
| 4 | + <meta charset="utf-8"> |
| 5 | + <meta http-equiv="X-UA-Compatible" content="IE=edge"> |
| 6 | + <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 7 | + <title>Trianglify Basic Example</title> |
| 8 | + <style> |
| 9 | + html, body { |
| 10 | + margin: 0 0; |
| 11 | + padding: 0 0; |
| 12 | + text-align: center; |
| 13 | + background: #000; |
| 14 | + font-family: system-ui; |
| 15 | + } |
| 16 | + |
| 17 | + h1 { |
| 18 | + font-size: 18px; |
| 19 | + } |
| 20 | + |
| 21 | + .demo { |
| 22 | + background: #FFF; |
| 23 | + display: inline-block; |
| 24 | + padding: 20px; |
| 25 | + margin: 20px; |
| 26 | + } |
| 27 | + </style> |
| 28 | + </head> |
| 29 | + <body> |
| 30 | + <!-- This is the standalone build of Trianglify. It's easy to drop in to a |
| 31 | + project without any need for bundlers or dependency management, but in most |
| 32 | + modern web projects you will probably want to use the NPM module instead --> |
| 33 | + <script src="../dist/trianglify.bundle.umd.js"></script> |
| 34 | + <script> |
| 35 | + |
| 36 | + const JITTER_FACTOR = 0.2 |
| 37 | + |
| 38 | + |
| 39 | + // utility for building up HTML trees |
| 40 | + const h = (tagName, attrs = {}, children = []) => { |
| 41 | + const elem = document.createElement(tagName) |
| 42 | + attrs && Object.keys(attrs).forEach( |
| 43 | + k => attrs[k] !== undefined && elem.setAttribute(k, attrs[k]) |
| 44 | + ) |
| 45 | + children && children.forEach(c => elem.appendChild(c)) |
| 46 | + return elem |
| 47 | + } |
| 48 | + |
| 49 | + const addToPage = (pattern, description) => { |
| 50 | + document.body.appendChild(h('div', {'class': 'demo'}, [ |
| 51 | + pattern.toCanvas(), |
| 52 | + h('h1', null, [document.createTextNode(description)]) |
| 53 | + ] |
| 54 | + )) |
| 55 | + } |
| 56 | + |
| 57 | + const seed = Math.random() |
| 58 | + |
| 59 | + // Example 1: you can use the built-in color functions to customize the |
| 60 | + // color rendering of Trianglify. Here, we use the 'sparkle' color |
| 61 | + // function to apply a 10% jitter to the normal color gradients, which |
| 62 | + // will yield a glitter-like effect. |
| 63 | + const sparkle = trianglify({ |
| 64 | + seed, |
| 65 | + width: 400, |
| 66 | + height: 300, |
| 67 | + cellSize: 15, |
| 68 | + colorFunction: trianglify.colorFunctions.sparkle(0.2) |
| 69 | + }) |
| 70 | + addToPage(sparkle, 'trianglify.colorFunctions.sparkle(0.2)') |
| 71 | + |
| 72 | + // Example 2: you can use the interpolateLinear color function to |
| 73 | + // customize how much the x or y gradient dominates the image. |
| 74 | + // Higher values for the bias will result in a more pronounced x-gradient, |
| 75 | + // while lower values will results in a more pronounced y-gradient |
| 76 | + const interpolate = trianglify({ |
| 77 | + seed, |
| 78 | + width: 400, |
| 79 | + height: 300, |
| 80 | + cellSize: 15, |
| 81 | + colorFunction: trianglify.colorFunctions.interpolateLinear(0.1) |
| 82 | + }) |
| 83 | + addToPage(interpolate, 'trianglify.colorFunctions.interpolateLinear(0.1)') |
| 84 | + </script> |
| 85 | + </body> |
| 86 | +</html> |
0 commit comments