0.4.1 • Published 9 years ago

react-hex v0.4.1

Weekly downloads
3
License
MIT
Repository
github
Last release
9 years ago

react-hex Build Status

draw svg hexagonal grid with react

Install

$ npm i react-hex

Example

import React from 'react';
import ReactDom from 'react-dom';
import Hex, { gridPoints } from 'react-hex';

const Hexes = () => {
  const hexes = gridPoints('pointy-topped', 100, 100, 10, 25, 25).map(({ props }) => (
    <Hex {...props} fill="white" stroke="black" />
  ));

  return (
    <svg width="500" height="500">
      {hexes}
    </svg>
  );
};

ReactDom.render(<Hexes />); document.getElementById('example'));

hexes

More examples, see Storybook.

Documents

<Hex type={} x={} y={} size={} /> (default exported)

Main React component of hex.

namevalue typedescription
typePropTypes.oneOf('pointy-topped', 'flat-topped').isRequiredhexagon type
xPropTypes.number.isRequiredhexagon's center coordinate x
yPropTypes.number.isRequiredhexagon's center coordinate y
sizePropTypes.number.isRequiredhexagon edge length
import Hex from 'react-hex';

<Hex type="pointy-topped" x={0} y={0} size={50} />

gridPoint(oType, oX, oY, size, gridX, gridY)

return: { props: { type, x, y, size }, gridX, gridY }

Helper function to calculate hex location in grid. props field in returning object of this function can use for props of Hex component.

(prefix o means original.)

namevalue typedescription
oType'pointy-topped' or 'flat-topped'original hexagon type
oXnumberoriginal hexagon's center coordinate x
oYnumberoriginal hexagon's center coordinate y
sizenumberhexagon edge length
gridXnumbercoordinate x in hexagonal grid system
gridYnumbercoordinate y in hexagonal grid system
import Triangle, { gridPoint } from 'react-hex';

const { props } = gridPoint('pointy-topped', 0, 0, 50, 3, 4);

gridPoints(oDirection, oX, oY, size, girdWidth, gridHeight)

return: [ { props: { type, x, y, size }, gridX, gridY }, ... ]

Helper function to bulk calculate hexes location of grid.

(prefix o means original.)

namevalue typedescription
oType'pointy-topped' or 'flat-topped'original hexagon type
oXnumberoriginal hexagon's center coordinate x
oYnumberoriginal hexagon's center coordinate y
sizenumberhexagon edge length
gridWidthnumbergrid size of x
gridHeightnumbergrid size of y
import Hex, { gridPoints } from 'react-hex';

const triangles = gridPoints('pointy-topped', 100, 100, 50, 5, 5).map(({ props, gridX, gridY }) = (
  <Hex key={`${gridX}-${gridY}`} {...props}/>
));

pointy topped grid coordinate x,y

pointy-topped

flat topped grid coordinate x,y

flat-topped

License

MIT

0.4.1

9 years ago

0.4.0

9 years ago

0.3.7

9 years ago

0.3.6

9 years ago

0.3.5

9 years ago

0.3.4

9 years ago

0.3.3

9 years ago

0.3.2

9 years ago

0.3.1

10 years ago

0.3.0

10 years ago

0.2.0

10 years ago

0.1.0

10 years ago

0.0.5

10 years ago

0.0.4

10 years ago

0.0.3

10 years ago

0.0.2

10 years ago

0.0.1

10 years ago