Showing posts with label svg. Show all posts
Showing posts with label svg. Show all posts

Tuesday, October 9, 2018

how to zoom in on a complex svg structure

Leave a Comment

I have a complex set of shapes that are in the snippet. They are rendered with React but I'm really just looking for some pointers about how I would go about being able to zoom these shapes in and out.

My googling is failing and I can only really find examples of graphs.

How can zoom in and out complex structures like this?

    <svg height="767" width="903">      <g class="vx-group vx-tree" transform="translate(20, 70)">          <g class="vx-group" transform="translate(0, 70)">              <g class="vx-group" transform="translate(0, 0)">                  <path class="vx-link-vertical" d="M451.5,0C451.5,233.5,451.5,233.5,451.5,467" percent="0.5"                        stroke="#f7f7f3" stroke-width="1" stroke-opacity="0.2" fill="none"></path>              </g>              <g class="vx-group" transform="translate(0, 0)">                  <g class="vx-group" transform="translate(451.5, 0)" opacity="1">                      <g class="vx-group node__container" transform="translate(0, 0)">                          <svg class="" x="0" y="0" style="overflow: visible;">                              <polygon                                      points="25.98076211353316,-14.999999999999998 25.98076211353316,14.999999999999998 1.83697019872103e-15,30 -25.98076211353316,14.999999999999998 -25.980762113533157,-15.000000000000004 -5.510910596163089e-15,-30"                                      class="node__hexagon"></polygon>                          </svg>                          <g class="vx-group node__business-unit" transform="translate(0, 0)">                              <use xlink:href="#icon-BusinessUnit"></use>                          </g>                          <g class="hierarchy-label__container" transform="translate(0, -40)">                              <path class="" d="                                    M 0.0078125, 5.15625                                    L 34.64882865137755,25.156249999999996                                     M -0.9921875, 5.15625                                     L -34.63320365137754,25.156249999999996                                    H -65.8515625                                     a8,8 0 0 1 -8,-8                                      V -47.15625                                     a8,8 0 0 1 8,-8 H 65.8515625 a8,8 0 0 1 8,8                                     L 73.8515625, 17.156249999999996                                      a8,8 0 0 1 -8,8                                     L 34.64882865137755, 25.156249999999996                                     Z                                    ">                              </path>                              <svg x="0" y="0" style="overflow: visible;">                                  <text class="hierarchy-label__item__name" width="150" y="-25" x="0" text-anchor="middle"                                        style="pointer-events: none;">                                      <tspan x="0" dy="0em">Finance</tspan>                                  </text>                              </svg>                              <svg x="0" y="0" style="overflow: visible;">                                  <text class="hierarchy-label__item__type" width="150" y="-5" x="0" text-anchor="middle"                                        style="pointer-events: none;">                                      <tspan x="0" dy="0.71em">Business Unit</tspan>                                  </text>                              </svg>                          </g>                      </g>                  </g>              </g>          </g>      </g>  </svg>

5 Answers

Answers 1

You can simply use transform to scale the . Set the transform-origin from where you want to "pin" the zoom origin and use scale(x) in transform like in the above example that uses a range input element with minimum value to 1 and maximum to 200 for scalling from 1% to 200%:

const slider = document.getElementById("zoomRange");  const zvgZoom = document.getElementById("svgZoom");  const zoomValue = document.getElementById("zoomValue");    slider.oninput = function() {      //console.log('zoom', this.value / 100);      zoomValue.innerText = `${this.value}%`;      zvgZoom.style.transform = `scale(${this.value / 100})`;  }
#svgContainer {      background-color: #dedede;  }    #svgZoom {      transform-origin: 0% 0%;  }
<input type="range" min="1" max="200" value="100" class="slider" id="zoomRange">  <span id="zoomValue">100%</span>    <div id="svgContainer">      <svg id="svgZoom" height="767" width="903">          <g  class="vx-group vx-tree" transform="translate(20, 70)">              <g class="vx-group" transform="translate(0, 70)">                  <g class="vx-group" transform="translate(0, 0)">                      <path class="vx-link-vertical" d="M451.5,0C451.5,233.5,451.5,233.5,451.5,467" percent="0.5" stroke="#f7f7f3" stroke-width="1" stroke-opacity="0.2" fill="none"></path>                  </g>                  <g class="vx-group" transform="translate(0, 0)">                      <g class="vx-group" transform="translate(451.5, 0)" opacity="1">                          <g class="vx-group node__container" transform="translate(0, 0)">                              <svg class="" x="0" y="0" style="overflow: visible;">                                  <polygon points="25.98076211353316,-14.999999999999998 25.98076211353316,14.999999999999998 1.83697019872103e-15,30 -25.98076211353316,14.999999999999998 -25.980762113533157,-15.000000000000004 -5.510910596163089e-15,-30" class="node__hexagon"></polygon>                              </svg>                              <g class="vx-group node__business-unit" transform="translate(0, 0)">                                  <use xlink:href="#icon-BusinessUnit"></use>                              </g>                              <g class="hierarchy-label__container" transform="translate(0, -40)">                                 <path class="" d="                                    M 0.0078125, 5.15625                                    L 34.64882865137755,25.156249999999996                                     M -0.9921875, 5.15625                                     L -34.63320365137754,25.156249999999996                                    H -65.8515625                                     a8,8 0 0 1 -8,-8                                      V -47.15625                                     a8,8 0 0 1 8,-8 H 65.8515625 a8,8 0 0 1 8,8                                     L 73.8515625, 17.156249999999996                                      a8,8 0 0 1 -8,8                                     L 34.64882865137755, 25.156249999999996                                     Z                                    "></path>           			          <svg x="0" y="0" style="overflow: visible;">           			              <text class="hierarchy-label__item__name" width="150" y="-25" x="0" text-anchor="middle" style="pointer-events: none;">           			                  <tspan x="0" dy="0em">Finance</tspan>           			              </text>           			          </svg>                   			  <svg x="0" y="0" style="overflow: visible;">                   			      <text class="hierarchy-label__item__type" width="150" y="-5" x="0" text-anchor="middle" style="pointer-events: none;">                   			          <tspan x="0" dy="0.71em">Business Unit</tspan>                   			      </text>                   			  </svg>                             </g>                         </g>                     </g>                 </g>             </g>         </g>     </svg>  </div>

Answers 2

Scaling in svg is done with viewBox, which combines both scaling and offset. There is also nice article. From the following article:

If you think of the document as a canvas, the view box is part of the canvas you want the viewer to see.

It's like a screen of your cell phone in the camera app which shows part of the scene which is observed with specified scale and offsets.

enter image description here

Nice sample which demonstrates what is viewBox can be found here.

A little math and I implemented zoom in/zoom out with mousewheel. In addition added panning with mousemove and display scale value. An example which demonstrates how viewBox can be used:

const svgImage = document.getElementById("svgImage");  const svgContainer = document.getElementById("svgContainer");    var viewBox = {x:0,y:0,w:svgImage.getAttribute("width"),h:svgImage.getAttribute("height")};  svgImage.setAttribute('viewBox', `${viewBox.x} ${viewBox.y} ${viewBox.w} ${viewBox.h}`);  const svgSize = {w:svgImage.getAttribute("width"),h:svgImage.getAttribute("height")};  var isPanning = false;  var startPoint = {x:0,y:0};  var endPoint = {x:0,y:0};;  var scale = 1;    svgContainer.onmousewheel = function(e) {     e.preventDefault();     var w = viewBox.w;     var h = viewBox.h;     var mx = e.x;//mouse x       var my = e.y;         var dw = w*Math.sign(e.deltaY)*0.05;     var dh = h*Math.sign(e.deltaY)*0.05;     var dx = dw*mx/svgSize.w;     var dy = dh*my/svgSize.h;     viewBox = {x:viewBox.x+dx,y:viewBox.y+dy,w:viewBox.w-dw,h:viewBox.h-dh};     scale = svgSize.w/viewBox.w;     zoomValue.innerText = `${Math.round(scale*100)/100}`;     svgImage.setAttribute('viewBox', `${viewBox.x} ${viewBox.y} ${viewBox.w} ${viewBox.h}`);  }      svgContainer.onmousedown = function(e){     isPanning = true;     startPoint = {x:e.x,y:e.y};     }    svgContainer.onmousemove = function(e){     if (isPanning){    endPoint = {x:e.x,y:e.y};    var dx = (startPoint.x - endPoint.x)/scale;    var dy = (startPoint.y - endPoint.y)/scale;    var movedViewBox = {x:viewBox.x+dx,y:viewBox.y+dy,w:viewBox.w,h:viewBox.h};    svgImage.setAttribute('viewBox', `${movedViewBox.x} ${movedViewBox.y} ${movedViewBox.w} ${movedViewBox.h}`);     }  }    svgContainer.onmouseup = function(e){     if (isPanning){     endPoint = {x:e.x,y:e.y};    var dx = (startPoint.x - endPoint.x)/scale;    var dy = (startPoint.y - endPoint.y)/scale;    viewBox = {x:viewBox.x+dx,y:viewBox.y+dy,w:viewBox.w,h:viewBox.h};    svgImage.setAttribute('viewBox', `${viewBox.x} ${viewBox.y} ${viewBox.w} ${viewBox.h}`);    isPanning = false;     }  }    svgContainer.onmouseleave = function(e){   isPanning = false;  }
<span id="zoomValue">1</span>  <div id="svgContainer">  <svg id="svgImage" height="964" width="767">      <g  class="vx-group vx-tree" transform="translate(20, 70)">          <g class="vx-group" transform="translate(0, 70)">              <g class="vx-group" transform="translate(0, 0)">                  <path class="vx-link-vertical" d="M451.5,0C451.5,233.5,451.5,233.5,451.5,467" percent="0.5" stroke="#f7f7f3" stroke-width="1" stroke-opacity="0.2" fill="none"></path>              </g>              <g class="vx-group" transform="translate(0, 0)">                  <g class="vx-group" transform="translate(451.5, 0)" opacity="1">                      <g class="vx-group node__container" transform="translate(0, 0)">                          <svg class="" x="0" y="0" style="overflow: visible;">                              <polygon points="25.98076211353316,-14.999999999999998 25.98076211353316,14.999999999999998 1.83697019872103e-15,30 -25.98076211353316,14.999999999999998 -25.980762113533157,-15.000000000000004 -5.510910596163089e-15,-30" class="node__hexagon"></polygon>                          </svg>                          <g class="vx-group node__business-unit" transform="translate(0, 0)">                              <use xlink:href="#icon-BusinessUnit"></use>                          </g>                          <g class="hierarchy-label__container" transform="translate(0, -40)">                             <path class="" d="                                M 0.0078125, 5.15625                                L 34.64882865137755,25.156249999999996                                 M -0.9921875, 5.15625                                 L -34.63320365137754,25.156249999999996                                H -65.8515625                                 a8,8 0 0 1 -8,-8                                  V -47.15625                                 a8,8 0 0 1 8,-8 H 65.8515625 a8,8 0 0 1 8,8                                 L 73.8515625, 17.156249999999996                                  a8,8 0 0 1 -8,8                                 L 34.64882865137755, 25.156249999999996                                 Z                                "></path>       			          <svg x="0" y="0" style="overflow: visible;">       			              <text class="hierarchy-label__item__name" width="150" y="-25" x="0" text-anchor="middle" style="pointer-events: none;">       			                  <tspan x="0" dy="0em">Finance</tspan>       			              </text>       			          </svg>               			  <svg x="0" y="0" style="overflow: visible;">               			      <text class="hierarchy-label__item__type" width="150" y="-5" x="0" text-anchor="middle" style="pointer-events: none;">               			          <tspan x="0" dy="0.71em">Business Unit</tspan>               			      </text>               			  </svg>                         </g>                     </g>                 </g>             </g>         </g>     </g>     </svg>  </div>

Math:

enter image description here

Answers 3

Zooming and panning are common and useful techniques in data visualization, which work particularly well with SVG based visualization since vector graphic does not suffer from pixelation as its bitmap counterpart would.

This answer explores D3's built-in support for both zooming and panning, by Nick Qi Zhu's book Data Visualization with D3.js Cookbook (2013).

Start by opening your local copy of the following file in your web browser:

https://github.com/NickQiZhu/d3-cookbook/blob/master/src/chapter10/zoom.html.

In this recipe will implement geometric zooming and panning using D3 zoom support. Let's see how this is done in code:

<script type="text/javascript"> var width = 960, height = 500, r = 50;  var data = [     [width / 2 - r, height / 2 - r],     [width / 2 - r, height / 2 + r],     [width / 2 + r, height / 2 - r],     [width / 2 + r, height / 2 + r] ];  var svg = d3.select("body").append("svg")         .attr("width", width)         .attr("height", height)         .call(             d3.behavior.zoom()                 .scaleExtent([1, 10])                 .on("zoom", zoom)         )         .append("g");  svg.selectAll("circle")         .data(data)         .enter().append("circle")         .attr("r", r)         .attr("transform", function (d) {             return "translate(" + d + ")";         });  function zoom() {     svg.attr("transform", "translate("          + d3.event.translate          + ")scale(" + d3.event.scale + ")"); } </script> 

This recipe generates the following zooming and panning effect:

Original:

Original

Zoom:

Zoom

Pan:

Pan

You will notice zooming and panning reacts perfectly well to both mouse wheel and multi-touch gesture (as opposing to the other answers). Most of the heavy lifting is done by D3 library so we needed little code.

Answers 4

When I failed to find leightweight script for zooming (none of the tested were able to keep mouse cursor as centerpoint), I made "a bit" research and ended up with a solution my own. I found the easiest way to use viewbox.

Outer svg must have viewBox declared (or it should be created at the beginning of function).

<svg id="svgImage" width="900" height="500" viewBox="0 0 900 500"> <style>text { fill: white; }</style> <g id="par" class="vx-group vx-tree" transform="translate(20, 70)">      <g class="vx-group" transform="translate(0, 70)">         <g class="vx-group" transform="translate(0, 0)">             <path class="vx-link-vertical" d="M451.5,0C451.5,233.5,451.5,233.5,451.5,467" percent="0.5" stroke="#f7f7f3" stroke-width="1" stroke-opacity="0.2" fill="none"></path>         </g>         <g class="vx-group" transform="translate(0, 0)">             <g class="vx-group" transform="translate(451.5, 0)" opacity="1">                 <g class="vx-group node__container" transform="translate(0, 0)">                     <svg class="" x="0" y="0" style="overflow: visible;">                         <polygon points="25.98076211353316,-14.999999999999998 25.98076211353316,14.999999999999998 1.83697019872103e-15,30 -25.98076211353316,14.999999999999998 -25.980762113533157,-15.000000000000004 -5.510910596163089e-15,-30" class="node__hexagon"></polygon>                     </svg>                     <g class="vx-group node__business-unit" transform="translate(0, 0)">                         <use xlink:href="#icon-BusinessUnit"></use>                     </g>                     <g class="hierarchy-label__container" transform="translate(0, -40)">                         <path class="" d="                         M 0.0078125, 5.15625                         L 34.64882865137755,25.156249999999996                          M -0.9921875, 5.15625                          L -34.63320365137754,25.156249999999996                         H -65.8515625                          a8,8 0 0 1 -8,-8                           V -47.15625                          a8,8 0 0 1 8,-8 H 65.8515625 a8,8 0 0 1 8,8                          L 73.8515625, 17.156249999999996                           a8,8 0 0 1 -8,8                          L 34.64882865137755, 25.156249999999996                          Z                          "></path>                         <svg x="0" y="0" style="overflow: visible;"><text class="hierarchy-label__item__name" width="150" y="-25" x="0" text-anchor="middle" style="pointer-events: none;"><tspan x="0" dy="0em">Finance</tspan></text></svg>                         <svg x="0" y="0" style="overflow: visible;"><text class="hierarchy-label__item__type" width="150" y="-5" x="0" text-anchor="middle" style="pointer-events: none;"><tspan x="0" dy="0.71em">Business Unit</tspan></text></svg>                     </g>                 </g>             </g>         </g>     </g> </g> </svg>  <script> const svgImage = document.getElementById("svgImage"); const svgSize = {w:svgImage.clientWidth ,h:svgImage.clientHeight}; var oldScale = 1;  svgImage.onmousewheel = function(e) {     e.preventDefault();      var svgW     = svgSize.w,         svgH     = svgSize.h,         mX       = e.offsetX,         mY       = e.offsetY,         delta    = (e.wheelDelta) ? -e.wheelDelta : e.detail,         newScale = oldScale + (oldScale*delta/1200); //1200: intensity      var vb      = svgImage.getAttribute('viewBox').split(" ");     var newW    = svgW * newScale,         newH    = svgH * newScale,         newX    = vb[0]*1 + (vb[2]*1 - newW) * (mX/svgW),         newY    = vb[1]*1 + (vb[3]*1 - newH) * (mY/svgH);      viewBox = { x:Math.round(newX), y:Math.round(newY), w:newW, h:newH };     svgImage.setAttribute('viewBox', `${viewBox.x} ${viewBox.y} ${viewBox.w} ${viewBox.h}`);     oldScale = newScale; </script> 

PS. Very good series of articles about SVG coordinate system manipulation (and not only) is written by Sara Soueidan. Good blog to dig around.

Answers 5

There are several ways to deal with SVGs in React and React Native, but I definitely use the simplest way, hence, using the font technology.

following steps show the best clear way to use SVGs and deal with them:

  1. Collecting all SVGs and put all of them in a folder. (better: rename and enumerate them like 1_lablab 2_laflaf 3_etc)
  2. Register in icomoon and press import icons to upload all SVG in the created folder
  3. press generate font to generate your font that just contains your SVGs
  4. Use Online font converter to make other extensions of font for cross-browser

The online font converter gives you several extensions toward its CSS. after embedding your font to your project, assign each SVG shape to an element className, then with font-size you can scale it very easy.

For example, I put a simple sample code below:

@font-face {     font-family: 'icons';     src: url('../font/icon/icons.eot?xwgbwn');     src: url('../font/icon/icons.eot?xwgbwn#iefix') format('embedded-opentype'),      url('../font/icon/icons.ttf?xwgbwn') format('truetype'),      url('../font/icon/icons.woff?xwgbwn') format('woff'),      url('../font/icon/icons.svg?xwgbwn#icons') format('svg');     font-weight: normal;     font-style: normal }  [class^="icon-"], [class*=" icon-"] {     font-family: 'icons' !important;     speak: none;     font-style: normal;     font-weight: normal;     font-variant: normal;     text-transform: none;     line-height: 1;     -webkit-font-smoothing: antialiased;     -moz-osx-font-smoothing: grayscale }  .icon-tech-m:before {     content: "\e949";  } 

Ok, now when I assign icon-tech-m to an element className I can see mySVGas a font-shape an I can scale it easily just withfont-size`.

... renderThatSVG = () => (     <span className="icon-tech-m" /> ); ... 

For more sample, you can see DiGiKala Mag, I wrote all of them alone, it is the biggest middle-east e-commerce. in this version, I just use font-shape to deal with SVGs.

Read More

Wednesday, September 12, 2018

Flip most of an svg image, but not the text within it

Leave a Comment

I have a big collection of svg images like this:
enter image description here

<svg version="1.1" viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg">   <g stroke="lightgray" stroke-dasharray="1,1" stroke-width="1" transform="scale(4, 4)">     <line x1="0" y1="0" x2="256" y2="256"></line>     <line x1="256" y1="0" x2="0" y2="256"></line>     <line x1="128" y1="0" x2="128" y2="256"></line>     <line x1="0" y1="128" x2="256" y2="128"></line>   </g>   <g transform="scale(1, -1) translate(0, -900)">       <style>         .stroke1 {fill: #BF0909;}         .stroke2 {fill: #BFBF09;}         .stroke3 {fill: #09BF09;}         .stroke4 {fill: #09BFBF;}         .stroke5 {fill: #0909BF;}         .stroke6 {fill: #BF09BF;}         .stroke7 {fill: #BFBFBF;}         .stroke8 {fill: #090909;}       </style>        <path class="stroke1" d="M 272 567 Q 306 613 342 669 Q 370 718 395 743 Q 405 753 400 769 Q 396 782 365 808 Q 337 827 316 828 Q 297 827 305 802 Q 318 769 306 741 Q 267 647 207 560 Q 150 476 72 385 Q 60 375 58 367 Q 54 355 70 358 Q 82 359 109 384 Q 155 421 213 493 Q 226 509 241 527 L 272 567 Z" fill="#BF0909"></path>        <path class="stroke2" d="M 241 527 Q 262 506 258 375 Q 258 374 258 370 Q 254 253 221 135 Q 215 114 224 80 Q 236 44 248 32 Q 267 16 279 44 Q 294 86 294 134 Q 303 420 314 485 Q 321 515 295 543 Q 289 549 272 567 C 251 589 227 553 241 527 Z" fill="lightgray"></path>        <path class="stroke3" d="M 521 560 Q 561 621 602 708 Q 620 751 638 773 Q 645 786 639 799 Q 633 811 602 830 Q 572 846 554 843 Q 535 839 546 817 Q 561 795 552 757 Q 513 619 407 448 Q 398 436 397 430 Q 394 418 409 423 Q 439 432 503 532 L 521 560 Z" fill="lightgray"></path>        <path class="stroke4" d="M 503 532 Q 527 510 555 520 Q 795 608 782 549 Q 783 543 743 468 Q 736 458 741 453 Q 745 447 756 459 Q 852 532 894 549 Q 904 552 905 561 Q 906 574 876 592 Q 852 605 828 621 Q 800 637 783 630 Q 686 590 521 560 C 492 555 479 550 503 532 Z" fill="lightgray"></path>        <path class="stroke5" d="M 568 72 Q 531 81 494 91 Q 482 94 483 86 Q 484 79 494 71 Q 569 7 596 -33 Q 611 -49 626 -36 Q 659 -3 661 82 Q 655 149 655 345 Q 656 382 667 407 Q 676 426 659 439 Q 634 461 604 470 Q 585 477 577 469 Q 571 462 582 447 Q 619 384 603 127 Q 597 82 589 74 Q 582 67 568 72 Z" fill="lightgray"></path>        <path class="stroke6" d="M 444 320 Q 419 262 385 208 Q 364 180 381 144 Q 388 128 409 139 Q 460 181 468 264 Q 472 295 467 319 Q 463 328 456 328 Q 449 327 444 320 Z" fill="lightgray"></path>        <path class="stroke7" d="M 738 307 Q 789 249 847 168 Q 860 146 876 139 Q 885 138 893 146 Q 908 159 900 204 Q 891 264 743 338 Q 734 345 731 332 Q 728 319 738 307 Z" fill="lightgray"></path>   </g> </svg> 

and list of coordinates for each image on which a number has to be drawn (see text-elements of following svg code). The problem is: When I add the required text elements, they are flipped vertically and horizontally:
enter image description here

<svg version="1.1" viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg">   <g stroke="lightgray" stroke-dasharray="1,1" stroke-width="1" transform="scale(4, 4)">     <line x1="0" y1="0" x2="256" y2="256"></line>     <line x1="256" y1="0" x2="0" y2="256"></line>     <line x1="128" y1="0" x2="128" y2="256"></line>     <line x1="0" y1="128" x2="256" y2="128"></line>   </g>   <g transform="scale(1, -1) translate(0, -900)">       <style>         .stroke1 {fill: #BF0909;}         .stroke2 {fill: #BFBF09;}         .stroke3 {fill: #09BF09;}         .stroke4 {fill: #09BFBF;}         .stroke5 {fill: #0909BF;}         .stroke6 {fill: #BF09BF;}         .stroke7 {fill: #BFBFBF;}         .stroke8 {fill: #090909;}         text {             font-family: Helvetica;             font-size: 80px;             fill: #FFFFFF;             paint-order: stroke;             stroke: #000000;             stroke-width: 4px;             stroke-linecap: butt;             stroke-linejoin: miter;             font-weight: 800;         }       </style>       <path class="stroke1" d="M 272 567 Q 306 613 342 669 Q 370 718 395 743 Q 405 753 400 769 Q 396 782 365 808 Q 337 827 316 828 Q 297 827 305 802 Q 318 769 306 741 Q 267 647 207 560 Q 150 476 72 385 Q 60 375 58 367 Q 54 355 70 358 Q 82 359 109 384 Q 155 421 213 493 Q 226 509 241 527 L 272 567 Z" fill="#BF0909"></path>        <path class="stroke2" d="M 241 527 Q 262 506 258 375 Q 258 374 258 370 Q 254 253 221 135 Q 215 114 224 80 Q 236 44 248 32 Q 267 16 279 44 Q 294 86 294 134 Q 303 420 314 485 Q 321 515 295 543 Q 289 549 272 567 C 251 589 227 553 241 527 Z" fill="lightgray"></path>        <path class="stroke3" d="M 521 560 Q 561 621 602 708 Q 620 751 638 773 Q 645 786 639 799 Q 633 811 602 830 Q 572 846 554 843 Q 535 839 546 817 Q 561 795 552 757 Q 513 619 407 448 Q 398 436 397 430 Q 394 418 409 423 Q 439 432 503 532 L 521 560 Z" fill="lightgray"></path>        <path class="stroke4" d="M 503 532 Q 527 510 555 520 Q 795 608 782 549 Q 783 543 743 468 Q 736 458 741 453 Q 745 447 756 459 Q 852 532 894 549 Q 904 552 905 561 Q 906 574 876 592 Q 852 605 828 621 Q 800 637 783 630 Q 686 590 521 560 C 492 555 479 550 503 532 Z" fill="lightgray"></path>        <path class="stroke5" d="M 568 72 Q 531 81 494 91 Q 482 94 483 86 Q 484 79 494 71 Q 569 7 596 -33 Q 611 -49 626 -36 Q 659 -3 661 82 Q 655 149 655 345 Q 656 382 667 407 Q 676 426 659 439 Q 634 461 604 470 Q 585 477 577 469 Q 571 462 582 447 Q 619 384 603 127 Q 597 82 589 74 Q 582 67 568 72 Z" fill="lightgray"></path>        <path class="stroke6" d="M 444 320 Q 419 262 385 208 Q 364 180 381 144 Q 388 128 409 139 Q 460 181 468 264 Q 472 295 467 319 Q 463 328 456 328 Q 449 327 444 320 Z" fill="lightgray"></path>        <path class="stroke7" d="M 738 307 Q 789 249 847 168 Q 860 146 876 139 Q 885 138 893 146 Q 908 159 900 204 Q 891 264 743 338 Q 734 345 731 332 Q 728 319 738 307 Z" fill="lightgray"></path>        <text x="317" y="812">1</text>       <text x="273" y="558">2</text>       <text x="556" y="828">3</text>       <text x="513" y="532">4</text>       <text x="586" y="463">5</text>       <text x="455" y="316">6</text>       <text x="742" y="326">7</text>   </g> </svg> 

I just can't figure out a way to get the text to be displayed at the right position without without it being flipped horizontally and vertically. I already tried to wrap the text in another g-tag and do the transform again do revert it, but then the text would disappear. I also tried to move the text out of the original g-tag, but then my coordinates aren't valid anymore...

Edit: I'm looking for a way to generate the svgs in a way that they are compatible with Safari (on Windows), Chrome and Firefox.

Edit2: Unfortunately I can't use transform-box because I need to support a Safari based browser which doesn't support it.

4 Answers

Answers 1

Manipulating the SVG, all you need to do is

  • move the <text> elements outside the group with the transform attribute
  • recompute the y attribute with

    y -> 940 - y 

    This supposes that the transform attribute is always transform="scale(1, -1) translate(0, -900)" and the font size 80px. A general formula would be

    y -> (negative y translation + font size / 2) - y 

Here is a node script utilizing cheerio to change existing files:

const cheerio = require('cheerio'); const $ = cheerio.load(xmlText, { xmlMode: true });  function flipTextInPlace ($) {     $('text').each(() => {        const y = $(this).attr('y');        $(this).attr('y', 940 - y);     }).appendTo('svg'); } 

Answers 2

I hope this helps:

text:nth-child(1) {    transform: scale(1, -1);    transform-origin: 317px 812px;/*same as text coords*/    dominant-baseline: hanging;  }
<svg version="1.1" viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg">    <g stroke="lightgray" stroke-dasharray="1,1" stroke-width="1" transform="scale(4, 4)">      <line x1="0" y1="0" x2="256" y2="256"></line>      <line x1="256" y1="0" x2="0" y2="256"></line>      <line x1="128" y1="0" x2="128" y2="256"></line>      <line x1="0" y1="128" x2="256" y2="128"></line>    </g>    <g transform="scale(1, -1) translate(0, -900)">        <style>          .stroke1 {fill: #BF0909;}          .stroke2 {fill: #BFBF09;}          .stroke3 {fill: #09BF09;}          .stroke4 {fill: #09BFBF;}          .stroke5 {fill: #0909BF;}          .stroke6 {fill: #BF09BF;}          .stroke7 {fill: #BFBFBF;}          .stroke8 {fill: #090909;}          text {              font-family: Helvetica;              font-size: 80px;              fill: #FFFFFF;              paint-order: stroke;              stroke: #000000;              stroke-width: 4px;              stroke-linecap: butt;              stroke-linejoin: miter;              font-weight: 800;          }        </style>        <path class="stroke1" d="M 272 567 Q 306 613 342 669 Q 370 718 395 743 Q 405 753 400 769 Q 396 782 365 808 Q 337 827 316 828 Q 297 827 305 802 Q 318 769 306 741 Q 267 647 207 560 Q 150 476 72 385 Q 60 375 58 367 Q 54 355 70 358 Q 82 359 109 384 Q 155 421 213 493 Q 226 509 241 527 L 272 567 Z" fill="#BF0909"></path>          <path class="stroke2" d="M 241 527 Q 262 506 258 375 Q 258 374 258 370 Q 254 253 221 135 Q 215 114 224 80 Q 236 44 248 32 Q 267 16 279 44 Q 294 86 294 134 Q 303 420 314 485 Q 321 515 295 543 Q 289 549 272 567 C 251 589 227 553 241 527 Z" fill="lightgray"></path>          <path class="stroke3" d="M 521 560 Q 561 621 602 708 Q 620 751 638 773 Q 645 786 639 799 Q 633 811 602 830 Q 572 846 554 843 Q 535 839 546 817 Q 561 795 552 757 Q 513 619 407 448 Q 398 436 397 430 Q 394 418 409 423 Q 439 432 503 532 L 521 560 Z" fill="lightgray"></path>          <path class="stroke4" d="M 503 532 Q 527 510 555 520 Q 795 608 782 549 Q 783 543 743 468 Q 736 458 741 453 Q 745 447 756 459 Q 852 532 894 549 Q 904 552 905 561 Q 906 574 876 592 Q 852 605 828 621 Q 800 637 783 630 Q 686 590 521 560 C 492 555 479 550 503 532 Z" fill="lightgray"></path>          <path class="stroke5" d="M 568 72 Q 531 81 494 91 Q 482 94 483 86 Q 484 79 494 71 Q 569 7 596 -33 Q 611 -49 626 -36 Q 659 -3 661 82 Q 655 149 655 345 Q 656 382 667 407 Q 676 426 659 439 Q 634 461 604 470 Q 585 477 577 469 Q 571 462 582 447 Q 619 384 603 127 Q 597 82 589 74 Q 582 67 568 72 Z" fill="lightgray"></path>          <path class="stroke6" d="M 444 320 Q 419 262 385 208 Q 364 180 381 144 Q 388 128 409 139 Q 460 181 468 264 Q 472 295 467 319 Q 463 328 456 328 Q 449 327 444 320 Z" fill="lightgray"></path>          <path class="stroke7" d="M 738 307 Q 789 249 847 168 Q 860 146 876 139 Q 885 138 893 146 Q 908 159 900 204 Q 891 264 743 338 Q 734 345 731 332 Q 728 319 738 307 Z" fill="lightgray"></path>            <g>        <text x="317" y="812">1</text>        <text x="273" y="558">2</text>        <text x="556" y="828">3</text>        <text x="513" y="532">4</text>        <text x="586" y="463">5</text>        <text x="455" y="316">6</text>        <text x="742" y="326">7</text>      </g>    </g>     </svg>

Unfortunately this works on Chrome & Firefox but not on Safari. Didn't check Edge & IE.

Answers 3

You could use css transform on your text nodes

Edit

Inspired from @enxaneta answer, you could run this little script

const text = document.querySelectorAll("text");    text.forEach(function(el){       el.style.transformOrigin = el.getAttribute('x')+'px '+el.getAttribute('y')+'px';    el.style.transform = "scale(1,-1)";       });
text{    dominant-baseline:central;  }
<svg version="1.1" viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg">    <g stroke="lightgray" stroke-dasharray="1,1" stroke-width="1" transform="scale(4, 4)">      <line x1="0" y1="0" x2="256" y2="256"></line>      <line x1="256" y1="0" x2="0" y2="256"></line>      <line x1="128" y1="0" x2="128" y2="256"></line>      <line x1="0" y1="128" x2="256" y2="128"></line>    </g>    <g transform="scale(1, -1) translate(0, -900)">        <style>          .stroke1 {fill: #BF0909;}          .stroke2 {fill: #BFBF09;}          .stroke3 {fill: #09BF09;}          .stroke4 {fill: #09BFBF;}          .stroke5 {fill: #0909BF;}          .stroke6 {fill: #BF09BF;}          .stroke7 {fill: #BFBFBF;}          .stroke8 {fill: #090909;}          text {              font-family: Helvetica;              font-size: 80px;              fill: #FFFFFF;              paint-order: stroke;              stroke: #000000;              stroke-width: 4px;              stroke-linecap: butt;              stroke-linejoin: miter;              font-weight: 800;          }        </style>        <path class="stroke1" d="M 272 567 Q 306 613 342 669 Q 370 718 395 743 Q 405 753 400 769 Q 396 782 365 808 Q 337 827 316 828 Q 297 827 305 802 Q 318 769 306 741 Q 267 647 207 560 Q 150 476 72 385 Q 60 375 58 367 Q 54 355 70 358 Q 82 359 109 384 Q 155 421 213 493 Q 226 509 241 527 L 272 567 Z" fill="#BF0909"></path>          <path class="stroke2" d="M 241 527 Q 262 506 258 375 Q 258 374 258 370 Q 254 253 221 135 Q 215 114 224 80 Q 236 44 248 32 Q 267 16 279 44 Q 294 86 294 134 Q 303 420 314 485 Q 321 515 295 543 Q 289 549 272 567 C 251 589 227 553 241 527 Z" fill="lightgray"></path>          <path class="stroke3" d="M 521 560 Q 561 621 602 708 Q 620 751 638 773 Q 645 786 639 799 Q 633 811 602 830 Q 572 846 554 843 Q 535 839 546 817 Q 561 795 552 757 Q 513 619 407 448 Q 398 436 397 430 Q 394 418 409 423 Q 439 432 503 532 L 521 560 Z" fill="lightgray"></path>          <path class="stroke4" d="M 503 532 Q 527 510 555 520 Q 795 608 782 549 Q 783 543 743 468 Q 736 458 741 453 Q 745 447 756 459 Q 852 532 894 549 Q 904 552 905 561 Q 906 574 876 592 Q 852 605 828 621 Q 800 637 783 630 Q 686 590 521 560 C 492 555 479 550 503 532 Z" fill="lightgray"></path>          <path class="stroke5" d="M 568 72 Q 531 81 494 91 Q 482 94 483 86 Q 484 79 494 71 Q 569 7 596 -33 Q 611 -49 626 -36 Q 659 -3 661 82 Q 655 149 655 345 Q 656 382 667 407 Q 676 426 659 439 Q 634 461 604 470 Q 585 477 577 469 Q 571 462 582 447 Q 619 384 603 127 Q 597 82 589 74 Q 582 67 568 72 Z" fill="lightgray"></path>          <path class="stroke6" d="M 444 320 Q 419 262 385 208 Q 364 180 381 144 Q 388 128 409 139 Q 460 181 468 264 Q 472 295 467 319 Q 463 328 456 328 Q 449 327 444 320 Z" fill="lightgray"></path>          <path class="stroke7" d="M 738 307 Q 789 249 847 168 Q 860 146 876 139 Q 885 138 893 146 Q 908 159 900 204 Q 891 264 743 338 Q 734 345 731 332 Q 728 319 738 307 Z" fill="lightgray"></path>          <text x="317" y="812">1</text>        <text x="273" y="558">2</text>        <text x="556" y="828">3</text>        <text x="513" y="532">4</text>        <text x="586" y="463">5</text>        <text x="455" y="316">6</text>        <text x="742" y="326">7</text>    </g>  </svg>

Answers 4

    </path>     </g>    <g>     <text x="317" y="812">1</text>     <text x="273" y="500">2</text>     <text x="556" y="828">3</text>     <text x="513" y="532">4</text>     <text x="586" y="463">5</text>     <text x="455" y="316">6</text>     <text x="742" y="326">7</text>   </g> </svg> <script>   var elements = document.querySelectorAll('*[y]');   for (var i = 0; i < elements.length; i++)     elements[i].setAttribute('y', 958-(elements[i].getAttribute('y'))); </script> 

958 comes from: 900(initial transform) + 58(character height).

Read More

Thursday, July 12, 2018

SVG `<path>` javascript animation not working as expected

Leave a Comment

What I have achieved:

// Get the id of the <path> element and the length of <path>  var myline = document.getElementById("myline");  var length = myline.getTotalLength();  circle = document.getElementById("circle");  // The start position of the drawing  myline.style.strokeDasharray = length;    // Hide the triangle by offsetting dash. Remove this line to show the triangle before scroll draw  myline.style.strokeDashoffset = length;    // Find scroll percentage on scroll (using cross-browser properties), and offset dash same amount as percentage scrolled  window.addEventListener("scroll", myFunction);    function myFunction() {    // What % down is it?    var scrollpercent = (document.body.scrollTop + document.documentElement.scrollTop) / (document.documentElement.scrollHeight - document.documentElement.clientHeight);    // Length to offset the dashes    var draw = length * scrollpercent;      // Reverse the drawing (when scrolling upwards)    myline.style.strokeDashoffset = length - draw;      //get point at length    endPoint = myline.getPointAtLength(draw);    circle.setAttribute("cx", endPoint.x);    circle.setAttribute("cy", endPoint.y);    }
body {    height: 2000px;    background: #f1f1f1;  }    #circle {    fill: red;  }    #mySVG {    position: absolute;    top: 15%;    width: 100%;    height: 1000px;      }    .st1 {    fill: none;    stroke-dashoffset: 3px;    stroke: grey;    stroke-width: 4;    stroke-miterlimit: 10;    stroke-dasharray: 20;  }  .st0 {    fill: none;    stroke-dashoffset: 3px;    stroke: red;    stroke-width: 5;    stroke-miterlimit: 10;    stroke-dasharray: 20;  }
<svg id="mySVG" viewBox="0 0 60 55" preserveAspectRatio="xMidYMin slice" style="width: 6%; padding-bottom: 42%; height: 1px; overflow: visible">    <path  class="st1" stroke-dasharray="10,9" d="M 20 0 v 20 a 30 30 0 0 0 30 30 h 600 a 40 40 0 0 1 0 80 h -140 a 30 30 0 0 0 0 60 h 200 a 40 40 0 0 1 0 80 h -100 a 30 30 0 0 0 -30 30 v 20" /> Sorry, your browser does not support inline SVG.  </svg>    <svg id="mySVG" viewBox="0 0 60 55" preserveAspectRatio="xMidYMin slice" style="width: 6%; padding-bottom: 42%; height: 1px; overflow: visible">    <circle id="circle" cx="10" cy="10" r="10"/>    <path id="myline" class="st0" stroke-dasharray="10,9" d="M 20 0 v 20 a 30 30 0 0 0 30 30 h 600 a 40 40 0 0 1 0 80 h -140 a 30 30 0 0 0 0 60 h 200 a 40 40 0 0 1 0 80 h -100 a 30 30 0 0 0 -30 30 v 20" /> Sorry, your browser does not support inline SVG.  </svg>

What I want is no matter which size or shape the SVG <path> is the growing line should be in the middle of the screen.

I tried changing the values of myline.style.strokeDashoffset = length //+newvalue - draw; and all but all it did was just ruins the consistency. so is there anyone who can help me solve this issue.?

Any help would be highly appreciatable.

4 Answers

Answers 1

(Update / New answer)

I think this is exactly what you want...

let roadmapSvg = document.getElementById("roadmap-svg");  let track = document.getElementById("track");  let body = document.getElementById("body");  let head = document.getElementById("head");    let totalLength = track.getTotalLength();  let trackPoints = [];  let getTrackBounds = () => track.getBoundingClientRect();  let scaleFactor;    body.style.strokeDasharray = totalLength;  body.style.strokeDashoffset = totalLength;    function setScaleFactor(){    scaleFactor = roadmapSvg.getBoundingClientRect().width / roadmapSvg.viewBox.baseVal.width;  }     setScaleFactor();    function setTrackPoints(){    let divisions = 1000;    let unitLength = totalLength / divisions;    trackPoints = [];    for(let i=0; i < divisions; i++){      let length = unitLength * i;      let {x,y} = track.getPointAtLength(length);      trackPoints.push({x: x*scaleFactor, y: y*scaleFactor, length});    }  }  setTrackPoints();      function draw(){    let currentLength = getCurrentLength();    body.style.strokeDashoffset = totalLength - currentLength;    headPos = track.getPointAtLength(currentLength);    head.setAttribute("cx", headPos.x);    head.setAttribute("cy", headPos.y);  }    function getCurrentLength(){    let centreY = window.innerHeight / 2;    let trackBounds = getTrackBounds();    let currentY = centreY - trackBounds.y;    if(currentY < 0) return 0;    if(currentY > trackBounds.height) return totalLength;        for(let point of trackPoints){      if(point.y >= currentY){        return point.length;      }    }        // (For safety) Sometimes none of the conditions match bcoz of low precision... Such situation only occurs a point very close to total length... Thus...    return totalLength;  }    document.addEventListener("scroll", draw);    window.addEventListener("resize", () => {    setScaleFactor();    setTrackPoints();    draw();  });
body {    background: #f1f1f1;    margin: 0;    padding: 0 20%;    font-family: sans-serif;  }    #roadmap-svg{    display: block;    max-width: 600px;    margin: 20px auto;    overflow: visible;  }    #roadmap-svg #head{    fill: red;  }    #roadmap-svg #track{    fill: none;    stroke-dashoffset: 3px;    stroke: grey;    stroke-width: 4;    stroke-miterlimit: 10;    stroke-dasharray: 20;  }  #roadmap-svg #body{    fill: none;    stroke-dashoffset: 3px;    stroke: red;    stroke-width: 5;    stroke-miterlimit: 10;    stroke-dasharray: 20;  }    .center-line{    position: fixed;    left: 0;    right: 0;    top: 50%;    border-top: 1px solid red;    background-color: rgba(255,255,255,0.9);  }
<div>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Tempora in eaque rem eligendi corrupti voluptate, maxime cum cumque, eius delectus minus neque, dolorem optio cupiditate ratione! Excepturi fugit culpa quo?  Cum optio error ex voluptatem rerum eius sunt, nemo necessitatibus, exercitationem voluptatum illum, rem quibusdam accusamus deserunt sed. Iste odio obcaecati enim voluptate temporibus ab illo maxime et sit minima.  Odio ut dignissimos sed dicta recusandae esse, at molestiae quibusdam, consequatur aspernatur facilis, perferendis voluptatum adipisci. Dolores molestiae quos, doloribus excepturi officiis laborum ex officia reprehenderit esse perspiciatis alias itaque.  Delectus illum, asperiores at a ab quibusdam corporis necessitatibus. Libero eos vero blanditiis modi cum rem maxime delectus quisquam, facilis saepe sed eius corrupti nobis sunt, unde obcaecati commodi velit.  Saepe adipisci consectetur blanditiis quos enim praesentium, at magnam quibusdam nisi! Dolore, esse beatae! Enim, quam cum, qui voluptates fugiat, nihil mollitia possimus doloremque porro aspernatur nesciunt velit. Cum, adipisci?  Dolores doloribus nihil delectus consequuntur id assumenda tempora, illum, earum ab quasi quaerat sequi et hic veniam excepturi eligendi quod perspiciatis voluptatem ratione reprehenderit! Corrupti minima facilis soluta adipisci animi!  Iure, sed exercitationem. Quidem assumenda omnis dicta ducimus sunt, quibusdam excepturi molestias cumque! Illum ipsum perferendis dicta optio eum consequuntur soluta, corrupti nostrum est sed quaerat voluptates dolores perspiciatis? Ex!  Consequatur corporis ratione beatae. Magni amet doloribus deserunt, accusamus suscipit earum accusantium perferendis adipisci inventore, ab commodi odio necessitatibus aut omnis. Et quisquam esse deleniti, reprehenderit nihil optio aperiam fugit.  Aliquid error voluptatibus, quis quo eveniet nulla corrupti veniam culpa voluptas possimus tenetur nisi recusandae quae modi, animi dolores. Provident saepe nobis quos tenetur, veritatis laborum cupiditate molestias fugit consectetur.  A, perspiciatis illo sequi non eos facere temporibus dignissimos blanditiis ipsum harum eius culpa adipisci est ab nobis saepe mollitia quis laboriosam tenetur, repellat molestias. Quos ipsa magni dolores rerum.</div>  <svg id="roadmap-svg" viewBox="0 0 760 300">    <path  id="track" stroke-dasharray="10,9" d="M 20 0 v 20 a 30 30 0 0 0 30 30 h 600 a 40 40 0 0 1 0 80 h -140 a 30 30 0 0 0 0 60 h 200 a 40 40 0 0 1 0 80 h -100 a 30 30 0 0 0 -30 30 v 20" />    <path id="body" stroke-dasharray="10,9" d="M 20 0 v 20 a 30 30 0 0 0 30 30 h 600 a 40 40 0 0 1 0 80 h -140 a 30 30 0 0 0 0 60 h 200 a 40 40 0 0 1 0 80 h -100 a 30 30 0 0 0 -30 30 v 20" />    <circle id="head" cx="10" cy="10" r="10"/>  </svg>  <div class="center-line">Center Line</div>  <div>Lorem ipsum dolor sit amet consectetur adipisicing elit. Sapiente officia saepe facilis? Cupiditate rem vel, quaerat ratione ipsam magnam fugiat praesentium incidunt! Eveniet cum officia impedit obcaecati id animi rerum?  Non beatae inventore quos optio temporibus ratione doloremque ullam animi dolore reiciendis sint, esse consequatur asperiores assumenda repudiandae obcaecati ab quas molestias harum eveniet amet natus ea? Ipsum, dolore suscipit.  Explicabo assumenda minus, reprehenderit modi, laboriosam placeat saepe at repudiandae perferendis fugit asperiores itaque. Vero fugiat voluptas asperiores dolores dolorum quis ipsa sapiente deleniti odio, deserunt, iure voluptates. Error, tempore.  Doloribus nesciunt praesentium ad aut minus aliquam aspernatur quas qui incidunt sunt, maxime tempora facilis, cum assumenda. Dolorum a tempore itaque impedit, ad, corporis tenetur enim nulla quas, harum fuga!  Quae repellat, obcaecati voluptate inventore quidem, labore quo corporis repudiandae, vel doloremque perferendis numquam aliquam nisi? Vel architecto ullam fugiat error corrupti? Cumque amet illo, possimus assumenda eos unde deleniti.  Enim tenetur possimus a neque, voluptatum reprehenderit, cum magni blanditiis quam atque dolorum veniam eveniet repellendus. Modi quibusdam maxime corrupti harum! Ullam vitae assumenda laboriosam nam officia eaque. Totam, dolorem.  Ad sapiente itaque blanditiis, sint iusto nemo laborum corrupti cupiditate obcaecati quam ipsa quis perferendis vitae enim atque ex a ratione. Doloribus aspernatur id ipsa recusandae labore aliquid, totam aperiam?  Recusandae delectus quidem, aspernatur nulla expedita accusantium quod praesentium inventore qui, pariatur ullam maxime! Numquam, sed sequi rem voluptates asperiores qui, culpa nesciunt magnam, quas doloribus praesentium et adipisci tempora.  Veniam, placeat vel nesciunt recusandae voluptates laboriosam totam doloremque saepe. Nam quo similique vero esse possimus architecto officiis harum ratione perspiciatis dolor ut, molestias odit consequatur quam asperiores? Id, quasi!  Ex expedita impedit aliquam et commodi voluptatibus, consequatur voluptate ea explicabo deserunt. Sapiente quo consequuntur enim dolores ea officia. Inventore ipsa dignissimos iste qui magnam reiciendis eveniet optio laudantium fugiat!</div>

Answers 2

I tried several ways to get point moving

  • inside the box
  • not sliding away until end
  • with minimal changes to original.

I made the following changes (working example below):

  • divider for percent I put 2000 (equal to 2000px, the containing body height)
  • I multiplied the scroll amount from top by 18 (suitable value was a compromise whether the top or bottom becomes behaving wierdly)
  • then finally I checked the percent value is not greater to one (it started to eat the 'worm' from the other end).

That's it! Maybe not the fanciest, but works.

Problematic here is the svg line is not linear from top to down, so direct element related values could not be chosen, or I at least did not find some. Thus, I ended up to simple solution and playing with parameters.

// Get the id of the <path> element and the length of <path>  var myline = document.getElementById("myline");  var length = myline.getTotalLength();  circle = document.getElementById("circle");  // The start position of the drawing  myline.style.strokeDasharray = length;    // Hide the triangle by offsetting dash. Remove this line to show the triangle before scroll draw  myline.style.strokeDashoffset = length;    // Find scroll percentage on scroll (using cross-browser properties), and offset dash same amount as percentage scrolled  window.addEventListener("scroll", myFunction);    function myFunction() {    // What % down is it?    var scrollpercent = (document.documentElement.scrollTop * 18) / 2000;    if (scrollpercent > 1) scrollpercent = 1;    var draw = length * scrollpercent;      // Reverse the drawing (when scrolling upwards)    myline.style.strokeDashoffset = length - draw;      //get point at length    endPoint = myline.getPointAtLength(draw);    circle.setAttribute("cx", endPoint.x);    circle.setAttribute("cy", endPoint.y);  }
body {    height: 2000px;    background: #f1f1f1;  }    #circle {    fill: red;  }    #mySVG {    position: absolute;    top: 15%;    width: 100%;    height: 1000px;      }    .st1 {    fill: none;    stroke-dashoffset: 3px;    stroke: grey;    stroke-width: 4;    stroke-miterlimit: 10;    stroke-dasharray: 20;  }  .st0 {    fill: none;    stroke-dashoffset: 3px;    stroke: red;    stroke-width: 5;    stroke-miterlimit: 10;    stroke-dasharray: 20;  }
<svg id="mySVG" viewBox="0 0 60 55" preserveAspectRatio="xMidYMin slice" style="width: 6%; padding-bottom: 42%; height: 1px; overflow: visible">    <path  class="st1" stroke-dasharray="10,9" d="M 20 0 v 20 a 30 30 0 0 0 30 30 h 600 a 40 40 0 0 1 0 80 h -140 a 30 30 0 0 0 0 60 h 200 a 40 40 0 0 1 0 80 h -100 a 30 30 0 0 0 -30 30 v 20" /> Sorry, your browser does not support inline SVG.  </svg>    <svg id="mySVG" viewBox="0 0 60 55" preserveAspectRatio="xMidYMin slice" style="width: 6%; padding-bottom: 42%; height: 1px; overflow: visible">    <circle id="circle" cx="10" cy="10" r="10"/>    <path id="myline" class="st0" stroke-dasharray="10,9" d="M 20 0 v 20 a 30 30 0 0 0 30 30 h 600 a 40 40 0 0 1 0 80 h -140 a 30 30 0 0 0 0 60 h 200 a 40 40 0 0 1 0 80 h -100 a 30 30 0 0 0 -30 30 v 20" /> Sorry, your browser does not support inline SVG.  </svg>

Answers 3

As per the comments and few modification to css and svg code, i was able to get the line to the center of the page, please check the working example below:

// Get the id of the <path> element and the length of <path>  var myline = document.getElementById("myline");  var length = myline.getTotalLength();  circle = document.getElementById("circle");  // The start position of the drawing  myline.style.strokeDasharray = length;    // Hide the triangle by offsetting dash. Remove this line to show the triangle before scroll draw  myline.style.strokeDashoffset = length;    // Find scroll percentage on scroll (using cross-browser properties), and offset dash same amount as percentage scrolled  window.addEventListener("scroll", myFunction);    function myFunction() {    // What % down is it?    var scrollpercent = (document.body.scrollTop + document.documentElement.scrollTop) / (document.documentElement.scrollHeight - document.documentElement.clientHeight);    // Length to offset the dashes    var draw = length * scrollpercent;      // Reverse the drawing (when scrolling upwards)    myline.style.strokeDashoffset = length - draw;      //get point at length    endPoint = myline.getPointAtLength(draw);    circle.setAttribute("cx", endPoint.x);    circle.setAttribute("cy", endPoint.y);  }
body {    margin: 0;    height: 1000px;    background: #f1f1f1;  }  #circle {    fill: red;  }  #mySVG {    top: 15%;    position: absolute;    width: 100%;  }  .st1 {    fill: none;    stroke-dashoffset: 1;    stroke: grey;    stroke-width: .5;    stroke-miterlimit: 1;    stroke-dasharray: 2;  }  .st0 {    fill: none;    stroke-dashoffset: 3px;    stroke: red;    stroke-width: 1;    stroke-miterlimit: 1;    stroke-dasharray: 2;  }  .grid {    position: fixed;    width: 1px;    height: 100%;    background: blue;    left: 50%;    top: 0;  }
<div class="grid"></div>  <svg id="mySVG" viewBox="0 0 200 72" preserveAspectRatio="xMidYMin slice">    <path class="st1" stroke-dasharray="10,9" d="m 0,5 0,4 c 0,3 2,6 5,6 l 108,0 c 4,0 7,4 7,8 0,4 -3,7 -7,7 l -25,0 c -3,0 -6,3 -6,6 0,3 3,6 6,6 l 35,0 c 4,0 7,4 7,8 0,4 -3,7 -7,7 l -18,0 c -3,0 -5,3 -5,6 l 0,4" />  </svg>    <svg id="mySVG" viewBox="0 0 200 72" preserveAspectRatio="xMidYMin slice">    <circle id="circle" cx="0" cy="3" r="2" />    <path id="myline" class="st0" stroke-dasharray="10,9" d="m 0,5 0,4 c 0,3 2,6 5,6 l 108,0 c 4,0 7,4 7,8 0,4 -3,7 -7,7 l -25,0 c -3,0 -6,3 -6,6 0,3 3,6 6,6 l 35,0 c 4,0 7,4 7,8 0,4 -3,7 -7,7 l -18,0 c -3,0 -5,3 -5,6 l 0,4" />  </svg>

Answers 4

I'd choose a different approach. I understand you want to find the point on the path that is closest to the middle of the screen. Let's do that:

  1. Find the coordinates of the middle of the screen and convert them to the coordinate system of the path. The SVG API has two functions for that: .getScreenCTM() and SVGPoint.matrixTransform().
  2. Find the point on the path (and its distance along the path) that is nearest to these coordinates. There is a bit of math and a search algorithm involved to do that. Mike Bostock has shown such an algorithm, and it's used here. Note that his function is open to a bit of tweaking (the precision parameter).
  3. Use these data to draw the circle and the dashoffset.

It is probably a good idea to refine this by introducing a throttle for the scroll events (second variant) and then set CSS transitions to avoid visible jumps.

Transitions for the circle positioning only work with a CSS transform property. (My solution does not neccessarily move the circle along the path while transitioning. It's possible to achieve that, but goes far beyond the scope of this answer.)

var mySVG = document.getElementById("mySVG");  // Get the id of the <path> element and the length of <path>  var myline = document.getElementById("myline");  var pathLength = myline.getTotalLength();  circle = document.getElementById("circle");  // The start position of the drawing  myline.style.strokeDasharray = pathLength;    // Hide the triangle by offsetting dash. Remove this line to show the triangle before scroll draw  myline.style.strokeDashoffset = pathLength;    // throttled scroll event listener  function throttle(ms, callback) {      var timer, lastCall=0;        return function() {          var now = new Date().getTime(),              diff = now - lastCall;          if (diff >= ms) {              lastCall = now;              callback();          }      };  }    window.addEventListener("scroll", throttle(100, myFunction));    // one initial execution  myFunction();    function myFunction() {    var center = mySVG.createSVGPoint();    // middle of browser viewport    center.x = window.innerWidth / 2;    center.y = window.innerHeight / 2;    // transform to path coordinate system    var matrix = myline.getScreenCTM().inverse();    center = center.matrixTransform(matrix);      //find nearest length on path    var draw = getNearestLength(center);      // Reverse the drawing (when scrolling upwards)    myline.style.strokeDashoffset = -draw - pathLength;      //get point at length    endPoint = myline.getPointAtLength(draw);    circle.style.transform = "translate(" + endPoint.x + "px, " + endPoint.y + "px)";  }    function getNearestLength(point) {    var precision = 8,        best,        bestLength,        bestDistance = Infinity;    // linear scan for coarse approximation    for (var scan, scanLength = 0, scanDistance; scanLength <= pathLength; scanLength += precision) {      if ((scanDistance = distance2(scan = myline.getPointAtLength(scanLength))) < bestDistance) {        best = scan, bestLength = scanLength, bestDistance = scanDistance;      }    }    // binary search for precise estimate    precision /= 2;    while (precision > 0.5) {      var before,          after,          beforeLength,          afterLength,          beforeDistance,          afterDistance;      if ((beforeLength = bestLength - precision) >= 0 && (beforeDistance = distance2(before = myline.getPointAtLength(beforeLength))) < bestDistance) {        best = before, bestLength = beforeLength, bestDistance = beforeDistance;      } else if ((afterLength = bestLength + precision) <= pathLength && (afterDistance = distance2(after = myline.getPointAtLength(afterLength))) < bestDistance) {        best = after, bestLength = afterLength, bestDistance = afterDistance;      } else {        precision /= 2;      }    }    return bestLength;      function distance2(p) {      var dx = p.x - point.x,          dy = p.y - point.y;      return dx * dx + dy * dy;    }  }
body {    height: 2000px;    background: #f1f1f1;  }    #circle {    fill: red;  }    #mySVG {    position: absolute;    top: 15%;    width: 100%;    height: 1000px;      }    .st1 {    fill: none;    stroke-dashoffset: 3px;    stroke: grey;    stroke-width: 4;    stroke-miterlimit: 10;    stroke-dasharray: 20;  }  .st0 {    fill: none;    stroke-dashoffset: 3px;    stroke: red;    stroke-width: 5;    stroke-miterlimit: 10;    stroke-dasharray: 20;    transition: stroke-dashoffset 0.2s;  }    #circle {     transform: translate(10px, 10px);     transition: transform 0.2s;     }
<svg id="mySVG" viewBox="0 0 60 55" preserveAspectRatio="xMidYMin slice" style="width: 6%; padding-bottom: 42%; height: 1px; overflow: visible">    <path  class="st1" stroke-dasharray="10,9" d="M 20 0 v 20 a 30 30 0 0 0 30 30 h 600 a 40 40 0 0 1 0 80 h -140 a 30 30 0 0 0 0 60 h 200 a 40 40 0 0 1 0 80 h -100 a 30 30 0 0 0 -30 30 v 20" /> Sorry, your browser does not support inline SVG.  </svg>    <svg id="mySVG" viewBox="0 0 60 55" preserveAspectRatio="xMidYMin slice" style="width: 6%; padding-bottom: 42%; height: 1px; overflow: visible">    <circle id="circle" cx="0" cy="0" r="10"/>    <path id="myline" class="st0" stroke-dasharray="10,9" d="M 20 0 v 20 a 30 30 0 0 0 30 30 h 600 a 40 40 0 0 1 0 80 h -140 a 30 30 0 0 0 0 60 h 200 a 40 40 0 0 1 0 80 h -100 a 30 30 0 0 0 -30 30 v 20" /> Sorry, your browser does not support inline SVG.  </svg>

Read More

Wednesday, April 11, 2018

How to make svg <image> element show up in safari/ios

Leave a Comment

My scenario: I'm displaying a leaflet map. A map has several tiles, each tile might contain one or more icons. Here is how a tile might look like:

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" pointer-events="none" width="256" height="256" viewBox="0 0 256 256" class="leaflet-tile leaflet-tile-loaded" style="width: 256px; height: 256px; transform: translate3d(455px, -4px, 0px); opacity: 1;">     <g></g>     <image x="213.9375" y="252.875" width="19px" height="19px" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAOxAAADsQBlSsOGwAAA/1JREFUWIXll21olWUYx3/Py9nZc8523uZyW+Z8gVXK3Dw2iJx0ilGGKVOnOIp0RJjRhyDWSAgJI4R9mA2hpohGVKizpQZGG4MWM5ZntnK1mQutdQz3ctzOzs778zx9sJ1tsJdzju5DdH26r+u+r+v/4+J57hf4v5swMcjLy9sVDAarBUFQF1JQ13XJZDId8ng8Z6YBOByOKzU1NWudTudC6uN2u6mtrXV7vd4SAHnKXMzpdFJWVpZYJV0j0N1I+EYbtk11CJIhoTRVVRFFMd5lea7Fs1n4Zhu+1veIDvx6NyCI2DfXp1IqOYDoYC++1oOEb3w7LR785QtkWz6ZG95cGADVf5uxtloCV0+Drs24Zqy9Dsm2FFPhjvsHoEfG8Xd8iP+HBvRocN5ioxerkSx5GPPXJwwgziJNoOtTbjeUMtZ+OCFxAF2LcafpFWLD1+8RQFMJ9pxDGx9MuFA8NeRj+PRLCefOCDB2qR7LM+8jLypIGgBAHe3H21iFHgulBhAbusbIuX04yj9CylicEkTk7y7unH991o92TgBdU4kO9DDacgB7xUmEtIyUIEK/fY2v9WDyABPU4ZvfEXAfx7HtKIKY0p6F//Ixxq+cTBJAmAwHuhuJ/NmB9bnalAAARpsPEOprSRzAsqEa0ZgZ98cufQBqNKWdDkA0ZqAFvYkDyNkPY992fNoBM/LNftJyizEVVSauLEiYnbvJqjyFYDAnDgBgzH8C26Y64ie2FsP75auYi17AuMI1r7ZxWSmLXmzC8MAqQr1fYch+JDkAAGVVORbX23Ffj4wzfLYK69PvYMgpnDFHtuVj33oMc8legj3nkawPkbbcRfiP9uQBADIefw3zuqq4r40P4m3ai33LESTrknhcSMvA4tqPbetRord+RPMPYC6uJPLXZWIDPSiPbk4NAMBa9i7pBRvjfmy4j5GLb+GoOIGo2DGt2UX27gsIkoHw9WaUwu3oWoxAdxPpBRsRTXYCP51KHQBBxL7lCGkPPhYPRfo78LfXs3jf96SvfIpA12fIOcUYcosIdH2ObF2CcfmTBK6eQZAVzCUv3wMAIMjpOCpOIDtWxmPB3guoPg8A6au3E+5rnmy9x01s6BqZ698gveDZWa9sCQMAiIqdrJ2fIJqzJ8GMVlTfLcJ9LdNar6wux7xuD6Jim7tmMgAAkm0pWTs+Rki7+1/r0XHknLWTrbfnY3HVTOvUfQUAMOSswVHeAKKMrqqEf29GCwxhcdVgXFbKlNv+vJbaCQMYV7iwP38YSbFidu5BsuSmVCcOoOu67Ha7UdVkHkYK9Hf9O/45oYzOzk40TZMm/KlPs52hUKh66qNhIUzTNElRlEMej+fsQur8d+wfq09oFkuvTdQAAAAASUVORK5CYII=" class="" xlink:href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAOxAAADsQBlSsOGwAAA/1JREFUWIXll21olWUYx3/Py9nZc8523uZyW+Z8gVXK3Dw2iJx0ilGGKVOnOIp0RJjRhyDWSAgJI4R9mA2hpohGVKizpQZGG4MWM5ZntnK1mQutdQz3ctzOzs778zx9sJ1tsJdzju5DdH26r+u+r+v/4+J57hf4v5swMcjLy9sVDAarBUFQF1JQ13XJZDId8ng8Z6YBOByOKzU1NWudTudC6uN2u6mtrXV7vd4SAHnKXMzpdFJWVpZYJV0j0N1I+EYbtk11CJIhoTRVVRFFMd5lea7Fs1n4Zhu+1veIDvx6NyCI2DfXp1IqOYDoYC++1oOEb3w7LR785QtkWz6ZG95cGADVf5uxtloCV0+Drs24Zqy9Dsm2FFPhjvsHoEfG8Xd8iP+HBvRocN5ioxerkSx5GPPXJwwgziJNoOtTbjeUMtZ+OCFxAF2LcafpFWLD1+8RQFMJ9pxDGx9MuFA8NeRj+PRLCefOCDB2qR7LM+8jLypIGgBAHe3H21iFHgulBhAbusbIuX04yj9CylicEkTk7y7unH991o92TgBdU4kO9DDacgB7xUmEtIyUIEK/fY2v9WDyABPU4ZvfEXAfx7HtKIKY0p6F//Ixxq+cTBJAmAwHuhuJ/NmB9bnalAAARpsPEOprSRzAsqEa0ZgZ98cufQBqNKWdDkA0ZqAFvYkDyNkPY992fNoBM/LNftJyizEVVSauLEiYnbvJqjyFYDAnDgBgzH8C26Y64ie2FsP75auYi17AuMI1r7ZxWSmLXmzC8MAqQr1fYch+JDkAAGVVORbX23Ffj4wzfLYK69PvYMgpnDFHtuVj33oMc8legj3nkawPkbbcRfiP9uQBADIefw3zuqq4r40P4m3ai33LESTrknhcSMvA4tqPbetRord+RPMPYC6uJPLXZWIDPSiPbk4NAMBa9i7pBRvjfmy4j5GLb+GoOIGo2DGt2UX27gsIkoHw9WaUwu3oWoxAdxPpBRsRTXYCP51KHQBBxL7lCGkPPhYPRfo78LfXs3jf96SvfIpA12fIOcUYcosIdH2ObF2CcfmTBK6eQZAVzCUv3wMAIMjpOCpOIDtWxmPB3guoPg8A6au3E+5rnmy9x01s6BqZ698gveDZWa9sCQMAiIqdrJ2fIJqzJ8GMVlTfLcJ9LdNar6wux7xuD6Jim7tmMgAAkm0pWTs+Rki7+1/r0XHknLWTrbfnY3HVTOvUfQUAMOSswVHeAKKMrqqEf29GCwxhcdVgXFbKlNv+vJbaCQMYV7iwP38YSbFidu5BsuSmVCcOoOu67Ha7UdVkHkYK9Hf9O/45oYzOzk40TZMm/KlPs52hUKh66qNhIUzTNElRlEMej+fsQur8d+wfq09oFkuvTdQAAAAASUVORK5CYII=" style="pointer-events: auto;"></image> </svg> 

Result in Chrome,Firefox (inspected via devtools):

enter image description here

In Safari however, the icons are not rendered. The element is there, but the picture is missing. Screenshot from browserstack for safari, ios6. The highlighted blue box is the element (inspected via devtools again), showing that the element is in position, with the correct dimensions, but no image is showing:

enter image description here

What I have tried:

  • Using absolute and relative url paths for the image resource instead of inline base64. Makes no difference. The image is also hosted on the same domain, no cross-domain issues apply.
  • Using various combinations of xlink:href and href (just xlink:href, just href, etc).
    • Modifying the image/svg tag. Added the appropriate namespaces and the xlink:href tag (default library only used href), as per this suggestion.

What I haven't tried:

  • Completely replacing the svg mechanism of leaflet with another (say...canvas). Much of the application relies on the svg renderer, so I'd rather not go there.

Are there any other suggestions I could try except replacing the svg renderer?

Minimum reproducible example: https://jsfiddle.net/tocxvxy3/3/

1 Answers

Answers 1

You could try to directly put the png file name, intead of embbeding it in svg, as in leaflet examples http://leafletjs.com/examples/custom-icons/ , maybe the ios support is better this way?

var map = L.map('map').setView([51.5, -0.09], 13);    	L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {  		attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'  	}).addTo(map);    	var LeafIcon = L.Icon.extend({  		options: {  			shadowUrl: 'leaf-shadow.png',  			iconSize:     [50, 50],  			shadowSize:   [50, 64],  			iconAnchor:   [22, 94],  			shadowAnchor: [4, 62],  			popupAnchor:  [-3, -76]  		}  	});    	var greenIcon = new LeafIcon({iconUrl: 'https://memegenerator.net/img/images/50x50/7452314.jpg', iconRetinaUrl: 'https://static01.nyt.com/images/2012/09/14/blogs/Fils-Aime/Fils-Aime-thumbLarge.jpg'});    	L.marker([51.5, -0.09], {icon: greenIcon}).addTo(map);
html, body {    height: 100%;    margin: 0;  }  #map {    width: 600px;    height: 400px;  }
<link href="https://unpkg.com/leaflet@1.3.1/dist/leaflet.css" rel="stylesheet"/>  <script src="https://unpkg.com/leaflet@1.3.1/dist/leaflet.js"></script>  <div id='map'></div>

There's also an iconRetinaUrl option if you want to try

var greenIcon = new LeafIcon({   iconUrl: 'https://memegenerator.net/img/images/50x50/7452314.jpg',   iconRetinaUrl: 'https://static01.nyt.com/images/2012/09/14/blogs/Fils-Aime/Fils-Aime-thumbLarge.jpg' }); 
Read More

Wednesday, February 28, 2018

Safari doesn't emit pointer events on overflowing SVG contents

Leave a Comment

I'm trying to add click events to an SVG Element which has visible overflow and a shape element(circle/path) inside it which overflows the SVG.

On Safari(9,10,11), click event doesn't work in the area where the shape element(circle/path) overflows while it works fine in the area present within the SVG.

var count = 0;    function clickMe() {    console.log("in click func");    count++;    document.getElementById("counter").innerHTML = count;  }
#counter {    font-size: 2em;  }    #starSvg {    pointer-events: auto;    overflow: visible;    position: absolute;    left: 200px;    top: 250px;    border: 1px solid red;  }    #starPolygon {    overflow: visible;    fill: rgba(0, 153, 219, 1);    pointer-events: visiblePainted;    stroke-width: 4;    stroke-linecap: square;    stroke-linejoin: round;    stroke: rgba(219, 0, 153, 1);    cursor: pointer;    shape-rendering: geometricPrecision  }    p {    margin: 10px 0;  }
<div>    <p>Open this webpage on Chrome &amp; safari</p>    <p>On Chrome: Click work on all four hands of the star.</p>    <p>On Safari: Click works only on the hands inside the red area(SVG bounding Rect).</p>      <p style="position: absolute; top: 100px; left: 200px;">Click Event Counter:      <span id="counter">0</span>    </p>    <div class="containter">      <svg onclick="clickMe()" id="starSvg" width="100%" height="100%">            <g width="100%" height="100%" transform="" style="overflow: visible; transform: rotate(45deg) translate(0, 0);">              <polygon id="starPolygon" shape-rendering="geometricPrecision" points="0 -90,15 -15,90 0,15 15,0 90,-15 15,-90 0,-15 -15"></polygon>            </g>          </svg>    </div>  </div>

Is this also a bug in Safari? The click event works fine in all browsers including IE.

1 Answers

Answers 1

This mirrors Webkit bug report #140723 here: https://bugs.webkit.org/show_bug.cgi?id=140723

That bug report links to this codepen example reproducing the exact same results you've found: https://codepen.io/anon/pen/pvPQqY

And with the fix as I've described below applied here:

https://codepen.io/anon/pen/YeOmGW

===========

Edited for clarity: The evidence is clear that the initial clipping is taking effect at the outer most view port (svg) boundary, while at the same time the overflow property is allowing the visibility of the shape outside that clipped area. In effect rendering the pointer events void.

Said differently, evidence that clipping is applied IAW the first sentence here: http://w3.org/TR/SVG11/masking.html#AutoClipAtViewportNotViewBox , while at the same time conforming to the overflow rules (last three) here: http://w3.org/TR/SVG11/masking.html#OverflowAndClipProperties is the cause of this issue with Safari;

To overcome this issue, the OP must create a wrapper viewport to create a (workaround) solution to the dilemma created by the inconsistent implementations.

This is similar to adding any other HTML structure that might be required to wrap required content. (I'm really focused on helping solve the problem)

Hope this helps.

var count = 0;  document.querySelector('svg').addEventListener('click',clickMe);    function clickMe(e) {    console.log("clicked on: " + e.target.id);    count++;    document.getElementById("counter").innerHTML = count;  }
#counter {    font-size: 2em;  }    #starSvg {    pointer-events: auto;    position: absolute;    width: 100%;    height: 100%;    left:0;    top:0;  }    #starPolygon {    transform: translate(50%, 50%) rotate(45deg);    fill: rgba(0, 153, 219, 1);    stroke-width: 4;    stroke-linecap: square;    stroke-linejoin: round;    stroke: rgba(219, 0, 153, 1);    cursor: pointer;    shape-rendering: geometricPrecision  }  #starClip {    width: 100%;    height: 100%;    stroke-width: 1;    stroke: red;    fill: transparent;  }    p {    margin: 10px 0;  }
<div>    <p>Open this webpage on Chrome &amp; safari</p>    <p>On Chrome: Click work on all four hands of the star.</p>    <p>On Safari: Click works only on the hands inside the red area(SVG bounding Rect).</p>      <p style="position: absolute; top: 100px; left: 200px;">Click Event Counter:      <span id="counter">0</span>    </p>    <div class="containter">      <svg id="starSvg">            <rect id="starClip" x="50%" y="50%"></rect>            <g>              <polygon id="starPolygon" x="0" y="0" points="0 -90,15 -15,90 0,15 15,0 90,-15 15,-90 0,-15 -15"></polygon>            </g>          </svg>    </div>  </div>

Read More

Friday, November 3, 2017

Firefox getBoundingClientRect() does not account for transforms

Leave a Comment

I'm adding some SVG Paths to my web page, but am having difficulty with Firefox 43.0. It appears that when I apply transform: scale(0.1) to my path, Firefox does not update the bounding client rectangle (via getBoundingClientRect())

Here's a screenshot of my Path before the transform, and the correct bounding rectangle:

Path without transform

And here it is with the transform applied, with the visual clearly outside the bounding box:

Path with transform

By contrast, here is Chrome updating its bounding box as expected. (Note the constrained proportions.)

Path with transform in Chrome

This problem isn't present on either Chrome or Edge. I did find this old bug from 2012 which says the problem was fixed in version 12.0, and the documentation states:

Starting from Gecko 12.0 (Firefox 12.0 / Thunderbird 12.0 / SeaMonkey 2.9), the effect of CSS transforms is considered when computing the element's bounding rectangle.

...which doesn't seem to be true. For the other browsers, I was scaling down my circle to 10% of its original size, then computing the coordinate offset from the client rectangle to center it on its original 100% scale position. However, since the client rectangle isn't updated after the transform in Firefox, it's messing up the calculations.

How do I work around this for Firefox?

1 Answers

Answers 1

Transforms can be set via attributes e.g.

<path transform="scale(0.1)" d="..."/> 

or CSS as you're doing. Using CSS is the newer way to do it; the SVG 1.1 specification only specifies attribute transforms.

getBoundingClientRect doesn't take CSS transforms into account currently on Firefox but it does take transform attributes into account.

Read More

Monday, October 23, 2017

Why svg animation is not displaying button click event?

Leave a Comment

Im created some svg animation and I added for my Web application ,now when I click the Add Score button after I try to display this animation same page move to top (like Facebook text delight animation-im attached sample image ),

(look and click snippet, animation is already displayed- without button click ,I don't want to do that )

2nd issue , Im click the button I want to this play that animation and after redirect next page ,

its not working for me, please help me to fix it

svg{    background: transparent;      padding-top:-20px;  }    .conf0{fill:#FC6394;}  .conf1{fill:#EF3C8A;}  .conf2{fill:#5ADAEA;}  .conf3{fill:#974CBE;}  .conf4{fill:#3CBECD;}  .conf5{fill:#813BBE;}  .conf6{fill:#F9B732;}  .conf7{display:none;fill:none;stroke:#000000;stroke-miterlimit:10;}  .conf8{fill:none;stroke:#F9B732;stroke-width:9;stroke-linecap:round;stroke-miterlimit:10;}      .confetti-cone{    transform-origin: 200px 50px;    animation:confetti-cone1 1.2s ease infinite;  }  @keyframes confetti-cone1{    0%{      transform:translate(40px, 95px)  rotate(45deg) scale(1, 1);    }    15%{      transform:translate(10px, 145px)  rotate(45deg) scale(1.1, 0.85);      }    100%{      transform:translate(40px, 105px)  rotate(45deg) scale(1, 1);    }  }    #yellow-strip {    fill:none;    stroke:#F9B732;    stroke-width:9;    stroke-linecap:round;    stroke-miterlimit:10;    animation: confdash 1.2s ease infinite;  }      @keyframes confdash {    0%{      stroke-dasharray:1000;      stroke-dashoffset:500;      transform:translate(-30px, 30px);      opacity:0;    }    2%{      stroke-dasharray:1000;      stroke-dashoffset:500;      transform:translate(-30px, 30px);      opacity:0;    }    35%{      stroke-dasharray:1000;      stroke-dashoffset:900;      transform:translate(-2px, 0px);      opacity:1;    }      85%{      stroke-dasharray:1000;      stroke-dashoffset:1000;      transform:translate(1px, -5px);      opacity:1;    }    90%{      stroke-dashoffset:1000;      stroke-dashoffset:1000;      transform:translate(2px, -8px);      opacity:0;    }    100%{      stroke-dashoffset:1000;      stroke-dashoffset:500;      transform:translate(2px, -8px);      opacity:0;    }  }        #conf-a{    transform-origin: center center;    animation: confa 1.2s ease-out infinite;  }  @keyframes confa {    0%{      opacity:0;      transform: translate(-30px, 20px) rotate(0);    }    15%{      opacity:1;      transform: translate(25px, -10px) rotate(60deg);    }    80%{      opacity:1;      transform: translate(33px, -18px) rotate(180deg);    }    100%{      opacity:0;      transform: translate(37px, -23px) scale(0.5)rotate(230deg);    }  }      #conf-b{    transform-origin: center center;    animation: confb 1.2s ease-out infinite;  }  @keyframes confb {    0%{      opacity:0;      transform: translate(-30px, 20px) rotate(0);    }    12%{      opacity:1;      transform: translate(25px, -10px) rotate(60deg);    }    76%{      opacity:1;      transform: translate(33px, -18px) rotate(180deg);    }    100%{      opacity:0;      transform: translate(37px, -23px) scale(0.5) rotate(240deg);    }  }    #conf-c{    transform-origin: center center;    animation: confc 1.2s ease-out infinite;  }  @keyframes confc {    0%{      opacity:0.7;      transform: translate(-30px, 20px) rotate(0);    }    18%{      opacity:1;      transform: translate(5px, -10px) rotate(60deg);    }    76%{      opacity:1;      transform: translate(13px, -18px) rotate(180deg);    }    100%{      opacity:0;      transform: translate(17px, -23px) scale(0.5) rotate(230deg);    }  }    #conf-d{    transform-origin: center center;    animation: confd 1.2s ease-out infinite;  }  @keyframes confd {    0%{      opacity:0.7;      transform: translate(-20px, 20px) rotate(0);    }    18%{      opacity:1;      transform: translate(-5px, -10px) rotate(60deg);    }    76%{      opacity:1;      transform: translate(-8px, -18px) rotate(180deg);    }    100%{      opacity:0;      transform: translate(-10px, -23px) scale(0.5) rotate(230deg);    }  }
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js" integrity="sha384-b/U6ypiBEHpOf/4+1nzFpr53nxSS+GLCkfwBdFNTxtclqqenISfwAzpKaMNFNmj4" crossorigin="anonymous"></script>  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1" crossorigin="anonymous"></script>  <div class="footer-tab">    <!--button-->    <div class="crtnewgrp-btn">      <div class="d-flex flex-column">        <a href="addacore" style="text-decoration: none;"> <div class="p-2"><button type="button" class="btn btn-primary btn-lg btn-block" >Add score</button></div>        </a>      </div>      </div>      <!--/button-->  </div>    <svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"       	 viewBox="0 0 1200 800" style="enable-background:new 0 0 1200 800;" xml:space="preserve">       	 <g class="confetti-cone">       		 <path class="conf0" d="M131.5,172.6L196,343c2.3,6.1,11,6.1,13.4,0l65.5-170.7L131.5,172.6z"/>       		 <path class="conf1" d="M131.5,172.6L196,343c2.3,6.1,11,6.1,13.4,0l6.7-17.5l-53.6-152.9L131.5,172.6z"/>         			<path class="conf2" d="M274.2,184.2c-1.8,1.8-4.2,2.9-7,2.9l-129.5,0.4c-5.4,0-9.8-4.4-9.8-9.8c0-5.4,4.4-9.8,9.9-9.9l129.5-0.4       				c5.4,0,9.8,4.4,9.8,9.8C277,180,275.9,182.5,274.2,184.2z"/>       			<polygon class="conf3" points="231.5,285.4 174.2,285.5 143.8,205.1 262.7,204.7 			"/>       			<path class="conf4" d="M166.3,187.4l-28.6,0.1c-5.4,0-9.8-4.4-9.8-9.8c0-5.4,4.4-9.8,9.9-9.9l24.1-0.1c0,0-2.6,5-1.3,10.6       				C161.8,183.7,166.3,187.4,166.3,187.4z"/>       			<ellipse transform="matrix(0.7071 -0.7071 0.7071 0.7071 -89.8523 231.0278)" class="conf2" cx="233.9" cy="224" rx="5.6" ry="5.6"/>       			<path class="conf5" d="M143.8,205.1l5.4,14.3c6.8-2.1,14.4-0.5,19.7,4.8c7.7,7.7,7.6,20.1-0.1,27.8c-1.7,1.7-3.7,3-5.8,4l11.1,29.4       				l27.7,0l-28-80.5L143.8,205.1z"/>       			<path class="conf2" d="M169,224.2c-5.3-5.3-13-6.9-19.7-4.8l13.9,36.7c2.1-1,4.1-2.3,5.8-4C176.6,244.4,176.6,231.9,169,224.2z"/>       			<ellipse transform="matrix(0.7071 -0.7071 0.7071 0.7071 -119.0946 221.1253)" class="conf6" cx="207.4" cy="254.3" rx="11.3" ry="11.2"/>       	</g>         		<rect x="113.7" y="135.7" transform="matrix(0.7071 -0.7071 0.7071 0.7071 -99.5348 209.1582)" class="conf7" width="178" height="178"/>       	<line class="conf7" x1="76.8" y1="224.7" x2="328.6" y2="224.7"/>       	<polyline class="conf7" points="202.7,350.6 202.7,167.5 202.7,98.9 	"/>            <!-- here comes the confettis-->          	<circle class="conf2" id="conf-b" cx="195.2" cy="232.6" r="5.1"/>        	<circle class="conf0" id="conf-b" cx="230.8" cy="219.8" r="5.4"/>        	<circle class="conf0" id="conf-c" cx="178.9" cy="160.4" r="4.2"/>        	<circle class="conf6" id="conf-d"cx="132.8" cy="123.6" r="5.4"/>        	<circle class="conf0" id="conf-d" cx="151.9" cy="105.1" r="5.4"/>          	<path class="conf0" id="conf-d" d="M129.9,176.1l-5.7,1.3c-1.6,0.4-2.2,2.3-1.1,3.5l3.8,4.2c1.1,1.2,3.1,0.8,3.6-0.7l1.9-5.5        		C132.9,177.3,131.5,175.7,129.9,176.1z"/>        	<path class="conf6" id="conf-b" d="M284.5,170.7l-5.4,1.2c-1.5,0.3-2.1,2.2-1,3.3l3.6,3.9c1,1.1,2.9,0.8,3.4-0.7l1.8-5.2        		C287.4,171.9,286.1,170.4,284.5,170.7z"/>        	<circle class="conf6" id="conf-c"cx="206.7" cy="144.4" r="4.5"/>          <path class="conf2" id="conf-c" d="M176.4,192.3h-3.2c-1.6,0-2.9-1.3-2.9-2.9v-3.2c0-1.6,1.3-2.9,2.9-2.9h3.2c1.6,0,2.9,1.3,2.9,2.9v3.2        		C179.3,191,178,192.3,176.4,192.3z"/>        	<path class="conf2" id="conf-b" d="M263.7,197.4h-3.2c-1.6,0-2.9-1.3-2.9-2.9v-3.2c0-1.6,1.3-2.9,2.9-2.9h3.2c1.6,0,2.9,1.3,2.9,2.9v3.2        		C266.5,196.1,265.2,197.4,263.7,197.4z"/>            <!-- yellow-strip-1-->           <path id="yellow-strip" d="M179.7,102.4c0,0,6.6,15.3-2.3,25c-8.9,9.7-24.5,9.7-29.7,15.6c-5.2,5.9-0.7,18.6,3.7,28.2        		c4.5,9.7,2.2,23-10.4,28.2"/>        	<path class="conf8" id="yellow-strip" d="M252.2,156.1c0,0-16.9-3.5-28.8,2.4c-11.9,5.9-14.9,17.8-16.4,29c-1.5,11.1-4.3,28.8-31.5,33.4"/>        	<path class="conf0" id="conf-a" d="M277.5,254.8h-3.2c-1.6,0-2.9-1.3-2.9-2.9v-3.2c0-1.6,1.3-2.9,2.9-2.9h3.2c1.6,0,2.9,1.3,2.9,2.9v3.2        		C280.4,253.5,279.1,254.8,277.5,254.8z"/>        	<path class="conf3" id="conf-c" d="M215.2,121.3L215.2,121.3c0.3,0.6,0.8,1,1.5,1.1l0,0c1.6,0.2,2.2,2.2,1.1,3.3l0,0c-0.5,0.4-0.7,1.1-0.6,1.7v0        		c0.3,1.6-1.4,2.8-2.8,2l0,0c-0.6-0.3-1.2-0.3-1.8,0h0c-1.4,0.7-3.1-0.5-2.8-2v0c0.1-0.6-0.1-1.3-0.6-1.7l0,0        		c-1.1-1.1-0.5-3.1,1.1-3.3l0,0c0.6-0.1,1.2-0.5,1.5-1.1v0C212.5,119.8,214.5,119.8,215.2,121.3z"/>        	<path class="conf3" id="conf-b" d="M224.5,191.7L224.5,191.7c0.3,0.6,0.8,1,1.5,1.1l0,0c1.6,0.2,2.2,2.2,1.1,3.3v0c-0.5,0.4-0.7,1.1-0.6,1.7l0,0        		c0.3,1.6-1.4,2.8-2.8,2h0c-0.6-0.3-1.2-0.3-1.8,0l0,0c-1.4,0.7-3.1-0.5-2.8-2l0,0c0.1-0.6-0.1-1.3-0.6-1.7v0        		c-1.1-1.1-0.5-3.1,1.1-3.3l0,0c0.6-0.1,1.2-0.5,1.5-1.1l0,0C221.7,190.2,223.8,190.2,224.5,191.7z"/>        	<path class="conf3" id="conf-a" d="M312.6,242.1L312.6,242.1c0.3,0.6,0.8,1,1.5,1.1l0,0c1.6,0.2,2.2,2.2,1.1,3.3l0,0c-0.5,0.4-0.7,1.1-0.6,1.7v0        		c0.3,1.6-1.4,2.8-2.8,2l0,0c-0.6-0.3-1.2-0.3-1.8,0h0c-1.4,0.7-3.1-0.5-2.8-2v0c0.1-0.6-0.1-1.3-0.6-1.7l0,0        		c-1.1-1.1-0.5-3.1,1.1-3.3l0,0c0.6-0.1,1.2-0.5,1.5-1.1v0C309.9,240.6,311.9,240.6,312.6,242.1z"/>        	<path class="conf8" id="yellow-strip" d="M290.7,215.4c0,0-14.4-3.4-22.6,2.7c-8.2,6.2-8.2,23.3-17.1,29.4c-8.9,6.2-19.8-2.7-32.2-4.1        		c-12.3-1.4-19.2,5.5-20.5,10.9"/>    </svg>

this is a Facebook text delight animation example
enter image description here Thanks

1 Answers

Answers 1

Is it something like this that you wanted?

  1. We start with the SVG hidden with display: none.
  2. When you click on the button, we add the show class to the SVG. That changes the display property to block. Making the SVG visible.
  3. We also set up a timeout using window.setTimeout(). After a few seconds, it redirects to another page by setting window.location.href.

var addScoreButton = document.getElementById("add-score-button");  var mysvg = document.getElementById("mysvg");      addScoreButton.addEventListener("click", addScoreClick);      function addScoreClick(evt)  {    mysvg.classList.add("show");    setTimeout(function() {      window.alert("Redirecting to Google");      //window.location.href="https://www.google.com";    }, 3600);  }
svg {    display: none;  }    svg.show {    display: block;  }      .conf0{fill:#FC6394;}  .conf1{fill:#EF3C8A;}  .conf2{fill:#5ADAEA;}  .conf3{fill:#974CBE;}  .conf4{fill:#3CBECD;}  .conf5{fill:#813BBE;}  .conf6{fill:#F9B732;}  .conf7{display:none;fill:none;stroke:#000000;stroke-miterlimit:10;}  .conf8{fill:none;stroke:#F9B732;stroke-width:9;stroke-linecap:round;stroke-miterlimit:10;}      .confetti-cone {    transform-origin: 200px 50px;    animation:confetti-cone1 1.2s ease infinite;  }    @keyframes confetti-cone1{    0%{      transform:translate(40px, 95px)  rotate(45deg) scale(1, 1);    }    15%{      transform:translate(10px, 145px)  rotate(45deg) scale(1.1, 0.85);      }    100%{      transform:translate(40px, 105px)  rotate(45deg) scale(1, 1);    }  }    #yellow-strip {    fill:none;    stroke:#F9B732;    stroke-width:9;    stroke-linecap:round;    stroke-miterlimit:10;    animation: confdash 1.2s ease infinite;  }      @keyframes confdash {    0%{      stroke-dasharray:1000;      stroke-dashoffset:500;      transform:translate(-30px, 30px);      opacity:0;    }    2%{      stroke-dasharray:1000;      stroke-dashoffset:500;      transform:translate(-30px, 30px);      opacity:0;    }    35%{      stroke-dasharray:1000;      stroke-dashoffset:900;      transform:translate(-2px, 0px);      opacity:1;    }      85%{      stroke-dasharray:1000;      stroke-dashoffset:1000;      transform:translate(1px, -5px);      opacity:1;    }    90%{      stroke-dashoffset:1000;      stroke-dashoffset:1000;      transform:translate(2px, -8px);      opacity:0;    }    100%{      stroke-dashoffset:1000;      stroke-dashoffset:500;      transform:translate(2px, -8px);      opacity:0;    }  }        #conf-a{    transform-origin: center center;    animation: confa 1.2s ease-out infinite;  }  @keyframes confa {    0%{      opacity:0;      transform: translate(-30px, 20px) rotate(0);    }    15%{      opacity:1;      transform: translate(25px, -10px) rotate(60deg);    }    80%{      opacity:1;      transform: translate(33px, -18px) rotate(180deg);    }    100%{      opacity:0;      transform: translate(37px, -23px) scale(0.5)rotate(230deg);    }  }      #conf-b{    transform-origin: center center;    animation: confb 1.2s ease-out infinite;  }  @keyframes confb {    0%{      opacity:0;      transform: translate(-30px, 20px) rotate(0);    }    12%{      opacity:1;      transform: translate(25px, -10px) rotate(60deg);    }    76%{      opacity:1;      transform: translate(33px, -18px) rotate(180deg);    }    100%{      opacity:0;      transform: translate(37px, -23px) scale(0.5) rotate(240deg);    }  }    #conf-c{    transform-origin: center center;    animation: confc 1.2s ease-out infinite;  }  @keyframes confc {    0%{      opacity:0.7;      transform: translate(-30px, 20px) rotate(0);    }    18%{      opacity:1;      transform: translate(5px, -10px) rotate(60deg);    }    76%{      opacity:1;      transform: translate(13px, -18px) rotate(180deg);    }    100%{      opacity:0;      transform: translate(17px, -23px) scale(0.5) rotate(230deg);    }  }    #conf-d{    transform-origin: center center;    animation: confd 1.2s ease-out infinite;  }  @keyframes confd {    0%{      opacity:0.7;      transform: translate(-20px, 20px) rotate(0);    }    18%{      opacity:1;      transform: translate(-5px, -10px) rotate(60deg);    }    76%{      opacity:1;      transform: translate(-8px, -18px) rotate(180deg);    }    100%{      opacity:0;      transform: translate(-10px, -23px) scale(0.5) rotate(230deg);    }  }
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js" integrity="sha384-b/U6ypiBEHpOf/4+1nzFpr53nxSS+GLCkfwBdFNTxtclqqenISfwAzpKaMNFNmj4" crossorigin="anonymous"></script>  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1" crossorigin="anonymous"></script>  <div class="footer-tab">    <!--button-->    <div class="crtnewgrp-btn">      <div class="d-flex flex-column">        <div class="p-2"><button type="button" id="add-score-button" class="btn btn-primary btn-lg btn-block" >Add score</button></div>      </div>      </div>      <!--/button-->  </div>    <svg id="mysvg"       version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"       	 viewBox="0 0 1200 800" style="enable-background:new 0 0 1200 800;" xml:space="preserve">       	 <g class="confetti-cone">       		 <path class="conf0" d="M131.5,172.6L196,343c2.3,6.1,11,6.1,13.4,0l65.5-170.7L131.5,172.6z"/>       		 <path class="conf1" d="M131.5,172.6L196,343c2.3,6.1,11,6.1,13.4,0l6.7-17.5l-53.6-152.9L131.5,172.6z"/>         			<path class="conf2" d="M274.2,184.2c-1.8,1.8-4.2,2.9-7,2.9l-129.5,0.4c-5.4,0-9.8-4.4-9.8-9.8c0-5.4,4.4-9.8,9.9-9.9l129.5-0.4       				c5.4,0,9.8,4.4,9.8,9.8C277,180,275.9,182.5,274.2,184.2z"/>       			<polygon class="conf3" points="231.5,285.4 174.2,285.5 143.8,205.1 262.7,204.7 			"/>       			<path class="conf4" d="M166.3,187.4l-28.6,0.1c-5.4,0-9.8-4.4-9.8-9.8c0-5.4,4.4-9.8,9.9-9.9l24.1-0.1c0,0-2.6,5-1.3,10.6       				C161.8,183.7,166.3,187.4,166.3,187.4z"/>       			<ellipse transform="matrix(0.7071 -0.7071 0.7071 0.7071 -89.8523 231.0278)" class="conf2" cx="233.9" cy="224" rx="5.6" ry="5.6"/>       			<path class="conf5" d="M143.8,205.1l5.4,14.3c6.8-2.1,14.4-0.5,19.7,4.8c7.7,7.7,7.6,20.1-0.1,27.8c-1.7,1.7-3.7,3-5.8,4l11.1,29.4       				l27.7,0l-28-80.5L143.8,205.1z"/>       			<path class="conf2" d="M169,224.2c-5.3-5.3-13-6.9-19.7-4.8l13.9,36.7c2.1-1,4.1-2.3,5.8-4C176.6,244.4,176.6,231.9,169,224.2z"/>       			<ellipse transform="matrix(0.7071 -0.7071 0.7071 0.7071 -119.0946 221.1253)" class="conf6" cx="207.4" cy="254.3" rx="11.3" ry="11.2"/>       	</g>         		<rect x="113.7" y="135.7" transform="matrix(0.7071 -0.7071 0.7071 0.7071 -99.5348 209.1582)" class="conf7" width="178" height="178"/>       	<line class="conf7" x1="76.8" y1="224.7" x2="328.6" y2="224.7"/>       	<polyline class="conf7" points="202.7,350.6 202.7,167.5 202.7,98.9 	"/>            <!-- here comes the confettis-->          	<circle class="conf2" id="conf-b" cx="195.2" cy="232.6" r="5.1"/>        	<circle class="conf0" id="conf-b" cx="230.8" cy="219.8" r="5.4"/>        	<circle class="conf0" id="conf-c" cx="178.9" cy="160.4" r="4.2"/>        	<circle class="conf6" id="conf-d"cx="132.8" cy="123.6" r="5.4"/>        	<circle class="conf0" id="conf-d" cx="151.9" cy="105.1" r="5.4"/>          	<path class="conf0" id="conf-d" d="M129.9,176.1l-5.7,1.3c-1.6,0.4-2.2,2.3-1.1,3.5l3.8,4.2c1.1,1.2,3.1,0.8,3.6-0.7l1.9-5.5        		C132.9,177.3,131.5,175.7,129.9,176.1z"/>        	<path class="conf6" id="conf-b" d="M284.5,170.7l-5.4,1.2c-1.5,0.3-2.1,2.2-1,3.3l3.6,3.9c1,1.1,2.9,0.8,3.4-0.7l1.8-5.2        		C287.4,171.9,286.1,170.4,284.5,170.7z"/>        	<circle class="conf6" id="conf-c"cx="206.7" cy="144.4" r="4.5"/>          <path class="conf2" id="conf-c" d="M176.4,192.3h-3.2c-1.6,0-2.9-1.3-2.9-2.9v-3.2c0-1.6,1.3-2.9,2.9-2.9h3.2c1.6,0,2.9,1.3,2.9,2.9v3.2        		C179.3,191,178,192.3,176.4,192.3z"/>        	<path class="conf2" id="conf-b" d="M263.7,197.4h-3.2c-1.6,0-2.9-1.3-2.9-2.9v-3.2c0-1.6,1.3-2.9,2.9-2.9h3.2c1.6,0,2.9,1.3,2.9,2.9v3.2        		C266.5,196.1,265.2,197.4,263.7,197.4z"/>            <!-- yellow-strip-1-->           <path id="yellow-strip" d="M179.7,102.4c0,0,6.6,15.3-2.3,25c-8.9,9.7-24.5,9.7-29.7,15.6c-5.2,5.9-0.7,18.6,3.7,28.2        		c4.5,9.7,2.2,23-10.4,28.2"/>        	<path class="conf8" id="yellow-strip" d="M252.2,156.1c0,0-16.9-3.5-28.8,2.4c-11.9,5.9-14.9,17.8-16.4,29c-1.5,11.1-4.3,28.8-31.5,33.4"/>        	<path class="conf0" id="conf-a" d="M277.5,254.8h-3.2c-1.6,0-2.9-1.3-2.9-2.9v-3.2c0-1.6,1.3-2.9,2.9-2.9h3.2c1.6,0,2.9,1.3,2.9,2.9v3.2        		C280.4,253.5,279.1,254.8,277.5,254.8z"/>        	<path class="conf3" id="conf-c" d="M215.2,121.3L215.2,121.3c0.3,0.6,0.8,1,1.5,1.1l0,0c1.6,0.2,2.2,2.2,1.1,3.3l0,0c-0.5,0.4-0.7,1.1-0.6,1.7v0        		c0.3,1.6-1.4,2.8-2.8,2l0,0c-0.6-0.3-1.2-0.3-1.8,0h0c-1.4,0.7-3.1-0.5-2.8-2v0c0.1-0.6-0.1-1.3-0.6-1.7l0,0        		c-1.1-1.1-0.5-3.1,1.1-3.3l0,0c0.6-0.1,1.2-0.5,1.5-1.1v0C212.5,119.8,214.5,119.8,215.2,121.3z"/>        	<path class="conf3" id="conf-b" d="M224.5,191.7L224.5,191.7c0.3,0.6,0.8,1,1.5,1.1l0,0c1.6,0.2,2.2,2.2,1.1,3.3v0c-0.5,0.4-0.7,1.1-0.6,1.7l0,0        		c0.3,1.6-1.4,2.8-2.8,2h0c-0.6-0.3-1.2-0.3-1.8,0l0,0c-1.4,0.7-3.1-0.5-2.8-2l0,0c0.1-0.6-0.1-1.3-0.6-1.7v0        		c-1.1-1.1-0.5-3.1,1.1-3.3l0,0c0.6-0.1,1.2-0.5,1.5-1.1l0,0C221.7,190.2,223.8,190.2,224.5,191.7z"/>        	<path class="conf3" id="conf-a" d="M312.6,242.1L312.6,242.1c0.3,0.6,0.8,1,1.5,1.1l0,0c1.6,0.2,2.2,2.2,1.1,3.3l0,0c-0.5,0.4-0.7,1.1-0.6,1.7v0        		c0.3,1.6-1.4,2.8-2.8,2l0,0c-0.6-0.3-1.2-0.3-1.8,0h0c-1.4,0.7-3.1-0.5-2.8-2v0c0.1-0.6-0.1-1.3-0.6-1.7l0,0        		c-1.1-1.1-0.5-3.1,1.1-3.3l0,0c0.6-0.1,1.2-0.5,1.5-1.1v0C309.9,240.6,311.9,240.6,312.6,242.1z"/>        	<path class="conf8" id="yellow-strip" d="M290.7,215.4c0,0-14.4-3.4-22.6,2.7c-8.2,6.2-8.2,23.3-17.1,29.4c-8.9,6.2-19.8-2.7-32.2-4.1        		c-12.3-1.4-19.2,5.5-20.5,10.9"/>    </svg>

Read More