How We Created a Static Site That Generates Tartan Patterns in SVG
Tartan, the iconic patterned cloth synonymous with Scotland, particularly its kilts, takes center stage on tartanify.com. This site boasts a library of over 5,000 tartan patterns (in SVG and PNG formats), meticulously curated to exclude those with restrictive usage rights.
The project, conceived by Sylvain Guizard during a Scottish summer holiday, initially envisioned manual creation of the pattern library using design software like Adobe Illustrator or Sketch. However, the sheer volume of patterns (thousands!) quickly made this approach untenable. The breakthrough came with the realization that tartans possess a defined structure and are represented by simple strings encoding thread counts and color codes.
Tartan Structure and SVG Representation
Tartan's characteristic pattern arises from interwoven colored threads at right angles. Vertical and horizontal bands share identical color and width sequences. The intersections of these bands create visually blended colors. The twill weaving technique adds distinctive diagonal lines. This article recreates this effect using SVG rectangles as threads:
Let's examine this SVG structure:
1 |
|
The horizontal and vertical stripe groups create identical squares, but the vertical one is masked, revealing only the white areas where the horizontal and vertical threads intersect. A patterned mask, reflecting the weaving, is achieved by defining a pattern tile:
The patternUnits
attribute is changed from objectBoundingBox
to userSpaceOnUse
, specifying width and height in pixels.
1 |
|
Automating Tartan Generation with React
The manual SVG approach is automated using React. The SvgDefs
component generates the <defs></defs>
markup:
1 2 3 4 5 |
|
A tartan is represented as a stripe array. Each stripe is an object with fill
(hex color) and size
properties:
1 2 3 4 |
|
Tartan data often comes as "Palette" and "Threadcount" strings:
1 2 3 4 5 |
|
(The conversion of these strings to the stripes array is detailed in a separate Gist.)
The SvgTile
component generates the SVG structure from the tartan
array:
1 2 3 4 5 6 7 8 9 |
|
Using SVG Tartans as Background Images
On tartanify.com, each tartan is a full-screen background image. This requires encoding the SVG as a data URI:
1 2 3 |
|
The SvgBg
component creates a full-screen div with the encoded SVG as its background:
1 2 3 4 5 6 7 |
|
Downloadable SVG and PNG Tartans
The SvgDownloadLink
component allows SVG downloads:
1 2 3 4 5 |
|
The PngDownloadLink
component generates high-resolution PNGs using a canvas:
1 2 3 4 5 6 |
|
Gatsby for Static Site Generation
Tartanify.com leverages Gatsby, a React-based static site generator. The gatsby-config.js
file includes plugins for processing CSV data:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
The gatsby-node.js
file uses Gatsby's Node APIs to create pages for each tartan and paginated letter-based index pages, handling slug generation and pagination. The tartan templates (tartan.js
) and index templates (tartans.js
) utilize the React components created earlier. Navigation between paginated index pages is managed by the TartansNavigation
component.
This detailed explanation covers the core aspects of the tartanify.com project. The complete code is available on GitHub. This project showcases a fun and effective way to learn new technologies through a compelling side project.
The above is the detailed content of How We Created a Static Site That Generates Tartan Patterns in SVG. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











I see Google Fonts rolled out a new design (Tweet). Compared to the last big redesign, this feels much more iterative. I can barely tell the difference

Have you ever needed a countdown timer on a project? For something like that, it might be natural to reach for a plugin, but it’s actually a lot more

Everything you ever wanted to know about data attributes in HTML, CSS, and JavaScript.

When the number of elements is not fixed, how to select the first child element of the specified class name through CSS. When processing HTML structure, you often encounter different elements...

Questions about purple slash areas in Flex layouts When using Flex layouts, you may encounter some confusing phenomena, such as in the developer tools (d...

Tartan is a patterned cloth that’s typically associated with Scotland, particularly their fashionable kilts. On tartanify.com, we gathered over 5,000 tartan

At the start of a new project, Sass compilation happens in the blink of an eye. This feels great, especially when it’s paired with Browsersync, which reloads

How to implement Windows-like in front-end development...
