Chapter 10: SVG polyline

SVG Polyline means using the <polyline> tag inside SVG to draw a chain of connected straight lines (an open path made of multiple line segments). It’s like <polygon> but does NOT close automatically — the last point does not connect back to the first one. That’s the big difference!

  • Use polyline when you want an open shape (like a zigzag line, graph plot, path that doesn’t loop, outline without filling a closed area).
  • Use polygon when you want a closed shape (triangle, star, house) that fills inside.

1. Basic Syntax of <polyline>

The main attribute is points — same as polygon, but no auto-closing.

XML
  • Self-closing tag (/>)
  • Must be inside <svg>…</svg>
  • points is required — list of x,y pairs separated by spaces (comma inside each pair)
  • At least 2 points needed (otherwise invisible)
  • No automatic closing → last point stays open (unlike <polygon>)
  • Fill usually set to none (or transparent) because open shapes don’t have a clear “inside” — browser might fill oddly if you don’t.

2. Your First SVG Polyline (Copy-Paste Right Now!)

Save as polyline.html:

HTML

What you see: A blue zigzag line going up-down-up-down — open at both ends, no closing connection.

Breakdown:

  • points=”20,180 100,20 200,180 300,20 380,180″
    • Starts at bottom-left-ish
    • Goes up to near top, down again, up, down to end
  • fill=”none” → no inside fill (very important for open shapes!)
  • stroke=”blue” stroke-width=”6″ → makes the lines visible and thick
  • Zoom browser — stays perfectly sharp!

3. All Important Attributes (Full Class Notes Style)

Attribute Meaning Required to See? Default Example values Notes
points List of x,y coordinate pairs Yes “0,100 50,25 100,75 150,0” Space separated pairs, comma inside
stroke Color of the line segments Yes (usually) none red, #00ff00 Without stroke = invisible!
stroke-width Thickness of lines No 1 4, 10 In user units
fill Fill color (inside if closed-ish) No black none, yellow Set to “none” for open polylines
stroke-linecap Style of line ends No butt round, square “round” looks smooth
stroke-linejoin Style of joins/corners No miter round, bevel Controls sharp corners
stroke-dasharray Dashed/dotted pattern No “10 5”, “5 5 10” Dash-gap pattern
opacity Transparency for whole polyline No 1 0.7 Affects stroke + fill

4. More Real & Useful Examples (Try These!)

Example 1: Simple Graph Line (Like Stock Chart)

XML

→ Wavy upward trend line (open path)

Example 2: Dashed Open Shape (Like Outline Path)

XML

→ Dashed zigzag

Example 3: Hover Effect with CSS

HTML

Hover → thicker solid orange line!

Example 4: Filled Polyline (Rare, but Possible – Looks Like Polygon)

XML

→ If you fill it, browser treats it almost like a polygon (but still technically open — fill rule might vary slightly in some browsers)

5. Quick Comparison: <polyline> vs <polygon>

Feature <polyline> <polygon>
Closes automatically? No – open path Yes – last point connects to first
Best for Lines, plots, paths, open zigzags Closed shapes (triangle, star, house)
Fill behavior Usually “none” (no clear inside) Fills the enclosed area nicely
Points needed 2+ 3+
Common use Graphs, diagrams, connectors Icons, buttons, filled shapes

6. Teacher’s Tips (Hyderabad Student Style 😄)

  • Always set fill=”none” for typical polylines — otherwise browser might fill weirdly
  • Stroke is king here — forget stroke color/thickness = invisible shape!
  • For very complex paths (curves too) → use <path> instead (more powerful, but harder)
  • Responsive websites: Use viewBox so polyline scales perfectly on mobile
  • Common mistake: Copy polygon code → forget to change to polyline + set fill=”none” → looks closed but isn’t!
  • Real jobs: Polylines great for data visualization (charts), SVG icons with strokes, animated drawing effects (stroke-dashoffset animation)

Understood polyline fully? Now tell me — want to see animated drawing polyline (like line growing)? Or polyline with markers (arrowheads)? Or full graph example with axes + polyline plot? Or move to <path> next (the king of SVG shapes)? Just say — we’re building your SVG mastery one shape at a time! 🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *