Canvas beginPath() starts a new path and clears the subpaths accumulated by earlier drawing commands. Call it before defining an independent shape so later strokes and fills do not reuse previous geometry or receive unintended styles. section 15.2.2 section 15.2.3
Use beginPath() as the first step in each independent Canvas shape sequence:
- Start a path with beginPath(). section 15.2.2
- Add geometry with commands such as moveTo(), lineTo(), rect(), arc(), or ellipse(). section 15.2.2 section 15.2.4
- Render the path with stroke() or fill(), after setting the desired strokeStyle or fillStyle. section 15.2.3
Drawing separate shapes
context.beginPath(); context.arc(706, 106, 66, 0, 2 * Math.PI); context.stroke(); context.beginPath(); context.ellipse(706, 273, 66, 40, 0, 0, 2 * Math.PI); context.fill(); The second beginPath() keeps the ellipse independent from the circle. section 15.2.4
Listing 15.5 Drawing a line (main.js)
context.beginPath(); context.moveTo(66, 60); context.lineTo(186, 300); context.stroke();
Inspect the complete four-command sequence for drawing a line: beginPath(), moveTo(), lineTo(), and stroke(). section 15.2.2
