Skip to content
Advertisement

Performances of ‘lineTo’ method when going beyond canvas dimensions

Using a canvas context, assuming I call beginPath then twice lineTo with x=-999, y=-999 and x=50, y=50 then finally stroke, it will draw a line from the top-left corner to the (50,50) point.

During the process did the top-left outside part of the line was draw from the outside or did it was draw from the visible canvas part, in our case (0,0).

  • In the first case, is it worth safing the coordinates with a safeCoordinates function, particurlarly if we need a lot of points, when we want to draw a mathematical function for example.

  • In the second case well ok it’s perfect.

See the above example where I have two red line without safing the coordinates and two green line with safe coordinates :

JavaScript
JavaScript
JavaScript

Advertisement

Answer

To answer your question we need a benchmark. Look at the following, the safe draw is taking longer than the unsafe draw. Probably due to extra calculation on your part.

JavaScript
JavaScript
JavaScript

To be sure about that you can also try to draw an extra big line and see if it changes the result. If the line is drawed, then it should slow down the execution. But answers is no.

JavaScript
JavaScript
JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement