Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Fabric.js – How to draw a hexagonal grid (honey comb) with Polygon class
We can create a Polygon object by creating an instance of fabric.Polygon. A polygon object can be characterized by any closed shape consisting of a set of connected straight line segments. Since it is one of the basic elements of FabricJS, we can also easily customize it by applying properties like angle, opacity etc.
Syntax
new fabric.Polygon( points: Array, options: Object )
Parameters
-
points ? This parameter accepts an Array which denotes the array of points that make up the polygon object.
-
options (optional) ? This parameter is an Object which provides additional customizations to our object. Using this parameter origin, stroke width and a lot of other properties can be changed related to the Polygon object.
Example 1: Drawing a Hexagon using Polygon
Let's see a code example to see how we draw a hexagon using polygon. We can draw many types of hexagons, however, in this example we will draw a regular hexagon. We know that a regular hexagon has six equal sides.
<!DOCTYPE html>
<html>
<head>
<!-- Adding the Fabric JS Library-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/510/fabric.min.js"></script>
</head>
<body>
<h2>Drawing a Hexagon using Polygon</h2>
<p>You can see a hexagon object has been added to the canvas</p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
// Initiate a polygon object
var hexagon = new fabric.Polygon(
[
{ x: 50, y: 0 },
{ x: 25, y: 43.30},
{ x: -25, y: 43.301 },
{ x: -50, y: 0},
{ x: -25, y: -43.301},
{ x: 25, y: -43.301 },
],
{
stroke: "red",
left: 140,
top: 10,
strokeWidth: 2,
strokeLineJoin: "bevil",
}
);
// Adding it to the canvas
canvas.add(hexagon);
</script>
</body>
</html>
Example 2: Drawing a Hexagonal Grid using Polygon
Let's see a code example to see how we can create a hexagonal grid (honeycomb pattern). We create a function called drawHexagon(left, top) where (left, top) is the position of a hexagon. This function draws the hexagon whenever it is called. We also create the drawGrid() function which draws consecutive hexagons by calculating proper spacing to create the honeycomb pattern.
<!DOCTYPE html>
<html>
<head>
<!-- Adding the Fabric JS Library-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/510/fabric.min.js"></script>
</head>
<body>
<h2>Drawing a Hexagonal Grid using Polygon</h2>
<p>You can see that a hexagonal grid has been drawn</p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(350);
// Hexagon dimensions
const hexRadius = 30;
const hexWidth = hexRadius * 2;
const hexHeight = Math.sqrt(3) * hexRadius;
// Function to draw a single hexagon
function drawHexagon(left, top) {
var hexagon = new fabric.Polygon(
[
{ x: hexRadius, y: 0 },
{ x: hexRadius/2, y: hexHeight/2},
{ x: -hexRadius/2, y: hexHeight/2 },
{ x: -hexRadius, y: 0},
{ x: -hexRadius/2, y: -hexHeight/2},
{ x: hexRadius/2, y: -hexHeight/2 },
],
{
stroke: "#EEC33D",
fill: "#BB900C",
strokeWidth: 2,
left: left,
top: top
}
);
// Adding it to the canvas
canvas.add(hexagon);
}
// Function to draw hexagonal grid (honeycomb pattern)
function drawGrid() {
const rows = 4;
const cols = 6;
for (let row = 0; row < rows; row++) {
for (let col = 0; col < cols; col++) {
// Calculate position for honeycomb pattern
let x = col * (hexWidth * 0.75) + 50;
let y = row * hexHeight + (col % 2) * (hexHeight / 2) + 50;
drawHexagon(x, y);
}
}
}
// Calling drawGrid function
drawGrid();
</script>
</body>
</html>
How It Works
The hexagonal grid pattern requires specific spacing calculations:
- Horizontal spacing: Each column is offset by 75% of the hexagon width to create the interlocking pattern
- Vertical offset: Alternating columns are shifted by half the hexagon height to create the honeycomb effect
- Hexagon coordinates: Six points are calculated using trigonometry to form a regular hexagon
Conclusion
In this tutorial, we demonstrated how to use FabricJS Polygon class to create both individual hexagons and complete hexagonal grids. The key is calculating proper spacing and offsets to achieve the authentic honeycomb pattern.
