One solution
You can always use the move line trick, but depending on your goal:
If you show many polygons next to each other, you can look at the polygons as simple squares.
- Draw them as such in an external canvas next to each other. This will produce a result without spaces.
- Then convert the main canvas to the position where you want these polygons to appear. Add twist and / or skew depending on the purpose.
- Finally, draw a screen canvas on the main canvas as an image. The problem has disappeared.
This will give you an accurate result without any extra steps when stroking, and the calculations for the boxes will become very simple and fast (think a 2d grid).
However, you must use a screen canvas. If you transform the main canvas and draw the figures, you will encounter the same problem as now. This is due to the fact that each point is converted, and if there is a need for interpolation, it will be calculated separately for each shape of the path. Drawing on the image will add interpolation over the entire surface and only where there are gaps (opaque alpha). Since we are already “space-free”, this is no longer a problem.
This will require an additional step in planning for their proper placement, but this is a simple step.
Example
Step 1 - pull the boxes into the screen canvas:
This code relies on a screen canvas, resulting in two rectangles without a space:

(the example uses a screen to display the result, see the next step to use the screen saver)
var ctx = document.querySelector("canvas").getContext("2d"); ctx.fillStyle = "red"; ctx.fillRect(10, 10, 50, 50); ctx.fillRect(60, 10, 50, 50);
<canvas/>
Step 2 - transforming the main canvas and drawing off-screen
When entering into the main canvas with a set of transformations, the result will be (a pseudo-random transformation is just for display):

var ctx = document.querySelector("canvas").getContext("2d");
<canvas />
Step 3 (optional) - Interaction
If you want to interact with the fields, you just apply the same transformation, then add the path to the box and check it for mouse position. Redraw one state, erase by clearing and pulling the screen canvas from above:
var ctx = document.querySelector("canvas").getContext("2d");
<canvas />
source share