Ray by Day JS

Day 2 - Static SVG embedded in HTML

Day 2: Inline SVG and DOM Manipulation

Building on Day 1's external file approach, Day 2 demonstrates the power of embedding SVG content directly within HTML. This approach provides a crucial advantage: the ability to manipulate SVG elements as part of the Document Object Model (DOM).

When SVG is embedded inline (rather than loaded as an external file), each SVG element becomes a manipulable DOM node. This means we can use JavaScript to dynamically change colors, positions, sizes, and other properties of individual "pixels" in real-time, without needing to regenerate the entire image.

The image below shows the same 20×20 pixel grid as Day 1, but now embedded directly in the HTML. This inline approach enables interactive features like the color manipulation controls you'll see below the image.

SVG Content embedded directly in HTML

STATIC IMAGE (sort of)

Excerpt showing SVG content as embedded in the HTML code

[Code not loaded]

JavaScript that changes the color of a random pixel

[Code not loaded]

JavaScript that changes specified pixel

[Code not loaded]

Resources used in this page

Prev Home Next