svg-patterns
Version:
SVG patterns for Data Visualization.
49 lines (41 loc) • 1.38 kB
JavaScript
;
var dom = require('virtual-dom/h');
var _require = require('../helpers'),
M = _require.M,
l = _require.l,
pattern = _require.pattern; // credit to @riccardoscalco!
// taken from https://github.com/riccardoscalco/textures/blob/ca09566cb9e2dd0bf572639a7e17a9a96717c5e1/textures.coffee#L307
var tile = function tile(s) {
var x = s * Math.tan(Math.PI / 6) * .5;
var e = s * .5 / Math.cos(Math.PI / 6);
return M(0, s / 2) + l(x, s / 2) + l(e, 0) + l(x, -s / 2) + l(-x, -s / 2) + l(-e, 0) + l(-x, s / 2) + 'Z' + M(x + e + x, s / 2) + l(e, 0);
};
var ratio = +(Math.tan(Math.PI / 6) + 1 / Math.cos(Math.PI / 6)).toFixed(3);
var defaults = {
size: 10,
// size of the pattern
fill: 'none',
// any SVG-compatible color
strokeWidth: 1,
stroke: '#343434',
// any SVG-compatible color
background: null // any SVG-compatible color
};
var hexagons = function hexagons() {
var opt = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
opt = Object.assign({}, defaults, opt);
Object.assign(opt, {
width: opt.size * ratio,
height: opt.size,
bg: opt.background,
children: [dom('path', {
d: tile(opt.size),
fill: opt.fill,
stroke: opt.stroke,
'stroke-width': opt.strokeWidth + '',
'stroke-linecap': 'square'
})]
});
return pattern(opt);
};
module.exports = hexagons;