Web Animation inside SVG breaks when used as a background image - javascript
I just generated myself an SVG-based loading indicator using an online service, but every time a page loads that uses it I get a warning from Chrome which informs me that SMIL animations are getting deprecated, which is rather obnoxious. In an attempt to get rid of it I decided to look into replacing the <animate> tags with Web Animations. Below is the original SVG:
<svg width='200px' height='200px' xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" preserveAspectRatio="xMidYMid" class="uil-ring-alt">
<circle cx="50" cy="50" r="40" stroke="white" fill="none" stroke-width="10" stroke-linecap="round"/>
<circle cx="50" cy="50" r="40" stroke="#808080" fill="none" stroke-width="6" stroke-linecap="round">
<animate attributeName="stroke-dashoffset" dur="2s" repeatCount="indefinite" from="0" to="502"/>
<animate attributeName="stroke-dasharray" dur="2s" repeatCount="indefinite" values="150.6 100.4;1 250;150.6 100.4"/>
</circle>
</svg>
And here is what I ended up with:
<svg width='200px' height='200px' xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" preserveAspectRatio="xMidYMid" xmlns:xlink="http://www.w3.org/1999/xlink">
<circle cx="50" cy="50" r="40" stroke="white" fill="none" stroke-width="10" stroke-linecap="round"/>
<circle cx="50" cy="50" r="40" stroke="#808080" fill="none" stroke-width="6" stroke-linecap="round" id="line"/>
<script type="text/javascript" xlink:href="https://cdn.rawgit.com/web-animations/web-animations-js/45d8e40300e82ff02ccfbbc78c89500de0f5616f/web-animations.min.js"></script>
<script type="text/javascript"><![CDATA[
var line = document.getElementById("line");
line.animate(
[
{strokeDashoffset:0},
{strokeDashoffset:502}
],
{ duration: 2000, iterations: Infinity }
);
line.animate(
[
{strokeDasharray:"150.6 100.4"},
{strokeDasharray:"1 250"},
{strokeDasharray:"150.6 100.4"}
],
{ duration: 2000, iterations: Infinity }
);
]]></script>
</svg>
I was about to be really excited that I managed to make it work, then my smile froze on my face as soon as I noticed that the exact same SVG, when used as a background-image in CSS, refuses to animate at all (demo below; note that I inlined the SVG using a data URI here, but the same happens when the SVG is loaded using a regular URL).
body:before {
content: '';
display: block;
width: 200px;
height: 200px;
background: url('data:image/svg+xml;utf8,<svg width="200px" height="200px" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" preserveAspectRatio="xMidYMid" xmlns:xlink="http://www.w3.org/1999/xlink"><circle cx="50" cy="50" r="40" stroke="white" fill="none" stroke-width="10" stroke-linecap="round"/><circle cx="50" cy="50" r="40" stroke="#808080" fill="none" stroke-width="6" stroke-linecap="round" id="line"/><script type="text/javascript" xlink:href="https://cdn.rawgit.com/web-animations/web-animations-js/45d8e40300e82ff02ccfbbc78c89500de0f5616f/web-animations.min.js"></script><script type="text/javascript"><![CDATA[var line = document.getElementById("line");line.animate([{strokeDashoffset:0},{strokeDashoffset:502}],{ duration: 2000, iterations: Infinity });line.animate([{strokeDasharray:"150.6 100.4"},{strokeDasharray:"1 250"},{strokeDasharray:"150.6 100.4"}],{ duration: 2000, iterations: Infinity });]]></script></svg>') no-repeat center center;
background-size: contain;
}
Should I just ignore the warning and stay with SMIL or is there a way to make Web Animations work inside SVGs?
Unfortunately there is not support yet for animation with backgrounImage property.
While CSS Backgrounds and Borders Module Level 3 Editor’s Draft says
“Animatable: no” for background-image at the time of writing, support
for crossfading images in CSS appeared in Chrome 19 Canary. Until
widespread support arrives this can be faked via image sprites and
background-position or opacity. To animate gradients they must be the
same type
You can read more in the following articles:
http://oli.jp/2010/css-animatable-properties/
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_animated_properties
Related
How do I style one of the parameters in a circle inside an SVG?
I have a SVG with circles inside it. And I want them to be increasing and decreasing in radius for ever (like a pulsating circle). My problem is, can I do it with #keyframes? Or do I need jquery? And if so, how? Here is my code: <div class="mapa"> <svg (svg code here......) <circle opacity="0.3" cx="842" cy="451.814" r="25.582" id="1"/> <circle opacity="0.3" cx="542" cy="405.814" r="25.582" id="1"/> </svg> </div> How do I style the 'r' parameter? I read I cannot style the 'r' parameter, but this worked: <circle cx="168" cy="179" r="59" fill="white" stroke="black" onmouseover="evt.target.setAttribute('r', '72');" onmouseout="evt.target.setAttribute('r', '59');" /> However, I want to do it with continuous increase and decrease in radius? And not on mouseover/mouseleave. Something like (r=25, then r=30, then back to 25, and goes on forever). How do I do this? Thanks for your time, if you can give me any tips I'd apreciate it a lot!
Try to use svg smil animate <svg width="150" height="150"> <circle opacity="0.3" cx="84%" cy="45%" r="3" id="1"> <animate attributeName="r" values="3; 10; 3" keyTimes="0; 0.5; 1" dur="1s" repeatCount="indefinite" /> </circle> <circle opacity="0.3" cx="50%" cy="50%" r="10" id="2"> <animate attributeName="r" values="10; 3; 10" keyTimes="0; 0.5; 1" dur="1s" repeatCount="indefinite"/> </circle> </svg>
The easiest solution in CSS with a little hack around SVG containers. You change the container, not the svg. The circle element just fills 100% of the container. And the container artificially makes a circle with a border-radius. svg { border-radius: 50%; transition: all 1s; } svg:hover { width: 200px; height: 200px; } <svg width="100" height="100"> <circle cx="50" cy="50" r="100%" fill="green" /> </svg> You can figure out how to implement your own keyframes, if this solution works for you. And just to be clear, JQuery is a framework. You shouldn't bring up JQuery unless this question is about JQuery's framework. The language you're looking for is "Javascript" and it's in all major browsers by default. You can use Javascript to do this. const grow = function(radius) { var circle = document.getElementsByTagName("circle")[0]; circle.setAttribute('r', radius); } setTimeout(function() { grow(100); setTimeout(function() { grow(40); }, 2000); }, 2000); circle { transition: all 1s; } <svg width="200" height="200"> <circle cx="100" cy="100" r="40" fill="green" /> </svg>
Scale overriding SVG width and height attributes
I have an SVG element with a defined width and height, like <svg width="100px" height="100px"></svg>, filled with various elements. I want to have a kind of "zoom" feature, where a particular region of the SVG is zoomed in on to fill the whole SVG element. I planned to do this with the scale and translate attributes, i.e. by applying scale(x) to the SVG element and then calculating what I need to translate by in order to have the desired region remain visible. I expected this would keep the SVG at 100x100px and simply hide any element outside this region. However, this doesn't happen; the whole SVG element just gets bigger instead, even though the dimensions are explicitly defined as attributes. Clearly I'm misunderstanding the way that scaling and SVG dimensions work, does anyone know how I can achieve what I'm trying to do here?
You can warp the svg with a div element and use overflow: hidden. <div style="width: 300px; height: 300px; overflow: hidden"> <svg width="100" height="100" style="transform: scale(4);"> <circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" /> </svg> </div>
Do you mean something like this? function setViewBox(vbx){ svg.setAttribute("viewBox",vbx) } <svg viewBox="0 0 100 100" width="200px" height="200px" id="svg"> <rect x="0" y="0" width="100" height="100" stroke="black" fill="white" onclick="setViewBox('0 0 100 100')"/> <circle cx="25" cy="25" r="25" fill="red" onclick="setViewBox('0 0 50 50')"/> <rect x="60" y="10" width="30" height="30" fill="green" onclick="setViewBox('50 0 50 50')"/> <rect x="10" y="60" width="30" height="30" fill="blue" transform="rotate(45,25,75)" onclick="setViewBox('0 50 50 50')"/> <path d="M50 100L75 50L100 100z" fill="yellow" onclick="setViewBox('50 50 50 50')"/> </svg> or more like that? var last=null function setTransform(evt,trs){ reset() svg.appendChild(evt.target) evt.target.setAttribute("transform","scale(2 2) translate("+trs+")") last=evt.target } function reset(){ if(last) last.removeAttribute("transform") } <svg viewBox="0 0 100 100" width="200px" height="200px" id="svg"> <rect x="0" y="0" width="100" height="100" stroke="black" fill="white" onclick="reset()"/> <circle cx="25" cy="25" r="25" fill="red" onclick="setTransform(event,'0 0')"/> <rect x="60" y="10" width="30" height="30" fill="green" onclick="setTransform(event,'-50 0')"/> <rect x="10" y="60" width="30" height="30" fill="blue" transform="rotate(45,25,75)" onclick="setTransform(event,'0 -50')"/> <path d="M50 100L75 50L100 100z" fill="yellow" onclick="setTransform(event,'-50 -50')"/> </svg>
Could not play SVG animation once placed within webpage
I have created an animation using svgcircus.com, if I place it in webpage the animation doesn't play and just see static svg without animation but if I drag and drop SVG directly into browser then it plays: Here is how I call it: <img src="marker-with-wave.svg"/> Here is my SVG: <?xml version="1.0" standalone="no"?> <!-- Generator: SVG Circus (http://svgcircus.com) --> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> <svg id="SVG-Circus-59ac8158-f093-4171-00c0-061a33e45165" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 100 100" preserveAspectRatio="xMidYMid meet"> <circle id="actor_4" cx="50" cy="50" r="25" opacity="1" fill="rgba(0,0,0,0)" fill-opacity="1" stroke="rgba(0,160,160,1)" stroke-width="2" stroke-opacity="1" stroke-dasharray=""></circle> <circle id="actor_3" cx="50" cy="50" r="20" opacity="1" fill="rgba(0,0,0,0)" fill-opacity="1" stroke="rgba(0,160,160,1)" stroke-width="2" stroke-opacity="1" stroke-dasharray=""></circle> <circle id="actor_2" cx="50" cy="50" r="15" opacity="1" fill="rgba(0,0,0,0)" fill-opacity="1" stroke="rgba(0,160,160,1)" stroke-width="2" stroke-opacity="1" stroke-dasharray=""></circle> <circle id="actor_1" cx="50" cy="50" r="10" opacity="1" fill="rgba(0,0,0,0)" fill-opacity="1" stroke="rgba(0,160,160,1)" stroke-width="2" stroke-opacity="1" stroke-dasharray=""></circle> <g transform="translate(20 -1020)"> <path style="fill:#00A0A0;" d="M30,1043.4c-5.523,0-10,4.477-10,10c0,1.776,0.477,3.438,1.289,4.882 c0.135,0.24,0.276,0.476,0.43,0.704L30,1073.4l8.281-14.414c0.127-0.189,0.237-0.389,0.351-0.586l0.079-0.118 c0.811-1.445,1.289-3.106,1.289-4.882C40,1047.877,35.522,1043.4,30,1043.4z M30,1048.4c2.761,0,5,2.239,5,5c0,2.761-2.239,5-5,5 c-2.761,0-5-2.239-5-5C25,1050.639,27.239,1048.4,30,1048.4z"/> <path style="fill:#008282;" d="M30,1047.15c-3.452,0-6.25,2.798-6.25,6.25c0,3.451,2.798,6.25,6.25,6.25 c3.451,0,6.25-2.799,6.25-6.25C36.25,1049.948,33.451,1047.15,30,1047.15z M30,1049.65c2.071,0,3.75,1.679,3.75,3.75 s-1.679,3.75-3.75,3.75s-3.75-1.679-3.75-3.75S27.929,1049.65,30,1049.65z"/> </g> <script type="text/ecmascript"><![CDATA[(function(){var actors={};actors.actor_1={node:document.getElementById("SVG-Circus-59ac8158-f093-4171-00c0-061a33e45165").getElementById("actor_1"),type:"circle",cx:50,cy:50,dx:20,dy:32,opacity:1};actors.actor_2={node:document.getElementById("SVG-Circus-59ac8158-f093-4171-00c0-061a33e45165").getElementById("actor_2"),type:"circle",cx:50,cy:50,dx:30,dy:30,opacity:1};actors.actor_3={node:document.getElementById("SVG-Circus-59ac8158-f093-4171-00c0-061a33e45165").getElementById("actor_3"),type:"circle",cx:50,cy:50,dx:40,dy:30,opacity:1};actors.actor_4={node:document.getElementById("SVG-Circus-59ac8158-f093-4171-00c0-061a33e45165").getElementById("actor_4"),type:"circle",cx:50,cy:50,dx:50,dy:30,opacity:1};var tricks={};tricks.trick_1=(function(_,t){t=(function(n){return--n*n*n+1})(t)%1,t=0>t?1+t:t;var i;i=0.00>=t?1+(0.5-1)/0.00*t:t>=0.13?0.5-(t-0.13)*((0.5-1)/(1-0.13)):0.5;var a=_._tMatrix,r=-_.cx*i+_.cx,x=-_.cy*i+_.cy,c=a[0]*i,n=a[1]*i,M=a[2]*i,f=a[3]*i,g=a[0]*r+a[2]*x+a[4],m=a[1]*r+a[3]*x+a[5];_._tMatrix[0]=c,_._tMatrix[1]=n,_._tMatrix[2]=M,_._tMatrix[3]=f,_._tMatrix[4]=g,_._tMatrix[5]=m});tricks.trick_2=(function(t,i){i=(function(n){return.5>n?2*n*n:-1+(4-2*n)*n})(i)%1,i=0>i?1+i:i;var _=t.node;0.1>=i?_.setAttribute("opacity",i*(t.opacity/0.1)):i>=0.2?_.setAttribute("opacity",t.opacity-(i-0.2)*(t.opacity/(1-0.2))):_.setAttribute("opacity",t.opacity)});var scenarios={};scenarios.scenario_1={actors: ["actor_1","actor_2","actor_3","actor_4"],tricks: [{trick: "trick_1",start:0,end:1.00},{trick: "trick_2",start:0,end:1}],startAfter:0,duration:2000,actorDelay:100,repeat:0,repeatDelay:1000};var _reqAnimFrame=window.requestAnimationFrame||window.mozRequestAnimationFrame||window.webkitRequestAnimationFrame||window.oRequestAnimationFrame,fnTick=function(t){var r,a,i,e,n,o,s,c,m,f,d,k,w;for(c in actors)actors[c]._tMatrix=[1,0,0,1,0,0];for(s in scenarios)for(o=scenarios[s],m=t-o.startAfter,r=0,a=o.actors.length;a>r;r++){if(i=actors[o.actors[r]],i&&i.node&&i._tMatrix)for(f=0,m>=0&&(d=o.duration+o.repeatDelay,o.repeat>0&&m>d*o.repeat&&(f=1),f+=m%d/o.duration),e=0,n=o.tricks.length;n>e;e++)k=o.tricks[e],w=(f-k.start)*(1/(k.end-k.start)),tricks[k.trick]&&tricks[k.trick](i,Math.max(0,Math.min(1,w)));m-=o.actorDelay}_reqAnimFrame(fnTick)};_reqAnimFrame(fnTick);})()]]></script> </svg>
Instead of <img /> if I use <object></object> SVG is animation is working. <object type="image/svg+xml" data="marker-with-wave.svg"> Your browser does not support SVG </object>
How to get all elements at mouse position?
I have many elements on the same position and I want to listen for hover event on every element behind even if they are behind other elements, is there a way I can do this? (They are not hierarchically related and sometimes they are circles, polygons, etc, so checking for bounding rect is not ok) http://jsfiddle.net/4NdNS/4/ $circles.on("mouseover",function(){console.log(this);});
this is the solution: FIDDLE html: <div id=response></div> <svg id="mycircle Area"> <circle id="C1" fill="none" r="20" stroke="black" stroke-width="4" cx="100" cy="100"></circle> <circle fill="none" r="20" stroke="black" stroke-width="4" cx="100" cy="100"></circle> <circle fill="none" r="20" stroke="black" stroke-width="4" cx="100" cy="100"></circle> </svg> jq: $('circle').on("mousedown",function(e){ $("#response").append($(e).attr('id')+' '); e.preventDefault(); }); css: circle{ pointer-events: all; } this is your edited fiddle
How can I implement eraser function in SVG?
I think SVG is better than HTML5 canvas for some features, but I can't imagine an easy way to make eraser function. I there any way or any example?
This is a really janky way of doing it, but you could simply mimic your standard pen tool with a white stroke.
After looking at many examples (including Geert Bellemans answer here), I finally came up with this code that works. To use the Eraser, you draw new circles and append them inside the node. To draw new lines/shapes, you append them inside the node. Here it is: <svg xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" width="500px" height="638px"> <mask id="mask_1"> <rect width="100%" height="100%" fill="#fff"></rect> <circle cx="223" cy="122" r="8" stroke="#000" stroke-width="0px" fill="#000"></circle> <circle cx="222" cy="124" r="8" stroke="#000" stroke-width="0px" fill="#000"></circle> <circle cx="221" cy="125" r="8" stroke="#000" stroke-width="0px" fill="#000"></circle> <circle cx="220" cy="126" r="8" stroke="#000" stroke-width="0px" fill="#000"></circle> <circle cx="220" cy="127" r="8" stroke="#000" stroke-width="0px" fill="#000"></circle> </mask> <g mask="url(#mask_1)"> <polyline fill="none" points=" 210,149 212,148 213,144 215,142 216,139 219,135 220,133 224,128 226,126 229,121 233,117 237,112 243,107 248,103 253,99 258,96 263,92 268,90 271,88 272,87 273,87 274,87 275,86 275,85 276,85 277,85 278,85 279,85 280,84 " stroke="#ff0000" stroke-width="4"></polyline> </g> </svg>
You could use the clipPath element. Put your eraser drawing inside a clipPath element and clip the drawing you want to overlay with the eraser. This way the background stays visible. <clipPath id="test"> <polyline points="298,351 302,350 307,347 313,343 320,339 329,336 343,332 357,328 374,325 389,321 402,320 416,319 428,317 439,316 447,314 455,313 462,312 467,312 471,312 478,310 482,310 485,310 486,310 487,309 490,309 491,309 493,309 494,308 495,308 497,308 497,306 499,304 501,301 502,298 504,294 505,290 506,287 508,283 509,281 512,275 513,273 513,270 514,266 516,263 517,259 517,255 518,251 520,245 521,241 521,239 521,236 521,235 521,232 521,229 521,228 520,228 516,228 513,228 508,228 502,232 494,237 487,241 478,248 466,256 452,267 435,278 421,289 408,300 394,310 386,320 375,329 368,339 362,346 356,350 352,356 347,362 344,363 341,367 341,369 340,371 340,377 339,382 337,390 335,400 330,409 326,419 322,428 318,436 314,444 312,450 310,455 307,458 307,459 307,462 309,461 312,458 314,455 317,452 321,450 324,446 326,444 332,440 337,436 349,428 364,420 376,412 390,405 405,397 421,390 436,383 452,378 470,373 483,369 501,366 513,363 527,362 536,360 541,359 544,359 548,362 554,363 563,367 577,370 587,375 602,379 617,383 633,390 655,396 674,402 697,409 714,415 733,420 748,425 759,429 767,432 770,432 771,432 773,432 777,431 783,424 792,417 802,410 813,400 827,389 843,377 866,358 884,343 898,333 909,327 920,320 927,317 932,313 935,310 938,309 940,308" fill="none" stroke="#ffffff" stroke-width="20"></polyline> </clipPath> <path d="M600,329 600,325 600,321 595,317 591,310 590,306 586,304 585,301 582,298 577,296 574,294 571,293 567,290 563,290 556,287 556,287 551,286 544,285 539,285 539,285 532,285 524,285 516,283 506,283 498,283 490,283 482,285 474,287 467,289 459,291 455,293 451,293 448,296 447,296 445,297 445,298 445,300 445,301 445,302 445,304 445,305 445,306 445,309 445,312 445,316 445,320 445,325 447,329 448,332 449,335 449,337 452,340 454,344 456,346 459,348 460,352 464,355 467,356 468,359 472,360 474,362 475,363 478,363 481,363 487,366 490,367 493,367 497,367 498,367 502,367 505,367 508,367 512,367 516,367 520,367 525,367 531,367 539,367 545,367 555,367 560,367 564,367 567,367 568,367 570,367 570,366 571,364 571,363 571,362 573,362" fill="none" stroke="#000000" stroke-width="3" clip-path="url(#test)"></polyline>
You could redraw objects with display attribute set to "none" See here or here