Table of Contents
Tartan Structure and SVG Representation
Automating Tartan Generation with React
Using SVG Tartans as Background Images
Downloadable SVG and PNG Tartans
Gatsby for Static Site Generation
Home Web Front-end CSS Tutorial How We Created a Static Site That Generates Tartan Patterns in SVG

How We Created a Static Site That Generates Tartan Patterns in SVG

Apr 09, 2025 am 11:29 AM

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:

<svg height="280" viewbox="0 0 280 280" width="280" x="0" xmlns="http://www.w3.org/2000/svg" y="0"><defs><mask height="1" width="1" x="0" y="0"><rect fill="url(#diagonalStripes)" height="100%" width="100%" x="0" y="0"></rect></mask></defs><g><rect fill="#FF8A00" height="40" width="100%" x="0" y="0"></rect><rect fill="#E52E71" height="10" width="100%" x="0" y="40"></rect><rect fill="#FFFFFF" height="10" width="100%" x="0" y="50"></rect><rect fill="#E52E71" height="70" width="100%" x="0" y="60"></rect><rect fill="#100E17" height="20" width="100%" x="0" y="130"></rect><rect fill="#E52E71" height="70" width="100%" x="0" y="150"></rect><rect fill="#FFFFFF" height="10" width="100%" x="0" y="220"></rect><rect fill="#E52E71" height="10" width="100%" x="0" y="230"></rect><rect fill="#FF8A00" height="40" width="100%" x="0" y="240"></rect></g><g mask="url(#grating)"><rect fill="#FF8A00" height="100%" width="40" x="0" y="0"></rect><rect fill="#E52E71" height="100%" width="10" x="40" y="0"></rect><rect fill="#FFFFFF" height="100%" width="10" x="50" y="0"></rect><rect fill="#E52E71" height="100%" width="70" x="60" y="0"></rect><rect fill="#100E17" height="100%" width="20" x="130" y="0"></rect><rect fill="#E52E71" height="100%" width="70" x="150" y="0"></rect><rect fill="#FFFFFF" height="100%" width="10" x="220" y="0"></rect><rect fill="#E52E71" height="100%" width="10" x="230" y="0"></rect><rect fill="#FF8A00" height="100%" width="40" x="240" y="0"></rect></g></svg>
Copy after login

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.

<svg height="0" width="0"><defs><pattern height="8" patternunits="userSpaceOnUse" width="8" x="0" y="0"><polygon fill="white" points="0,4 0,8 8,0 4,0"></polygon><polygon fill="white" points="4,8 8,8 8,4"></polygon></pattern></defs></svg>
Copy after login

Automating Tartan Generation with React

The manual SVG approach is automated using React. The SvgDefs component generates the <defs></defs> markup:

const SvgDefs = () => {
  return (
    <defs><pattern height="8" patternunits="userSpaceOnUse" width="8" x="0" y="0"><polygon fill="#ffffff" points="0,4 0,8 8,0 4,0"></polygon><polygon fill="#ffffff" points="4,8 8,8 8,4"></polygon></pattern><mask height="1" width="1" x="0" y="0"><rect fill="url(#diagonalStripes)" height="100%" width="100%" x="0" y="0"></rect></mask></defs>
  )
}
Copy after login

A tartan is represented as a stripe array. Each stripe is an object with fill (hex color) and size properties:

const tartan = [
  { fill: "#FF8A00", size: 40 },
  // ... more stripes
];
Copy after login

Tartan data often comes as "Palette" and "Threadcount" strings:

<code>// Palette
O#FF8A00 P#E52E71 W#FFFFFF K#100E17

// Threadcount
O/40 P10 W10 P70 K/10.</code>
Copy after login

(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:

const SvgTile = ({ tartan }) => {
  // ... (code to calculate stripe positions and total size) ...
  return (
    <svg height="{size}" viewbox="{`0" width="{size}" x="0" xmlns="http://www.w3.org/2000/svg" y="0">
      <svgdefs></svgdefs>
      {/* ... (code to generate rect elements for horizontal and vertical stripes) ... */}
    </svg>
  )
}
Copy after login

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:

.bg-element {
  background-image: url('data:image/svg xml;charset=utf-8,<svg>...</svg>');
}
Copy after login

The SvgBg component creates a full-screen div with the encoded SVG as its background:

const SvgBg = ({ tartan }) => {
  const tartanStr = ReactDOMServer.renderToStaticMarkup(<svgtile tartan="{tartan}"></svgtile>);
  const tartanData = encodeURIComponent(tartanStr);
  return (
    <div style="{{" backgroundimage: xml width: height:></div>
  );
};
Copy after login

Downloadable SVG and PNG Tartans

The SvgDownloadLink component allows SVG downloads:

const SvgDownloadLink = ({ svgData, fileName = "file" }) => {
  return (
    <a download="{`${fileName}.svg`}" href="%7B%60data:image/svg" xml>Download as SVG</a>
  );
};
Copy after login

The PngDownloadLink component generates high-resolution PNGs using a canvas:

const PngDownloadLink = ({ svgData, width, height, fileName = "file" }) => {
  // ... (code to create canvas, draw SVG, and get data URL) ...
  return (
    <a download="{`${fileName}.png`}" ref="{aEl}">Download as PNG</a>
  );
};
Copy after login

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:

// gatsby-config.js
module.exports = {
  /* ... */
  plugins: [
    'gatsby-transformer-csv',
    {
      resolve: 'gatsby-source-filesystem',
      options: {
        path: `${__dirname}/src/data`,
        name: 'data',
      },
    },
  ],
};
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Vue 3 Vue 3 Apr 02, 2025 pm 06:32 PM

It&#039;s out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

Can you get valid CSS property values from the browser? Can you get valid CSS property values from the browser? Apr 02, 2025 pm 06:17 PM

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That&#039;s like this.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

I&#039;d say "website" fits better than "mobile app" but I like this framing from Max Lynch:

Stacked Cards with Sticky Positioning and a Dash of Sass Stacked Cards with Sticky Positioning and a Dash of Sass Apr 03, 2025 am 10:30 AM

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

Using Markdown and Localization in the WordPress Block Editor Using Markdown and Localization in the WordPress Block Editor Apr 02, 2025 am 04:27 AM

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

Comparing Browsers for Responsive Design Comparing Browsers for Responsive Design Apr 02, 2025 pm 06:25 PM

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

How to Use CSS Grid for Sticky Headers and Footers How to Use CSS Grid for Sticky Headers and Footers Apr 02, 2025 pm 06:29 PM

CSS Grid is a collection of properties designed to make layout easier than it’s ever been. Like anything, there&#039;s a bit of a learning curve, but Grid is

Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Apr 05, 2025 pm 05:51 PM

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...

See all articles