Fireship · @fireship
SVG Explained in 100 Seconds
Uploaded March 24, 2021 · Length 2:20
Summary
svg explained in 100 seconds, scalable vector graphics basics, html for 2d graphics
- svg
- scalable vector graphics
- vector graphics
- html for graphics
- 2d graphics
Transcript preview
The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.
- 0:00
scalable vector graphics or svg is an
- 0:03
image format that's like html for
- 0:05
2d graphics they differ from raster
- 0:07
images like png or jpeg
- 0:09
that use a grid of tiny pixels to create
- 0:11
an image and as you zoom in the pixels
- 0:13
become larger making the image grainy in
- 0:15
contrast a vector image can be scaled to
- 0:18
any size without losing its resolution
- 0:20
because instead of fixed pixels its
- 0:22
appearance is based on geometry
- 0:24
you can create svgs with tools like
- 0:25
figma or illustrator
- 0:27
or by writing the code directly which is
- 0:29
easier than you might think
- 0:30
and opens the door to animation and
- 0:32
interactivity create one by opening an
- 0:34
svg tag
- 0:35
then define a coordinate system with the
- 0:37
view box attribute
- 0:38
that gives us a frame with a width and
- 0:40
height of 100 units on which we can draw
- 0:42
graphics draw basic
- 0:44
shapes by adding elements like rectangle
- 0:46
circle and polygon position the
- 0:47
rectangle by defining its x-y value
- 0:50
on the view box then give it a size
- 0:52
which can take values that are either
- 0:54
explicit or responsive
- 0:55
we can change the color of the shape by
- 0:57
defining its fill or define an outline
- 0:59
with the stroke attribute and if our
- 1:01
styling gets too complex we can extract
- 1:03
everything into a separate css
- 1:04
stylesheet
- 1:05
by applying a class to it just like any
- 1:07
html element
- 1:08
we have the full power of css at our
- 1:10
fingertips which means we can react to
- 1:12
events on the shapes and then change
- 1:14
their styling or animation accordingly
- 1:16
but most graphics are more than just
- 1:18
basic shapes they contain complex
- 1:20
artwork with all kinds of twists and
- 1:22
turns and that's where the path element
- 1:23
comes in
- 1:24
the shape of a path is determined by one
- 1:26
attribute d
- 1:27
for draw coding a path is like
- 1:29
controlling the tip of a pen with a
- 1:30
series of commands
- 1:32
the most basic command is m for move
- 1:34
which will move the pen tip to an x y
- 1:36
coordinate
- 1:36
an uppercase letter means move relative
- 1:38
to the view box and lowercase means
- 1:40
move relative to the last point in the
- 1:42
path but move doesn't actually draw
- 1:44
anything
- 1:45
to put the pen tip down on the paper and
- 1:47
draw something use the l command
- 1:49
it works exactly like move but draws a
- 1:51
line that can be styled
- 1:52
straight lines are cool but what if we
- 1:54
wanted to add a curve like we would with
- 1:56
the handles in illustrator
- 1:57
create bezier curves with the c and q
- 1:59
commands define the position of two


