Certainly! Below is a fully functional example that demonstrates how to create and manipulate an SVG element programmatically using the DOM API. This example creates an SVG with a circle, a rectangle, and some text, all created and styled using JavaScript DOM methods.
What this example does:
- Creates an SVG element.
- Adds a blue circle.
- Adds a red rectangle.
- Adds some text.
- Appends the SVG to the page.
- Adds a button to dynamically change the circle's color on click.
Complete HTML + JavaScript example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>SVG DOM API Example</title>
<style>
body {
font-family: Arial, sans-serif;
padding: 20px;
}
#svg-container {
border: 1px solid #ccc;
display: inline-block;
margin-top: 20px;
}
button {
margin-top: 20px;
padding: 8px 12px;
font-size: 16px;
cursor: pointer;
}
</style>
</head>
<body>
<h1>SVG Created with DOM API</h1>
<div id="svg-container"></div>
<button id="changeColorBtn">Change Circle Color</button>
<script>
// Namespace for SVG elements
const SVG_NS = "http://www.w3.org/2000/svg";
// Create SVG element
const svg = document.createElementNS(SVG_NS, "svg");
svg.setAttribute("width", "300");
svg.setAttribute("height", "200");
svg.setAttribute("viewBox", "0 0 300 200");
svg.style.backgroundColor = "#f0f0f0";
// Create a blue circle
const circle = document.createElementNS(SVG_NS, "circle");
circle.setAttribute("cx", "75");
circle.setAttribute("cy", "100");
circle.setAttribute("r", "50");
circle.setAttribute("fill", "blue");
circle.setAttribute("stroke", "black");
circle.setAttribute("stroke-width", "2");
// Create a red rectangle
const rect = document.createElementNS(SVG_NS, "rect");
rect.setAttribute("x", "150");
rect.setAttribute("y", "50");
rect.setAttribute("width", "100");
rect.setAttribute("height", "100");
rect.setAttribute("fill", "red");
rect.setAttribute("stroke", "black");
rect.setAttribute("stroke-width", "2");
// Create text element
const text = document.createElementNS(SVG_NS, "text");
text.setAttribute("x", "150");
text.setAttribute("y", "180");
text.setAttribute("fill", "black");
text.setAttribute("font-family", "Arial, sans-serif");
text.setAttribute("font-size", "16");
text.textContent = "SVG with DOM API";
// Append elements to SVG
svg.appendChild(circle);
svg.appendChild(rect);
svg.appendChild(text);
// Append SVG to container div
const container = document.getElementById("svg-container");
container.appendChild(svg);
// Button to change circle color randomly
const btn = document.getElementById("changeColorBtn");
btn.addEventListener("click", () => {
// Generate random color
const randomColor = "#" + Math.floor(Math.random() * 16777215).toString(16);
circle.setAttribute("fill", randomColor);
});
</script>
</body>
</html>
Explanation:
createElementNS is used instead of createElement because SVG elements belong to the SVG namespace (http://www.w3.org/2000/svg).
- We set attributes like
cx, cy, r for the circle, and x, y, width, height for the rectangle.
- The text element is created and positioned with
x and y.
- The button changes the circle's fill color to a random color on each click.
- The SVG is appended to a container div in the HTML.
How to use:
- Copy the above code into a
.html file.
- Open it in any modern browser.
- Click the Change Circle Color button to see the circle's color change dynamically.
If you want me to add more shapes, animations, or interactivity, just ask!