-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
229 additions
and
285 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,123 +1,122 @@ | ||
import React, { Component } from "react"; | ||
import React from "react"; | ||
import PropTypes from "prop-types"; | ||
import classNames from "classnames"; | ||
import { HexEngineProvider } from "./Context"; | ||
import Point from "./models/Point"; | ||
import Orientation from "./models/Orientation"; | ||
|
||
class HexEngine extends Component { | ||
static propTypes = { | ||
children: PropTypes.node.isRequired, | ||
classes: PropTypes.objectOf(PropTypes.string), | ||
/** Determines if the hexagons are oriented with a point or an edge facing up */ | ||
flat: PropTypes.bool, | ||
/** CSS string or number */ | ||
height: PropTypes.oneOfType([ | ||
PropTypes.string.isRequired, | ||
PropTypes.number.isRequired | ||
]), | ||
/** Origin of the grid */ | ||
origin: PropTypes.oneOfType([ | ||
PropTypes.instanceOf(Point), | ||
PropTypes.shape({ | ||
x: PropTypes.number.isRequired, | ||
y: PropTypes.number.isRequired | ||
}) | ||
]), | ||
/** Size of the hexagons in each dimension */ | ||
size: PropTypes.oneOfType([ | ||
PropTypes.instanceOf(Point), | ||
PropTypes.shape({ | ||
x: PropTypes.number.isRequired, | ||
y: PropTypes.number.isRequired | ||
}) | ||
]), | ||
/** Space between hexagons */ | ||
spacing: PropTypes.number, | ||
/** CSS string or number */ | ||
width: PropTypes.oneOfType([ | ||
PropTypes.string.isRequired, | ||
PropTypes.number.isRequired | ||
]), | ||
/** The viewBox {x,y,width,height} of the svg view area */ | ||
viewBox: PropTypes.shape({ | ||
height: PropTypes.number.isRequired, | ||
width: PropTypes.number.isRequired, | ||
x: PropTypes.number.isRequired, | ||
y: PropTypes.number.isRequired | ||
}) | ||
}; | ||
function getPointOffset(corner, orientation, size) { | ||
let angle = (2.0 * Math.PI * (corner + orientation.startAngle)) / 6; | ||
return new Point(size.x * Math.cos(angle), size.y * Math.sin(angle)); | ||
} | ||
|
||
static defaultProps = { | ||
classes: { grid: "", layout: "" }, | ||
flat: true, | ||
height: 480, | ||
origin: new Point(0, 0), | ||
size: new Point(10, 10), | ||
spacing: 1.0, | ||
width: 640, | ||
viewBox: { | ||
height: 100, | ||
width: 100, | ||
x: -50, | ||
y: -50 | ||
} | ||
}; | ||
function calculateCoordinates(orientation, size) { | ||
const center = new Point(0, 0); | ||
return [...Array(6).keys()].map((_, i) => { | ||
const offset = getPointOffset(i, orientation, size); | ||
return new Point(center.x + offset.x, center.y + offset.y); | ||
}); | ||
} | ||
|
||
static getPointOffset(corner, orientation, size) { | ||
let angle = (2.0 * Math.PI * (corner + orientation.startAngle)) / 6; | ||
return new Point(size.x * Math.cos(angle), size.y * Math.sin(angle)); | ||
} | ||
const HexEngine = props => { | ||
const { | ||
children, | ||
classes, | ||
flat, | ||
height, | ||
origin, | ||
size, | ||
spacing, | ||
viewBox, | ||
width | ||
} = props; | ||
|
||
static calculateCoordinates(orientation, size) { | ||
const center = new Point(0, 0); | ||
return [...Array(6).keys()].map((_, i) => { | ||
const offset = HexEngine.getPointOffset(i, orientation, size); | ||
return new Point(center.x + offset.x, center.y + offset.y); | ||
}); | ||
} | ||
const orientation = flat ? Orientation.Flat : Orientation.Pointy; | ||
const points = calculateCoordinates(orientation, size) | ||
.map(point => point.toString()) | ||
.join(" "); | ||
const layout = { orientation, origin, size, spacing }; | ||
|
||
render() { | ||
const { | ||
children, | ||
classes, | ||
flat, | ||
height, | ||
origin, | ||
size, | ||
spacing, | ||
viewBox, | ||
width | ||
} = this.props; | ||
return ( | ||
<svg | ||
key={JSON.stringify(layout)} | ||
className={classNames("grid", classes.grid)} | ||
height={height} | ||
version="1.1" | ||
viewBox={`${viewBox.x} ${viewBox.y} ${viewBox.width} ${viewBox.height}`} | ||
width={width} | ||
xmlns="http://www.w3.org/2000/svg" | ||
> | ||
<HexEngineProvider | ||
value={{ | ||
layout, | ||
points, | ||
viewBox | ||
}} | ||
> | ||
<g className={classes.layout}>{children}</g> | ||
</HexEngineProvider> | ||
</svg> | ||
); | ||
}; | ||
|
||
const orientation = flat ? Orientation.Flat : Orientation.Pointy; | ||
const points = HexEngine.calculateCoordinates(orientation, size) | ||
.map(point => point.toString()) | ||
.join(" "); | ||
const layout = { orientation, origin, size, spacing }; | ||
HexEngine.propTypes = { | ||
children: PropTypes.node.isRequired, | ||
classes: PropTypes.objectOf(PropTypes.string), | ||
/** Determines if the hexagons are oriented with a point or an edge facing up */ | ||
flat: PropTypes.bool, | ||
/** CSS string or number */ | ||
height: PropTypes.oneOfType([ | ||
PropTypes.string.isRequired, | ||
PropTypes.number.isRequired | ||
]), | ||
/** Origin of the grid */ | ||
origin: PropTypes.oneOfType([ | ||
PropTypes.instanceOf(Point), | ||
PropTypes.shape({ | ||
x: PropTypes.number.isRequired, | ||
y: PropTypes.number.isRequired | ||
}) | ||
]), | ||
/** Size of the hexagons in each dimension */ | ||
size: PropTypes.oneOfType([ | ||
PropTypes.instanceOf(Point), | ||
PropTypes.shape({ | ||
x: PropTypes.number.isRequired, | ||
y: PropTypes.number.isRequired | ||
}) | ||
]), | ||
/** Space between hexagons */ | ||
spacing: PropTypes.number, | ||
/** CSS string or number */ | ||
width: PropTypes.oneOfType([ | ||
PropTypes.string.isRequired, | ||
PropTypes.number.isRequired | ||
]), | ||
/** The viewBox {x,y,width,height} of the svg view area */ | ||
viewBox: PropTypes.shape({ | ||
height: PropTypes.number.isRequired, | ||
width: PropTypes.number.isRequired, | ||
x: PropTypes.number.isRequired, | ||
y: PropTypes.number.isRequired | ||
}) | ||
}; | ||
|
||
return ( | ||
<svg | ||
key={JSON.stringify(layout)} | ||
className={classNames("grid", classes.grid)} | ||
height={height} | ||
version="1.1" | ||
viewBox={`${viewBox.x} ${viewBox.y} ${viewBox.width} ${viewBox.height}`} | ||
width={width} | ||
xmlns="http://www.w3.org/2000/svg" | ||
> | ||
<HexEngineProvider | ||
value={{ | ||
layout, | ||
points, | ||
viewBox | ||
}} | ||
> | ||
<g className={classes.layout}>{children}</g> | ||
</HexEngineProvider> | ||
</svg> | ||
); | ||
HexEngine.defaultProps = { | ||
classes: { grid: "", layout: "" }, | ||
flat: true, | ||
height: 480, | ||
origin: new Point(0, 0), | ||
size: new Point(10, 10), | ||
spacing: 1.0, | ||
width: 640, | ||
viewBox: { | ||
height: 100, | ||
width: 100, | ||
x: -50, | ||
y: -50 | ||
} | ||
} | ||
}; | ||
|
||
export default HexEngine; | ||
export const HexEngine_ = HexEngine; | ||
export default HexEngine_; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,35 @@ | ||
import React, { Component } from "react"; | ||
import React from "react"; | ||
import PropTypes from "prop-types"; | ||
import HexUtils from "./HexUtils"; | ||
import { withHexEngine } from "./Context"; | ||
|
||
class Path extends Component { | ||
static propTypes = { | ||
end: PropTypes.object, | ||
layout: PropTypes.object, | ||
start: PropTypes.object | ||
}; | ||
|
||
// TODO Refactor | ||
getPoints() { | ||
const { end, layout, start } = this.props; | ||
if (!end || !start) { | ||
return ""; | ||
} | ||
function getPoints(start, end, layout) { | ||
if (!end || !start) { | ||
return ""; | ||
} | ||
|
||
// Get all the intersecting hexes between start and end points | ||
const distance = HexUtils.distance(start, end); | ||
const intersects = []; | ||
const step = 1.0 / Math.max(distance, 1); | ||
for (let i = 0; i <= distance; i++) { | ||
intersects.push(HexUtils.round(HexUtils.hexLerp(start, end, step * i))); | ||
} | ||
const distance = HexUtils.distance(start, end); | ||
const step = 1.0 / Math.max(distance, 1); | ||
const points = [...Array(distance + 1).keys()] | ||
.map(i => { | ||
const hex = HexUtils.round(HexUtils.hexLerp(start, end, step * i)); | ||
const pixel = HexUtils.hexToPixel(hex, layout); | ||
return ` ${pixel.x},${pixel.y} `; | ||
}) | ||
.join("L"); | ||
|
||
// Construct Path points out of all the intersecting hexes (e.g. M 0,0 L 10,20, L 30,20) | ||
let points = "M"; | ||
points += intersects | ||
.map(hex => { | ||
let p = HexUtils.hexToPixel(hex, layout); | ||
return ` ${p.x},${p.y} `; | ||
}) | ||
.join("L"); | ||
return `M${points}`; | ||
} | ||
|
||
return points; | ||
} | ||
export const Path = ({ start, end, layout }) => ( | ||
<path d={getPoints(start, end, layout)} /> | ||
); | ||
|
||
render() { | ||
return <path d={this.getPoints()} />; | ||
} | ||
} | ||
Path.propTypes = { | ||
end: PropTypes.object, | ||
layout: PropTypes.object, | ||
start: PropTypes.object | ||
}; | ||
|
||
export default withHexEngine(Path); | ||
export const Path_ = Path; | ||
export default withHexEngine(Path_); |
Oops, something went wrong.