liveBook · Manning

fillStyle property

As explained in D3.js in Action, Third Edition

fillStyle is a Canvas 2D context property that specifies the color or paint used to fill drawn content and text.

The fillStyle property sets the color or paint used when Canvas fills a shape or draws text. Set it on the 2D context before calling fill() or fillText(). Canvas defaults to black, so changing fillStyle makes the visual result explicit. section 15.2.3 section 15.2.6

Use fillStyle as part of a small drawing sequence:

  • Create a path such as a rectangle, assign a color such as "#6ba5d7", and call fill(). section 15.2.3
  • Group shapes that share a fill style, then change fillStyle before drawing the next color group. section 15.2.3
  • Use the same property before fillText() to control text color; combine it with font and textAlign for presentation. section 15.2.6

Coloring visualization circles

A Canvas visualization can assign each node’s subject a color through fillStyle, then fill each circle after beginning a new path. Its strokeStyle can independently encode the painting medium. section 15.3

How D3.js in Action, Third Edition covers it

How deep it goes
Demonstrates assignment before fill() and fillText(), batching, and color changes, without detailing paint types or rendering internals.
Examples
Demonstrates colored rectangles, labeled Canvas shapes, and subject-colored circles in a mixed SVG-and-Canvas visualization.
What it assumes
Assumes a Canvas 2D context and basic drawing commands such as fill(), fillText(), and beginPath().
What you can do after
Apply it via the Canvas 2D API to color filled shapes, text, and data-driven marks.
How it connects
Connects fill(), fillText(), strokeStyle, beginPath(), font, and textAlign.

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.