YouTip LogoYouTip

Canvas Beginpath

## HTML Canvas `beginPath()` Method The `beginPath()` method of the Canvas 2D API starts a new path by emptying the list of sub-paths. Call this method when you want to create a brand-new path, ensuring that subsequent drawing commands are not appended to previous ones. --- ## Definition and Usage The `beginPath()` method resets the current path. When you draw on a canvas, the 2D context maintains a "current path". If you do not call `beginPath()` before drawing a new shape, any new lines or shapes you define will be added to the existing path. When you eventually call `stroke()` or `fill()`, the browser will redraw the entire accumulated path, which can lead to unexpected styling issues (such as previous lines changing color or thickness). * **To define paths, use:** `moveTo()`, `lineTo()`, `quadricCurveTo()`, `bezierCurveTo()`, `arcTo()`, and `arc()`. * **To render the path on the canvas, use:** `stroke()` (for outlines) or `fill()` (for solid shapes). --- ## Syntax ```javascript context.beginPath(); ``` ### Parameters This method does not accept any parameters. ### Return Value This method does not return any value (`undefined`). --- ## Browser Support The `beginPath()` method is widely supported across all modern web browsers: * Google Chrome * Mozilla Firefox * Microsoft Edge / Internet Explorer 9+ * Safari * Opera *Note: Internet Explorer 8 and earlier versions do not support the `` element.* --- ## Code Example The following example demonstrates how to draw two distinct paths with different colors (green and purple) on a canvas. By using `beginPath()`, we ensure that the two lines are treated as independent paths with their own styles. ### HTML ```html Your browser does not support the HTML5 canvas tag. ``` ### JavaScript ```javascript var c = document.getElementById("myCanvas"); var ctx = c.getContext("2d"); // 1. Draw the first path (Green Line) ctx.beginPath(); // Start a new path ctx.lineWidth = "5"; ctx.strokeStyle = "green"; // Set stroke color to green ctx.moveTo(0, 75); // Move pen to starting point ctx.lineTo(250, 75); // Draw a line to the endpoint ctx.stroke(); // Render the green line // 2. Draw the second path (Purple Line) ctx.beginPath(); // Reset the path to start a new one ctx.strokeStyle = "purple"; // Set stroke color to purple ctx.moveTo(50, 0); // Move pen to a new starting point ctx.lineTo(150, 130); // Draw a line to the endpoint ctx.stroke(); // Render the purple line ``` --- ## Key Considerations & Best Practices ### Why is `beginPath()` crucial? If you omit the second `beginPath()` in the example above, the canvas will group both lines into a single path. When `ctx.stroke()` is called the second time with `ctx.strokeStyle = "purple"`, the browser will redraw the entire path (both the first and second lines) in purple. As a result, both lines would end up purple instead of one green and one purple. ### Always separate your shapes As a best practice, always call `beginPath()` before starting a new shape or line segment unless you intentionally want them to be part of the same sub-path (for example, when drawing complex polygons).
← Canvas MovetoCanvas Stroke β†’