I'm drawing polygons on a canvas with an underlying grid
I want to split this polygon into multiple polygons now (based on the grid)
So instead of 1 polygon, I get the coordinates for 4 polygons.
Is there an easy solution for this I'm not thinking about?
This is the code for my test canvas (codepen)
<script>
var bw = 200;
var bh = 200;
var p = 0;
var cw = bw + (p*2) + 1;
var ch = bh + (p*2) + 1;
var grid = 50;
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
function drawBoard(){
context.beginPath();
for (var x = 0; x <= bw; x += grid){
context.moveTo(0.5 + x + p, p);
context.lineTo(0.5 + x + p, bh + p);
}
for (var x = 0; x <= bh; x += grid) {
context.moveTo(p, 0.5 + x + p);
context.lineTo(bw + p, 0.5 + x + p);
}
context.lineWidth = 1;
context.strokeStyle = "black";
context.stroke();
}
drawBoard();
// Polygon
context.fillStyle = '#f00';
context.beginPath();
context.moveTo(0, 0);
context.lineTo(100,50);
context.lineTo(50, 100);
context.lineTo(0, 90);
context.closePath();
context.fill();
</script>