import React, { Component } from 'react';
import { Stage, Layer, Line, Text, Rect, Circle, Group, Label, Tag } from 'react-konva';
export default class Grid extends Component {
constructor(...props) {
super(...props);
this.xOffset = this.props.xOffset || 0;
this.yOffset = this.props.yOffset || 0;
this.state = {
verticalLines: [],
horizontalLines: []
}
}
render () {
// this.mainVLine = this.textLine(this.props.width / 2, 0, this.props.width / 2, this.props.height);
// this.mainHLine = this.textLine(0, this.props.height / 2, this.props.width, this.props.height / 2);
// const vLines = [this.mainVLine];
// for (let i = 1; this.mainVLine.x1 - i * this.props.step > 0; ++i) {
// vLines.push(this.textLine(this.mainVLine.x1 - i * this.props.step, this.mainVLine.y1, this.mainVLine.x2 - i * this.props.step, this.mainVLine.y2));
// }
// for (let i = 1; this.mainVLine.x1 + i * this.props.step < this.props.width; ++i) {
// vLines.push(this.textLine(this.mainVLine.x1 + i * this.props.step, this.mainVLine.y1, this.mainVLine.x2 + i * this.props.step, this.mainVLine.y2));
// }
// const hLines = [this.mainHLine];
// for (let i = 1; this.mainHLine.y1 - i * this.props.step > 0; ++i) {
// hLines.push(this.textLine(this.mainHLine.x1, this.mainHLine.y1 - i * this.props.step, this.mainHLine.x2, this.mainHLine.y2 - i * this.props.step));
// }
// for (let i = 1; this.mainHLine.y1 + i * this.props.step < this.props.height; ++i) {
// hLines.push(this.textLine(this.mainHLine.x1, this.mainHLine.y1 + i * this.props.step, this.mainHLine.x2, this.mainHLine.y2 + i * this.props.step));
// }
this.mainVLine = this.textLine(0-this.xOffset, 0-this.yOffset, 0-this.xOffset, this.props.height-this.yOffset);
this.mainHLine = this.textLine(0-this.xOffset, 0-this.yOffset, this.props.width-this.xOffset, 0-this.yOffset);
const vLines = [this.mainVLine];
for (let i = 1; this.mainVLine.x1 + i * this.props.step < this.props.width; ++i) {
vLines.push(this.textLine(this.mainVLine.x1 + i * this.props.step, this.mainVLine.y1, this.mainVLine.x2 + i * this.props.step, this.mainVLine.y2));
}
const hLines = [this.mainHLine];
for (let i = 1; this.mainHLine.y1 + i * this.props.step < this.props.height; ++i) {
hLines.push(this.textLine(this.mainHLine.x1, this.mainHLine.y1 + i * this.props.step, this.mainHLine.x2, this.mainHLine.y2 + i * this.props.step));
}
this.state = {
verticalLines: vLines,
horizontalLines: hLines,
};
return (
<Group>
{this.state.verticalLines.map((el, i) => (
<Line points={[el.x1 + this.props.xOffset, el.y1 + this.props.yOffset, el.x2 + this.props.xOffset, el.y2 + this.props.yOffset]} stroke={this.props.stroke} key={`grid_v_line${i}`} />
))}
{this.state.horizontalLines.map((el, i) => (
<Line points={[el.x1 + this.props.xOffset, el.y1 + this.props.yOffset, el.x2 + this.props.xOffset, el.y2 + this.props.yOffset]} stroke={this.props.stroke} key={`grid_h_line${i}`} />
))}
</Group>
);
}
textLine(x1, y1, x2, y2) {
return {x1: x1, y1: y1, x2: x2, y2: y2};
}
}