@@ -2,7 +2,7 @@ import React, { Component } from "react";
22import PropTypes from "prop-types" ;
33import classNames from "classnames" ;
44import memoize from "memoize-one" ;
5- import Hexagon from "./Hexagon/Hexagon " ;
5+ import Hex from "./models/Hex " ;
66import HexUtils from "./HexUtils" ;
77import Orientation from "./models/Orientation" ;
88import Point from "./models/Point" ;
@@ -33,48 +33,51 @@ export class Layout extends Component {
3333 return new Point ( size . x * Math . cos ( angle ) , size . y * Math . sin ( angle ) ) ;
3434 }
3535
36- // TODO Refactor
3736 static calculateCoordinates ( orientation , size ) {
38- const corners = [ ] ;
3937 const center = new Point ( 0 , 0 ) ;
40-
41- Array . from ( new Array ( 6 ) , ( x , i ) => {
38+ return [ ...Array ( 6 ) . keys ( ) ] . map ( ( _ , i ) => {
4239 const offset = Layout . getPointOffset ( i , orientation , size ) ;
43- const point = new Point ( center . x + offset . x , center . y + offset . y ) ;
44- return corners . push ( point ) ;
40+ return new Point ( center . x + offset . x , center . y + offset . y ) ;
4541 } ) ;
42+ }
43+
44+ static filterViewBox ( layout , viewBox ) {
45+ const { height, width, x, y } = viewBox ;
46+ const x_ = x - layout . size . x + layout . spacing + 1 ;
47+ const y_ = y - layout . size . y + layout . spacing + 1 ;
48+ const width_ = width + layout . size . x + layout . spacing + 1 ;
49+ const height_ = height + layout . size . y + layout . spacing + 1 ;
4650
47- return corners ;
51+ return {
52+ x : x_ ,
53+ y : y_ ,
54+ width : width_ ,
55+ height : height_
56+ } ;
4857 }
4958
5059 filterChildren = memoize ( ( children , layout , viewBox ) => {
5160 const childrenArray = React . Children . toArray ( children ) ;
52- const { height, width, x, y } = viewBox ;
53- const cornerCoords = Layout . calculateCoordinates (
54- layout . orientation ,
55- layout . size
56- ) ;
61+ const { x, y, width, height } = Layout . filterViewBox ( layout , viewBox ) ;
62+
5763 return childrenArray . filter ( child => {
5864 if ( ! child . props ) {
5965 return true ;
6066 }
6167 if (
62- child . type === Hexagon ||
63- ( child . props . q !== undefined &&
64- child . props . r !== undefined &&
65- child . props . s !== undefined )
68+ child . props !== undefined &&
69+ child . props . q !== undefined &&
70+ child . props . r !== undefined &&
71+ child . props . s !== undefined
6672 ) {
67- const point = HexUtils . hexToPixel ( child . props , layout ) ;
68- const corners = cornerCoords . map (
69- coord => new Point ( coord . x + point . x , coord . y + point . y )
73+ const { q, r, s } = child . props ;
74+ const point = HexUtils . hexToPixel ( new Hex ( q , r , s ) , layout ) ;
75+ return (
76+ point . x > x &&
77+ point . x < width + x &&
78+ point . y > y &&
79+ point . y < height + y
7080 ) ;
71- return corners . filter (
72- corner =>
73- corner . x > x &&
74- corner . x < width + x &&
75- corner . y > y &&
76- corner . y < + height + y
77- ) . length ;
7881 }
7982 return true ;
8083 } ) ;
0 commit comments