liveBook · Manning

strokeStyle property

As explained in D3.js in Action, Third Edition

strokeStyle is a Canvas rendering-context property specifying the color or paint used to outline paths and shapes.

The Canvas 2D strokeStyle property sets the color or paint used for outlining paths and shapes. Assign it before calling stroke(). section 15.2.3

A typical drawing sequence starts a path, defines a shape, chooses its strokeStyle, and renders the outline. Use beginPath() before a new shape so an earlier path does not inherit the current stroke style. section 15.2.3

Outline a rectangle

context.beginPath(); context.rect(346, 233, 106, 80); context.strokeStyle = "#6ba5d7"; context.stroke();

Group shapes that share a style, then draw them together. When the outline color changes, update strokeStyle before rendering the next path; combine it with fillStyle when a shape needs both an interior and a border. section 15.2.3

Style circles by category

For each Canvas circle, assign fillStyle from the subject color and choose strokeStyle from its medium before calling beginPath(), arc(), fill(), and stroke(). section 15.3

How D3.js in Action, Third Edition covers it

How deep it goes
Explains assignment, stroke(), path resetting, batching, and per-node styling without descending into paint-value formats.
Examples
Uses a bordered rectangle and painting circles whose border color represents the medium.
What it assumes
Assumes basic Canvas context and path commands while demonstrating beginPath(), rect(), arc(), fill(), and stroke().
What you can do after
Build it from scratch by assigning strokeStyle and calling stroke() after defining and resetting a Canvas path.
How it connects
Connects strokeStyle with Canvas 2D paths, beginPath(), stroke(), fillStyle, fill(), arc(), and mixed-mode rendering.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of D3.js in Action, Third Edition. The section links point to the parts of the book it draws on.