Learning to Code with SVG
Lesson Plan:
Coding a Car Scene in SVG on a 500 by 500 grid
Objective:
Hands-on learning of SVG by drawing a car scene with basic shapes; circles, ellipses, rectangles, and paths.
Learning how to group elements and apply a default style to child elements.
Introduce the stroke-dasharray style.
Lab Time:
Approximately 1.5 hours, not including Lecture time. Students should copy and paste lines, and then change the attribute values to greatly reduce typing time and typos.
Age range:
4-8th grades, or any age student unfamiliar with SVG
Requirements:
Familiar with a simple text editor like Notepad on Windows, or Text Wrangler on a Mac.
Familiar with copy and paste shortcuts (ctrl-c and ctrl-v on windows and command-c and command-v on a Mac).
Ability to save file with a .svg extension.
Should be familiar with adding <circle>, <ellipse>, <rect>, <path> elements in SVG
Should be familiar with adding style attributes and the name:value pair syntax that specifies a property and its value.
Resources:
Lecture:
Explain how to group elements using the <g> element. Nodes within the group are called child elements and will default to the style of the group so that the style does not have to be added to each child node.

Explain the transform attribute and the translate transformation. The translate transformation takes 2 parameters; the x and y offsets to move the object in the x and y directions. The x and y offsets can be positive or negative. When the transformation is an attribute of a group element, the translation transformation is applied to all child elements.

Explain the stroke-dasharray style property. It takes an array of values in the form, dash,gap,dash,gap,... and repeats as needed to stroke the object. Arrays should be an even number, 2,4,6,... elements, for expected results.
Procedure:
Have students get an SVG template with 500x500 grid from:
http://steamcoded.org/lessons/grid500x500.svg.txt
Copy the code and paste it into a text editor.
Save the file as Car.svg then open in a browser. Keep the text editor and browser windows open.

Add SVG elements where indicated using the instructions on page 4 (see below). Important: Students should save the file and refresh the browser after adding each SVG element to their file.

When complete, change the style attribute of the first <g> element from "display:initial" to "display:none" which hides the grid. Then change the style style attribute of the second <g> element from "opacity:0.5" to "opacity:1"
Take Away:
Students should feel comfortable adding circles, ellipses, rectangles, and lines when creating SVG images.
Students should understand how grouping elements is useful when defining a common style or transformation.
Students should understand how to move an object using the translate transformation.
Additional Activity
Students can change the colors and move objects arround.
Students can copy and paste the <script> from:
http://steamcoded.org/lessons/DrawShapes.js.txt,
then save the file, refresh, and watch it draw.
What the Car Scene should look like - Page 3 of 6 in PDF
Instruction Sheet for Students - Pages 4 & 5 of 7 in PDF
Coding a Car Scene in SVG on a 500 by 500 grid
Copy the code from: http://steamcoded.org/lessons/grid500x500.svg.txt and save the file as Car.svg and open the file in a browser.

In the editor, add the SVG elements (per instructions below) where indicated in the SVG code, i.e. inside the second grouping (<g> element). Important: Save the file and refresh the browser after each step.
1:
Create a rectangle element, width: 500, height: 275 style: fill:blue;
2:
Create a circle element about (435,65), radius: 35
style: fill:yellow;stroke:yellow;stroke-width:50px;stroke-dasharray:4,4,4,20;
3:
Create a grouping <g> element, style: fill:none;stroke:black;stroke-width:2px;
4:
Create lines using the <path> element as a child element of step 3,
Moveto (175,100), Lineto (195,85), Lineto (210,100), Lineto(225,85), Lineto (250,100)
5:
Create more lines using the <path> element as a child element of step 3,
Moveto (225,50), Lineto (245,35), Lineto (260,50), Lineto(275,35), Lineto (300,50)
6:
Create a grouping <g> element with transform attribute: translate(175,275)
7:
Create 5 circle elements as child elements of step 6, Note: cx and cy default to 0
radius: 150, style: fill:red;
radius: 130, style: fill:orange;
radius: 110, style: fill:yellow;
radius: 90, style: fill:blue;
radius: 70, style: fill:green;
8:
Create a grouping <g> element, style: fill:white;
9:
Create 4 circle elements as child elements of step 7,
cx: 50, cy: 100, radius: 20
cx: 75, cy: 125, radius: 38
cx: 115, cy: 110, radius: 29
cx: 105, cy: 138, radius: 29
10:
Create a rectangle element, x: 0, y: 275, width: 500, height: 200, style: fill:gray;
11:
Create a grouping <g> element, style: fill:yellow;stroke:black;
12:
Create 4 rectangle elements as child elements of step 11,
x: 0, y: 350, width: 50, height: 25
x: 125, y: 350, width: 100, height: 25
x: 300, y: 350, width: 100, height: 25
x: 475, y: 350, width: 25, height: 25
13:
Create a grouping <g> element, style: fill:green;stroke:black;
14:
Create 2 elements as child elements of step 13,
rectangle x: 100, y: 250, width: 350, height: 100 rx: 25
ellipse cx: 275, cy: 250, rx: 125, ry: 75
15:
Create 3 elements,
ellipse cx: 275, cy: 250, rx: 110, ry: 60, style="fill:white;
rectangle x: 265, y: 179, width: 20, height: 85, style="fill:green;
rectangle x: 150, y: 250, width: 250, height: 85, style="fill:green;
16:
Create 4 circle elements,
cx: 190, cy: 350, radius: 40, style="fill:black;
cx: 360, cy: 350, radius: 40, style="fill:black;
cx: 190, cy: 350, radius: 20, style="fill:gray;
cx: 360, cy: 350, radius: 20, style="fill:gray;
17:
Create a grouping <g> element,
style: fill:black;stroke:black;stroke-width:60px;stroke-dasharray:1.5,1.5,1.5,20;
18:
Create 2 circle elements as child elements of step 17,
cx: 190, cy: 350, radius: 5
cx: 360, cy: 350, radius: 5
19:
Create a grouping <g> element, style: stroke:black;
20:
Create 5 elements as child elements of step 19,
ellipse cx: 448, cy: 282, rx: 7, ry: 12, style="fill:yellow;
rectangle x: 100, y: 300, width: 10, height: 15, rx: 5, style="fill:red;
rectangle x: 415, y: 300, width: 15, height: 10, rx: 5, style="fill:orange;
rectangle x: 95, y: 320, width: 30, height: 20, rx: 5, style="fill:gray;
rectangle x: 425, y: 320, width: 30, height: 20, rx: 5, style="fill:gray;
21:
Create a line using the <path>,
Moveto (125,250), Lineto (95,200), style: fill:black;stroke:black;stroke-width:2px;
22:
Create a circle element about (95,200), radius: 3, style: fill:black;
23:
Change the SVG viewBox height from 500 to 475
When complete, change the style attribute of the first element from "display:initial" to "display:none" which hides the grid. Then change the style style attribute of the second element from "opacity:0.5" to "opacity:1"
Answer Sheet for Instructor - Pages 6 & 7 of 7 in PDF
Coding a Car Scene in SVG on a 500 by 500 grid
Answer Sheet
Common mistakes are incorrectly creating group elements, closing the opening g tag with a /, missing the closing g tag, missing double quote marks around attribute values, missing space between attributes, missing the start < and ending /> symbols, elements not placed within the grouping, and transposing x and y coordinates.
1:
<rect x="0" y="0" width="500" height="275" style="fill:blue" />
2:
<circle cx="435" cy="65" r="35"
style="fill:yellow;stroke:yellow;stroke-width:50px;stroke-dasharray:4,4,4,20" />
3:
<g style="fill:none;stroke:black;stroke-width:2px;">
  
-- Step 4 & 5 path elements go here --
</g>
4:
<path d="M175,100L195,85L210,100L225,85L250,100" />
5:
<path d="M225,50L245,35L260,50L275,35L300,50" />
6:
<g transform="translate(175,275)">
  
-- Step 7 circle elements go here --
</g>
7:
<circle r="150" style="fill:red;" />
<circle r="130" style="fill:orange;" />
<circle r="110" style="fill:yellow;" />
<circle r="90" style="fill:green;" />
<circle r="70" style="fill:blue;" />
8:
<g style="fill:white;">
  
-- Step 9 circle elements go here --
</g>
9:
<circle cx="50" cy="100" r="20" />
<circle cx="75" cy="125" r="38" />
<circle cx="115" cy="110" r="29" />
<circle cx="105" cy="138" r="29" />
10:
<rect x="0" y="275" width="500" height="200" style="fill:gray" />
11:
<g style="fill:yellow;stroke:black;">
  
-- Step 12 rectangle elements go here --
</g>
12:
<rect x="0" y="350" width="50" height="25" />
<rect x="125" y="350" width="100" height="25" />
<rect x="300" y="350" width="100" height="25" />
<rect x="475" y="350" width="25" height="25" />
13:
<g style="fill:green;stroke:black;">
  
-- Step 14 elements go here --
</g>
14:
<rect x="100" y="250" width="350" height="100" rx="25" />
<ellipse cx="275" cy="250" rx="125" ry="75" />
15:
<ellipse cx="275" cy="250" rx="110" ry="60" style="fill:white;" />
<rect x="265" y="179" width="20" height="85" style="fill:green;" />
<rect x="150" y="250" width="250" height="85" style="fill:green;" />
16:
<circle cx="190" cy="350" r="40" style="fill:black" />
<circle cx="360" cy="350" r="40" style="fill:black" />
<circle cx="190" cy="350" r="20" style="fill:gray" />
<circle cx="360" cy="350" r="20" style="fill:gray" />
17:
<g style="fill:black;stroke:black;stroke-width:60px;stroke-dasharray:1.5,1.5,1.5,20;">
  
-- Step 18 circle elements go here --
</g>
18:
<circle cx="190" cy="350" r="5" />
<circle cx="360" cy="350" r="5" />
19:
<g style="stroke:black;">
  
-- Step 20 elements go here --
</g>
20:
<ellipse cx="448" cy="282" rx="7" ry="12" style="fill:yellow;" />
<rect x="100" y="300" width="10" height="15" rx="5" style="fill:red;" />
<rect x="415" y="300" width="15" height="10" rx="5" style="fill:orange;" />
<rect x="95" y="320" width="30" height="20" rx="5" style="fill:gray;" />
<rect x="425" y="320" width="30" height="20" rx="5" style="fill:gray;" />
21:
<path d="M125,250L95,200" style="fill:black;stroke:black;stroke-width:2px;" />
22:
<circle cx="95" cy="200" r="3" style="fill:black;" />
23:
<svg width="100%" height="100%" viewBox="0 0 500 475"