使用帆布html&用六角形绘制网格JavaScript
我尝试使用六角形形状创建一个大小x * x的画布,目前我基于此参考,有了一些改编,我的代码到现在为此看起来像:
const canvas = document.getElementById('canvas');
const context = canvas.getContext('2d');
const shapeType = 6;
const angle = 2 * Math.PI / shapeType;
const radius = 20;
function init() {
drawGrid(5);
}
init();
function drawGrid(size) {
for (let y = radius, i = 0; i < size; i++) {
y += radius * Math.sin(angle);
for (let x = radius, j = 0;
j < size;
x += radius * (1 + Math.cos(angle)), y += (-1) ** j++ * radius * Math.sin(angle)) {
drawHexagon(x, y);
}
}
}
function drawHexagon(x, y) {
context.beginPath();
for (let i = 0; i < shapeType; i++) {
let xx = x + radius * Math.cos(angle * i);
let yy = y + radius * Math.sin(angle * i);
context.lineTo(xx, yy);
}
context.closePath();
context.stroke();
}
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>HexGrid</title>
<style>
.test {
display: flex;
justify-content: space-between;
}
.canvas {
border: 1px solid #000000;
}
</style>
</head>
<body>
<canvas id="canvas" width="500" height="500" class="canvas"></canvas>
<script src="main.js"></script>
</body>
</html>
到目前为止,每件事都很好,但是只要使用奇数,但是如果大小为,我就会明白:
作为JavaScript的初学者,我在此中被阻止了,有人知道我该如何解决?
I try to create a canvas with X * X of size, using hexagon shapes, currently I base on this reference, with some adaptations, my code until now look like this:
const canvas = document.getElementById('canvas');
const context = canvas.getContext('2d');
const shapeType = 6;
const angle = 2 * Math.PI / shapeType;
const radius = 20;
function init() {
drawGrid(5);
}
init();
function drawGrid(size) {
for (let y = radius, i = 0; i < size; i++) {
y += radius * Math.sin(angle);
for (let x = radius, j = 0;
j < size;
x += radius * (1 + Math.cos(angle)), y += (-1) ** j++ * radius * Math.sin(angle)) {
drawHexagon(x, y);
}
}
}
function drawHexagon(x, y) {
context.beginPath();
for (let i = 0; i < shapeType; i++) {
let xx = x + radius * Math.cos(angle * i);
let yy = y + radius * Math.sin(angle * i);
context.lineTo(xx, yy);
}
context.closePath();
context.stroke();
}
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>HexGrid</title>
<style>
.test {
display: flex;
justify-content: space-between;
}
.canvas {
border: 1px solid #000000;
}
</style>
</head>
<body>
<canvas id="canvas" width="500" height="500" class="canvas"></canvas>
<script src="main.js"></script>
</body>
</html>
Until now, every thing is good, but just with odd size, but if the size is even, I get this:
As a beginner in JavaScript, I'm blocked in this, any one have any idea how can I solve this please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
常规的六边形网格
一个六角形的网格具有常规的间距,就像一个正方形的网格一样,但是,如果偏移(在这种情况下为下降),则每2列降低了一半的六角形高度。
要获得X和Y网格间距,偏移量是Y网格间距的一半。
可以使用六角形的位置(GX,GY为列,行位置)
示例
示例用六边形网格填充画布。
Regular Hexagon Grid
A grid of hexagons has a regular spacing just like a grid of squares however every 2nd column if offset (down in this case) by half the hexagons height.
To get the x and y grid spacing, and the offset is half the y grid spacing.
The hexagon's position can be calculated using (gx, gy are column, row positions)
Example
Example fills canvas with hexagon grid.
我转换了 blindman67的响应对可重复使用的功能。
它是高度可配置的。
I converted Blindman67's response to a reusable function.
It is highly configurable.