Skip to content

Commit

Permalink
shadow mode is semi-supported
Browse files Browse the repository at this point in the history
  • Loading branch information
qrohlf committed May 4, 2020
1 parent 65300ae commit 1d37c59
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 10 deletions.
3 changes: 2 additions & 1 deletion examples/basic-web-example.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
<script>
// set up the base pattern
var pattern = trianglify({
color_function: 'sparkle',
color_function: 'shadow',
jitter: 0.1,
tintLevel: 1,
height: window.innerHeight,
width: window.innerWidth,
cell_size: 30 + Math.random() * 100})
Expand Down
4 changes: 2 additions & 2 deletions lib/points.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export default function generate_grid(width, height, bleed_x, bleed_y, cell_size
var points = [];
for (var i = -bleed_x; i < w; i += cell_size) {
for (var j = -bleed_y; j < h; j += cell_size) {
var x = (i + half_cell_size) + (rand_fn() * double_v + negative_v);
var y = (j + half_cell_size) + (rand_fn() * double_v + negative_v);
const x = (i + half_cell_size) + (rand_fn() * double_v + negative_v);
const y = (j + half_cell_size) + (rand_fn() * double_v + negative_v);
points.push([Math.floor(x), Math.floor(y)]);
}
}
Expand Down
15 changes: 12 additions & 3 deletions lib/trianglify.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const defaults = {
color_space: 'lab', // Color space used for gradient construction & interpolation
color_function: null, // Color function f(x, y) that returns a color specification that is consumable by chroma-js
jitter: 0.1, // Jitter level for the 'sparkle' color function
tintLevel: 1, // Tint level for the 'shadows' color function
stroke_width: 1.51, // Width of stroke. Defaults to 1.51 to fix an issue with canvas antialiasing.
points: undefined // An Array of [x,y] coordinates to trianglulate. Defaults to undefined, and points are generated.
}
Expand All @@ -50,11 +51,19 @@ export default function Trianglify(_opts) {
if (!(xScale && yScale)) {
throw new Error('the sparkle color funtion requires both x_colors and y_colors to be defined')
}
return chroma.interpolate(
return chroma.mix(
xScale(x + offset()),
yScale(y + offset()),
0.5
0.5,
opts.color_space
)
},
// this *sort of* works, but it really wants z-axis data to do a "proper"
// job.
shadow: (x, y, xScale, yScale, opts) => {
const color = chroma.mix(xScale(x), yScale(y), 0.5, opts.color_space)
const tint = rand() * opts.tintLevel
return color.darken(tint)
}
}

Expand Down Expand Up @@ -83,7 +92,7 @@ export default function Trianglify(_opts) {

const default_color_function = (x, y, xScale, yScale) => {
if (xScale && yScale) {
return chroma.interpolate(xScale(x), yScale(y), 0.5, opts.color_space);
return chroma.mix(xScale(x), yScale(y), 0.5, opts.color_space);
} else {
xScale
? xScale(x)
Expand Down
58 changes: 55 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"postinstall": "node scripts/postinstall.js"
},
"dependencies": {
"chroma-js": "^0.6.3",
"chroma-js": "^2.1.0",
"colors": "^1.1.2",
"delaunator": "^1.0.4",
"jsdom": "^7.0.2",
Expand Down

0 comments on commit 1d37c59

Please sign in to comment.