I'm trying to create an angular (1.5.5) directive where the template is an svg element. I read this answer, and several others, but I'm not able to get my simple svg element to show.
I have tried to simplify it as much as possible. So this simple directive should draw a circle when used inside a svg element, at least that is what I'm trying to achieve.
app.directive("svg01", () => {
return {
templateNamespace: "svg",
template: `
<circle cx="50" cy="50" r="40" stroke="blue" stroke-width="4" fill="green" />
`
//template: `
// <svg width="100" height="100">
// <circle cx="50" cy="50" r="40" stroke="blue" stroke-width="4" fill="white" />
// </svg>
//`
}
});
This does not show the circle with this html
<div style="height: 100px; background-color: lightgreen">
<svg width="100" height="100">
<svg01></svg01>
</svg>
</div>
if instead I change the directive to include the svg element, then it shows (since this is now an html element I assume)
app.directive("svg01", () => {
return {
templateNamespace: "svg",
//template: `
// <circle cx="50" cy="50" r="40" stroke="blue" stroke-width="4" fill="green" />
//`
template: `
<svg width="100" height="100">
<circle cx="50" cy="50" r="40" stroke="blue" stroke-width="4" fill="white" />
</svg>
`
}
});
<div style="height: 100px; background-color: lightgreen">
<svg01></svg01>
</div>
And it doesn't matter if I set the templateNamespace to "html" or "svg". It shows in either case.
Is it not possible to define an svg template in a directive, and instead I need to add it programmatically as suggested in this answer
What am I missing ?
Thanks
Ahh - I found the problem, and I will post this as an answer instead of modifying my question, adding replace: true and the shape is shown
app.directive("svg01", () => {
return {
replace: true,
templateNamespace: "svg",
template: `
<circle cx="50" cy="50" r="40" stroke="blue" stroke-width="4" fill="yellow" />
`
Related
How can I import a png image into a nextJS svg component.
This is the sample code I'm working with.
<Page title="Logo Designer" className="max-w-xl">
<div>
<svg id="logoDesigner" width={700} height={600} style={{ backgroundColor: 'lightgray' }}>
<rect class="draggable" x="4" y="5" width="80" height="100" fill="#007bff" />
<rect class="draggable" x="18" y="5" width="80" height="400" fill="#888" />
</svg>
</div>
</Page>
you can use the image tag
<image
width="100"
height="100"
xlink:href="my-logo.png"
/>
make sure to change the width height and xlink:href based on your need
I'm using create-content-loader package for creating placeholder loading but can't make it responsive.
How to make svg fill parent content. I don't want to set width hardcoded 1060px. I tried with viewbox property too.
<ContentLoader
height={40}
width={1060}
speed={2}
primaryColor="#d9d9d9"
secondaryColor="#ecebeb"
>
<rect x="10" y="40" rx="0" ry="0" width="75" height="10" />
<rect x="10" y="60" rx="0" ry="0" width="75" height="10" />
<rect x="10" y="100" rx="0" ry="0" width="75" height="10" />
<rect x="10" y="80" rx="0" ry="0" width="75" height="10" />
<rect x="10" y="120" rx="0" ry="0" width="75" height="10" />
<rect x="110" y="40" rx="0" ry="0" width="370" height="125" />
<rect x="275" y="63" rx="0" ry="0" width="72" height="4" />
<rect x="430" y="5" rx="5" ry="5" width="75" height="20" />
<rect x="110" y="10" rx="0" ry="0" width="200" height="15" />
<rect x="10" y="5" rx="4" ry="4" width="75" height="20" />
<circle cx="493" cy="54" r="2" />
<circle cx="497" cy="47" r="7" />
<circle cx="497" cy="77" r="7" />
<circle cx="497" cy="107" r="7" />
</ContentLoader>```
ContentLoader uses html5 canvas for rendering, so viewbox won't work. What I would do is:
add ref to container element (which holds ContentLoader).
get that element width by using ref
pass that value to the ContentLoader width prop.
Keep in mind that on the first render ref.current will be null or undefined
Update:
Also you can try to extract svg from create-content-loader example and turn it into your component. Just change svg attributes to match rect related svg props. Than set css class on svg to have these properties:
.responsive-svg {
width: 100%;
height: auto;
}
Svg (before you turn it into react component) might look something like this:
<svg
role="img"
width="400"
height="160"
aria-labelledby="loading-aria"
viewBox="0 0 400 160"
preserveAspectRatio="none"
>
<title id="loading-aria">Loading...</title>
<rect
x="0"
y="0"
width="100%"
height="100%"
clip-path="url(#clip-path)"
style='fill: url("#fill");'
></rect>
<defs>
<clipPath id="clip-path">
<rect x="48" y="8" rx="3" ry="3" width="88" height="6" />
<rect x="48" y="26" rx="3" ry="3" width="52" height="6" />
<rect x="0" y="56" rx="3" ry="3" width="410" height="6" />
<rect x="0" y="72" rx="3" ry="3" width="380" height="6" />
<rect x="0" y="88" rx="3" ry="3" width="178" height="6" />
<circle cx="20" cy="20" r="20" />
</clipPath>
<linearGradient id="fill">
<stop
offset="0.599964"
stop-color="#f3f3f3"
stop-opacity="1"
>
<animate
attributeName="offset"
values="-2; -2; 1"
keyTimes="0; 0.25; 1"
dur="2s"
repeatCount="indefinite"
></animate>
</stop>
<stop
offset="1.59996"
stop-color="#ecebeb"
stop-opacity="1"
>
<animate
attributeName="offset"
values="-1; -1; 2"
keyTimes="0; 0.25; 1"
dur="2s"
repeatCount="indefinite"
></animate>
</stop>
<stop
offset="2.59996"
stop-color="#f3f3f3"
stop-opacity="1"
>
<animate
attributeName="offset"
values="0; 0; 3"
keyTimes="0; 0.25; 1"
dur="2s"
repeatCount="indefinite"
></animate>
</stop>
</linearGradient>
</defs>
</svg>
I'm working on an angularjs directive where a circle element needs to be disabled once it has been clicked on once.
The ng-click event works as expected, but the ng-disable is not working. I can see the disabled class being added in the html, but the clicking event is still being triggered.
Any thoughts?
<svg width="100" height="100">
<circle ng-disabled="disableme" ng-click="clickme()" ng-init="count=0" cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
</svg>
app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
$scope.clickme = function () {
$scope.count=$scope.count+1;
$scope.disableme = true;
};
http://plnkr.co/edit/dK07QfKoA9qOrNsdlypE?p=preview
ng-disabled does not work on svg. Alternatively, you can do this with ng-class
.disable {
pointer-events: none;
opacity: 0.5;
}
Add the ng-class to the svg element
<svg width="100" height="100" ng-class="{'disable': disableme}">
<circle ng-click="clickme()" ng-init="count=0" cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
</svg>
Demo
Can anyone help me identify why my text is not rendering in the defined path #textPath03 ? The path is rendered as supposed, but when I put it inside a <defs> and try to use it, it doesn`t work.
<svg version="1.1" height="700" width="700" viewBox="-5 -5 110 110" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" data-reactid="13">
<defs data-reactid="14">
<path id="#textPath03" d="M95,50 A45,45 0 0 0 50,5" data-reactid="15"></path>
</defs>
<line x1="50" y1="50" x2="100" y2="50" stroke="black" data-reactid="16"></line>
<line x1="50" y1="50" x2="50" y2="0" stroke="black" data-reactid="17"></line>
<path d="M90,50 A40,40 0 0 0 50,10" fill="none" stroke="green" data-reactid="18"></path>
<use xlink:href="#textPath03" fill="none" stroke="red" data-reactid="19"></use>
<text x="2" y="40%" font-size="20" data-reactid="20">
<textPath xlink:href="#textPath03" data-reactid="21">Teste</textPath>
</text>
</svg>
This is a REACT rendered SVG.
Thanks!!
The path id contains an extraneous # character. Only the reference to the path should have that. Removing it fixes your example...
<svg version="1.1" height="700" width="700" viewBox="-5 -5 110 110" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" data-reactid="13">
<defs data-reactid="14">
<path id="textPath03" d="M95,50 A45,45 0 0 0 50,5" data-reactid="15"></path>
</defs>
<line x1="50" y1="50" x2="100" y2="50" stroke="black" data-reactid="16"></line>
<line x1="50" y1="50" x2="50" y2="0" stroke="black" data-reactid="17"></line>
<path d="M90,50 A40,40 0 0 0 50,10" fill="none" stroke="green" data-reactid="18"></path>
<use xlink:href="#textPath03" fill="none" stroke="red" data-reactid="19"></use>
<text x="2" y="40%" font-size="20" data-reactid="20">
<textPath xlink:href="#textPath03" data-reactid="21">Teste</textPath>
</text>
</svg>
I'm trying to use the react-native-svg library particularly with the Symbol element as follows. I used the example code here:
<Svg
height="150"
width="110"
>
<Symbol id="symbol" viewbox="0 0 150 110" width="100" height="50">
<Circle cx="50" cy="50" r="40" strokeWidth="8" stroke="red" fill="red"/>
<Circle cx="90" cy="60" r="40" strokeWidth="8" stroke="green" fill="white"/>
</Symbol>
<Use
href="#symbol"
x="0"
y="0"
/>
When I run this code, I get the following error in the logs and the app crashes:
<Error>: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSRegularExpression enumerateMatchesInString:options:range:usingBlock:]: nil argument'
When I don't use the Symbol object everything works fine. Has anyone run into this issue before?
I was actually able to fix this by copying and pasting the example on this page:
https://developer.mozilla.org/en-US/docs/Web/SVG/Element/symbol
You need the width and height elements on the Use tag as follows:
<Svg height="150" width="110">
<Symbol id="symbol" viewbox="0 0 150 110" width="100" height="50">
<Circle cx="50" cy="50" r="40" strokeWidth="8" stroke="red" fill="red"/>
<Circle cx="90" cy="60" r="40" strokeWidth="8" stroke="green" fill="white"/>
</Symbol>
<Use href="#symbol" x="0" y="0" width="100" height="50"/>
</Svg>
I definitely thought I tried that before, so it may have also been me running the following clear cache and re-compiling commands as well:
rm -rf node_modules && npm install and npm start -- --reset-cache
Either way, problem fixed!