The beginPath function

Share RGraph:   Share or Like RGraph on Facebook!
[No canvas support]
A simple rectangle using the beginPath function

The beginPath function can be used to begin a new path, with nothing in it, to draw on your canvas. It's a good idea to use because if you start using animation you may get trailing, connecting lines, which only become apparent in animations. These lines can be be due to paths at the end of the drawing being left open and then connecting to the start of drawing on the next frame.

The beginPath function in effect "resets" the path - emptying it of all sub-paths. It's a good idea to use this function whenever you start a new "section" of drawing. There is an associated closePath function but this has the (sometimes undesired) side effect of drawing a line from the last point in your path back to the starting point.

An example

    window.onload = function ()
        var canvas  = document.getElementById("cvs");
        var context = canvas.getContext('2d');

        // Start a new path

© Copyright Richard Heyes 2008-2017