View the inverse region of clip-path
Asked Answered
E

1

6

This is a simple fiddle that displays a circular region inside a rectangle https://jsfiddle.net/3v6yhf0m/

svg {
  border: 3px dashed #999;
}
svg > rect:hover {
  fill: green;
}
<svg width="120" height="120" xmlns="http://www.w3.org/2000/svg">
  <defs>
    <clipPath id="myClip">
      <circle cx="30" cy="30" r="20"/>
    </clipPath>
  </defs>

  <rect x="10" y="10" width="100" height="100" fill="blue"
      clip-path="url(#myClip)"/>
</svg>

But I want to view the region of rectangle that lies outside the circle using clip-path https://jsfiddle.net/yhbeevya/

svg {
  border: 3px dashed #999;background-color:blue;fill:blue;
}
svg > rect:hover {
  fill: green;
}
<svg width="120" height="120" xmlns="http://www.w3.org/2000/svg">
  <defs>
    <clipPath id="myClip">
      <circle cx="30" cy="30" r="20"/>
    </clipPath>
  </defs>

  <rect x="10" y="10" width="100" height="100" fill="white"
      clip-path="url(#myClip)"/>
</svg>

Please share your ideas

Emmittemmons answered 22/5, 2017 at 7:18 Comment(0)
C
8

Use a mask instead. The black parts of the mask show the background, white displays the shape itself.

svg {
  border: 3px dashed #999;background-color:blue;fill:blue;
}
svg > rect:hover {
  fill: green;
}
<svg width="120" height="120" xmlns="http://www.w3.org/2000/svg">
  <defs>
    <mask id="myMask">
      <rect width="100%" height="100%" fill="white"/>
      <circle cx="30" cy="30" r="20" fill="black"/>
    </mask>
  </defs>

  <rect x="10" y="10" width="100" height="100" fill="white"
      mask="url(#myMask)"/>
</svg>
Composure answered 22/5, 2017 at 7:32 Comment(1)
Thanks, I will try thisEmmittemmons

© 2022 - 2024 — McMap. All rights reserved.