Sub topic 3
Vector graphics: an image made of shapes
This chapter assumes you know what a raster image is (a grid of pixels), can read basic XML or HTML tags, and recognize extensions such as .svg. It covers the vector model itself. Scaling, file formats and conversion get their own chapters, so they appear here only where needed.
The core idea: store instructions, not pixels
A raster image records the color of every pixel. A vector image records a recipe for drawing. The file says "draw a blue square here, then a red circle there," and software works out which pixels to color each time the image is shown.
The step that turns shape descriptions into pixels is called rendering, or rasterizing. It happens on your screen or printer at the size you ask for. Because the recipe has no fixed pixel size, the same file can be drawn small or large from the same data.
A vector image is built from a few kinds of ingredients:
- Primitives: ready-made shapes such as rectangles, circles, ellipses, lines and polygons.
- Paths: free-form outlines made from points joined by straight or curved segments.
- Styles: how each shape looks, including fill color, stroke (outline) color, stroke width and opacity.
- Structure: groups, layers, transformations (move, rotate, scale) and the order in which shapes are drawn.
A first vector image
SVG (Scalable Vector Graphics) is a vector format written as XML text. This example holds two shapes.
<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>Reading it line by line:
- The
<svg>element is the canvas.widthandheightset its display size.viewBox="0 0 200 100"sets the internal coordinate system: it starts at 0,0 and spans 200 units across and 100 down. - The
<rect>has its top-left corner at x=10, y=10 and measures 80 by 80 units. Its fill is a blue given as a hex color. - The
<circle>has its center at cx=150, cy=50 and a radius of 40 units.
The y axis points down. The origin (0,0) is the top-left corner, as in most screen coordinate systems. In school geometry the y axis points up, so this catches many learners.
Notice how small this is. The file is a few hundred bytes. A 200 by 100 raster image of the same picture stores 20,000 pixel values before compression.
Paths, points and Bézier curves
Rectangles and circles are not enough for logos, lettering or illustrations. For those, vector graphics use paths. A path is an ordered list of points, called anchor points, joined by segments. A segment can be a straight line or a curve.
The most common curve is the cubic Bézier curve. It is defined by four points:
- A start point.
- A first control point, which pulls the curve away from the start.
- A second control point, which pulls the curve toward the end.
- An end point.
The curve begins at the start point heading toward the first control point. It ends at the end point arriving from the direction of the second control point. The curve usually does not touch the control points themselves. They act like magnets that bend it.
<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>The d attribute holds the path commands:
| Command | Meaning | Arguments |
|---|---|---|
| `M x y` | Move to a point without drawing | One point |
| `L x y` | Draw a straight line to a point | One point |
| `C x1 y1, x2 y2, x y` | Cubic Bézier curve | Two control points, then the end point |
| `Q x1 y1, x y` | Quadratic Bézier curve | One control point, then the end point |
| `Z` | Close the path back to its start | None |
Uppercase commands use absolute coordinates. Lowercase versions (m, l, c) use coordinates relative to the current point. Both are valid, and tools pick whichever gives the shorter text.
In this example the curve starts at (10, 80), is pulled up by control points at (40, 10) and (65, 10), and ends at (95, 80). The result is an arch.
Design tools such as Adobe Illustrator, Inkscape, Figma and Affinity Designer show these same parts as the pen tool's anchor points and handles. Dragging a handle moves a control point. The tool rewrites the path numbers for you.
Stroke and fill
Every closed shape can have two separate appearances.
- The fill colors the inside of the shape. It can be a flat color, a gradient (a smooth blend between colors) or a pattern.
- The stroke draws along the outline. You control its color, width, line caps (how open ends look: butt, round or square) and line joins (how corners look: miter, round or bevel).
An open path, like the arch above, has no inside to speak of. That is why the example sets fill="none". Without it, the browser would fill the area between the curve and the straight line joining its two ends.
A common mistake is to forget that stroke is centered on the outline. A 4-unit stroke extends 2 units inside the shape and 2 units outside. A shape placed at the edge of the viewBox can then have its stroke clipped.
The viewBox: why the drawing scales
The viewBox attribute is what makes SVG resolution independent. It defines the drawing's own coordinate space. The width and height attributes define how large that space is shown. The renderer maps one onto the other.
In the arch example, the drawing space is 100 by 100 units, and it is displayed at 300 by 300 pixels. Each unit becomes 3 pixels. If you change the display size to 600 by 600, each unit becomes 6 pixels. The coordinates in the path do not change, and the curve is redrawn smoothly at the new size.
If you leave out viewBox, the drawing has no internal scale to map. Changing the width and height then crops or leaves empty space instead of scaling. A missing viewBox is a frequent reason an exported SVG will not resize.
How a renderer draws a path
Screens are made of pixels, so every vector image has to be turned into a grid at some point. The process runs roughly like this:
- Parse the file into a list of shapes and styles.
- Apply transformations and the viewBox scale to get positions in device pixels.
- Flatten curves into many tiny line segments, fine enough to look smooth at the target size.
- Fill the area inside each outline and draw the stroke.
- Apply anti-aliasing, which blends edge pixels with the background to avoid jagged stair-steps.
This means a vector image is never "pure." The final display is always a raster. What stays vector is the stored source. If you zoom in, the renderer repeats steps 2 to 5 at the new scale, using the original shape data, not the earlier pixels.
What vector graphics do well
- Small files for simple art. A logo with a few shapes takes a few hundred bytes to a few kilobytes.
- Sharp at any size. The same file works for a favicon and a billboard.
- Easy editing. You can select one shape, change its color, move a point or reorder layers without damaging the rest. Each object stays separate.
- Text-based formats. SVG can be styled with CSS, changed with JavaScript, searched, and tracked in version control with readable diffs.
- Flat color control. Exact brand colors are stored as exact values.
Limits you will meet in practice
Vector is not a general replacement for raster. These edge cases explain when it fails.
- Photographs. A photo has millions of subtly different colors with no clear edges. Describing it as shapes would need so many tiny shapes that the file would be larger and slower than a JPEG, and it would still look artificial.
- Complexity cost. File size and render time grow with the number of points and effects. A map with 500,000 nodes or an illustration with many layered gradients and blurs can be heavier than a raster of the same picture.
- Effects that depend on pixels. Painterly brush textures, noise, and some filters are naturally pixel-based. Many tools embed a raster inside the vector file for these. That embedded part does not scale cleanly.
- Rendering differences. Different programs can draw the same file slightly differently. Anti-aliasing, gradient handling, filters and fonts are the usual causes.
- Fonts. If a file uses live text and the viewer lacks the font, a substitute font appears and the layout changes. Designers fix this by converting text to outlines, meaning each letter becomes a path. The drawing then looks identical everywhere, but the text can no longer be edited or read by search tools.
- Very thin strokes at small sizes. A hairline stroke may disappear or look uneven when drawn on a low-resolution display.
Using vector images on the web
You place an SVG with the same img element used for raster formats. The logo below is an SVG and the photo is a JPEG.
<img src="logo.svg" alt="Company logo" width="120">
<img src="photo.jpg" alt="Mountain lake at sunrise" width="600" height="400">Both lines work the same way for the page author. The difference lies in what the browser does with the file: it decodes the JPEG's pixels but renders the SVG's shapes at whatever size the layout asks for. The alt text matters for both, because screen readers cannot see the image.
When you need to style or animate parts of an SVG with CSS or JavaScript, you can write the <svg> markup directly inside the HTML instead. Through an img tag, the page's CSS cannot reach the shapes inside.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| SVG shows at the wrong size or will not scale | No `viewBox`, or `width`/`height` in conflict with it | Add a `viewBox` matching the drawing's coordinates |
| Shape is cut off at the edge | Stroke extends beyond the viewBox | Add padding or enlarge the viewBox |
| Open path has an unexpected filled area | Fill not set to `none` | Add `fill="none"` |
| Text looks different on another computer | Font missing | Convert text to outlines, or embed or link the font |
| File is huge for a simple icon | Too many points, embedded raster, or editor metadata | Simplify the path, remove embedded images, run an SVG optimizer |
| Image is blurry in a vector file | It contains an embedded raster picture | Replace it with real vector shapes, or use a higher-resolution source |
Exercise
An SVG has viewBox="0 0 50 50" and is displayed with width="200" height="200". A path in it contains the point (10, 25). At what pixel position from the top-left of the displayed image does that point land? What happens to that position if you change the display size to 400 by 400?
Show answer
The scale is 200 / 50 = 4 pixels per unit, so (10, 25) lands at (40, 100). At 400 by 400 the scale is 8 pixels per unit, so the point lands at (80, 200). The path data itself is unchanged and the shape is simply redrawn at the new scale.
Exercise
A designer exports a logo as SVG. On the website it looks sharp on a laptop but blurry on a large monitor, and the file is 1.4 MB. Give two likely causes and one way to check each.
Show answer
The file probably contains an embedded raster image, which does not scale, and which also explains the large size. Open the SVG in a text editor and look for an <image> element or a long base64 data:image string. A second cause is that the logo was traced or placed as a low-resolution bitmap inside the vector tool. Zoom in far inside the design tool: if the edges turn into visible pixels, that part is raster and must be redrawn as paths or replaced with a higher-resolution source.