On an <canvas>, straight lines are path segments. Use moveTo() to place the pen, lineTo() to add a segment, then stroke() to paint it.
Style lines with lineWidth, strokeStyle, lineCap, lineJoin, and setLineDash(). Start each separate stroke with beginPath(). For richer shapes, continue with paths, arcs, and curves.
Baseline · Widely available.
moveTo(), lineTo(), and stroke() are stable Canvas 2D features across modern browsers since about July 2015.
Path first, paint second.
lineTo() does not draw by itself. Always finish with stroke() (or fill() for closed shapes).
Reset with setLineDash([]) so later strokes are solid.
Edge Cases & Pitfalls
Most blank lines come from a missing stroke() or leftover dash/cap state.
paint
lineTo is not a paint call
The path stays invisible until stroke() (or fill() on a closed path).
path
Use beginPath() for separate strokes
Without it, new segments attach to the previous path and share one stroke.
state
Dash and cap persist
Reset with setLineDash([]) and set lineCap back to "butt" when needed. Prefer save()/restore().
joins
lineJoin needs corners
miter, round, and bevel only show at corners of connected segments — not on a single straight line.
half-pixel
1px lines can look blurry
For crisp 1px horizontals/verticals, try half-pixel coords (e.g. y + 0.5) so the stroke sits on a pixel boundary.
Compatibility
Browser Support
moveTo(), lineTo(), stroke(), and line styling are part of the Canvas 2D API in every modern browser.
✓ Baseline · Widely available
lineTo()
Widely available since about July 2015. Chrome, Firefox, Safari, Edge, and Opera support straight lines, caps, joins, and dashes. Internet Explorer 9+ supports core line drawing.
98%Global support (approx.)
Google ChromeAll modern
Full support
Mozilla FirefoxAll modern
Full support
Apple SafariAll modern
Full support
Microsoft EdgeAll modern
Full support
OperaAll modern
Full support
Samsung InternetAll modern
Full support
Internet Explorer9+
Supported
lineTo / strokeWidely supported
Bottom line: Safe to use for charts, UI strokes, and diagrams everywhere modern Canvas 2D runs.