On hover change SVG from colour to gradient with a transition - html

I have an SVG image which I would like to change from a block colour to a gradient. I have got this working, however I would like to have a transition or animation. Is this possible - if so how would it be achieved?
http://jsfiddle.net/otaxjpa2/
HTML:
<svg width="96px" height="96px" viewBox="0 0 512 512" enable-background="new 0 0 512 512" xml:space="preserve">
<defs>
<linearGradient id="gradient" gradientUnits="userSpaceOnUse" fy="90%" >
<stop offset="0" style="stop-color:#1EBEE0"/>
<stop offset="1" style="stop-color:#952491"/>
<animate attributeName="fy"from="0" to="1" dur="5s" repeatCount="indefinite" />
</linearGradient>
</defs>
<path id="time-3-icon" d="M256,50C142.229,50,50,142.229,50,256c0,113.77,92.229,206,206,206c113.77,0,206-92.23,206-206
C462,142.229,369.77,50,256,50z M256,417c-88.977,0-161-72.008-161-161c0-88.979,72.008-161,161-161c88.977,0,161,72.007,161,161
C417,344.977,344.992,417,256,417z M382.816,265.785c1.711,0.297,2.961,1.781,2.961,3.518v0.093c0,1.72-1.223,3.188-2.914,3.505
c-37.093,6.938-124.97,21.35-134.613,21.35c-13.808,0-25-11.192-25-25c0-9.832,14.79-104.675,21.618-143.081
c0.274-1.542,1.615-2.669,3.181-2.669h0.008c1.709,0,3.164,1.243,3.431,2.932l18.933,119.904L382.816,265.785z"/>
</svg>
CSS:
svg {
fill: blue;
transition: all 0.3s ease;
display: inline-block;
-webkit-transition: fill .4s ease;
-moz-transition: fill .4s ease;
-o-transition: fill .4s ease;
transition: fill .4s ease;
}
svg:hover {
fill: url(#gradient);
}
Any pointers would be much appreciated!

You can avoid using two rects by transitioning the stop-colors instead:
svg {
display: inline-block;
}
stop {
transition: .4s ease;
}
/* Use the colors to transition to */
svg:hover stop:first-child {
stop-color: #1EBEE0;
}
svg:hover stop:last-child {
stop-color: #952491;
}
<svg width="96px" height="96px" viewBox="0 0 512 512" enable-background="new 0 0 512 512" xml:space="preserve">
<defs>
<linearGradient id="gradient" gradientUnits="userSpaceOnUse" fy="90%">
<stop offset="0" stop-color="blue" /> <!-- Colors to transition from -->
<stop offset="1" stop-color="blue" />
</linearGradient>
<mask id="clock-icon-mask" maskUnits="userSpaceOnUse" x="0" y="0" width="512" height="512">
<path d="M256,50C142.229,50,50,142.229,50,256c0,113.77,92.229,206,206,206c113.77,0,206-92.23,206-206C462,142.229,369.77,50,256,50z M256,417c-88.977,0-161-72.008-161-161c0-88.979,72.008-161,161-161c88.977,0,161,72.007,161,161C417,344.977,344.992,417,256,417z M382.816,265.785c1.711,0.297,2.961,1.781,2.961,3.518v0.093c0,1.72-1.223,3.188-2.914,3.505c-37.093,6.938-124.97,21.35-134.613,21.35c-13.808,0-25-11.192-25-25c0-9.832,14.79-104.675,21.618-143.081c0.274-1.542,1.615-2.669,3.181-2.669h0.008c1.709,0,3.164,1.243,3.431,2.932l18.933,119.904L382.816,265.785z"
fill="white" />
</mask>
</defs>
<g mask="url(#clock-icon-mask)">
<rect x="0" y="0" width="512" height="512" fill="url(#gradient)" />
</g>
</svg>
Here's a demo of the technique using a simplified shape (no mask required).

Put the two fill styles on overlapping <rect> objects, and use the clock icon as a mask object applied to both <rect> objects. You can then animate the apparent fill style by animating the opacity of the uppermost <rect>. Remember to apply a white fill to the mask object (white=opaque, black=transparent).
If the following snippet doesn't work, try this jsfiddle link.
#clock-gradient {
opacity: 0.0;
display: inline-block;
-webkit-transition: opacity .4s ease;
-moz-transition: opacity .4s ease;
-o-transition: opacity .4s ease;
transition: opacity .4s ease;
}
#clock-gradient:hover {
opacity: 1.0;
}
<svg width="96px" height="96px" viewBox="0 0 512 512" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<linearGradient id="gradient" gradientUnits="userSpaceOnUse" fy="90%">
<stop offset="0" style="stop-color:#1EBEE0" />
<stop offset="1" style="stop-color:#952491" />
</linearGradient>
<mask id="clock-icon-mask" maskUnits="userSpaceOnUse" x="0" y="0" width="512" height="512">
<path d="M256,50C142.229,50,50,142.229,50,256c0,113.77,92.229,206,206,206c113.77,0,206-92.23,206-206C462,142.229,369.77,50,256,50z M256,417c-88.977,0-161-72.008-161-161c0-88.979,72.008-161,161-161c88.977,0,161,72.007,161,161C417,344.977,344.992,417,256,417z M382.816,265.785c1.711,0.297,2.961,1.781,2.961,3.518v0.093c0,1.72-1.223,3.188-2.914,3.505c-37.093,6.938-124.97,21.35-134.613,21.35c-13.808,0-25-11.192-25-25c0-9.832,14.79-104.675,21.618-143.081c0.274-1.542,1.615-2.669,3.181-2.669h0.008c1.709,0,3.164,1.243,3.431,2.932l18.933,119.904L382.816,265.785z" fill="white" />
</mask>
</defs>
<g mask="url(#clock-icon-mask)">
<rect x="0" y="0" width="512" height="512" fill="blue" />
<rect id="clock-gradient" x="0" y="0" width="512" height="512" fill="url(#gradient)" />
</g>
</svg>

Related

Change svg gradient to solid white with a transition?

I can't seem to get a transition on an SVG when I hover it.
The svg has a gradient inside ... but I want it to transition to white on hover.
Any ideas?
svg {
width: 20px;
height: 20px;
path {
transition: fill 0.4s ease;
&:hover {
fill: $white;
}
}
}
<svg id="Facebook_icon" data-name="Facebook icon" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="8.713" height="16.779" viewBox="0 0 8.713 16.779">
<defs>
<linearGradient id="linear-gradient" x1="0.5" x2="0.5" y2="1" gradientUnits="objectBoundingBox">
<stop offset="0" stop-color="#f7d077"/>
<stop offset="0.112" stop-color="#ffefaa"/>
<stop offset="0.269" stop-color="#ffdb74"/>
<stop offset="0.552" stop-color="#dba846"/>
<stop offset="0.808" stop-color="#dba846"/>
<stop offset="1" stop-color="#f4cd6f"/>
</linearGradient>
</defs>
<path id="Icon" d="M8.713,2.785H7.134c-1.238,0-1.479.588-1.479,1.451v1.9H8.61L8.225,9.125H5.655v7.653H2.576V9.125H0V6.142H2.576v-2.2A3.594,3.594,0,0,1,6.412,0a21.049,21.049,0,0,1,2.3.117Z" fill="url(#linear-gradient)"/>
</svg>
For CSSOM, a gradient is of type <image>, and you can't transition from an <image> to a solid <color>.
What you can do however is to transition from an <image> to an other <image>, so we should have been able to transition between two gradients, but as of this writing it seems no browser supports transitioning between svg gradients and none supports CSS gradients as fill value.
What does work however is to transition the stop-color values of the <stop> elements.
For this to happen only when the <path> is hovered, I did change the structure of your svg.
svg {
width: 20px;
height: 20px;
}
svg stop {
transition: stop-color 0.4s ease;
}
svg path:hover ~ defs stop {
stop-color: white;
}
<svg id="Facebook_icon" data-name="Facebook icon" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="8.713" height="16.779" viewBox="0 0 8.713 16.779">
<path id="Icon" d="M8.713,2.785H7.134c-1.238,0-1.479.588-1.479,1.451v1.9H8.61L8.225,9.125H5.655v7.653H2.576V9.125H0V6.142H2.576v-2.2A3.594,3.594,0,0,1,6.412,0a21.049,21.049,0,0,1,2.3.117Z"
fill="url(#linear-gradient)"/>
<defs>
<linearGradient id="linear-gradient" x1="0.5" x2="0.5" y2="1" gradientUnits="objectBoundingBox">
<stop offset="0" stop-color="#f7d077"/>
<stop offset="0.112" stop-color="#ffefaa"/>
<stop offset="0.269" stop-color="#ffdb74"/>
<stop offset="0.552" stop-color="#dba846"/>
<stop offset="0.808" stop-color="#dba846"/>
<stop offset="1" stop-color="#f4cd6f"/>
</linearGradient>
</defs>
</svg>

SVG text, tspan font-size and centered

I have an issue with font size on SVG as us can see in svg the font-size:16px different from paragraph font-size but the they have the same value 16px. I would like to have font size in svg such on paragraph.
The second issue, it's how to center <tspan text-anchor="middle" class="tc-label-value">85</tspan> when I delete <tspan class="label-text" text-anchor="middle" x="0" y="50" dy="15">Text in SVG</tspan>
I mean the is a case when I have Text in svg and have not
https://codepen.io/palaniichukdmytro/pen/BaaKbze
SVG text, & span font-size and center solution
body {
margin: 0;
}
.wr {
width: 180px;
margin: 0 auto;
}
.label-text {
font-size: 12px;
text-align: center;
}
.tc-label-value {
font-size: 12px;
text-align: center;
}
.par {
font-size: 16px;
text-align: center;
}
<div class='wr'>
<svg viewBox="0 0 100 100" width="100%" height="100%" style="display: block;">
<path d="M 95.5 50 A 45.5 45.5 0 1 1 11.315120324302569 26.047336589080288" stroke-width="9" stroke-dasharray="168.16760675098308" stroke-dashoffset="2.842170943040401e-14" stroke="#a3bfff" stroke-linecap="round" fill="none" style="transition: stroke-dashoffset 500ms ease-out 0s;"></path>
<path d="M 11.315120324302569 26.047336589080288 A 45.5 45.5 0 0 1 95.5 49.999999999999986" stroke-width="9" stroke-dasharray="117.71732472568812" stroke-dashoffset="0" stroke="#66bb6a" stroke-linecap="round" fill="none" style="transition: stroke-dashoffset 500ms ease-out 0s;"></path>
<text transform="translate(50)" x="0" y="50">
<tspan text-anchor="middle" class="tc-label-value">85</tspan>
<tspan class="label-text" text-anchor="middle" x="0" y="50" dy="15">Text in SVG</tspan>
</text>
</svg>
<p class='par'>Text Paragraph</p>
</div>
The font size for the svg element should be 8.88px. Why? Because the svg's width is 100 units or px ( viewBox="0 0 100 100" ) and is scaled up to 180px ( .wr{width: 180px;} ). Since you need the font size inside the svg to look like 16px, the real font size should be 16 * 100 / 180 = 8.88
.wr {
width: 180px;
}
.par{
font-size: 16px;
}
svg{font-size: 8.88px;}
<div class='wr'>
<svg viewBox="0 0 100 100" style="display: block;">
<path d="M 95.5 50 A 45.5 45.5 0 1 1 11.315120324302569 26.047336589080288" stroke-width="9" stroke-dasharray="168.16760675098308" stroke-dashoffset="2.842170943040401e-14" stroke="#a3bfff" stroke-linecap="round" fill="none" style="transition: stroke-dashoffset 500ms ease-out 0s;"></path>
<path d="M 11.315120324302569 26.047336589080288 A 45.5 45.5 0 0 1 95.5 49.999999999999986" stroke-width="9" stroke-dasharray="117.71732472568812" stroke-dashoffset="0" stroke="#66bb6a" stroke-linecap="round" fill="none" style="transition: stroke-dashoffset 500ms ease-out 0s;"></path>
<text transform="translate(50)" x="0" y="50">
<tspan text-anchor="middle" class="tc-label-value">85</tspan>
<tspan class="label-text" text-anchor="middle" x="0" y="50" dy="15">Text in SVG</tspan>
</text>
</svg>
<p class='par'>Text Paragraph</p>
</div>

Text fade in with SVG

What I want to happen is when you roll over the image, the vector shows up but I also want text to show up as well. Right now, the text is there but it's consistently there. I only want it to appear when you hover over the image.
svg{
background:url('https://vignette.wikia.nocookie.net/lunifer-kingdom/images/f/fb/Wolf_howling_at_moon.jpg/revision/latest?cb=20140105045552');
background-size:cover;
width:40vmin; height:auto;
display:block;
}
h5 {
height: auto;
width: 280px;
padding: 10px;
color: white;
margin-top: -105px;
font-size: 15px;
}
text {
font-size:350px;
transition:font-size .4s ease-out;
font-weight:900;
font-family:arial;
}
svg:hover text{
transition:font-size .4s ease-in;
font-size: 10px;
}
<svg viewbox="0 0 50 50" width="50" height="50">
<defs>
<mask id="mask" x="0" y="0" width="100" height="49">
<rect x="0.5" y="0.5" width="49" height="49" fill="#fff"/>
<text x="18" text-anchor="middle" y="50" dy="0">D</text>
<text x="23" id="ltrV" text-anchor="middle" y="50" dy="0">A</text>
<text x="28" text-anchor="middle" y="50" dy="0">R</text>
<text x="33" text-anchor="middle" y="50" dy="0">K</text>
</mask>
</defs>
<rect x="0.5" y="0.5" width="49" height="49" mask="url(#mask)" fill-opacity="1" fill="#C0C0C0"/>
<div id="overlay">
<h5><center>text</center></h5>
</svg>
Here is the fiddle to solve your problem.
Solution fiddle link
Further I'd like to inform you that, in your layout you have put HTML div tag inside SVG tag, which is not a good approach. Even in must have case, you should add HTML tags inside SVG using foreignObject element. Please refer this link for more info on this :https://bl.ocks.org/Jverma/2385cb7794d18c51e3ab
What I did is put this code outside SVG and gave it opacity 0:
<div id="overlay">
<h5><center>text</center></h5>
</div>
And making opacity 1 on hover using this code:
svg:hover ~ #overlay {
transition: opacity .7s ease-in;
opacity: 1;
}

strokes above an SVG for some reason

I have an SVG I made, now it has a couple of blocks that move in a straight line.
sometimes when the blocks move it just adds a stroke above them, although I have stroke disabled.
NOTE: if you look closer you can see the orange strokes above the blocks
here's what it looks like.
#keyframes move {
0% {
transform: translate3d(250px , 0px , 0px);
}
50% {
transform: translate3d(-200px , 0px , 0px);
}
100% {
transform: translate3d(250px , 0px , 0px);
}
}
#loading_bar .cls-2 {
stroke: none;
transition: transform 2s ease-in-out;
animation: move 4s infinite forwards;
}
<svg id="svg" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1920 1080">
<defs>
<style>
.cls-1{fill:#64b2c1;}
.cls-2{fill:orange;}
.cls-3{fill:transparent;stroke-miterlimit:10;}
.cls-4{fill:#191919;}
</style>
</defs>
<g id="background">
<rect class="cls-1" width="1920" height="1080"/>
<rect class="cls-2" x="871" y="769" width="90" height="84"/>
</g>
<g id="loading_bar">
<rect class="cls-4" x="545" y="769" width="792" height="84"/>
<rect class="cls-2" x="789" y="789" width="40" height="40"/>
<rect class="cls-2" x="988" y="789" width="40" height="40"/>
<rect class="cls-2" x="888" y="789" width="40" height="40"/></g></svg>

How to add a link inside an svg circle

I have drawn a circle using svg. This circle has a hover effect. I would like to add a link within in the circle and for the link text to change color along with the hover effect.
svg#circle {
height: 250px;
width: 250px;
}
circle {
stroke-dasharray: 700;
stroke-dashoffset: 700;
stroke-linecap: butt;
-webkit-transition: all 2s ease-out;
-moz-transition: all 2s ease-out;
-ms-transition: all 2s ease-out;
-o-transition: all 2s ease-out;
transition: all 2s ease-out;
}
circle:hover {
fill: pink;
stroke-dashoffset: 0;
stroke-dasharray: 700;
stroke-width: 10;
}
<svg id="circle">
<circle cx="125" cy="125" r="100" stroke="darkblue" stroke-width="3" fill="green" />
</svg>
You need to add a text element wrapped in an anchor link.
Note, the text element, being on top of the circle will block the hover action on that circle. So, I've wrapped the whole thing in a g group and placed the hover capture on that instead.
svg#circle {
height: 250px;
width: 250px;
}
g circle {
stroke-dasharray: 700;
stroke-dashoffset: 700;
stroke-linecap: butt;
-webkit-transition: all 2s ease-out;
-moz-transition: all 2s ease-out;
-ms-transition: all 2s ease-out;
-o-transition: all 2s ease-out;
transition: all 2s ease-out;
}
g:hover circle {
fill: pink;
stroke-dashoffset: 0;
stroke-dasharray: 700;
stroke-width: 10;
}
text {
fill: pink;
font-size: 24px;
}
a:hover text {
fill: blue;
}
<svg id="circle">
<g>
<circle cx="125" cy="125" r="100" stroke="darkblue" stroke-width="3" fill="green" />
<a xlink:href="https://www.google.co.uk/" target="_top">
<text x="50%" y="50%" style="text-anchor: middle">google</text>
</a>
</g>
</svg>
I think this will work :
<svg id="circle">
<a xlink:href="https://www.google.com" style="cursor: pointer" target="_blank">
<circle cx="125" cy="70" r="60" stroke="darkblue" stroke-width="3" fill="green" />
</a>
</svg>
EDIT: Dynamically adding link to SVG Circle.
function addAnchor(){
var dummyElement = document.createElement("div");
dummyElement.innerHTML = '<a xlink:href="https://www.google.com" style="cursor: pointer" target="_blank"></a>';
var htmlAnchorElement = dummyElement.querySelector("a");
var circleSVG = document.getElementById("circle");
htmlAnchorElement.innerHTML = circleSVG.innerHTML;
circleSVG.innerHTML = dummyElement.innerHTML;
}
<svg id="circle">
<circle cx="125" cy="70" r="60" stroke="darkblue" stroke-width="3" fill="green" />
</svg>
<button onclick="addAnchor()">Add Anchor</button>
very simple!..
just wrap the entire SVG in a link...this worked for me anyway!!
initialise the link,
insert svg,
close svg,
close link
<svg style="align-self: center" height="125" width="190" >
<defs>
<linearGradient id="grad1" x1="0%" y1="0%" x2="100%" y2="0%">
<stop offset="3%" style="stop-color:rgb(255,255,0);stop-opacity:0" />
<stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" />
</linearGradient>
</defs>
<ellipse cx="100" cy="70" rx="85" ry="55" fill="url(#grad1)" />
<text fill="#000066" font-size="40" font-family="Verdana" x="50" y="86">MBS</text>
Sorry, your browser does not support SVG.
</svg> </a>
Very, Very Simple. Add onClick in tag element,
Using the inLine event handler.
Event handlers are not only for HTML objects, like DIV, TR, P, TABLE, etc.
https://www.w3schools.com/jsref/event_onclick.asp
<svg>
<circle onClick="location.href='https://stackoverflow.com'" cx="50" cy="50" r="25" stroke="darkblue" stroke-width="3" fill="green" />
</svg>