RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 642689
Accepted
Alexandr_TT
Alexandr_TT
Asked:2020-03-22 16:12:35 +0000 UTC2020-03-22 16:12:35 +0000 UTC 2020-03-22 16:12:35 +0000 UTC

几种颜色填充圆圈周围的笔画

  • 772

我想围绕一个圆圈创建一条彩虹,如下图所示。

在此处输入图像描述

但是如何绘制弯曲的多色渐变呢?

这是我当前的代码:

<svg width="500" height="500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<linearGradient id="test">
<stop offset="0%" stop-color="#f00"/>
<stop offset="100%" stop-color="#0ff"/>
</linearGradient>

</defs>

<circle cx="50" cy="50" r="40" fill="none" stroke="url(#test)" stroke-width="6"/>

</svg>

免费翻译问题svg 圆形笔划上的多种颜色

javascript
  • 3 3 个回答
  • 10 Views

3 个回答

  • Voted
  1. Best Answer
    Sasha Omelchenko
    2020-03-22T17:16:40Z2020-03-22T17:16:40Z

    这在Leah Veru 的 polyfill 的帮助 下是可能的。conic-gradient

    .circle {
      background: conic-gradient(#f00, #ff00bd, #0020ff, #00f3ff, #00ff08, #fff700, #f00);
      border-radius: 50%;
      width: 200px;
      height: 200px;
      position: relative;
    }
    
    .circle:after {
      position: absolute;
      content: "";
      left: 20px;
      top: 20px;
      width: calc(100% - 40px);
      height: calc(100% - 40px);
      background: #fff;
      border-radius: 50%;
    }
    <div class="circle"></div>
    
    <script src="https://leaverou.github.io/prefixfree/prefixfree.min.js"></script>
    <script src="https://leaverou.github.io/conic-gradient/conic-gradient.js"></script>

    • 9
  2. Alexandr_TT
    2020-03-22T16:12:35Z2020-03-22T16:12:35Z

    这种方法行不通。SVG 没有锥形渐变。为了模仿这种效果,您需要用很多小片段来伪造它。或者使用其他一些类似的技术。

    更新:

    这是一个例子。我正在对 .360deg创建的六个跨度上的色调进行 近似path's。每个都path包含一个跨越 60 度圆的弧。我用来linear gradient从每条路径的开头到结尾插入颜色。

    它并不完美(您可以看到颜色相交处有一些缝隙),但大多数人不会注意到。您可以通过使用六个以上的段来提高准确性。

     <svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="220" height="220" viewBox="-10 -10 220 220">
          <defs>
            <linearGradient id="redyel" gradientUnits="objectBoundingBox" x1="0" y1="0" x2="1" y2="1">
                <stop offset="0%" stop-color="#ff0000"/>   
                <stop offset="100%" stop-color="#ffff00"/>   
            </linearGradient>
            <linearGradient id="yelgre" gradientUnits="objectBoundingBox" x1="0" y1="0" x2="0" y2="1">
                <stop offset="0%" stop-color="#ffff00"/>   
                <stop offset="100%" stop-color="#00ff00"/>   
            </linearGradient>
            <linearGradient id="grecya" gradientUnits="objectBoundingBox" x1="1" y1="0" x2="0" y2="1">
                <stop offset="0%" stop-color="#00ff00"/>   
                <stop offset="100%" stop-color="#00ffff"/>   
            </linearGradient>
            <linearGradient id="cyablu" gradientUnits="objectBoundingBox" x1="1" y1="1" x2="0" y2="0">
                <stop offset="0%" stop-color="#00ffff"/>   
                <stop offset="100%" stop-color="#0000ff"/>   
            </linearGradient>
            <linearGradient id="blumag" gradientUnits="objectBoundingBox" x1="0" y1="1" x2="0" y2="0">
                <stop offset="0%" stop-color="#0000ff"/>   
                <stop offset="100%" stop-color="#ff00ff"/>   
            </linearGradient>
            <linearGradient id="magred" gradientUnits="objectBoundingBox" x1="0" y1="1" x2="1" y2="0">
                <stop offset="0%" stop-color="#ff00ff"/>   
                <stop offset="100%" stop-color="#ff0000"/>   
            </linearGradient>
          </defs>
        
          <g fill="none" stroke-width="15" transform="translate(100,100)">
            <path d="M 0,-100 A 100,100 0 0,1 86.6,-50" stroke="url(#redyel)"/>
            <path d="M 86.6,-50 A 100,100 0 0,1 86.6,50" stroke="url(#yelgre)"/>
            <path d="M 86.6,50 A 100,100 0 0,1 0,100" stroke="url(#grecya)"/>
            <path d="M 0,100 A 100,100 0 0,1 -86.6,50" stroke="url(#cyablu)"/>
            <path d="M -86.6,50 A 100,100 0 0,1 -86.6,-50" stroke="url(#blumag)"/>
            <path d="M -86.6,-50 A 100,100 0 0,1 0,-100" stroke="url(#magred)"/>
          </g>
        </svg>

    小提琴的例子

    更新 2:

    对于具有六个或更多分段的变体,有一个解决方案javascript,它将创建一个具有任意数量分段的圆。

    下面的示例使用 12 个段。

    function makeColourWheel(numSegments)
    {
        if (numSegments <= 0)
            numSegments = 12;
        if (numSegments > 360)
            numSegments = 360;
    
        var  svgns = xmlns="http://www.w3.org/2000/svg";
        var  svg = document.getElementById("colourwheel");
        var  defs = svg.getElementById("defs");
        var  paths = svg.getElementById("paths");
    
        var  radius = 100;
        var  stepAngle = 2 * Math.PI / numSegments;
    
        var  lastX = 0;
        var  lastY = -radius;
        var  lastAngle = 0;
        
        for (var i=1; i<=numSegments; i++)
        {
            var  angle = i * stepAngle;
    
            // Рассчитайте эту конечную точку дуги
            var x = radius * Math.sin(angle);
            var y = -radius * Math.cos(angle);
            // Создайте элемент пути
            var arc = document.createElementNS(svgns, "path");
            arc.setAttribute("d", "M " + lastX.toFixed(3) + "," + lastY.toFixed(3)
                                  + " A 100,100 0 0,1 " + x.toFixed(3) + "," + y.toFixed(3));
            arc.setAttribute("stroke", "url(#wheelseg" + i + ")");
            // Добавьте его в наш SVG
            paths.appendChild(arc);
            
            // Создайте градиент для этого сегмента
            var grad = document.createElementNS(svgns, "linearGradient");
            grad.setAttribute("id", "wheelseg"+i);
            grad.setAttribute("gradientUnits", "userSpaceOnUse");
            grad.setAttribute("x1", lastX.toFixed(3));
            grad.setAttribute("y1", lastY.toFixed(3));
            grad.setAttribute("x2", x.toFixed(3));
            grad.setAttribute("y2", y.toFixed(3));
            // Сделайте stop 0% для этого градиента
            var stop = document.createElementNS(svgns, "stop");
            stop.setAttribute("offset", "0%");
            hue = Math.round(lastAngle * 360 / Math.PI / 2);
            stop.setAttribute("stop-color", "hsl(" + hue + ",100%,50%)");
            grad.appendChild(stop);
            //Сделайте 100% stop для этого градиента
            stop = document.createElementNS(svgns, "stop");
            stop.setAttribute("offset", "100%");
            hue = Math.round(angle * 360 / Math.PI / 2);
            stop.setAttribute("stop-color", "hsl(" + hue + ",100%,50%)");
            grad.appendChild(stop);
            // Добавьте градиент к SVG
            defs.appendChild(grad);
    
            // Обновите последние x/y
            lastX = x;
            lastY = y;
            lastAngle = angle;
        }
    }
    
    
    makeColourWheel(60);
    <svg id="colourwheel" xmlns="http://www.w3.org/2000/svg" version="1.1" width="100%" height="100%" viewBox="-10 -10 220 220">
      <defs id="defs">
      </defs>
    
      <g id="paths" fill="none" stroke-width="15" transform="translate(100,100)">
      </g>
    </svg>

    svg multiple color on circle stroke的 松散翻译@Paul LeBeau的回答。

    • 7
  3. sercxjo
    2020-10-20T20:56:03Z2020-10-20T20:56:03Z

    我将在英文版中添加我对类似问题的回答的翻译:https ://stackoverflow.com/a/58399254/1566316添加。

    通过使用 transform:matrix3d 或 rotate3d 和 perspective 在 css 中将它们在一侧挤压成三角形,可以在线性渐变矩形上使用中心投影(透视)变换。

    如果matrix3d的第四个参数非零,则设置透视消失点在地平线x轴上坐标的倒数(1/x)值。第十三参数水平偏移矩形。虽然在矩阵中使用 calc 是可以接受的,但它不适用于单位,因此在缩放时,matrix3d 会变得混乱,它只能在 svg 中使用并且只能在 Firefox 中使用,因为 svg 不支持负责透视的参数在铬 (4, 8) 中。然而,后者已经有了一个 conic-gradient,虽然它不能应用于任何 svg 元素,但只能应用于 foreignObject。我不知道 svg 中的 tranform-origin 属性是如何工作的,所以矩阵的元素是随机选择的。

    SVG 版本的色轮。

    <svg version="2" viewBox="-207 -207 414 414" xmlns="http://www.w3.org/2000/svg">
     <defs>
      <radialGradient id="gWt">
       <stop style="stop-color:#fff" offset="0"/>
       <stop style="stop-color:#fff0" offset="1"/>
      </radialGradient>
      <!-- part for firefox: using central projection of 3 rectangles -->
      <linearGradient id="gYM" x1="0" x2="0" y1="0" y2="100%">
       <stop style="stop-color:#ef08" offset="0"/>
       <stop style="stop-color:#ff0" offset=".005"/>
       <stop style="stop-color:#fc0" offset=".1827"/>
       <stop style="stop-color:#f90" offset=".2924"/>
       <stop style="stop-color:#f60" offset=".3728"/>
       <stop style="stop-color:#f00" offset=".5"/>
       <stop style="stop-color:#f06" offset=".6272"/>
       <stop style="stop-color:#f09" offset=".7076"/>
       <stop style="stop-color:#f0c" offset=".8173"/>
       <stop style="stop-color:#f0f" offset=".995"/>
       <stop style="stop-color:#e0f8" offset="1"/>
      </linearGradient>
      <linearGradient id="gCY" href="#gYM">
       <stop style="stop-color:#0ef8" offset="0"/>
       <stop style="stop-color:#0ff" offset=".005"/>
       <stop style="stop-color:#0fc" offset=".1827"/>
       <stop style="stop-color:#0f9" offset=".2924"/>
       <stop style="stop-color:#0f6" offset=".3728"/>
       <stop style="stop-color:#0f0" offset=".5"/>
       <stop style="stop-color:#6f0" offset=".6272"/>
       <stop style="stop-color:#9f0" offset=".7076"/>
       <stop style="stop-color:#cf0" offset=".8173"/>
       <stop style="stop-color:#ff0" offset=".995"/>
       <stop style="stop-color:#fe08" offset="1"/>
      </linearGradient>
      <linearGradient id="gMC" href="#gYM">
       <stop style="stop-color:#f0e8" offset="0"/>
       <stop style="stop-color:#f0f" offset=".005"/>
       <stop style="stop-color:#c0f" offset=".1827"/>
       <stop style="stop-color:#90f" offset=".2924"/>
       <stop style="stop-color:#60f" offset=".3728"/>
       <stop style="stop-color:#00f" offset=".5"/>
       <stop style="stop-color:#06f" offset=".6272"/>
       <stop style="stop-color:#09f" offset=".7076"/>
       <stop style="stop-color:#0cf" offset=".8173"/>
       <stop style="stop-color:#0ff" offset=".995"/>
       <stop style="stop-color:#0fe8" offset="1"/>
      </linearGradient>
      <rect id="r" width="2000" height="700" style="transform:matrix3d(
          -29,0,0,100,
          0,1,0,0,
          0,0,1,0,
          200,-350,1,1
        ); mix-blend-mode:lighten"/>
     </defs>
     <clipPath id="cc">
      <circle r="200" id="c"/>
     </clipPath>
     <circle r="199" fill="#000"/>
     <g clip-path="url(#cc)" id="m">
      <use href="#r" fill="url(#gCY)" transform="rotate(-120)"/>
      <use href="#r" fill="url(#gYM)"/>
      <use href="#r" fill="url(#gMC)" transform="rotate(120)"/>
      <!-- for Chrome use conic gradient (does not work with any svg object but with foreign)
       Firefox ignores this object due to unknown keyword in style -->
     <foreignObject width="400" height="400" x="-200" y="-200" style="background:conic-gradient(from 90deg,
       #f00,#f0f,#00f,#0ff,#0f0,#ff0,#f00);
      background-repeat: no-repeat;
      background-position: center center;
      background-size: auto;"/>
     </g>
     <g>
      <!-- Points for visual test -->
      <circle cx="-200" r="6" fill="#0ff"/>
      <circle cx="-195.6" cy="-41.6" r="6" fill="#0fc"/>
      <circle cx="-182.7" cy="-81.3" r="6" fill="#0f9"/>
      <circle cx="-161.8" cy="-117.6" r="6" fill="#0f6"/>
      <circle cx="-133.8" cy="-148.6" r="6" fill="#0f3"/>
      <circle cx="-100" cy="-173.2" r="6" fill="#0f0"/>
      <circle cx="-61.8" cy="-190.2" r="6" fill="#3f0"/>
      <circle cx="-20.9" cy="-198.9" r="6" fill="#6f0"/>
      <circle cx="20.9" cy="-198.9" r="6" fill="#9f0"/>
      <circle cx="61.8" cy="-190.2" r="6" fill="#cf0"/>
      <circle cx="100" cy="-173.2" r="6" fill="#ff0"/>
      <circle cx="133.8" cy="-148.6" r="6" fill="#fc0"/>
      <circle cx="161.8" cy="-117.6" r="6" fill="#f90"/>
      <circle cx="182.7" cy="-81.3" r="6" fill="#f60"/>
      <circle cx="195.6" cy="-41.6" r="6" fill="#f30"/>
      <circle cx="200" r="6" fill="#f00"/>
      <circle cx="-195.6" cy="41.6" r="6" fill="#0cf"/>
      <circle cx="-182.7" cy="81.3" r="6" fill="#09f"/>
      <circle cx="-161.8" cy="117.6" r="6" fill="#06f"/>
      <circle cx="-133.8" cy="148.6" r="6" fill="#03f"/>
      <circle cx="-100" cy="173.2" r="6" fill="#00f"/>
      <circle cx="-61.8" cy="190.2" r="6" fill="#30f"/>
      <circle cx="-20.9" cy="198.9" r="6" fill="#60f"/>
      <circle cx="20.9" cy="198.9" r="6" fill="#90f"/>
      <circle cx="61.8" cy="190.2" r="6" fill="#c0f"/>
      <circle cx="100" cy="173.2" r="6" fill="#f0f"/>
      <circle cx="133.8" cy="148.6" r="6" fill="#f0c"/>
      <circle cx="161.8" cy="117.6" r="6" fill="#f09"/>
      <circle cx="182.7" cy="81.3" r="6" fill="#f06"/>
      <circle cx="195.6" cy="41.6" r="6" fill="#f03"/>
     </g>
     <circle r="200" fill="url(#gWt)"/>
    </svg>

    渐变控制点通过以下公式获得(来自边长为 350x200 的直角三角形的几何形状):

    0.5 ± tan(v/255.*pi/3)/350*100.
    

    其中 v 是变化的颜色通道的值。

    SVG 版本的戒指。

    <svg version="2" viewBox="-207 -207 414 414" xmlns="http://www.w3.org/2000/svg">
     <defs>
      <radialGradient id="gWt">
       <stop style="stop-color:#fff" offset="0"/>
       <stop style="stop-color:#fff0" offset="1"/>
      </radialGradient>
      <!-- part for firefox: using central projection of 3 rectangles -->
      <linearGradient id="gYM" x1="0" x2="0" y1="0" y2="100%">
       <stop style="stop-color:#ef08" offset="0"/>
       <stop style="stop-color:#ff0" offset=".005"/>
       <stop style="stop-color:#fc0" offset=".1827"/>
       <stop style="stop-color:#f90" offset=".2924"/>
       <stop style="stop-color:#f60" offset=".3728"/>
       <stop style="stop-color:#f00" offset=".5"/>
       <stop style="stop-color:#f06" offset=".6272"/>
       <stop style="stop-color:#f09" offset=".7076"/>
       <stop style="stop-color:#f0c" offset=".8173"/>
       <stop style="stop-color:#f0f" offset=".995"/>
       <stop style="stop-color:#e0f8" offset="1"/>
      </linearGradient>
      <linearGradient id="gCY" href="#gYM">
       <stop style="stop-color:#0ef8" offset="0"/>
       <stop style="stop-color:#0ff" offset=".005"/>
       <stop style="stop-color:#0fc" offset=".1827"/>
       <stop style="stop-color:#0f9" offset=".2924"/>
       <stop style="stop-color:#0f6" offset=".3728"/>
       <stop style="stop-color:#0f0" offset=".5"/>
       <stop style="stop-color:#6f0" offset=".6272"/>
       <stop style="stop-color:#9f0" offset=".7076"/>
       <stop style="stop-color:#cf0" offset=".8173"/>
       <stop style="stop-color:#ff0" offset=".995"/>
       <stop style="stop-color:#fe08" offset="1"/>
      </linearGradient>
      <linearGradient id="gMC" href="#gYM">
       <stop style="stop-color:#f0e8" offset="0"/>
       <stop style="stop-color:#f0f" offset=".005"/>
       <stop style="stop-color:#c0f" offset=".1827"/>
       <stop style="stop-color:#90f" offset=".2924"/>
       <stop style="stop-color:#60f" offset=".3728"/>
       <stop style="stop-color:#00f" offset=".5"/>
       <stop style="stop-color:#06f" offset=".6272"/>
       <stop style="stop-color:#09f" offset=".7076"/>
       <stop style="stop-color:#0cf" offset=".8173"/>
       <stop style="stop-color:#0ff" offset=".995"/>
       <stop style="stop-color:#0fe8" offset="1"/>
      </linearGradient>
      <rect id="r" width="200" height="700" style="transform:matrix3d(
          -29,0,0,100,
          0,1,0,0,
          0,0,1,0,
          200,-350,1,1
        )"/>
     </defs>
     <clipPath id="cc">
      <path d="M200,0A200,200,0, 1,0,0,200 200,200,0,0,0,200,0zM100,0A100,100,0, 0,1,0,100 100,100,0, 1,1,100,0z"/>
     </clipPath>
     <!-- circle r="199" fill="#000"/ -->
     <g clip-path="url(#cc)" id="m">
      <use href="#r" fill="url(#gCY)" transform="rotate(-120)"/>
      <use href="#r" fill="url(#gYM)"/>
      <use href="#r" fill="url(#gMC)" transform="rotate(120)"/>
      <!-- for Chrome use conic gradient (does not work with any svg object but with foreign)
       Firefox ignores this object due to unknown keyword in style -->
     <foreignObject width="400" height="400" x="-200" y="-200" style="background:conic-gradient(from 90deg,
       #f00,#f0f,#00f,#0ff,#0f0,#ff0,#f00);
      background-repeat: no-repeat;
      background-position: center center;
      background-size: auto;"/>
     </g>
    </svg>

    在 HTML 中,您可以使用 transform:rotate3D。通过将视点放置在非常靠近矩形的位置,您几乎可以得到一个三角形。

    *{
        margin:0;
        padding:0;
    }
    .c{
       height:90vmin;
       width:90vmin;
       margin:5vmin;
       overflow: hidden;
       border-radius:50%;
       position:absolute;
    }
    .c>div{
       position:relative;
       height:180vmin;
       width:100vmin;
       perspective: 0.40vmin;
       top:-45vmin;
       left:-4.8vmin;
    }
    .c>div>div{
       height: 100%;
       width: 100%;
       left:0;
       top:1%;
       height: 98%;
       transform-origin:99.98% 50%;
       position:relative;
       transform: rotate3d(0, 1, 0, -80deg);
       border:black;
    }
    #YM>div{
       background:linear-gradient(#ef08,#ff0 0.5%,#fc0 18.27%,#f90 29.24%,#f60 37.28%,#f00 50%,#f06 62.72%,#f09 70.76%,#f0c 81.73%,#f0f 99.5%,#e0f8);
    }
    #MC{
       transform: rotateZ(120deg);
    }
    #CY{
       transform: rotateZ(-120deg);
    }
    #MC>div{
       background:linear-gradient(#f0e8,#f0f 0.5%,#c0f 18.27%,#90f 29.24%,#60f 37.28%,#00f 50%,#06f 62.72%,#09f 70.76%,#0cf 81.73%,#0ff 99.5%,#0fe8);
    }
    #CY>div{
       background:linear-gradient(#0ef8,#0ff 0.5%,#0fc 18.27%,#0f9 29.24%,#0f6 37.28%,#0f0 50%,#6f0 62.72%,#9f0 70.76%,#cf0 81.73%,#ff0 99.5%,#fe08);
    }
    <div class="c"><div id="YM"><div></div></div></div>
    <div class="c"><div id="MC"><div></div></div></div>
    <div class="c"><div id="CY"><div></div></div></div>

    • 2

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    Python 3.6 - 安装 MySQL (Windows)

    • 1 个回答
  • Marko Smith

    C++ 编写程序“计算单个岛屿”。填充一个二维数组 12x12 0 和 1

    • 2 个回答
  • Marko Smith

    返回指针的函数

    • 1 个回答
  • Marko Smith

    我使用 django 管理面板添加图像,但它没有显示

    • 1 个回答
  • Marko Smith

    这些条目是什么意思,它们的完整等效项是什么样的

    • 2 个回答
  • Marko Smith

    浏览器仍然缓存文件数据

    • 1 个回答
  • Marko Smith

    在 Excel VBA 中激活工作表的问题

    • 3 个回答
  • Marko Smith

    为什么内置类型中包含复数而小数不包含?

    • 2 个回答
  • Marko Smith

    获得唯一途径

    • 3 个回答
  • Marko Smith

    告诉我一个像幻灯片一样创建滚动的库

    • 1 个回答
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Алексей Шиманский 如何以及通过什么方式来查找 Javascript 代码中的错误? 2020-08-03 00:21:37 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    user207618 Codegolf——组合选择算法的实现 2020-10-23 18:46:29 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5