SVG Hover State with Multiple Elements - javascript

Good afternoon everyone,
I'm defining an SVG on my page with the following defs.
<svg width="0" height="0">
<defs>
<g id="stroke-hexagon">
<polygon fill="#002663" stroke="#FFFFFF" stroke-width="6" stroke-miterlimit="12" points="57.8,185 5.8,95 57.8,5 161.8,5 213.8,95 161.8,185 "/>
</g>
<g id="hexagon">
<polygon fill="#006890" points="52,180 0,90 52,0 156,0 208,90 156,180 "/>
</g>
</defs>
</svg>
...and implementing it later in the HTML using this:
<svg width="208px" height="180px" viewBox="0 0 208 180" >
<use xlink:href="#hexagon"></use>
<text class="faicon" x="50%" y="70px" fill="white" font-size="80px" text-anchor="middle">&#xf040</text>
<text text-anchor="middle" x="50%" y="70%" fill="white">Logo Here</text>
</svg>
Works totally fine. I am also able to style the polygon's fill with simple CSS. Looks like this:
#hexagon:hover polygon {
fill:#990000;
}
The hover effect fails, however, whenever the mouse leaves the polygon and instead hovers over either of the 'text' elements within the svg. Is there a way to define a CSS rule that prevents this behavior. Or, would it be better (easier) to change the attribute using JS / jQuery?
Thanks!

Your texts are rendered on top of your polygon and are therefore intercepting mouse events. You should set up a css rule like
text {
pointer-events: none;
}
This will prevent the text from becoming a target of mouse events which should give you the desired hover effect for the polygon.

Related

How can I cut text from a shape in SVG?

I have an SVG file that looks like below:
Is there a way to make the text transparent? That is, instead of a fill color I want to cut out the layers and show what's in the background (of the SVG i.e. whatever lies underneath the SVG). In other words, make the intersection of the path & text to be transparent?
Contents of the SVG file:
<svg width="36.087" height="34.314" viewBox="0 0 36.087 34.313999" x="1190.56" y="753.5780000000001">
<path fill="#63a95c" d="M36.087 13.107l-13.305-.66L18.047 0l-4.742 12.446L0 13.106l10.377 8.352L6.89 34.314l11.157-7.285 11.14 7.284-3.475-12.856" fill-rule="evenodd"/>
<text font-size="10px" x="10.498" y="23.484" fill="#ffffff" fill-opacity="1" font-family="OpenSans-Bold">8.5</text>
</svg>
I tried changing the transparency of the text element, but that only affects the text. The text inside the SVG is variable is populated dynamically so I can't "pre-process" the SVG file. Is there a way perhaps using evenodd fill or something similar to create an "exclusion" for intersection? Is it possible using one of the SVG JS libraries such as snap.svg or svg.js?
Edit:
The final SVG should look like this:
The SVG code posted above is for the star and the text. The final SVG should have the background color showing through the text while retaining the outer shape of the star.
Create a mask, put the text in it via a text element and then use the mask on the shape you want to clip a hole in. Something like this...
head, body {
width:100%;
height:100%;
}
<svg width="100%" height="100%" viewBox="0 0 200 200">
<defs>
<mask id="sample" maskUnits="userSpaceOnUse">
<rect width="100%" height="100%" fill="white"/>
<text x="12" y="23" font-size="10" font-family="Impact">9.0</text>
</mask>
</defs>
<path fill="#63a95c" d="M36.087 13.107l-13.305-.66L18.047 0l-4.742 12.446L0 13.106l10.377 8.352L6.89 34.314l11.157-7.285 11.14 7.284-3.475-12.856" fill-rule="evenodd" mask="url(#sample)"/>

How to bind an event to the element inside svg tag?

I've got an SVG (it's an nvd3 chart).
Now I need to show a tooltip when the users hovers some text.
My problem is that even if I manually right-click the "text" tag in Google Chrome and choose "Inspect element", it inspects the SVG container instead of the "text" element.
But when I click some "rect" tag, everything works like a charm.
So I can do:
$('rect').mouseover(sth)
and it works, but I cannot do
$('text').mouseover(sth)
because the event is never fired.
When I click the "text" tag, the "click" event is fired on the SVG tag instead of the appropriate one.
My SVG Code:
<svg>
<g class="nvd3 nv-wrap nv-multiBarHorizontalChart" transform="translate(100,0)">
<g>
<g class="nv-x nv-axis">
<g class="nvd3 nv-wrap nv-axis">
<g>
<g class="tick major" transform="translate(0,202.26543209876542)" style="opacity: 1;">
<text x="-5" dy=".32em" y="0" style="text-anchor: end; display: block;">Android 3.1</text>
</g>
</g>
</g>
</g>
</g>
</g>
</svg>
Fiddle:
http://jsfiddle.net/BChC9/
When you click the text, it alerts "text", but in my project, it alerts "svg". What could be the cause of it?
If it's only tooltips you need, you could add title elements to the SVG (try it):
<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="30">
<text x="10" y="20">
<title>This is a tooltip</title>
This is text
</text>
</svg>

SVG - how to define transformation on two distinct elements

I have the following svg:
<svg
width="1750"
height="1125"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink">
<g>
<g id="svg_4">
<g id="imgG_4">
<image
transform="rotate(35.3608 608.333 503.301)"
xlink:href="https://www.google.com/images/srpr/logo4w.png"
id="img_4"
height="188.79927"
width="188.79927"
y="408.90001"
x="706.21582"/>
</g>
<rect
transform="rotate(35.3608 783.333 667.587)"
id="border_4"
height="264.31644"
width="360.92146"
y="535.42838"
x="602.87256"
fill-opacity="0"
stroke-width="5"
stroke="#000000"
fill="#000000"/>
</g>
</g>
</svg>
I'd like to change the angle of the both the rect and image. I cannot however, figure out how to adjust the image such that its spacing within the rect is consistent as i rotate the rect.
E.g. After rotating both the rect and the image the image has the same amount of whitespace above and to the left as it did prior to rotation of both elements.
I cannot put the rotation on the container groups due to other technical restraints.
I'd like to end up with something like:
<svg
width="1750"
height="1125"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink">
<g>
<g
id="svg_4">
<g
id="imgG_4">
<image
transform="rotate(70.3608 608.333 503.301)"
xlink:href="https://www.google.com/images/srpr/logo4w.png"
id="img_4"
height="188.79927"
width="188.79927"
y="408.90001"
x="706.21582"
/>
</g>
<rect
transform="rotate(70.3608 783.333 667.587)"
id="border_4"
height="264.31644"
width="360.92146"
y="535.42838"
x="602.87256"
fill-opacity="0"
stroke-width="5"
stroke="#000000"
fill="#000000"
/>
</g>
</g>
</svg>
The caveat is that the x,y values have to change on the image in order to get the layout correct and I have know idea how to calculate them.
Any idea on how I would go about this? I will be using javascript to do the math involved...
Plnkr is here
Assuming you wanted to rotate both elements around the same rotation point as the rectangle (ie. 783.333,667.587), then all you need to is apply the additional rotation to the front of both element transforms. So:
<image transform="rotate(90 783.333 667.587) rotate(70.3608 608.333 503.301)"
<rect transform="rotate(90 783.333 667.587) rotate(70.3608 783.333 667.587)"
would rotate both elements an additional 90deg round the above centre of rotation.
However you said you want to do the maths yourself in Javascript. So to get you started, it might help to know that the transform:
rotate(r x y)
is equivalent to:
translate(x y) rotate(r) translate(-x -y)

Color SVG image with CSS

Is there a way I can apply colors to SVG image as a whole? not going down to each path and circle and line I have and doing it one by one?
I tried to group my svg elements with
<g class="myImage">
and in the myImage class i put fill:red to make it apply to all the elements in that group, but that doesnt work!!
How can I make it so I can only apply a color once and it goes to the whole image or elements in a group?
--added code
This is my SVG file (this is just a sample, i know the circle repeats 3 times)
<?xml version="1.0" standalone="no"?>
<?xml-stylesheet href="../css/logo.css" type="text/css"?>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
<g class="logo">
<circle cx="50%" cy="50%" r="35%"/>
<circle cx="50%" cy="50%" r="35%"/>
<circle cx="50%" cy="50%" r="35%"/>
</g>
</svg>
and in my logo.css file i have a class
.logo {
fill:red;
}
Hope this helps
You're doing it right. The fill should inherit from the <g> element and all the circles will be red, one on top of another. Firefox displays a big red circle.

How to extend the text along the entire textPath in SVG

How do I make the text on a path (see screenshot) extend so that it follows the entire textPath?
I have tried using the method attribute value stretch but it doesn't work like I expect - it doesn't stretch the text along the path.
Is there a way to make this work in all browsers?
The way to spread out the text over the entire textPath is to use the textLength attribute. Also see this other question for how to compute a good value for textLength. Here's how to do it:
<svg viewBox="0 0 500 300" version="1.1">
<defs>
<path id="s3" d="M 10,90 Q 100,15 200,70" />
</defs>
<text font-size="20">
<textPath xlink:href="#s3" textLength="205">
Short text
</textPath>
</text>
<use xlink:href="#s3" fill="none" stroke="black" stroke-width="1"/>
</svg>
Viewable example: http://jsfiddle.net/zkZ2n/
Here's the bugreport for Firefox not supporting textLength: https://bugzilla.mozilla.org/show_bug.cgi?id=569722

Categories

Resources