2012-01-13 7 views
11

मेरे पास एक छोटा एसवीजी विजेट है जिसका उद्देश्य कोणों की एक सूची प्रदर्शित करना है (छवि देखें)।इसके आसपास की सीमा के साथ एक एसवीजी "लाइन" कैसे बनाएं?

अभी, कोण रेखा तत्व हैं, जिनमें केवल स्ट्रोक है और कोई भर नहीं है। लेकिन अब मैं इसके अंदर "अंदर भरने" रंग और "स्ट्रोक/सीमा" रखना चाहता हूं। मुझे लगता है कि रेखा तत्व इसे संभाल नहीं सकता है, तो इसके बजाय मुझे क्या उपयोग करना चाहिए?

ध्यान दें कि लाइन के स्ट्रोक का लाइन-एंडकैप गोलाकार है। मैं समाधान में इस प्रभाव को बनाए रखना चाहता हूं।

Angle Line

<svg height="160" version="1.1" viewBox="-0.6 -0.6 1.2 1.2" width="160" xmlns="http://www.w3.org/2000/svg"> 
    <g> 
    <g> 
     <circle class="sensorShape" cx="0" cy="0" fill="#FFF" r="0.4" stroke="black" stroke-width="0.015"/> 
     <line stroke="black" stroke-width="0.015" x1="0" x2="0" y1="-0.4" y2="0.4"/> 
     <line stroke="black" stroke-width="0.015" x1="-0.4" x2="0.4" y1="0" y2="0"/> 
    </g> 
    <g class="lsNorthAngleHandsContainer"> 
     <line data-angle="348" stroke="red" stroke-linecap="round" stroke-width="0.04" transform="rotate(348)" x1="0" x2="0" y1="0" y2="-0.4"/> 
     <text font-size="0.08" transform="translate(-0.02316467632710395,-0.45125904029352226)"> 
     348 
     </text> 
    </g> 
    </g> 
</svg> 

उत्तर

14

, एक दूसरी पंक्ति जोड़े एक ही निर्देशांक लेकिन पतली लाइन चौड़ाई के साथ:

<g class="lsNorthAngleHandsContainer"> 
    <line data-angle="348" stroke="red" stroke-linecap="round" stroke-width="0.04" transform="rotate(348)" x1="0" x2="0" y1="0" y2="-0.4"/> 
    <line data-angle="348" stroke="yellow" stroke-linecap="round" stroke-width="0.025" transform="rotate(348)" x1="0" x2="0" y1="0" y2="-0.4"/> 
3

ताकि आप केवल "बाहर" रंग के साथ मोटा लाइन के शीर्ष पर "अंदर" रंग के साथ एक पतली रेखा आकर्षित कर सकते हैं यह लग रहा है अपनी लाइन की तरह अपारदर्शी है।

+0

धन्यवाद, यह एक अच्छा विचार है। लेकिन मैं कैसे सुनिश्चित करूं कि "शीर्ष" रेखा बिल्कुल "नीचे" रेखा के केंद्र में केंद्रित है? –

+1

बस पतली रेखा चौड़ाई के साथ समान निर्देशांक का उपयोग करें। गोल कैप्स के साथ यह सही लगेगा। – Janne

+0

आह, अब मैं इसके बारे में सोचता हूं मुझे विश्वास है कि आप सही हैं! धन्यवाद जेन। –

2

आप गोलाकार कोनों के साथ एक रेक्ट इस्तेमाल कर सकते हैं, लेकिन गणित थोड़ा परिवर्तन:

<rect data-angle="348" stroke="red" stroke-linecap="round" stroke-width="0.02" fill="#FF0" transform="rotate(348, 0, 0)" x="-0.02" y="-0.4" width=".06" height=".4" rx=".03" ry=".03"/> 

http://jsfiddle.net/RNAuP/

+0

यही कारण है कि मैंने मूल रूप से सोचा था कि समाधान होगा। हालांकि, आपका जैन के जितना अच्छा नहीं दिखता है। रेक्ट के प्रारंभ और अंत बिंदु सर्कल के केंद्र और सर्कल रूपरेखा पर पूरी तरह से अस्तर नहीं कर रहे हैं। –

+0

यदि आप इसे सही तरीके से लाइन करने के लिए प्राप्त कर सकते हैं तो यह समाधान हेरफेर करना आसान लगता है (कहें कि मैं गतिशील रूप से रूपरेखा को जोड़ना/निकालना चाहता हूं, जो मैं करता हूं)। इसलिए, यह एक अलग कारण के लिए अनावश्यक हो जाता है ... आपको आयत के प्रत्येक "छोटी तरफ" के मध्य बिंदु का उपयोग करना होगा। –

+0

यह अंकगणित का एक प्रश्न है, आपको रोटेशन सेंटर http://jsfiddle.net/F5e74/ से सीमा चौड़ाई को कम करना होगा, मैं स्पष्ट रूप से मानता हूं कि दो पंक्तियां एक अधिक सरल समाधान है। – Duopixel

0

आप इसे पथ के साथ भी कर सकते हैं, भले ही यह गोल बिट्स के आसपास मुश्किल हो:

<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" [ 
    <!-- I often use entities to be able to change lot of numbers at once in static SVG, also kind of makes the paths more readable. 
     Obvisouly, if you're generating the path you can use the same variables in code to append to d --> 
    <!ENTITY handFill "0.01"> 
    <!ENTITY handFill2 "0.02"><!-- Should be 2 * handFill to be centered --> 
    <!ENTITY handStroke "0.005"><!-- Should be less than handFill2 to not hide fill --> 
]> 
<svg height="160" version="1.1" viewBox="-0.6 -0.6 1.2 1.2" width="160" xmlns="http://www.w3.org/2000/svg"> 
    <g> 
    <g> 
     <circle class="sensorShape" cx="0" cy="0" fill="#FFF" r="0.4" stroke="black" stroke-width="0.015"/> 
     <line stroke="black" stroke-width="0.015" x1="0" x2="0" y1="-0.4" y2="0.4"/> 
     <line stroke="black" stroke-width="0.015" x1="-0.4" x2="0.4" y1="0" y2="0"/> 
    </g> 
    <g class="lsNorthAngleHandsContainer"> 
     <path d=" 
     M -&handFill;,0 l0,-0.4 
     a &handFill;,&handFill; 0 0,1 &handFill2;,0 
     l 0,0.4 
     a &handFill;,&handFill; 0 0,1 -&handFill2;,0 
     " stroke="red" stroke-linecap="round" stroke-width="&handStroke;" fill="yellow" transform="rotate(348)" /> 
     <text font-size="0.08" transform="translate(-0.02316467632710395,-0.45125904029352226)"> 
     348 
     </text> 
    </g> 
    </g> 
</svg> 
2

मुझे illustration से filling and stroking के बारे में W3C आलेख से प्रेरित सुरुचिपूर्ण समाधान मिला। मूल रूप से, आप परिभाषाओं के लिए पथ ले जाने और दो तत्वों आकर्षित करने के लिए इस परिभाषा का उपयोग करें:

<svg width="200" height="200" viewBox="0 0 100 100"> 
    <defs> 
     <line id="line1" x1="25" x2="75" y1="25" y2="75"/> 
    </defs> 
    <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#line1" class="stroke"></use> 
    <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#line1" class="line"></use> 
</svg> 

<defs> का उपयोग करके और <use> आप दोनों पंक्तियां अद्यतन करने के लिए केवल पथ तत्व बदल सकते हैं। JSFiddle डेमो

संबंधित मुद्दे