Sub topic 3
Vector graphics: an image made of shapes
This chapter assumes you know what a screen pixel is, have used image files such as .jpg, .png and .svg, and have read the previous chapter on raster images. Here you will learn how a vector image stores shapes, how to read basic SVG, and which mistakes come up most often.
The working model
A raster image stores a color for every pixel. A vector image stores instructions for drawing. The file says "draw a blue rectangle here" and "draw a red circle there". A program reads those instructions and paints pixels on your screen each time the image is displayed.
A vector image is built from a few ingredients:
- Shapes, such as rectangles, circles, lines and paths.
- Points and coordinates, which place each shape on a drawing area.
- Styles, such as fill color, stroke color and stroke width.
Because the file holds descriptions and not pixels, the drawing program can render it at any size. The renderer recalculates the shapes for the size you ask for.
Basic shapes in SVG
SVG (Scalable Vector Graphics) is a text-based vector format. This example draws a rectangle and a circle.
<svg xmlns="http://www.w3.org/2000/svg" width="200" height="100" viewBox="0 0 200 100">
<rect x="10" y="10" width="80" height="80" fill="#2a6fdb"/>
<circle cx="150" cy="50" r="40" fill="#e5484d"/>
</svg>The rect element has a top-left corner at (10, 10) and is 80 units wide and tall. The circle has its center at (150, 50) and a radius of 40. The fill attribute sets the color. The viewBox defines the coordinate system as 200 units wide and 100 units tall, starting at 0, 0.
Paths, points and curves
Most real artwork is made of paths. A path is a sequence of points joined by straight or curved segments. Each point is called an anchor point. Curves are usually cubic Bézier curves. A cubic Bézier curve has a start point, an end point and two control points. The control points are not on the curve. They pull it toward them and set its shape.
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" width="300" height="300">
<path d="M10 80 C 40 10, 65 10, 95 80" fill="none" stroke="#222" stroke-width="4"/>
</svg>In the d attribute, M10 80 moves to the start point. C 40 10, 65 10, 95 80 draws a cubic curve with control points (40, 10) and (65, 10), ending at (95, 80). Setting fill="none" leaves the inside unpainted, so only the stroke shows.
The viewBox is 100 by 100 units, but the displayed size is 300 by 300 pixels. The renderer multiplies every coordinate by three. Change the size to 3000 and the curve stays sharp, because the renderer redraws it from the same instructions.
Fill, stroke and layers
Each shape can have a fill (the inside) and a stroke (the outline). Shapes are drawn in the order they appear in the file, so later shapes cover earlier ones. Designers call this stacking order. Moving a shape up or down changes which one hides the other.
You can edit any shape later by changing its numbers. Change r="40" to r="20" and the circle shrinks, with no loss of quality. A raster editor can't do this, because it only has pixels and no shape to adjust.
Common errors
- Expecting photographs. A vector file can't capture the fine, continuous color of a photo without an enormous number of tiny shapes. Use raster for photos.
- Forgetting the viewBox. Without a
viewBox, an SVG may not scale with its width and height, and it can be cropped instead of resized. - Hairline strokes that vanish. A stroke is measured in drawing units. When you scale a drawing down, a thin stroke can become thinner than one pixel and look faint.
- Assuming "vector" means "small file". A drawing with tens of thousands of points can be larger than a compressed raster image.
- Placing a raster image inside a vector file. An SVG, PDF or EPS can embed a bitmap. The file has a vector extension, but that part of it still blurs when enlarged.
Exercise
In the path M10 80 C 40 10, 65 10, 95 80, which points are the control points, and why does enlarging the drawing not make the curve blurry?
Show answer
The control points are (40, 10) and (65, 10). They shape the curve but are not on it. The curve does not blur when enlarged because the file stores these coordinates and not pixels, so the renderer recalculates the curve at the new size.
Exercise
An SVG logo looks sharp at 100 pixels wide but blurry at 800 pixels wide. What is the most likely cause?
Show answer
The SVG probably contains an embedded raster image, so that part is still made of pixels and blurs when enlarged. Replace it with real shapes or a higher-resolution bitmap.