SVG Coding Examples: Useful Recipes For Writing Vectors By Hand
This article delves into the intriguing world of hand-coding SVGs, demonstrating practical examples and highlighting the mathematical underpinnings of SVG creation. It emphasizes that while tools like Illustrator and Affinity excel at drawing curves, manual coding opens up possibilities for animation and complex interactions.
The article guides readers through the fundamentals of SVG units and the crucial role of the `viewBox` attribute in establishing the coordinate system. The `viewBox` defines the document size, ensuring that elements exceeding its dimensions are hidden. This attribute is crucial for making SVGs behave like regular images, where either width or height is set, while the other dimension adjusts based on the aspect ratio.
### Understanding Units and the `viewBox`
The author explains that SVG units are relative to the `viewBox` and are detached from the browser. Units are conceptual and represent relationships within the coordinate system. For instance, if your document has a width and height defined in CSS `rem` units, the `viewBox` units become a relative concept used for sizing within the SVG’s coordinate system.
The article underscores that the `width` and `height` attributes become unnecessary when a `viewBox` attribute is present. This behavior aligns SVGs with images, making it straightforward to set either dimension and have the other dimension scale automatically.
### Coding SVG Elements with JavaScript
Moving beyond basic SVG elements like `rect`, `circle`, `line`, `ellipse`, `polygon`, and `polyline`, the author dives into using JavaScript to programmatically generate SVGs. This involves setting up variables that define the size of the SVG document, elements, and styles.
Two methods are presented: creating elements and setting their attributes with `document.createElementNS` and using JSX-style templates for interpolation. The latter is emphasized as it offers better readability and syntax highlighting.
### Dealing With Repetition
The article recognizes the repetitive nature of SVG coding, particularly when dealing with multiple elements sharing similar properties. JavaScript loops are introduced as a solution to this redundancy. An example using nested loops demonstrates how to generate SVG content using arrays for elements, fills, and transforms. This illustrates the power of JavaScript logic for creating concise and maintainable SVG code.
### Exploring Advanced SVG Techniques
The article explores two advanced SVG techniques, patterns and masks, that enhance visual design and offer more creative possibilities. These techniques are not supported by all vector editing software, like Affinity, which is why they were not used in the author’s calligraphy grid generator project.
### Graph Grid with Patterns
The concept of a graph grid as a repeating pattern of lines is presented. SVG patterns are used to achieve this repetition, where a `rect` element is filled with a defined pattern. The pattern’s `width`, `height`, and `viewBox` control its repetition across the SVG.
### Dot Grid with Patterns
Similar to graph grids, a dot grid is created using patterns. Instead of individual circles, a dashed line with `stroke-dasharray` and `stroke-dashoffset` is employed to generate the dotted effect.
### Conclusion
The article concludes that hand-coding SVGs is not as intimidating as it may seem. By breaking down the process into smaller steps, analyzing coordinate systems, and applying JavaScript logic, SVGs can be created efficiently. The author encourages readers to explore the SVG specifications further and experiment with drawing their own vectors.
This is a summary of the original article. You can find the full article at [Original Article URL].