简体   繁体   English

SVG 基于背景反转笔触颜色

[英]SVG Invert stroke colour based on background

I have an SVG with a stroke which I want to invert to white when the background underneath it is blue.我有一个带有stroke的 SVG,当它下面的背景为蓝色时,我想将其反转为白色。

Here's the effect I'm aiming for这是我想要的效果

The grey is a set colour from a CSS variable.灰色是来自 CSS 变量的设置颜色。

SVG: SVG:

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="25px" viewBox="0 0 25 210">
   <g>
     <path d="M -678 692 L 702 692" fill="none" stroke-width="5" stroke-miterlimit="10" stroke-dasharray="15 15" transform="rotate(270,12,692)" pointer-events="all"/>
   </g>
</svg>

I've already attempted to add two separate SVGs;我已经尝试添加两个单独的 SVG; one having a white stroke for the blue section and another being grey for the body however the gap between the SVGs changes on different screen sizes which I do not want.一个是蓝色部分的白色笔划,另一个是灰色的主体,但是 SVG 之间的间隙会在不同的屏幕尺寸上发生变化,这是我不想要的。

Using two SVGs使用两个 SVG

I've also attempted to use a combination of the CSS filter , backdrop-filter and mix-blend-mode properties however I was unable to get the correct effect, as filter affects the whole SVG and backdrop-filter does not modify the path's stroke colour.我还尝试使用 CSS filterbackdrop-filtermix-blend-mode属性的组合,但是我无法获得正确的效果,因为filter会影响整个 SVG 而backdrop-filter不会修改路径的笔划颜色。

 header { display: block; } .header__nav-wrapper { background: -webkit-gradient(linear, left top, right top, from(#122F76), to(#1941A2)); background: linear-gradient(0.25turn, #122F76, #1941A2); padding: 2.5rem 8rem 0 8rem; display: -webkit-box; display: -ms-flexbox; display: flex; -webkit-box-pack: center; -ms-flex-pack: center; justify-content: center; height: 150px; } .header__wave { display: block; position: relative; width: 100%; } .top-line { position: relative; width: 100%; height: 100px; } .top-line__line--left { left: 200px; top: -200px; } .top-line__line { position: absolute; stroke: #999; z-index: 999; }
 <header class="header"> <div class="header__nav-wrapper"></div> <!-- Include svg wave --> <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" class="header__wave" version="1.1" viewBox="0 0 1920 80"> <defs> <linearGradient x1="0%" y1="0%" x2="100%" y2="0%" id="mx-gradient-122f76-1-1941a2-1-e-0"> <stop offset="0%" style="stop-color: rgb(18, 47, 118); stop-opacity: 1;" /> <stop offset="100%" style="stop-color: rgb(25, 65, 162); stop-opacity: 1;" /> </linearGradient> </defs> <g> <path d="M 0 0 L 1920 0 L 1920 68 Q 1440 46.4 960 68 Q 480 89.6 0 68 L 0 12 Z" fill="url(#mx-gradient-122f76-1-1941a2-1-e-0)" pointer-events="all" /> </g> </svg> </header> <div class="top-line"> <div class="top-line__line top-line__line--left"> <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="25px" viewBox="0 0 25 400"> <g> <path d="M -678 692 L 702 692" fill="none" stroke-width="5" stroke-miterlimit="10" stroke-dasharray="15 15" transform="rotate(270,12,692)" pointer-events="all"/> </g> </svg> </div> </div>

Here's a CodePen link if you need to look at the code.如果您需要查看代码,这里有一个CodePen链接。

If possible I'd really appreciate a CSS-only approach however I'm more than happy to use JavaScript if need be.如果可能的话,我真的很感激只使用 CSS 的方法,但是如果需要的话,我非常乐意使用 JavaScript。

Thanks!谢谢!

In this example I use the dotted line as a mask on two <rect> .在这个例子中,我使用虚线作为两个<rect>的掩码。 Beneath (defined first) are two <rect> that has the color that the dotted line is supposed to have.下面(首先定义)是两个<rect> ,它们具有虚线应该具有的颜色。

The wave is also define as a mask and can be reused on the two <rect> that define the upper part of the SVG.波浪也被定义为掩码,可以在定义 SVG 上部的两个<rect>上重复使用。

 <svg xmlns="http://www.w3.org/2000/svg" class="header__wave" viewBox="0 0 200 80"> <defs> <linearGradient x1="0%" y1="0%" x2="100%" y2="0%" id="g1"> <stop offset="0%" stop-color="navy" stop-opacity="1" /> <stop offset="100%" stop-color="navy" stop-opacity=".2" /> </linearGradient> <mask id="line"> <rect x="0" y="0" width="200" height="80" fill="white" /> <line x1="100" y1="0" x2="100" y2="80" stroke="black" stroke-width="2" stroke-dasharray="5 2" /> </mask> <mask id="wave"> <rect width="200" height="31" fill="white" /> <path transform="translate(0 30)" fill="white" d="M 0 0 L 200 0 L 200 7 Q 150 5 100 7 Q 50 9 0 7 Z" /> </mask> </defs> <rect fill="gray" width="200" height="79" /> <rect fill="white" width="200" height="80" mask="url(#wave)" /> <g mask="url(#line)"> <rect fill="white" width="200" height="80" /> <rect fill="url(#g1)" width="200" height="80" mask="url(#wave)" /> </g> </svg>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM