Как сделать «изогнутый прямоугольник» в SVG? - PullRequest
0 голосов
/ 09 октября 2018

У меня есть проект, над которым я работаю, который требует от меня круговой навигации с кнопками, которые выглядят как полосы вокруг вещи Железного Человека, изображенной ниже.Я могу рисовать простые формы и достаточно хорошо следовать учебным пособиям, но я теряюсь в том, как рисовать эти столбцы с изогнутой формой, как на рисунке ниже.

Я включил пример пути клипа, но я думаю, что SVGчто мне нужно сделать.

HTML

<div class="button"></div>

CSS

.button {
    background: radial-gradient(circle at 50% 160%,transparent 45%,red 44.5%,red 85%,transparent 85%);
        -webkit-clip-path: polygon(5% 0, 100% 0, 65% 90%, 35% 90%);
        clip-path: polygon(20% 0, 80% 0, 65% 90%, 35% 90%);
}

enter image description here

Ответы [ 2 ]

0 голосов
/ 09 октября 2018

Сначала я создаю один сегмент (путь).Затем я снова использую его, используя вращение.

const SVG_NS = 'http://www.w3.org/2000/svg';
const SVG_XLINK = "http://www.w3.org/1999/xlink";
const deg = 180 / Math.PI;
let R = 50;// the outer radius
let r = 35;// the inner radius
let A = 2*Math.PI/7;// the angle for the segment + space
let a = 2*A/3; // the angle for the segment


let path = document.createElementNS(SVG_NS, 'path');
let p1 = {x:0,y:-R}
let p2 = {
  x : R*Math.cos(a - Math.PI/2),
  y : R*Math.sin(a - Math.PI/2)
}
let p3 = {
  x : r*Math.cos(a - Math.PI/2),
  y : r*Math.sin(a - Math.PI/2)
}
let p4 = {
  x : 0,
  y : -r
}
let d = `M${p1.x},${p1.y}
         A${R},${R} 0 0,1,${p2.x},${p2.y}
         L${p3.x},${p3.y}
         A${r},${r} 0 0,0,${p4.x},${p4.y}
         L${p1.x},${p1.y}Z
`;
path.setAttributeNS(null, "d", d);
path.setAttributeNS(null, "id", "arc");
defs.appendChild(path);




for(let i = 0; i < 7; i++){
 let use = document.createElementNS(SVG_NS, 'use');
  use.setAttributeNS(SVG_XLINK, "xlink:href", "#arc")
  use.setAttributeNS(null, "fill", "gold");
  use.setAttributeNS(null, "transform", `rotate(${i*A*deg})`);
  svg.appendChild(use); 
  
}
<svg id="svg" viewBox= "-100 -55 200 110">
  
  <defs id="defs"></defs>
  
  <circle r="25" fill="gold" />
  
  
</svg>

Или даже проще: на этот раз я использую stroke-dasharray и вычисляю размер для обводки и промежутков

const SVG_NS = 'http://www.w3.org/2000/svg';
let R = 40;

let perimeter = 2*Math.PI*R
let dash = .7*perimeter/7;
let gap = .3*perimeter/7;


let dasharray = document.createElementNS(SVG_NS, 'circle');
dasharray.setAttributeNS(null, "r", R);
dasharray.setAttributeNS(null, "stroke-dasharray", `${dash}, ${gap}`);

svg.appendChild(dasharray);
circle{stroke-width:20px; stroke:black;fill:none;}
<svg id="svg" viewBox= "-100 -55 200 110"></svg>
0 голосов
/ 09 октября 2018

Да, SVG ответ здесь.Базовая форма:

<svg version="1.1" baseProfile="full" width="50" height="50" xmlns="http://www.w3.org/2000/svg">
  <path d="M 50 0 A 50 50 0 0 0 14.6 14.6 L 28.79 28.79 A 30 30 0 0 1 50 20 Z"></path>
</svg>

enter image description here

Эта форма строится с использованием элемента path, как описано здесь https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Paths и немноготригонометрии.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...