Skip to content

Commit 5481056

Browse files
committed
improve examples, docs
1 parent b6d0bad commit 5481056

7 files changed

+41
-12
lines changed

Diff for: .gitignore

-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
11
node_modules
2-
bower_components
3-
test/test.browserify.js
42
dist

Diff for: examples/color-function-example.html

+28-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
<!-- This is the standalone build of Trianglify. It's easy to drop in to a
3131
project without any need for bundlers or dependency management, but in most
3232
modern web projects you will probably want to use the NPM module instead -->
33-
<script src="../dist/trianglify.bundle.umd.js"></script>
33+
<script src="../dist/trianglify.bundle.debug.js"></script>
3434
<script>
3535

3636
const JITTER_FACTOR = 0.2
@@ -54,7 +54,8 @@
5454
))
5555
}
5656

57-
const seed = Math.random()
57+
// use the same seed for everything, to allow for easier comparisons
58+
const seed = 'pears'
5859

5960
// Example 1: you can use the built-in color functions to customize the
6061
// color rendering of Trianglify. Here, we use the 'sparkle' color
@@ -81,6 +82,31 @@
8182
colorFunction: trianglify.colorFunctions.interpolateLinear(0.1)
8283
})
8384
addToPage(interpolate, 'trianglify.colorFunctions.interpolateLinear(0.1)')
85+
86+
// Example 3: you can write your own custom color function to have
87+
// total control over how the polygons are given color values.
88+
//
89+
// Here, we apply the xScale colors as a radial gradient:
90+
91+
// define a custom color function that applies a radial gradient:
92+
const radialGradient = (radius) => (centroid, normalizedX, normalizedY, vertices, xGradient, yGradient, opts, random) => {
93+
const distanceFromCenter = Math.sqrt(
94+
Math.pow(200 - centroid.x, 2) + Math.pow(150 - centroid.y, 2)
95+
)
96+
return(xGradient(distanceFromCenter / radius))
97+
}
98+
// figure out the gradient radius required to cover the image dimensions:
99+
const gradientRadius = Math.sqrt(
100+
Math.pow(400 / 2, 2) + Math.pow(300 / 2, 2)
101+
)
102+
const radial = trianglify({
103+
seed,
104+
width: 400,
105+
height: 300,
106+
cellSize: 15,
107+
colorFunction: radialGradient(gradientRadius)
108+
})
109+
addToPage(radial, 'custom radial gradient')
84110
</script>
85111
</body>
86112
</html>

Diff for: examples/custom-points-example.html

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515
</style>
1616
</head>
1717
<body>
18-
<script src="../dist/trianglify.bundle.umd.js"></script>
18+
<script src="../dist/trianglify.bundle.debug.js"></script>
1919
<script>
20-
2120
const width = 700
2221
const height = 700
2322

Diff for: examples/transparency-example.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110
</defs>
111111
<path fill="url(#a)" d="M0 0h500v500H0z"></path>
112112
</svg>
113-
<script src="../dist/trianglify.bundle.umd.js"></script>
113+
<script src="../dist/trianglify.bundle.debug.js"></script>
114114
<script>
115115
// set up the base pattern
116116
const pattern = trianglify({

Diff for: src/trianglify.js

+1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ export default function trianglify (_opts) {
109109
const yPercent = norm(centroid.y / height)
110110

111111
const color = opts.colorFunction(
112+
centroid,
112113
xPercent,
113114
yPercent,
114115
vertices,

Diff for: src/utils/colorFunctions.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ import chroma from 'chroma-js'
99
// colorFunction: trianglify.colorFunctions.sparkle(0.2)
1010
// })
1111
//
12-
// the above snippet would give you a trianglify pattern with a 20% random
12+
// the snippet above gives you a trianglify pattern with a 20% random
1313
// jitter applied to the x and y gradient scales
1414

1515
export const interpolateLinear = (bias = 0.5) => (
16-
(xPercent, yPercent, vertices, xScale, yScale, opts) =>
16+
(centroid, xPercent, yPercent, vertices, xScale, yScale, opts) =>
1717
chroma.mix(xScale(xPercent), yScale(yPercent), bias, opts.colorSpace)
1818
)
1919

20-
export const sparkle = (jitterFactor = 0.15) => (normalizedX, normalizedY, vertices, xGradient, yGradient, opts) => {
21-
const jitter = () => (Math.random() - 0.5) * jitterFactor
20+
export const sparkle = (jitterFactor = 0.15) => (centroid, normalizedX, normalizedY, vertices, xGradient, yGradient, opts, random) => {
21+
const jitter = () => (random() - 0.5) * jitterFactor
2222
const a = xGradient(normalizedX + jitter())
2323
const b = yGradient(normalizedY + jitter())
2424
return chroma.mix(a, b, 0.5, opts.colorSpace)

Diff for: src/utils/mulberry32.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
// Fast seeded rng. Seed must be numeric!
1+
// Fast seeded RNG adapted from the public-domain implementation
2+
// from @byrc: https://github.com/bryc/code/blob/master/jshash/PRNGs.md
3+
//
4+
// Usage:
5+
// const randFn = mulberry32('string seed')
6+
// const randomNumber = randFn() // [0, 1] random float
27
export default function mulberry32 (seed) {
38
if (!seed) { seed = Math.random().toString(36) } // support no-seed usage
49
var a = xmur3(seed)()

0 commit comments

Comments
 (0)