Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine clipPath, pattern, and linearGradient in SVG

Tags:

css

svg

I am trying to create a background that consists of multiple dots gradienting from say green to yellow from left to right. So they idea was to create a path, fill it with a gradient and clip path with a pattern:

https://codepen.io/Deka87/pen/pLPqJE?editors=1000

<svg width='100' height='100' viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
  <defs>
    <linearGradient id="img-dotted-gradient">
      <stop offset="0%" stop-color="green"></stop>
      <stop offset="100%" stop-color="yellow"></stop>
    </linearGradient>
    <pattern id="img-dotted-dots" x="0" y="0" width=".1" height=".1">
      <circle cx="2" cy="2" r="2" fill="green"></circle>
    </pattern>
  </defs>
  <path d="M0 0 H 100 V 100 H 0 Z" fill="url(#img-dotted-gradient)" clip-path="url(#img-dotted-dots)"></path>
</svg>

The gradient works OK, the clip path works OK (standalone). However they don't come together. Any help would be appreciated!

like image 656
sdvnksv Avatar asked Mar 22 '18 11:03

sdvnksv


People also ask

How do I create a linear gradient in SVG?

SVG Linear Gradient - <linearGradient>. The <linearGradient> element is used to define a linear gradient. The <linearGradient> element must be nested within a <defs> tag. The <defs> tag is short for definitions and contains definition of special elements (such as gradients).

How do I use text as a clipping path in SVG?

SVG also allows text to be used as a clipping path. Here I’ve kept the group that contains the circle and triangle, but changed the clipPath to a text element. Here’s the result and you can see the text allows part of the circle and part of the triangle to show through. The second ‘p’ in Clipping reveals a little of both shapes.

Can SVG clip a circle and triangle?

You can see the triangle (the polygon) is clipped, but the circle isn’t. SVG also allows text to be used as a clipping path. Here I’ve kept the group that contains the circle and triangle, but changed the clipPath to a text element.

What is clipping and masking in SVG?

A mask consists of a shape or image where each pixel has varying degrees of transparency and opaqueness that can peer through, or hide portions in a very subtle fashion. Now let’s discuss some elements and attributes which enable clipping and masking in SVG. An SVG clipPath accepts many attributes and content model types.


1 Answers

Only the shape of the elements in a <clipPath> matter. The colour and fill are ignored, so you can't do it that way.

But you can use a <mask> instead.

<svg width='100' height='100' viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
  <defs>
    <linearGradient id="img-dotted-gradient">
      <stop offset="0%" stop-color="green"></stop>
      <stop offset="100%" stop-color="yellow"></stop>
    </linearGradient>
    <pattern id="img-dotted-dots" x="0" y="0" width=".1" height=".1">
      <circle cx="2" cy="2" r="2" fill="white"></circle>
    </pattern>
    <mask id="img-dotted-mask">
      <rect width="100" height="100" fill="url(#img-dotted-dots)"/>
    </mask>
  </defs>
  <path d="M0 0 H 100 V 100 H 0 Z" fill="url(#img-dotted-gradient)" mask="url(#img-dotted-mask)"></path>
</svg>
like image 110
Paul LeBeau Avatar answered Oct 16 '22 22:10

Paul LeBeau