Skip to content

Commit a5674a7

Browse files
committed
Added some documentation & clean up
1 parent 458117a commit a5674a7

File tree

7 files changed

+48
-85
lines changed

7 files changed

+48
-85
lines changed

HexGrid.png

-107 KB
Binary file not shown.

README.md

Lines changed: 35 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,19 @@
77
![dev dependencies](https://img.shields.io/david/dev/Hellenic/react-hexgrid.svg)
88
![License](https://img.shields.io/npm/l/react-hexgrid.svg)
99

10-
React component to create interactive hexagons grids. It uses SVG so it works fast and can be styled easily, and it is flexible to customize.
10+
React components to build interactive hexagons grids. It uses SVG which makes it fast, scalable and easy to apply custom styles. You can easily customize the layout of the grid just by configuration.
1111

12-
With inspiration from
12+
Component-based approach allows you to customize the grid shape to suit your needs or even apply your own components / SVG elements to it. You can use pre-defined generator to create certain shape grid or you may build totally your own grid to the shape you wish, while still keeping it under control and interactive.
13+
14+
You could easily use this library to build (just to name a few) a nice portfolio, image library or even a game!
15+
16+
> With inspiration from
1317
[http://www.redblobgames.com/grids/hexagons](http://www.redblobgames.com/grids/hexagons).
1418

1519
## Pre-requisites
1620

1721
You should be familiar with Node + NPM, React and ES6 to use this library.
18-
Library also depends heavily on HTML5 features which all might not be supported by every browser yet.
22+
Library also depends heavily on HTML5 features (mostly SVG) which all might not be supported by every browser yet.
1923
For example [Drag & Drop](http://caniuse.com/#search=drag%20and) is still quite heavily under work.
2024

2125
## Getting Started
@@ -28,109 +32,59 @@ npm install --save react-hexgrid
2832

2933
## Example
3034

31-
```javascript
35+
```html
3236
import { HexGrid } from 'react-hexgrid';
3337
import './App.css';
3438

3539
class App extends Component {
36-
constructor(props) {
37-
super(props);
38-
let boardConfig = {
39-
width: 800, height: 800,
40-
layout: { width: 10, height: 10, flat: true, spacing: 1.1 },
41-
origin: { x: 0, y: 0 },
42-
map: 'hexagon',
43-
mapProps: [ 2 ]
44-
}
45-
let grid = HexGrid.generate(boardConfig);
46-
this.state = { grid, config: boardConfig };
47-
}
4840
render() {
49-
let { grid, config } = this.state;
50-
5141
return (
5242
<div className="App">
53-
<HexGrid width={config.width} height={config.height} hexagons={grid.hexagons} layout={grid.layout} />
43+
<HexGrid width={1200} height={800} viewBox="-50 -50 100 100">
44+
{/* Main grid with bit hexagons, all manual */}
45+
<Layout size={{ x: 10, y: 10 }} flat={true} spacing={1.1} origin={{ x: 0, y: 0 }}>
46+
<Hexagon q={0} r={0} s={0} />
47+
{/* Using pattern (defined below) to fill the hexagon */}
48+
<Hexagon q={0} r={-1} s={1} fill="pat-1" />
49+
<Hexagon q={0} r={1} s={-1} />
50+
<Hexagon q={1} r={-1} s={0}>
51+
<Text>1, -1, 0</Text>
52+
</Hexagon>
53+
<Hexagon q={1} r={0} s={-1}>
54+
<Text>1, 0, -1</Text>
55+
</Hexagon>
56+
{/* Pattern and text */}
57+
<Hexagon q={-1} r={1} s={0} fill="pat-2">
58+
<Text>-1, 1, 0</Text>
59+
</Hexagon>
60+
<Hexagon q={-1} r={0} s={1} />
61+
<Hexagon q={-2} r={0} s={1} />
62+
<Path start={new Hex(0, 0, 0)} end={new Hex(-2, 0, 1)} />
63+
</Layout>
64+
</HexGrid>
5465
</div>
5566
);
5667
}
5768
}
5869
```
5970
Will look something like this (custom CSS applied):
60-
![HexGrid image](https://raw.githubusercontent.com/Hellenic/react-hexgrid/master/HexGrid.png "HexGrid")
61-
62-
## Configurations
63-
64-
```javascript
65-
{
66-
width: 1000, // Width of the viewbox
67-
height: 800, // Height of the viewbox
68-
layout: { // Settings on how the tiles looks like
69-
width: 8, // Width of a single tile
70-
height: 8, // Height of a single tile
71-
flat: false, // Defines is the tile pointy one or a flat one
72-
spacing: 1.1, // Spacing between the tiles
73-
name: 'unique-name' // Used to further identify the grid, needed when using multiple instances
74-
},
75-
origin: { // Defines the offset for the grid. Depending on the grid type, you might need to adjust this
76-
x: 0,
77-
y: 0
78-
},
79-
map: 'hexagon', // Grid type (see GridGenerator.js)
80-
// Possible values: hexagon, triangle, parallelogram, rectangle, orientedRectangle
81-
mapProps: [ 3 ] // Properties for the grid type (depends on the grid type) (see GridGenerator.js)
82-
// * 'hexagon': [ radius: Number ]
83-
// * 'triangle': [ size: Number ]
84-
// * 'parallelogram': [ q1: Number, q2: Number, r1: Number, r1: Number ]
85-
// * 'rectangle': [ width: Number, height: Number ]
86-
// * 'orientedRectangle': [ width: Number, height: Number ]
87-
}
88-
```
71+
![HexGrid image](https://raw.githubusercontent.com/Hellenic/react-hexgrid/master/react-hexgrid.png "HexGrid")
8972

9073
## API reference
9174
```javascript
92-
// Available classes
93-
import { HexGrid, Layout, HexUtils, Hex } from 'react-hexgrid';
94-
95-
// Using HexGrid component
96-
<HexGrid
97-
width={1000} // Width of the viewbox
98-
height={800}
99-
path={{ start: new Hex(0,0,0), end: new Hex(3, -3, 0) }} // Path drawn from between the two points (WIP)
100-
actions={} // Action callbacks. See example for all available actions.
101-
hexagons={[]} // Hexagons, e.g. generated by HexGrid.generate or manually created list
102-
layout={layoutConfig} /> // Layout configuration, see configurations above. Affects how tiles get rendered.
103-
viewBox='-50 -50 100 100' // this is the default but you can override it if you wish.
75+
// Available components
76+
import { GridGenerator, HexGrid, HexUtils, Layout, Path, Pattern, Hexagon, Text, Hex } from 'react-hexgrid';
10477
```
10578

106-
* [HexGrid.js](https://github.com/Hellenic/react-hexgrid/tree/master/src/HexGrid.js), Main React component
107-
* HexGrid.generate(config)
108-
* Generates the hexagons. See configuration above.
109-
* [Layout.js](https://github.com/Hellenic/react-hexgrid/tree/master/src/Layout.js), controls the look and feel
110-
* constructor(layout, origin)
111-
* layout : Object { width: Number, height: Number, flat: Boolean, spacing: Number })
112-
* origin : Object [optional] { x: Number, y: Number }
113-
* [HexUtils.js](https://github.com/Hellenic/react-hexgrid/tree/master/src/HexUtils.js), Static methods for calculating distance, checking neighboring tiles, etc.
114-
* [Hex.js](https://github.com/Hellenic/react-hexgrid/tree/master/src/Hex.js)
115-
* contructor(q, r, s, props = {})
116-
* q, r, s : Number, coordinates
117-
* props : Object [optional] { text: String, image: String, className: String } The `className` can be used to style individual hexagons via CSS.
79+
TODO
11880

11981
## Examples
12082

12183
See examples folder.
12284

12385
### Basics
12486

125-
1. [basic-board](https://github.com/Hellenic/react-hexgrid/tree/master/examples/basic-board)- Just simple render of HexGrid
126-
1. [grid-types](https://github.com/Hellenic/react-hexgrid/tree/master/examples/grid-types) - All the different grid types and their configurations
127-
1. [tile-events](https://github.com/Hellenic/react-hexgrid/tree/master/examples/tile-events) - HexGrid with action functions passed down. Just logs to console when different events are triggered.
128-
1. [custom-grid](https://github.com/Hellenic/react-hexgrid/tree/master/examples/custom-grid) - Custom generated Hexagon grid and tile content
129-
130-
## Advanced
131-
132-
1. [pathfinding](https://github.com/Hellenic/react-hexgrid/tree/master/examples/pathfinding) - HexGrid with pathfinding between tiles and highlighting certain hexagons
133-
1. [drag & drop](https://github.com/Hellenic/react-hexgrid/tree/master/examples/drag-and-drop) - 2 HexGrids with possibility to drag hexagons from grid to another
87+
1. [react-hexgrid](https://github.com/Hellenic/react-hexgrid/tree/master/examples/hexgrid-v1) - Basic usage of react-hexgrid
13488

13589
## Testing changes locally
13690
You can test changes by importing the library directly from a folder:

examples/hexgrid-v1/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "hexgrid-v2",
2+
"name": "hexgrid-v1",
33
"version": "0.1.0",
44
"private": true,
55
"devDependencies": {

examples/hexgrid-v1/src/App.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import React, { Component } from 'react';
2-
import { HexGrid, Layout, Path, Hexagon, Text, Pattern, Hex } from 'react-hexgrid';
2+
import { GridGenerator, HexGrid, Layout, Path, Hexagon, Text, Pattern, Hex } from 'react-hexgrid';
33
import './App.css';
44

55
class App extends Component {
66
render() {
77
const hexagonSize = { x: 10, y: 10 };
8+
const moreHexas = GridGenerator.parallelogram(-2, 2, -2, 2);
89
return (
910
<div className="App">
1011
<h2>React Hexgrid v1</h2>
1112
<p>Constructing Hexgrid with component-based approach with custom SVG elements.</p>
1213
<HexGrid width={1200} height={800} viewBox="-50 -50 100 100">
14+
{/* Main grid with bit hexagons, all manual */}
1315
<Layout size={hexagonSize} flat={true} spacing={1.1} origin={{ x: 0, y: 0 }}>
1416
<Hexagon q={0} r={0} s={0} />
1517
{/* Using pattern (defined below) to fill the hexagon */}
@@ -29,11 +31,17 @@ class App extends Component {
2931
<Hexagon q={-2} r={0} s={1} />
3032
<Path start={new Hex(0, 0, 0)} end={new Hex(-2, 0, 1)} />
3133
</Layout>
34+
{/* Additional small grid, hexagons generated with generator */}
35+
<Layout size={{ x: 2, y: 2 }} origin={{ x: 50, y: -30 }}>
36+
{ moreHexas.map((hex, i) => <Hexagon key={i} q={hex.q} r={hex.r} s={hex.s} />) }
37+
</Layout>
3238
{/* You can define multiple patterns and switch between them with "fill" prop on Hexagon */}
3339
<Pattern id="pat-1" link="http://lorempixel.com/400/400/cats/1/" size={hexagonSize} />
3440
<Pattern id="pat-2" link="http://lorempixel.com/400/400/cats/2/" size={hexagonSize} />
3541
<g>
36-
<circle cx="50" cy="10" r="10" />
42+
<circle cx="50" cy="0" r="10" />
43+
<circle cx="50" cy="10" r="8" />
44+
<circle cx="45" cy="20" r="6" />
3745
</g>
3846
</HexGrid>
3947
</div>

react-hexgrid.png

88.3 KB
Loading
File renamed without changes.

src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import GridGenerator from './GridGenerator';
12
import HexGrid from './HexGrid';
23
import HexUtils from './HexUtils';
34
import Layout from './Layout';
@@ -9,4 +10,4 @@ import Text from './Hexagon/Text';
910

1011
import Hex from './models/Hex';
1112

12-
export { HexGrid, HexUtils, Layout, Path, Pattern, Hexagon, Text, Hex };
13+
export { GridGenerator, HexGrid, HexUtils, Layout, Path, Pattern, Hexagon, Text, Hex };

0 commit comments

Comments
 (0)