Use SVG to Create an Animatable Cross

Jhey Tompkins
InstructorJhey Tompkins
Share this video with your friends

Social Share Links

Send Tweet

We can use SVG to create a cross with one path. But, what if want to animate a cross as if it's being hand-drawn? We can do this by defining more than one path for our SVG.

Instructor: [0:00] Our tic-tac-toe board has naughts, but now it needs crosses. To do this, we can convert our X element overlay to an SVG. We can set a viewBox of " 100 100," and let's remove the text content.

[0:14] We could use a single path to draw a cross. In our case, we want to animate the cross as if it's being drawn by hand, so we're going to use two paths. For our first path, we move to position 80,20. Then, we draw a line to 20,80. We can give that a stroke of "black". If we check in the browser, that gives us this diagonal line.

[0:36] For our second path, we're going to start by moving to 20,20. Then, we're going to draw a line to 80,80. This will also have a stroke of "black".

[0:48] If we check the demo, we now have a cross. We can proceed to give it a matching stroke-width and linecap, and that gives us a cross.

[0:57] In review, we can use SVG to draw a cross, where we use multiple paths to draw a cross instead of a single path, because we're mindful of how we might like to animate that cross.