Trigger animation when svg is in the viewport - javascript
Trigger animation when svg is in the viewport
I have a page with a big header image and an svg animation below the fold.
Here's the example:
html, body {
height: 100%;
margin: 0
}
.header-image {
height: 100%;
background-color: #ffcc00
}
/*Line animations*/
.line {
stroke-dasharray: 150;
stroke-dashoffset: 150;
animation: dash 2s cubic-bezier(0.23, 1, 0.32, 1);;
-webkit-animation: dash 2s cubic-bezier(0.23, 1, 0.32, 1);;
-webkit-animation-fill-mode: forwards;
}
#line01, #line02 {
animation-delay:1s;
-webkit-animation-delay:1s;
}
#line03, #line04 {
animation-delay:1.2s;
-webkit-animation-delay:1.2s;
}
#line05, #line06 {
animation-delay:1.6s;
-webkit-animation-delay:1.6s;
}
#line07, #line08 {
animation-delay:1.9s;
-webkit-animation-delay:1.9s;
}
#line09, #line10 {
animation-delay:2.3s;
-webkit-animation-delay:2.3s;
}
#line11,
#line12,
#line13,
#line14,
#line15,
#line16,
#line17,
#line18{
animation-delay:3.1s;
-webkit-animation-delay:3.1s;
}
#line19,
#line20,
#line21,
#line22,
#line23,
#line24,
#line25,
#line26{
animation-delay:3.5s;
-webkit-animation-delay:3.5s;
}
/*Keyframes*/
#keyframes dash {
0% {stroke-dashoffset: 150;}
100% {stroke-dashoffset: 0;}
}
#-webkit-keyframes dash {
0% {stroke-dashoffset: 150;}
100% {stroke-dashoffset: 0;}
}
<div class="header-image">Please scroll down</div>
<div class="svg-animation">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="guida" x="0px" y="0px" viewBox="0 0 480.669 305.346" enable-background="new 0 0 480.669 305.346" xml:space="preserve">
<rect id="ombra-carta01" x="52.2" y="31.8" transform="matrix(-0.9026 0.4305 -0.4305 -0.9026 331.4025 234.5817)" fill="#A2DEF1" width="174" height="246.1"/>
<path id="ombra-occhiali" fill="#A2DEF1" d="M406.66 105.7 c-4.273-0.759-20.888-8.967-33.1-12.474c-12.225-3.512-30.973-5.673-34.362-5.288c-4.051 0.459-4.223 0.747-4.973 3.6 c-0.75 2.86-0.772 5.4 1.3 6.642c2.683 1.5 2.2 5 1.9 14.967c-2.108 4.115-3.231 8.417-3.774 11.1 c-1.042 5.2 3.5 8.4 6.4 3.745c2.822 4.8 8.4 8.9 19 11.889c18.588 5.2 27.35-5.681 30.177-9.188 c5.041-6.253 5.85-16.103 15.383-14.193c9.534 1.9 6.7 11.2 8.9 18.887c1.259 4.3 4.9 17.9 24.1 20.2 c10.921 1.3 17.632-0.292 22.104-3.697c0.914 5.5 6.3 4.3 7.35-0.96c0.546-2.726 1.171-7.168 0.795-11.811 c3.57-9.194 4.472-12.6 7.528-12.981c2.4-0.299 3.373-2.688 3.782-5.617c0.41-2.93 0.362-3.263-3.2-5.246 c-2.979-1.66-21.113-6.885-33.747-8.354C430.991 105.7 416.7 107.5 406.7 105.694z"/>
<path id="fine-asta-dx" fill="#254659" d="M333.111 84.4 c23.972 4.3 106.6 28 118.9 33.554c12.3 5.5 10.6 21.7 9.3 28.158c-1.297 6.473-9.299 6.76-7.104-4.195 c2.193-10.955 2.428-17.082-5.075-19.285c-7.504-2.203-109.515-28.483-116.636-30.113C325.357 90.9 328.4 83.2 333.1 84.4 z"/>
<path id="fine-asta-sx" fill="#254659" d="M469.952 111.8 c-23.768-5.291-109.196-15.194-122.675-14.834c-13.479 0.359-18.091 15.947-19.387 22.421s5.978 9.8 8.172-1.136 c2.194-10.955 4.337-16.699 12.11-15.843c7.773 0.9 112 15.9 119.2 17.127C474.609 120.8 474.8 112.5 470 111.8 z"/>
<path id="montatura" d="M435.583 151.139c16.586 1.1 22.116-5.72 25.721-14.82 c4.24-10.709 5.044-14.575 8.313-14.983c2.399-0.299 3.372-2.688 3.782-5.617c0.409-2.93 0.362-3.263-3.201-5.246 c-2.979-1.66-21.113-6.885-33.747-8.354c-0.287-0.033-0.577-0.063-0.868-0.093v3.04c10.722 1.3 21.4 4.6 24 7 c5.566 5 0.8 22.641-5.935 30.514c-3.718 4.371-10.928 6.027-18.09 5.686V151.139z M400.826 100.8 c-4.274-0.759-20.888-8.967-33.101-12.475c-1.937-0.556-4.037-1.078-6.215-1.562v3.164c10.889 2.3 22.7 6.8 25.8 13.1 c4.493 9.401-9.373 27.347-19.625 29.896c-1.816 0.452-3.909 0.615-6.135 0.52v2.915c12.997 0.5 19.536-7.608 21.905-10.548 c5.042-6.253 5.85-16.103 15.384-14.193c9.533 1.9 6.7 11.2 8.9 18.887c1.258 4.3 4.9 17.9 24.1 20.2 c1.308 0.2 2.6 0.3 3.7 0.354v-2.87c-6.226-0.296-12.414-2.102-16.24-4.946c-8.479-6.302-14.364-28.202-6.597-35.148 c4.281-3.828 13.587-4.225 22.837-3.108v-3.04C424.432 100.9 410.6 102.6 400.8 100.828z M361.51 86.8 c-11.565-2.569-25.294-4.049-28.147-3.726c-4.051 0.459-4.223 0.747-4.973 3.608c-0.75 2.86-0.772 5.4 1.3 6.6 c2.86 1.6 2.1 5.5 1.9 17.028c-0.191 10.5 2.7 19.4 21.6 24.697c3.01 0.8 5.8 1.3 8.3 1.36v-2.915 c-9.958-0.431-22.591-6.071-25.262-14.423c-3.147-9.843-0.785-27.983 6.275-30.445c2.736-0.953 10.573-0.455 19 1.337V86.792z"/>
<path id="riflesso-bottom" fill="#B0CAD9" d="M342.523 88.6 c6.276-2.188 39.4 3.3 44.7 14.452c4.493 9.401-9.373 27.347-19.625 29.896c-9.948 2.477-28.129-3.686-31.396-13.903 C333.102 109.2 335.5 91.1 342.5 88.619L342.523 88.619z M459.608 112.069c-4.949-4.436-37.619-12.161-46.861-3.896 c-7.768 6.946-1.882 28.8 6.6 35.148c8.228 6.1 27.4 7.4 34.33-0.739C460.369 134.7 465.2 117.1 459.6 112.069z"/>
<path id="riflesso-top-sx" fill="#D1E2EB" d="M345.894 88.2 c10.529-0.32 36.7 5.1 41.4 14.913c3.151 6.594-2.729 17.39-10.059 24.13L345.894 88.158z"/>
<path id="riflesso-top-dx" fill="#D1E2EB" d="M418.783 105.355l31.898 39.8 c1.146-0.716 2.158-1.559 2.992-2.539c6.695-7.873 11.501-25.523 5.935-30.514C455.452 108.3 431.7 102.3 418.8 105.355z"/>
<path id="retro-aste" fill="#617F91" d="M452.192 108.6 c-8.324-1.297-18.175-2.673-28.473-4.008c-2.935 0.211-5.595 0.716-7.759 1.6l0 0l-0.059 0.024l-0.017 0.007l-0.041 0 l-0.059 0.024l-0.058 0.025h-0.001l-0.057 0.024l-0.058 0.024l-0.042 0.019l-0.016 0.007l-0.113 0.051l-0.026 0.013l-0.029 0 l-0.057 0.026l-0.056 0.026l-0.011 0.004l-0.045 0.022l-0.056 0.026l-0.051 0.024l-0.004 0.002l-0.055 0.026l-0.055 0 l-0.035 0.018l-0.02 0.011l-0.054 0.026l-0.054 0.028l-0.021 0.01l-0.033 0.019l-0.054 0.028l-0.013 0.007l-0.087 0 l-0.005 0.003l-0.053 0.029l-0.043 0.023l-0.009 0.006l-0.053 0.028l-0.052 0.029l-0.027 0.016l-0.023 0.014l-0.051 0 l-0.051 0.029l-0.051 0.03l-0.05 0.03l-0.051 0.03l0 0l-0.05 0.03l-0.05 0.031l-0.049 0.031l-0.049 0.031l-0.048 0.031l-0.022 0 l-0.026 0.018l-0.048 0.031l-0.048 0.032l-0.009 0.006l-0.039 0.026l-0.08 0.056l-0.009 0.006l-0.005 0.004l-0.009 0 l-0.084 0.06l-0.029 0.021l-0.017 0.012l-0.046 0.034l-0.046 0.033l-0.016 0.013l-0.029 0.021l-0.045 0.034l-0.044 0 l-0.005 0.003l-0.039 0.031l-0.045 0.035l-0.035 0.028l-0.008 0.006l-0.003 0.002l-0.084 0.069l-0.023 0.02l-0.019 0 l-0.085 0.071l-0.011 0.011l-0.113 0.099l-0.043 0.039l-0.014 0.013l-0.029 0.026l-0.026 0.024l-0.017 0.015l-0.039 0 l-0.002 0.002l-0.042 0.04l-0.012 0.011l-0.03 0.03l-0.024 0.023l-0.017 0.017l-0.037 0.037l-0.004 0.004l-0.041 0.041l-0.009 0 l-0.031 0.032l-0.021 0.021l-0.019 0.021l-0.034 0.035l-0.005 0.007l-0.04 0.042c-0.938 1.008-1.644 2.262-2.142 3.7 c19.726 5.1 36.2 9.4 39 10.253c7.503 2.2 7.3 8.3 5.1 19.285l-0.005 0.024c2.821-3.578 5.257-8.839 6.642-14.028 c-1.398-4.124-4.051-7.831-8.789-9.959c-0.717-0.322-1.673-0.705-2.843-1.144c5.22 0.8 9.6 1.4 12.8 1.9 c-0.122-2.879-0.866-5.261-2.421-6.654C458.445 111 455.8 109.8 452.2 108.647L452.192 108.647z M350.696 88.3 c8.174 2 17.8 4.5 27.8 7.258c2.633 1.3 4.9 2.8 6.6 4.476h0.001l0.044 0.045l0.006 0 c0.072 0.1 0.1 0.1 0.2 0.219l0.025 0.027l0.018 0.019l0.042 0.045l0.043 0.045l0.014 0.015l0.028 0.031l0.041 0 l0.042 0.046l0.002 0.002l0.039 0.043l0.04 0.046l0.032 0.036l0.009 0.01l0.04 0.046l0.04 0.046l0.021 0.024l0.019 0 l0.039 0.046l0.039 0.047l0.011 0.012l0.028 0.034l0.038 0.047l0.038 0.046l0 0l0.037 0.047l0.038 0.047l0.026 0.033l0.01 0 l0.037 0.047l0.035 0.047l0.037 0.047l0.035 0.047l0.036 0.047l0.006 0.01l0.028 0.038l0.034 0.047l0.033 0.044l0.002 0 l0.034 0.048l0.033 0.048l0.022 0.031l0.011 0.016l0.033 0.048l0.032 0.048l0.013 0.019l0.021 0.029l0.031 0.047l0.032 0 l0.004 0.006l0.027 0.042l0.03 0.048l0.026 0.041l0.005 0.007l0.03 0.049l0.029 0.048l0.019 0.028l0.012 0.021l0.029 0 l0.029 0.049l0.009 0.017l0.019 0.032l0.028 0.048l0.028 0.049l0.002 0.004l0.025 0.046l0.026 0.049l0.022 0.039l0.005 0 l0.026 0.05l0.04 0.075l0.013 0.022l0.05 0.099l0.007 0.014l0.018 0.036l0.048 0.099l0.024 0.053l0.009 0.017l0.016 0 l0.016 0.033l0.009 0.019l0.022 0.051l0.001 0.002l0.023 0.053l0.007 0.015l0.016 0.039l0.014 0.031l0.009 0.021l0.021 0 l0.001 0.005l0.022 0.053l0.005 0.013l0.017 0.041l0.041 0.108l0.021 0.054l0.004 0.011l0.016 0 c0.462 1.3 0.6 2.7 0.5 4.182c-20.156-2.878-36.996-5.244-39.944-5.569c-7.773-0.856-9.917 4.888-12.11 15.843l-0.021 0.1 c-1.242-4.382-1.474-10.187-0.757-15.526c2.882-3.294 6.772-5.718 11.993-5.857c0.785-0.021 1.816-0.007 3.1 0 c-5.132-1.308-9.462-2.407-12.614-3.198c1.222-2.61 2.826-4.521 4.797-5.208C343.999 88.1 347 88 350.7 88.319z"/>
<path id="riflesso-aste-top-sx" fill="#8097A6" d="M345.893 88.158l12.551 15.6 c7.396 1 18 2.5 29.7 4.194c0.09-1.494-0.073-2.904-0.535-4.182l-0.016-0.044l-0.004-0.011l-0.021-0.054l-0.041-0.108 l-0.017-0.041l-0.005-0.013l-0.022-0.053l-0.001-0.005l-0.021-0.049l-0.009-0.021l-0.014-0.031l-0.016-0.039l-0.007-0.015 l-0.023-0.053l-0.001-0.002l-0.022-0.051l-0.009-0.019l-0.016-0.033l-0.016-0.036l-0.009-0.017l-0.024-0.053l-0.048-0.099 l-0.018-0.036l-0.007-0.014l-0.05-0.099l-0.013-0.022l-0.04-0.075l-0.026-0.05l-0.005-0.009l-0.022-0.039l-0.026-0.049l-0.025-0.046 l-0.002-0.004l-0.028-0.049l-0.028-0.048l-0.019-0.032l-0.009-0.017l-0.029-0.049l-0.029-0.048l-0.012-0.021l-0.019-0.028 l-0.029-0.048l-0.03-0.049l-0.005-0.007l-0.026-0.041l-0.03-0.048l-0.027-0.042l-0.004-0.006l-0.032-0.049l-0.031-0.047 l-0.021-0.029l-0.013-0.019l-0.032-0.048l-0.033-0.048l-0.011-0.016l-0.022-0.031l-0.033-0.048l-0.034-0.048l-0.002-0.003 l-0.033-0.044l-0.034-0.047l-0.028-0.038l-0.006-0.01l-0.036-0.047l-0.035-0.047l-0.037-0.047l-0.035-0.047l-0.037-0.047 l-0.01-0.013l-0.026-0.033l-0.038-0.047l-0.037-0.047l0 0l-0.038-0.046l-0.038-0.047l-0.028-0.034l-0.011-0.012l-0.039-0.047 l-0.039-0.046l-0.019-0.021l-0.021-0.024l-0.04-0.046l-0.04-0.046l-0.009-0.01l-0.032-0.036l-0.04-0.046l-0.039-0.043l-0.002-0.002 l-0.042-0.046l-0.041-0.046l-0.028-0.031l-0.014-0.015l-0.043-0.045l-0.042-0.045l-0.018-0.019l-0.025-0.027 c-0.07-0.073-0.142-0.146-0.214-0.219l-0.006-0.006l-0.044-0.045h-0.001c-1.659-1.653-3.926-3.147-6.559-4.476 c-10.013-2.731-19.623-5.251-27.797-7.258C348.887 88.2 347.3 88.1 345.9 88.158z"/>
<path id="riflesso-aste-top-dx" fill="#8097A6" d="M452.192 108.6 c-8.324-1.297-18.175-2.673-28.473-4.008c-1.761 0.126-3.422 0.358-4.937 0.716l9.427 11.751c10.996 2.9 19 5 20.9 5.5 c7.503 2.2 7.3 8.3 5.1 19.285l-0.005 0.024c2.821-3.578 5.257-8.839 6.642-14.028c-1.398-4.124-4.051-7.831-8.789-9.959 c-0.717-0.322-1.673-0.705-2.843-1.144c5.22 0.8 9.6 1.4 12.8 1.898c-0.122-2.879-0.866-5.261-2.421-6.654 C458.445 111 455.8 109.8 452.2 108.647z"/>
<rect id="carta01" x="46.3" y="26.9" transform="matrix(-0.9026 0.4305 -0.4305 -0.9026 318.206 227.8356)" fill="#FFFFFF" width="174" height="246.1"/>
<line class="line" id="line26" fill="none" stroke="#CCCCCC" stroke-width="4.5" x1="182.6" y1="239.7" x2="224" y2="220"/>
<line class="line" id="line25" fill="none" stroke="#CCCCCC" stroke-width="4.5" x1="179.3" y1="232.8" x2="238.6" y2="204.6"/>
<line class="line" id="line24" fill="none" stroke="#CCCCCC" stroke-width="4.5" x1="176.1" y1="226" x2="235.3" y2="197.7"/>
<line class="line" id="line23" fill="none" stroke="#CCCCCC" stroke-width="4.5" x1="172.8" y1="219.1" x2="232.1" y2="190.9"/>
<line class="line" id="line22" fill="none" stroke="#CCCCCC" stroke-width="4.5" x1="169.5" y1="212.2" x2="228.8" y2="184"/>
<line class="line" id="line21" fill="none" stroke="#CCCCCC" stroke-width="4.5" x1="166.2" y1="205.4" x2="225.6" y2="177.2"/>
<line class="line" id="line20" fill="none" stroke="#CCCCCC" stroke-width="4.5" x1="163" y1="198.5" x2="222.3" y2="170.3"/>
<line class="line" id="line19" fill="none" stroke="#CCCCCC" stroke-width="4.5" x1="159.7" y1="191.7" x2="219" y2="163.4"/>
<line class="line" id="line18" fill="none" stroke="#CCCCCC" stroke-width="4.5" x1="113.7" y1="272.5" x2="142.6" y2="258.7"/>
<line class="line" id="line17" fill="none" stroke="#CCCCCC" stroke-width="4.5" x1="110.4" y1="265.6" x2="168.8" y2="237.8"/>
<line class="line" id="line16" fill="none" stroke="#CCCCCC" stroke-width="4.5" x1="107.1" y1="258.8" x2="165.5" y2="231"/>
<line class="line" id="line15" fill="none" stroke="#CCCCCC" stroke-width="4.5" x1="103.9" y1="251.9" x2="162.3" y2="224.1"/>
<line class="line" id="line14" fill="none" stroke="#CCCCCC" stroke-width="4.5" x1="100.6" y1="245" x2="159" y2="217.3"/>
<line class="line" id="line13" fill="none" stroke="#CCCCCC" stroke-width="4.5" x1="97.3" y1="238.2" x2="155.7" y2="210.4"/>
<line class="line" id="line12" fill="none" stroke="#CCCCCC" stroke-width="4.5" x1="94.1" y1="231.3" x2="152.4" y2="203.5"/>
<line class="line" id="line11" fill="none" stroke="#CCCCCC" stroke-width="4.5" x1="90.8" y1="224.5" x2="149.2" y2="196.7"/>
<line class="line" id="line10" fill="none" stroke="#CCCCCC" stroke-width="4.5" x1="62.5" y1="166.6" x2="118.3" y2="140.1"/>
<line class="line" id="line09" fill="none" stroke="#CCCCCC" stroke-width="4.5" x1="59.3" y1="159.7" x2="187.5" y2="98.7"/>
<line class="line" id="line08" fill="none" stroke="#CCCCCC" stroke-width="4.5" x1="56" y1="152.9" x2="184.2" y2="91.8"/>
<line class="line" id="line07" fill="none" stroke="#CCCCCC" stroke-width="4.5" x1="52.7" y1="146" x2="181" y2="85"/>
<line class="line" id="line06" fill="none" stroke="#CCCCCC" stroke-width="4.5" x1="49.5" y1="139.1" x2="177.7" y2="78.1"/>
<line class="line" id="line05" fill="none" stroke="#CCCCCC" stroke-width="4.5" x1="46.2" y1="132.3" x2="174.4" y2="71.3"/>
<line class="line" id="line04" fill="none" stroke="#CCCCCC" stroke-width="4.5" x1="42.9" y1="125.4" x2="171.2" y2="64.4"/>
<line class="line" id="line03" fill="none" stroke="#CCCCCC" stroke-width="4.5" x1="39.7" y1="118.6" x2="167.9" y2="57.5"/>
<line class="line" id="line02" fill="none" stroke="#CCCCCC" stroke-width="4.5" x1="36.4" y1="111.7" x2="164.6" y2="50.7"/>
<line class="line" id="line01" fill="none" stroke="#CCCCCC" stroke-width="4.5" x1="54.4" y1="82.6" x2="130.7" y2="46.3"/>
</svg>
</div>
http://jsfiddle.net/qx7p46f3/4/
By the time the user has scrolled down, the animation has already played because it starts as soon as the page is loaded. I want to trigger it when the svg is in the viewport instead, when the user scrolls to that specific part of the page. How can I do that?
I don't know much about js but I'd rather not use jquery cause it doesn't seem to be the most efficient solution in this case, correct me if I'm wrong.
If you don't know much about javascript, then you might want to consider jQuery since it will be a lot easier for you to accomplish the desired effect.
Using a plugin like jQuery.onScreen you could simply write:
$('svg').onScreen({
container: window,
direction: 'vertical',
toggleClass: 'onScreen',
});
And in your css:
.onScreen .line {
animation: dash 2s cubic-bezier(0.23, 1, 0.32, 1);;
-webkit-animation: dash 2s cubic-bezier(0.23, 1, 0.32, 1);;
-webkit-animation-fill-mode: forwards;
}
Edit:
Without jQuery:
var wrapper = document.querySelector('.svg-animation');
window.onscroll = function (event) {
if (wrapper.getBoundingClientRect().top < 0) {
wrapper.className = "svg-animation onScreen";
window.onscroll = null;
}
}
Please note that this hasn't been tested in all browsers and I can't guarantee full support.
Here's a working demo: http://codepen.io/btpoe/pen/caeb32b678521d82144ae1f953c3e332
Related
Removing gap between two SVG paths without removing anti-aliasing
I have two SVG paths that have a gap between them. From reading through other questions (in particular this one) I understand this is because of the native anti-aliasing properties of SVGs. So I added shapeRendering="crispEdges" This does remove the gap. However it results in jagged edges because of the removal of anti-aliasing. <svg height="300" width="300" shapeRendering="crispEdges"> <path d="M150 10 a120 120 0 0 1 103.9230 60" fill="none" stroke="green" stroke-width="20" /> <path d="M253.9230 70 a120 120 0 0 1 0 120" fill="none" stroke="green" stroke-width="20" /> </svg> I've also tried the suggestion in this question to add crispEdges to the parent svg of the path and add shapeRendering="optimizeQuality" to the path but that didn't work. How can I remove the gap AND keep the smooth edges of my svg path?
You could also mitigate this gap rendering effect by applying a subtle svg feMorphology dilate filter – resulting in slightly expanded strokes closing thin gaps between paths: SVG feMorphology filter svg { overflow: visible; } .chart path { filter: url(#outline); } path:hover { stroke: red; } <svg height="300" width="300" shape-rendering="geometricPrecision"> <g class="original"> <path d="M150 10 a120 120 0 0 1 103.9230 60" fill="none" stroke="green" stroke-width="20" /> <path d="M253.9230 70 a120 120 0 0 1 0 120" fill="none" stroke="green" stroke-width="20" /> </g> <text x="50%" y="50%">Original</text> </svg> <svg height="300" width="300"> <filter filterUnits="userSpaceOnUse" id="outline" > <feMorphology in="SourceGraphic" result="DILATED" operator="dilate" radius="0.5" /> </filter> <g class="chart"> <path d="M150 10 a120 120 0 0 1 103.9230 60" fill="none" stroke="green" stroke-width="20" /> <path d="M253.9230 70 a120 120 0 0 1 0 120" fill="none" stroke="green" stroke-width="20" /> </g> <text x="50%" y="50%">Dilate filter</text> </svg> But this approach will also introduce slightly rounded edges (you can see this effect on hover). More importantly, svg filters are quite expensive with regards to rendering performance – rather negligible if you only display a few elements per page view. Add concatenated background path As suggested by #Robert Longson: you could also prepend a background path based on concatenated d path data. This task could be achieved with a javaScript helper method cloning the first path and displaying the concatenated paths. addBgPaths(".addBGPath"); function addBgPaths(selector) { let addPathSvgs = document.querySelectorAll(selector); addPathSvgs.forEach(function(svg) { let paths = document.querySelectorAll(".addBGPath path"); let firstPath = paths[0]; let firstPathCloned = firstPath.cloneNode(); //cloned elements shouldn't have ids to avoid non unique ids firstPathCloned.removeAttribute("id"); let dArr = [firstPath.getAttribute("d")]; for (let i = 1; i < paths.length; i++) { let path = paths[i]; let d = path.getAttribute("d"); dArr.push(d); } firstPathCloned.setAttribute("d", dArr.join(" ")); svg.insertBefore(firstPathCloned, svg.children[0]); }); } <svg height="300" width="300" shape-rendering="geometricPrecision"> <path d="M150 10 a120 120 0 0 1 103.9230 60" fill="none" stroke="green" stroke-width="20" /> <path d="M253.9230 70 a120 120 0 0 1 0 120" fill="none" stroke="green" stroke-width="20" /> <text x="0" y="50%">Original</text> </svg> <svg class="addBGPath" height="300" width="300" shape-rendering="geometricPrecision"> <path id="first2" d="M150 10 a120 120 0 0 1 103.9230 60" fill="none" stroke="green" stroke-width="20" /> <path d="M253.9230 70 a120 120 0 0 1 0 120" fill="none" stroke="green" stroke-width="20" /> <text x="0" y="50%">Add bg path</text> </svg> <svg class="addBGPath" height="300" width="300" shape-rendering="geometricPrecision"> <path id="first" d="M150 10 a120 120 0 0 1 103.9230 60" fill="none" stroke="green" stroke-width="20" /> <path d="M253.9230 70 a120 120 0 0 1 0 120" fill="none" stroke="red" stroke-width="20" /> <text x="0" y="50%">Add bg path (red)</text> </svg> <svg height="300" width="300" shape-rendering="geometricPrecision"> <path d="M150 10 a120 120 0 0 1 103.9230 60" fill="none" stroke="green" stroke-width="20" /> <path d="M253.9230 70 a120 120 0 0 1 0 120" fill="none" stroke="red" stroke-width="20" /> <text x="0" y="50%">Original (red)</text> </svg> If all your path segments have the same color, this is probably the most elegant solution. But this approach will also introduce colored "halos" when segments use different stroke colors (example #3 compared to #4).
If you able to edit the svg in editor, you can overlap like this. The darker green is the intersection between two paths.
As a quick fix, you can make the ends overlap with stroke-linecap="square" But ideally, you need to create a single path instead of two separate paths. <svg height="300" width="300" shapeRendering="crispEdges"> <path d="M150 10 a120 120 0 0 1 103.9230 60" fill="none" stroke="green" stroke-width="20" stroke-linecap="square" /> <path d="M253.9230 70 a120 120 0 0 1 0 120" fill="none" stroke="green" stroke-width="20" stroke-linecap="square" /> </svg>
How to do JS functions with PHP echo?
I'm trying to create a popout menu for an array of values that are echoed from the database. On the click of the svg, the popout menu that corresponds with the svg in the echo, needs to be shown. Except so far, it only works for the first one that is echoed. How to I fix it so that it show the popout that corresponds with the correct svg. Here's what I've currently got : PHP/HTML : echo(' <svg class="option-3" width="18" height="18" viewBox="0 0 18 18" fill="none" xmlns="http://www.w3.org/2000/svg"> <path fill-rule="evenodd" clip-rule="evenodd" d="M9 10.5C9.82843 10.5 10.5 9.82843 10.5 9C10.5 8.17157 9.82843 7.5 9 7.5C8.17157 7.5 7.5 8.17157 7.5 9C7.5 9.82843 8.17157 10.5 9 10.5Z" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/> <path fill-rule="evenodd" clip-rule="evenodd" d="M15 10.5C15.8284 10.5 16.5 9.82843 16.5 9C16.5 8.17157 15.8284 7.5 15 7.5C14.1716 7.5 13.5 8.17157 13.5 9C13.5 9.82843 14.1716 10.5 15 10.5Z" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/> <path fill-rule="evenodd" clip-rule="evenodd" d="M3 10.5C3.82843 10.5 4.5 9.82843 4.5 9C4.5 8.17157 3.82843 7.5 3 7.5C2.17157 7.5 1.5 8.17157 1.5 9C1.5 9.82843 2.17157 10.5 3 10.5Z" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/> </svg> <div class="menu-option-popout"></div> '); JS : document.querySelector(".option-3").addEventListener("click", function(){ document.querySelector(".menu-option-popout").style.display = "block"; });
If each popout <div> appears immediately after its corresponding <svg> tag (as in your example), you can take advantage of the .nextElementSibling property to get the <div> that follows the <svg> that was clicked. At the end of your HTML: <script> // Add an event listener to each .option-3 element: document.querySelectorAll('.option-3').forEach(item => { item.addEventListener('click', event => { let popout = event.target.nextElementSibling; // This element's next element (a .menu-option-popout) popout.style.display = 'block'; // Show the popout }) }) </script>
First of all PHP is executed first, so you will not be able to call php from a JS action unless using AJAX features. But it is not your use case here. That said, we can continue with : echo(' <svg class="option-3" width="18" height="18" viewBox="0 0 18 18" fill="none" xmlns="http://www.w3.org/2000/svg"> <path fill-rule="evenodd" clip-rule="evenodd" d="M9 10.5C9.82843 10.5 10.5 9.82843 10.5 9C10.5 8.17157 9.82843 7.5 9 7.5C8.17157 7.5 7.5 8.17157 7.5 9C7.5 9.82843 8.17157 10.5 9 10.5Z" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/> <path fill-rule="evenodd" clip-rule="evenodd" d="M15 10.5C15.8284 10.5 16.5 9.82843 16.5 9C16.5 8.17157 15.8284 7.5 15 7.5C14.1716 7.5 13.5 8.17157 13.5 9C13.5 9.82843 14.1716 10.5 15 10.5Z" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/> <path fill-rule="evenodd" clip-rule="evenodd" d="M3 10.5C3.82843 10.5 4.5 9.82843 4.5 9C4.5 8.17157 3.82843 7.5 3 7.5C2.17157 7.5 1.5 8.17157 1.5 9C1.5 9.82843 2.17157 10.5 3 10.5Z" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/> </svg> <div class="menu-option-popout"></div> '); should be written as this to improve the readability of your generated HTML document : echo '<svg class="option-3" width="18" height="18" viewBox="0 0 18 18" fill="none" xmlns="http://www.w3.org/2000/svg"> <path fill-rule="evenodd" clip-rule="evenodd" d="M9 10.5C9.82843 10.5 10.5 9.82843 10.5 9C10.5 8.17157 9.82843 7.5 9 7.5C8.17157 7.5 7.5 8.17157 7.5 9C7.5 9.82843 8.17157 10.5 9 10.5Z" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/> <path fill-rule="evenodd" clip-rule="evenodd" d="M15 10.5C15.8284 10.5 16.5 9.82843 16.5 9C16.5 8.17157 15.8284 7.5 15 7.5C14.1716 7.5 13.5 8.17157 13.5 9C13.5 9.82843 14.1716 10.5 15 10.5Z" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/> <path fill-rule="evenodd" clip-rule="evenodd" d="M3 10.5C3.82843 10.5 4.5 9.82843 4.5 9C4.5 8.17157 3.82843 7.5 3 7.5C2.17157 7.5 1.5 8.17157 1.5 9C1.5 9.82843 2.17157 10.5 3 10.5Z" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/> </svg> <div class="menu-option-popout"></div>'.PHP_EOL; And it is only HTML stored inside database, so there are many things to look at : Any bad escaped string can put the mess inside your displayed datas Any bad char's encoding can put the mess inside your displayed datas Maybe just call the equiv .toogle() JQuery method of ECMAScript on that .option-3 class will work and display your item as you wish.
SVG Glow Effect in CSS / JS / Jquery
I have a question regarding SVG Glow effect. I would like to set glow to group "suteki_201_" similar to the RED effect in this link: https://codepen.io/FelixRilling/pen/qzfoc (I would like his eyes to neon glow - blink animation in different color) i tried using different styles but no success... could you please help? My code for SVG image is: <svg version="1.1" id="Marko" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 612 792" style="enable-background:new 0 0 612 792;" xml:space="preserve"> <style type="text/css"> .dane0{fill:#D3D3D3;} .dane1{fill:#B0B0B0;} .dane2{fill:#303347;} .dane3{opacity:0.54;fill:#4A4D67;} .dane4{opacity:0.54;fill:#5F6380;} .dane5{opacity:0.37;fill:#010101;} .dane6{opacity:0.67;fill:#4A4D67;} .dane7{fill:#010101;} .dane8{opacity:0.72;} .dane9{fill:#77CFE2;} .dane10{fill:#9BD9E2;} .dane11{fill:#FBF5A9;} .dane12{fill:#C6C473;} .dane13{fill:#A3A061;} .dane14{opacity:0.67;} .dane15{fill:#242733;} .dane16{opacity:0.25;fill:#17191F;} </style> <g id="suteki_202_"> <path id="suteki_73_" class="dane0" d="M176.5,174.3c-2.7-0.1-3.3,4.8-3.3,4.8c4,9.4,12.2,16.7,12.2,16.7c-3.9-15.1-1.6-17.3-1.6-17.3 S179.2,174.4,176.5,174.3z"/> <path id="suteki_72_" class="dane1" d="M175.8,179.7c2.4,5.7,6.4,10.7,9.2,13.6c0.2,0.7,0.4,1.5,0.6,2.3c0,0-8.2-7.3-12.2-16.7 c0,0,0.6-4.8,3.3-4.8c0.7,0,1.6,0.3,2.4,0.8C176.4,174.9,175.8,179.7,175.8,179.7z"/> <g id="suteki_283_"> <path id="suteki_71_" class="dane2" d="M184.3,207.6c0,0-27.3-29.9-16.4-28.8c55.5,7.9,104.7,50.7,104.7,50.7s21.9,18.8,25.8,65.7 c0,0-13.3,38.9-54.7,46.1c0,0-23.2-3.4-37-16.4c0,0,13.9,19.5,44.4,37.1c0,0,15.2,5.9,56.7-30.5l2.3-16.8c0,0,7.4-15.6,36.7-24.6 c0,0,35.4,8.4,66.1-6.2c31.9-15.2,36.7-29.2,23-65.6c-44.6-27.2-121.9-61.5-121.9-61.5c-71.9-16.4-184.5-11.7-184.5-11.7 S129.6,184.2,184.3,207.6z"/> </g> <path id="suteki_70_" class="dane3" d="M350.8,226.7c0,0-6.3,11.2-16.2,8.6c-9.9-2.6-30.8-30.7-30.8-30.7s22.2,21.6,31.5,23.7 C344.5,230.6,350.8,226.7,350.8,226.7z"/> <path id="suteki_69_" class="dane4" d="M340,239.8c0,0-6.9,8.6-22.1-1.7c-11.1-7.5-13.5-22.3-27-43.2c17.3,18.7,20.4,30.1,30.6,37.1 C335.4,241.6,340,239.8,340,239.8z"/> <path id="suteki_68_" class="dane5" d="M362.1,227.6C349.9,239,326,252.3,326,252.3s2.7,16-0.5,22c-3.3,6-8.5,3.1-8.5,3.1 s-4.9-25.6-32.8-54.7c-86-75.6-118-43.7-118-43.7c55.1,4.4,106.6,50.4,106.6,50.4s21.9,18.8,25.8,65.7c0,0-13.3,38.9-54.7,46.1 c0,0-23.2-3.4-37-16.4c0,0,13.9,19.5,44.4,37.1c0,0,15.2,5.9,56.7-30.5l2.3-16.8c0,0,7.4-15.6,36.7-24.6c0,0,32.8,7,64.1-6.3 c0,0,6.3-2.3,19.2-10.5C411.1,257.1,362.1,227.6,362.1,227.6z"/> <path id="suteki_67_" class="dane6" d="M165.3,162.3c54.2-4.7,122.7,27.2,122.7,27.2l62.5,11.8c0,0,1.1,10.7,5.7,22.4l74.1,49.6 c14.3-10.9,15.8-26.6,5.7-55c0,0-29.6-20-121.9-61.5c-68.6-16.3-184.5-11.7-184.5-11.7s0,39.1,54.7,62.5 C184.3,207.6,135.6,168.3,165.3,162.3z"/> <path id="suteki_66_" class="dane7" d="M356.2,223.7l-22.2,0.2l-46-34.4l10.2,1.9c0,0-0.4-1.7,2.6-5c0,0-1.6,3.9-0.1,5.4l5,0.9 c0,0-0.3-5.1,3.3-6.8c0,0-2.8,3.1-0.2,7.4l41.8,7.7L356.2,223.7z"/> <g id="suteki_201_"> <g id="suteki_276_" class="dane8"> <path id="suteki_65_" class="dane9" d="M330,211.5c5.5,0,10-4.5,10-10c0-0.9-0.2-1.7-0.4-2.6l-17.7-3.2c-1.2,1.7-1.9,3.6-1.9,5.8 C320,207,324.5,211.5,330,211.5z"/> </g> <g id="suteki_274_" class="dane8"> <path id="suteki_64_" class="dane9" d="M329,207.9c3.5,0,6.3-2.8,6.3-6.3c0-0.6-0.1-1.1-0.2-1.6l-11.2-2.1c-0.8,1-1.2,2.3-1.2,3.7 C322.6,205.1,325.5,207.9,329,207.9z"/> </g> <g id="suteki_272_" class="dane8"> <path id="suteki_63_" class="dane10" d="M334,204.7c2,0,3.6-1.6,3.6-3.6c0-0.3-0.1-0.6-0.1-0.9l-6.4-1.2c-0.4,0.6-0.7,1.3-0.7,2.1 C330.4,203.1,332,204.7,334,204.7z"/> </g> </g> <path id="suteki_62_" class="dane0" d="M190.3,174c-3.9-0.1-4.8,6.9-4.8,6.9c5.8,13.6,17.7,24.2,17.7,24.2 c-5.6-21.8-2.4-25.1-2.4-25.1S194.2,174.1,190.3,174z"/> <path id="suteki_61_" class="dane1" d="M189.3,181.8c3.5,8.3,9.3,15.4,13.3,19.8c0.2,1.1,0.5,2.1,0.8,3.3c0,0-11.9-10.6-17.7-24.2 c0,0,0.8-7,4.8-6.9c1,0,2.3,0.5,3.5,1.1C190.1,174.9,189.3,181.8,189.3,181.8z"/> <path id="suteki_60_" class="dane11" d="M434.8,269.4c0,0-53.7-41.9-72.8-50.3c0,0-1.9-16-5.8-24c0,0-11.9-12.2-69.2-10.5 c0,0-82.3-34-131.3-23.3l3.3-2c51.3-8.3,128.8,21.7,128.8,21.7c37.1-2.1,72.2,8.4,72.2,8.4c5.2,8.8,7.2,26.4,7.2,26.4 c25.4,12.8,71,49.8,71,49.8S436.7,267.6,434.8,269.4z"/> <g id="suteki_266_"> <path id="suteki_59_" class="dane12" d="M364.8,217c0,0-1.2-17-6.4-25.8c0,0-29-11.4-71-8.1c0,0-46.3-16.9-87.3-23l-2.5,1.1 c41,5.9,89.4,23.5,89.4,23.5c57.3-1.8,69.2,10.5,69.2,10.5c3.9,7.9,5.8,24,5.8,24c19.1,8.5,72.8,50.3,72.8,50.3 c2-2.1,3.4-3.8,3.4-3.8S390.2,229.7,364.8,217z"/> <path id="suteki_58_" class="dane13" d="M157.2,160.5l-1.5,0.9c14.5-2.6,25.6-2.6,41.9-0.2l2.5-1.1 C184.2,157.7,168.1,158,157.2,160.5z"/> </g> <g id="suteki_263_" class="dane14"> <path id="suteki_57_" class="dane15" d="M363.9,217.8c0,0-1.2-17-6.4-25.8c0,0-29-11.4-71-8.1c0,0-46.3-16.9-87.3-23l-2.5,1.1 c41,5.9,89.4,23.5,89.4,23.5c57.3-1.8,69.2,10.5,69.2,10.5c3.9,7.9,5.8,24,5.8,24c19.1,8.5,72.8,50.3,72.8,50.3 c1.9-1.4,3.7-3.6,3.7-3.6S389.3,230.5,363.9,217.8z"/> <path id="suteki_56_" class="dane15" d="M155.7,161.4l-1.4,0.7c15-2.2,26.1-2.5,42.3-0.2l2.5-1.1 C183.3,158.5,166.6,158.9,155.7,161.4z"/> </g> <path id="suteki_55_" class="dane16" d="M436.2,218.6c11.6,39.4-17.1,70.5-93,56.6c-43.3,21-56.6,74.4-56.6,74.4l21.7-17.1l1.9-17.8 c0,0,7.4-15.6,36.7-24.6c0,0,33.3,7.7,64.6-5.6C417.2,280.4,460.8,270.4,436.2,218.6z"/> <path id="suteki_54_" class="dane5" d="M350.6,201c-2.8-0.5-62.6-11.5-62.6-11.5c-37.3-15.4-69.2-25-99.9-27.2 c-55.8-5.4-27.8,23.7-5.3,44.3l1.5,1c0,0-54.7-46.5-7.6-44.2c55-0.5,111.3,29.1,111.3,29.1l58.9,11.7c0,0,2.6,7,3.6,19.4l75.7,52.2 c1.8-1.2,2.7-1.7,4.2-2.7l-74.1-49.6C353.2,211.6,350.6,201,350.6,201z"/> <path id="suteki_53_" class="dane7" d="M430.9,273.3c0,0-23.3,22.1-72.6,18.3c0,0,51.7-6.3,62.4-24.7L430.9,273.3z"/> <g id="suteki_252_"> <path id="suteki_51_" class="dane0" d="M270.2,365.1c-2.5,1.2-4.9-3-4.9-3c-0.1-10.2,4.5-20.2,4.5-20.2c2.5,15.4,5.4,16.5,5.4,16.5 S272.7,363.9,270.2,365.1z"/> <path id="suteki_50_" class="dane0" d="M308.3,339.8c-2.6,3.1-8.6-1-8.6-1c-6.5-13.8-6.5-30.2-6.5-30.2c13,19.3,17.7,19,17.7,19 S310.9,336.7,308.3,339.8z"/> <path id="suteki_49_" class="dane0" d="M291.4,358.6c-2.9,2.8-8.4-1.9-8.4-1.9c-5-14.4-3.3-30.7-3.3-30.7 c11,20.5,15.6,20.8,15.6,20.8S294.3,355.7,291.4,358.6z"/> <path id="suteki_48_" class="dane1" d="M301.9,335.5c-3.9-8.2-5.4-17.4-6.1-23.5c-0.8-1.1-1.6-2.2-2.4-3.4c0,0,0,16.4,6.5,30.2 c0,0,6,4.2,8.6,1c0.6-0.7,1-1.7,1.4-2.9C307.1,339.1,301.9,335.5,301.9,335.5z"/> <path id="suteki_47_" class="dane1" d="M267.4,360.5c-0.1-6.2,1.6-12.3,3-16.2c-0.1-0.7-0.3-1.5-0.4-2.3c0,0-4.6,10-4.5,20.2 c0,0,2.5,4.2,4.9,3c0.7-0.3,1.3-0.9,1.9-1.7C269.9,364.6,267.4,360.5,267.4,360.5z"/> <path id="suteki_46_" class="dane1" d="M284.9,353.3c-3.1-8.8-3.6-18.3-3.6-24.3c-0.5-1-1.1-2-1.7-3.1c0,0-1.7,16.3,3.3,30.7 c0,0,5.5,4.8,8.4,1.9c0.8-0.8,1.4-2,1.9-3.3C290.4,358,284.9,353.3,284.9,353.3z"/> </g> </g> </svg> Thank you for your time!!!
Here is my solution, Its not perfect, but you can play with the colors,opacity and timings. To get the blur efects, I added Gaussian blur to svg More info on Gauusian Blur effect here here <defs> <filter id="f1"> <feGaussianBlur in="SourceGraphic" stdDeviation="3" /> </filter> </defs> Then, to apply the blur to any path / g add filter="url(#f1)". Notice the id: ( f1 ) I had to apply the filter to suteki_65_ to make the outer circle glow and also had to duplicate the circle in order to retain the shape of the eye, as the filter effect blurs the shape. And finally I applied CSS animation, to suteki_65_ in order to make it blink by changing the opacity. #keyframes blink { 0% { opacity: 1; fill:red; } 25% { opacity: 0.5; fill:pink; } 50% { opacity: 1; fill:orange; } 75% { opacity: 0.5; fill:blue; } 100% { opacity:1; fill:yellow; } } #suteki_65_ { animation: blink 1500ms infinite; } #suteki_65a_ { opacity: 0.3; } <svg version="1.1" id="Marko" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 612 792" style="enable-background:new 0 0 612 792;" xml:space="preserve"> <defs> <filter id="f1"> <feGaussianBlur in="SourceGraphic" stdDeviation="3" /> </filter> </defs> <style type="text/css"> .dane0{fill:#D3D3D3;} .dane1{fill:#B0B0B0;} .dane2{fill:#303347;} .dane3{opacity:0.54;fill:#4A4D67;} .dane4{opacity:0.54;fill:#5F6380;} .dane5{opacity:0.37;fill:#010101;} .dane6{opacity:0.67;fill:#4A4D67;} .dane7{fill:#010101;} .dane8{opacity:0.72;} .dane9{fill:#77CFE2;} .dane10{fill:#9BD9E2;} .dane11{fill:#FBF5A9;} .dane12{fill:#C6C473;} .dane13{fill:#A3A061;} .dane14{opacity:0.67;} .dane15{fill:#242733;} .dane16{opacity:0.25;fill:#17191F;} </style> <g id="suteki_202_"> <path id="suteki_73_" class="dane0" d="M176.5,174.3c-2.7-0.1-3.3,4.8-3.3,4.8c4,9.4,12.2,16.7,12.2,16.7c-3.9-15.1-1.6-17.3-1.6-17.3 S179.2,174.4,176.5,174.3z"/> <path id="suteki_72_" class="dane1" d="M175.8,179.7c2.4,5.7,6.4,10.7,9.2,13.6c0.2,0.7,0.4,1.5,0.6,2.3c0,0-8.2-7.3-12.2-16.7 c0,0,0.6-4.8,3.3-4.8c0.7,0,1.6,0.3,2.4,0.8C176.4,174.9,175.8,179.7,175.8,179.7z"/> <g id="suteki_283_"> <path id="suteki_71_" class="dane2" d="M184.3,207.6c0,0-27.3-29.9-16.4-28.8c55.5,7.9,104.7,50.7,104.7,50.7s21.9,18.8,25.8,65.7 c0,0-13.3,38.9-54.7,46.1c0,0-23.2-3.4-37-16.4c0,0,13.9,19.5,44.4,37.1c0,0,15.2,5.9,56.7-30.5l2.3-16.8c0,0,7.4-15.6,36.7-24.6 c0,0,35.4,8.4,66.1-6.2c31.9-15.2,36.7-29.2,23-65.6c-44.6-27.2-121.9-61.5-121.9-61.5c-71.9-16.4-184.5-11.7-184.5-11.7 S129.6,184.2,184.3,207.6z"/> </g> <path id="suteki_70_" class="dane3" d="M350.8,226.7c0,0-6.3,11.2-16.2,8.6c-9.9-2.6-30.8-30.7-30.8-30.7s22.2,21.6,31.5,23.7 C344.5,230.6,350.8,226.7,350.8,226.7z"/> <path id="suteki_69_" class="dane4" d="M340,239.8c0,0-6.9,8.6-22.1-1.7c-11.1-7.5-13.5-22.3-27-43.2c17.3,18.7,20.4,30.1,30.6,37.1 C335.4,241.6,340,239.8,340,239.8z"/> <path id="suteki_68_" class="dane5" d="M362.1,227.6C349.9,239,326,252.3,326,252.3s2.7,16-0.5,22c-3.3,6-8.5,3.1-8.5,3.1 s-4.9-25.6-32.8-54.7c-86-75.6-118-43.7-118-43.7c55.1,4.4,106.6,50.4,106.6,50.4s21.9,18.8,25.8,65.7c0,0-13.3,38.9-54.7,46.1 c0,0-23.2-3.4-37-16.4c0,0,13.9,19.5,44.4,37.1c0,0,15.2,5.9,56.7-30.5l2.3-16.8c0,0,7.4-15.6,36.7-24.6c0,0,32.8,7,64.1-6.3 c0,0,6.3-2.3,19.2-10.5C411.1,257.1,362.1,227.6,362.1,227.6z"/> <path id="suteki_67_" class="dane6" d="M165.3,162.3c54.2-4.7,122.7,27.2,122.7,27.2l62.5,11.8c0,0,1.1,10.7,5.7,22.4l74.1,49.6 c14.3-10.9,15.8-26.6,5.7-55c0,0-29.6-20-121.9-61.5c-68.6-16.3-184.5-11.7-184.5-11.7s0,39.1,54.7,62.5 C184.3,207.6,135.6,168.3,165.3,162.3z"/> <path id="suteki_66_" class="dane7" d="M356.2,223.7l-22.2,0.2l-46-34.4l10.2,1.9c0,0-0.4-1.7,2.6-5c0,0-1.6,3.9-0.1,5.4l5,0.9 c0,0-0.3-5.1,3.3-6.8c0,0-2.8,3.1-0.2,7.4l41.8,7.7L356.2,223.7z"/> <g id="suteki_201_" > <g id="suteki_276_" class="dane8"> <path id="suteki_65a_" class="dane9" d="M330,211.5c5.5,0,10-4.5,10-10c0-0.9-0.2-1.7-0.4-2.6l-17.7-3.2c-1.2,1.7-1.9,3.6-1.9,5.8 C320,207,324.5,211.5,330,211.5z"/> <path id="suteki_65_" filter="url(#f1)" class="dane9" d="M330,211.5c5.5,0,10-4.5,10-10c0-0.9-0.2-1.7-0.4-2.6l-17.7-3.2c-1.2,1.7-1.9,3.6-1.9,5.8 C320,207,324.5,211.5,330,211.5z"/> </g> <g id="suteki_274_" class="dane8"> <path id="suteki_64_" class="dane9" d="M329,207.9c3.5,0,6.3-2.8,6.3-6.3c0-0.6-0.1-1.1-0.2-1.6l-11.2-2.1c-0.8,1-1.2,2.3-1.2,3.7 C322.6,205.1,325.5,207.9,329,207.9z"/> </g> <g id="suteki_272_" class="dane8"> <path id="suteki_63_" class="dane10" d="M334,204.7c2,0,3.6-1.6,3.6-3.6c0-0.3-0.1-0.6-0.1-0.9l-6.4-1.2c-0.4,0.6-0.7,1.3-0.7,2.1 C330.4,203.1,332,204.7,334,204.7z"/> </g> </g> <path id="suteki_62_" class="dane0" d="M190.3,174c-3.9-0.1-4.8,6.9-4.8,6.9c5.8,13.6,17.7,24.2,17.7,24.2 c-5.6-21.8-2.4-25.1-2.4-25.1S194.2,174.1,190.3,174z"/> <path id="suteki_61_" class="dane1" d="M189.3,181.8c3.5,8.3,9.3,15.4,13.3,19.8c0.2,1.1,0.5,2.1,0.8,3.3c0,0-11.9-10.6-17.7-24.2 c0,0,0.8-7,4.8-6.9c1,0,2.3,0.5,3.5,1.1C190.1,174.9,189.3,181.8,189.3,181.8z"/> <path id="suteki_60_" class="dane11" d="M434.8,269.4c0,0-53.7-41.9-72.8-50.3c0,0-1.9-16-5.8-24c0,0-11.9-12.2-69.2-10.5 c0,0-82.3-34-131.3-23.3l3.3-2c51.3-8.3,128.8,21.7,128.8,21.7c37.1-2.1,72.2,8.4,72.2,8.4c5.2,8.8,7.2,26.4,7.2,26.4 c25.4,12.8,71,49.8,71,49.8S436.7,267.6,434.8,269.4z"/> <g id="suteki_266_"> <path id="suteki_59_" class="dane12" d="M364.8,217c0,0-1.2-17-6.4-25.8c0,0-29-11.4-71-8.1c0,0-46.3-16.9-87.3-23l-2.5,1.1 c41,5.9,89.4,23.5,89.4,23.5c57.3-1.8,69.2,10.5,69.2,10.5c3.9,7.9,5.8,24,5.8,24c19.1,8.5,72.8,50.3,72.8,50.3 c2-2.1,3.4-3.8,3.4-3.8S390.2,229.7,364.8,217z"/> <path id="suteki_58_" class="dane13" d="M157.2,160.5l-1.5,0.9c14.5-2.6,25.6-2.6,41.9-0.2l2.5-1.1 C184.2,157.7,168.1,158,157.2,160.5z"/> </g> <g id="suteki_263_" class="dane14"> <path id="suteki_57_" class="dane15" d="M363.9,217.8c0,0-1.2-17-6.4-25.8c0,0-29-11.4-71-8.1c0,0-46.3-16.9-87.3-23l-2.5,1.1 c41,5.9,89.4,23.5,89.4,23.5c57.3-1.8,69.2,10.5,69.2,10.5c3.9,7.9,5.8,24,5.8,24c19.1,8.5,72.8,50.3,72.8,50.3 c1.9-1.4,3.7-3.6,3.7-3.6S389.3,230.5,363.9,217.8z"/> <path id="suteki_56_" class="dane15" d="M155.7,161.4l-1.4,0.7c15-2.2,26.1-2.5,42.3-0.2l2.5-1.1 C183.3,158.5,166.6,158.9,155.7,161.4z"/> </g> <path id="suteki_55_" class="dane16" d="M436.2,218.6c11.6,39.4-17.1,70.5-93,56.6c-43.3,21-56.6,74.4-56.6,74.4l21.7-17.1l1.9-17.8 c0,0,7.4-15.6,36.7-24.6c0,0,33.3,7.7,64.6-5.6C417.2,280.4,460.8,270.4,436.2,218.6z"/> <path id="suteki_54_" class="dane5" d="M350.6,201c-2.8-0.5-62.6-11.5-62.6-11.5c-37.3-15.4-69.2-25-99.9-27.2 c-55.8-5.4-27.8,23.7-5.3,44.3l1.5,1c0,0-54.7-46.5-7.6-44.2c55-0.5,111.3,29.1,111.3,29.1l58.9,11.7c0,0,2.6,7,3.6,19.4l75.7,52.2 c1.8-1.2,2.7-1.7,4.2-2.7l-74.1-49.6C353.2,211.6,350.6,201,350.6,201z"/> <path id="suteki_53_" class="dane7" d="M430.9,273.3c0,0-23.3,22.1-72.6,18.3c0,0,51.7-6.3,62.4-24.7L430.9,273.3z"/> <g id="suteki_252_"> <path id="suteki_51_" class="dane0" d="M270.2,365.1c-2.5,1.2-4.9-3-4.9-3c-0.1-10.2,4.5-20.2,4.5-20.2c2.5,15.4,5.4,16.5,5.4,16.5 S272.7,363.9,270.2,365.1z"/> <path id="suteki_50_" class="dane0" d="M308.3,339.8c-2.6,3.1-8.6-1-8.6-1c-6.5-13.8-6.5-30.2-6.5-30.2c13,19.3,17.7,19,17.7,19 S310.9,336.7,308.3,339.8z"/> <path id="suteki_49_" class="dane0" d="M291.4,358.6c-2.9,2.8-8.4-1.9-8.4-1.9c-5-14.4-3.3-30.7-3.3-30.7 c11,20.5,15.6,20.8,15.6,20.8S294.3,355.7,291.4,358.6z"/> <path id="suteki_48_" class="dane1" d="M301.9,335.5c-3.9-8.2-5.4-17.4-6.1-23.5c-0.8-1.1-1.6-2.2-2.4-3.4c0,0,0,16.4,6.5,30.2 c0,0,6,4.2,8.6,1c0.6-0.7,1-1.7,1.4-2.9C307.1,339.1,301.9,335.5,301.9,335.5z"/> <path id="suteki_47_" class="dane1" d="M267.4,360.5c-0.1-6.2,1.6-12.3,3-16.2c-0.1-0.7-0.3-1.5-0.4-2.3c0,0-4.6,10-4.5,20.2 c0,0,2.5,4.2,4.9,3c0.7-0.3,1.3-0.9,1.9-1.7C269.9,364.6,267.4,360.5,267.4,360.5z"/> <path id="suteki_46_" class="dane1" d="M284.9,353.3c-3.1-8.8-3.6-18.3-3.6-24.3c-0.5-1-1.1-2-1.7-3.1c0,0-1.7,16.3,3.3,30.7 c0,0,5.5,4.8,8.4,1.9c0.8-0.8,1.4-2,1.9-3.3C290.4,358,284.9,353.3,284.9,353.3z"/> </g> </g> </svg>
How to prepare SVG code from Inkscape for Angular JS / Angular Material app directive template
I forked a CodePen app by the Angular Material Team and am trying to replace their user avatar icon with an icon I made in Inkscape. This is their code, in an app directive template field: <svg class="user-avatar" viewBox="0 0 128 128" height="64" width="64" pointer-events="none" display="block" > <path fill="#FF8A80" d="M0 0h128v128H0z"/> <path fill="#FFE0B2" d="M36.3 94.8c6.4 7.3 16.2 12.1 27.3 12.4 10.7-.3 20.3-4.7 26.7-11.6l.2.1c-17-13.3-12.9-23.4-8.5-28.6 1.3-1.2 2.8-2.5 4.4-3.9l13.1-11c1.5-1.2 2.6-3 2.9-5.1.6-4.4-2.5-8.4-6.9-9.1-1.5-.2-3 0-4.3.6-.3-1.3-.4-2.7-1.6-3.5-1.4-.9-2.8-1.7-4.2-2.5-7.1-3.9-14.9-6.6-23-7.9-5.4-.9-11-1.2-16.1.7-3.3 1.2-6.1 3.2-8.7 5.6-1.3 1.2-2.5 2.4-3.7 3.7l-1.8 1.9c-.3.3-.5.6-.8.8-.1.1-.2 0-.4.2.1.2.1.5.1.6-1-.3-2.1-.4-3.2-.2-4.4.6-7.5 4.7-6.9 9.1.3 2.1 1.3 3.8 2.8 5.1l11 9.3c1.8 1.5 3.3 3.8 4.6 5.7 1.5 2.3 2.8 4.9 3.5 7.6 1.7 6.8-.8 13.4-5.4 18.4-.5.6-1.1 1-1.4 1.7-.2.6-.4 1.3-.6 2-.4 1.5-.5 3.1-.3 4.6.4 3.1 1.8 6.1 4.1 8.2 3.3 3 8 4 12.4 4.5 5.2.6 10.5.7 15.7.2 4.5-.4 9.1-1.2 13-3.4 5.6-3.1 9.6-8.9 10.5-15.2M76.4 46c.9 0 1.6.7 1.6 1.6 0 .9-.7 1.6-1.6 1.6-.9 0-1.6-.7-1.6-1.6-.1-.9.7-1.6 1.6-1.6zm-25.7 0c.9 0 1.6.7 1.6 1.6 0 .9-.7 1.6-1.6 1.6-.9 0-1.6-.7-1.6-1.6-.1-.9.7-1.6 1.6-1.6z"/> <path fill="#E0F7FA" d="M105.3 106.1c-.9-1.3-1.3-1.9-1.3-1.9l-.2-.3c-.6-.9-1.2-1.7-1.9-2.4-3.2-3.5-7.3-5.4-11.4-5.7 0 0 .1 0 .1.1l-.2-.1c-6.4 6.9-16 11.3-26.7 11.6-11.2-.3-21.1-5.1-27.5-12.6-.1.2-.2.4-.2.5-3.1.9-6 2.7-8.4 5.4l-.2.2s-.5.6-1.5 1.7c-.9 1.1-2.2 2.6-3.7 4.5-3.1 3.9-7.2 9.5-11.7 16.6-.9 1.4-1.7 2.8-2.6 4.3h109.6c-3.4-7.1-6.5-12.8-8.9-16.9-1.5-2.2-2.6-3.8-3.3-5z"/> <circle fill="#444" cx="76.3" cy="47.5" r="2"/> <circle fill="#444" cx="50.7" cy="47.6" r="2"/> <path fill="#444" d="M48.1 27.4c4.5 5.9 15.5 12.1 42.4 8.4-2.2-6.9-6.8-12.6-12.6-16.4C95.1 20.9 92 10 92 10c-1.4 5.5-11.1 4.4-11.1 4.4H62.1c-1.7-.1-3.4 0-5.2.3-12.8 1.8-22.6 11.1-25.7 22.9 10.6-1.9 15.3-7.6 16.9-10.2z"/> </svg> The code I see in Inkscape's XML editor or when opening the SVG file with Notepad++ is quite a bit different: <svg xmlns:osb="http://www.openswatchbook.org/uri/2009/osb" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:cc="http://creativecommons.org/ns#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" width="210mm" height="297mm" viewBox="0 0 744.09448819 1052.3622047" id="svg2" version="1.1" inkscape:version="0.91 r13725" sodipodi:docname="rb icon.svg"> <defs id="defs4"> <linearGradient id="linearGradient4179" osb:paint="solid"> <stop style="stop-color:#000000;stop-opacity:1;" offset="0" id="stop4181" /> </linearGradient> </defs> <sodipodi:namedview id="base" pagecolor="#ffffff" bordercolor="#666666" borderopacity="1.0" inkscape:pageopacity="0.0" inkscape:pageshadow="2" inkscape:zoom="1.6394886" inkscape:cx="-607.16873" inkscape:cy="903.12934" inkscape:document-units="px" inkscape:current-layer="layer1" showgrid="false" showborder="false" showguides="false" inkscape:window-width="1536" inkscape:window-height="801" inkscape:window-x="-8" inkscape:window-y="-8" inkscape:window-maximized="1" /> <metadata id="metadata7"> <rdf:RDF> <cc:Work rdf:about=""> <dc:format>image/svg+xml</dc:format> <dc:type rdf:resource="http://purl.org/dc/dcmitype/StillImage" /> <dc:title></dc:title> </cc:Work> </rdf:RDF> </metadata> <g inkscape:label="Layer 1" inkscape:groupmode="layer" id="layer1"> <circle style="fill:#009cf0;fill-rule:evenodd;stroke:#000000;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;fill-opacity:1" id="path3336" cx="-602.85712" cy="69.505058" r="151.42857" /> <rect style="fill:#ffffff;fill-opacity:1" id="rect4158" width="193.80003" height="144.85301" x="-699.19604" y="24.652056" /> <rect style="fill:#ffffff;fill-opacity:1" id="rect4162" width="155.71428" height="155.71428" x="373.2374" y="322.82352" transform="matrix(-0.70710678,0.70710678,-0.70710678,-0.70710678,0,0)" /> <rect style="fill:#ffffff;fill-opacity:1" id="rect4173" width="19.512196" height="71.951218" x="-682.92682" y="-47.637794" ry="9.7560978" /> <text xml:space="preserve" style="font-style:normal;font-weight:normal;font-size:40px;line-height:125%;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#009cf0;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;" x="-635.36584" y="20.654888" id="text4195" sodipodi:linespacing="127%"><tspan sodipodi:role="line" id="tspan4197" x="-635.36584" y="20.654888" style="line-height:127%;-inkscape-font-specification:'sans-serif, Normal';font-family:sans-serif;font-weight:normal;font-style:normal;font-stretch:normal;font-variant:normal;font-size:60px;text-anchor:start;text-align:start;writing-mode:lr;fill:#009cf0;fill-opacity:1">R</tspan></text> <text xml:space="preserve" style="font-style:normal;font-weight:normal;font-size:40px;line-height:125%;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#009cf0;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" x="-597.46344" y="54.801228" id="text4199" sodipodi:linespacing="125%"><tspan sodipodi:role="line" id="tspan4201" x="-597.46344" y="54.801228" style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:60px;line-height:125%;font-family:sans-serif;-inkscape-font-specification:'sans-serif, Normal';text-align:start;writing-mode:lr-tb;text-anchor:start;fill:#009cf0;fill-opacity:1">B</tspan></text> <path style="fill:#009cf0;fill-opacity:1;stroke:none;stroke-width:0.67507702;stroke-opacity:1" d="m -505.412,36.002405 20.333,0 0,38.1298 -20.333,0 z" id="rect4207" inkscape:connector-curvature="0" /> <path style="fill:#009cf0;fill-opacity:1;stroke:none;stroke-width:0.67507702;stroke-opacity:1" d="m -719.52832,35.102183 20.337,0 0,38.1298 -20.337,0 z" id="rect4207-6" inkscape:connector-curvature="0" /> <rect style="fill:#ffffff;fill-opacity:1;stroke:#000000;stroke-opacity:1;stroke-width:1;stroke-miterlimit:4;stroke-dasharray:none" id="rect4307" width="47.575813" height="54.895168" x="-626.08392" y="115.31452" /> </g> </svg> and when I try replacing the default user avatar SVG with mine it breaks the website. The web console says Uncaught SyntaxError: Invalid or unexpected token. Here's the directive with the original SVG: app.directive('userAvatar', function() { return { replace: true, template: '<svg class="user-avatar" viewBox="0 0 128 128" height="64" width="64" pointer-events="none" display="block" > <path fill="#FF8A80" d="M0 0h128v128H0z"/> <path fill="#FFE0B2" d="M36.3 94.8c6.4 7.3 16.2 12.1 27.3 12.4 10.7-.3 20.3-4.7 26.7-11.6l.2.1c-17-13.3-12.9-23.4-8.5-28.6 1.3-1.2 2.8-2.5 4.4-3.9l13.1-11c1.5-1.2 2.6-3 2.9-5.1.6-4.4-2.5-8.4-6.9-9.1-1.5-.2-3 0-4.3.6-.3-1.3-.4-2.7-1.6-3.5-1.4-.9-2.8-1.7-4.2-2.5-7.1-3.9-14.9-6.6-23-7.9-5.4-.9-11-1.2-16.1.7-3.3 1.2-6.1 3.2-8.7 5.6-1.3 1.2-2.5 2.4-3.7 3.7l-1.8 1.9c-.3.3-.5.6-.8.8-.1.1-.2 0-.4.2.1.2.1.5.1.6-1-.3-2.1-.4-3.2-.2-4.4.6-7.5 4.7-6.9 9.1.3 2.1 1.3 3.8 2.8 5.1l11 9.3c1.8 1.5 3.3 3.8 4.6 5.7 1.5 2.3 2.8 4.9 3.5 7.6 1.7 6.8-.8 13.4-5.4 18.4-.5.6-1.1 1-1.4 1.7-.2.6-.4 1.3-.6 2-.4 1.5-.5 3.1-.3 4.6.4 3.1 1.8 6.1 4.1 8.2 3.3 3 8 4 12.4 4.5 5.2.6 10.5.7 15.7.2 4.5-.4 9.1-1.2 13-3.4 5.6-3.1 9.6-8.9 10.5-15.2M76.4 46c.9 0 1.6.7 1.6 1.6 0 .9-.7 1.6-1.6 1.6-.9 0-1.6-.7-1.6-1.6-.1-.9.7-1.6 1.6-1.6zm-25.7 0c.9 0 1.6.7 1.6 1.6 0 .9-.7 1.6-1.6 1.6-.9 0-1.6-.7-1.6-1.6-.1-.9.7-1.6 1.6-1.6z"/> <path fill="#E0F7FA" d="M105.3 106.1c-.9-1.3-1.3-1.9-1.3-1.9l-.2-.3c-.6-.9-1.2-1.7-1.9-2.4-3.2-3.5-7.3-5.4-11.4-5.7 0 0 .1 0 .1.1l-.2-.1c-6.4 6.9-16 11.3-26.7 11.6-11.2-.3-21.1-5.1-27.5-12.6-.1.2-.2.4-.2.5-3.1.9-6 2.7-8.4 5.4l-.2.2s-.5.6-1.5 1.7c-.9 1.1-2.2 2.6-3.7 4.5-3.1 3.9-7.2 9.5-11.7 16.6-.9 1.4-1.7 2.8-2.6 4.3h109.6c-3.4-7.1-6.5-12.8-8.9-16.9-1.5-2.2-2.6-3.8-3.3-5z"/> <circle fill="#444" cx="76.3" cy="47.5" r="2"/> <circle fill="#444" cx="50.7" cy="47.6" r="2"/> <path fill="#444" d="M48.1 27.4c4.5 5.9 15.5 12.1 42.4 8.4-2.2-6.9-6.8-12.6-12.6-16.4C95.1 20.9 92 10 92 10c-1.4 5.5-11.1 4.4-11.1 4.4H62.1c-1.7-.1-3.4 0-5.2.3-12.8 1.8-22.6 11.1-25.7 22.9 10.6-1.9 15.3-7.6 16.9-10.2z"/> </svg>' }; }); I've tried a dozen variations on this, but using a 1-line style where I insert only class="user-avatar" into the new XML here is what the updated directive looks like: app.directive('userAvatar', function() { return { replace: true, //template: '<svg class="user-avatar" viewBox="0 0 128 128" height="64" width="64" pointer-events="none" display="block" > <path fill="#FF8A80" d="M0 0h128v128H0z"/> <path fill="#FFE0B2" d="M36.3 94.8c6.4 7.3 16.2 12.1 27.3 12.4 10.7-.3 20.3-4.7 26.7-11.6l.2.1c-17-13.3-12.9-23.4-8.5-28.6 1.3-1.2 2.8-2.5 4.4-3.9l13.1-11c1.5-1.2 2.6-3 2.9-5.1.6-4.4-2.5-8.4-6.9-9.1-1.5-.2-3 0-4.3.6-.3-1.3-.4-2.7-1.6-3.5-1.4-.9-2.8-1.7-4.2-2.5-7.1-3.9-14.9-6.6-23-7.9-5.4-.9-11-1.2-16.1.7-3.3 1.2-6.1 3.2-8.7 5.6-1.3 1.2-2.5 2.4-3.7 3.7l-1.8 1.9c-.3.3-.5.6-.8.8-.1.1-.2 0-.4.2.1.2.1.5.1.6-1-.3-2.1-.4-3.2-.2-4.4.6-7.5 4.7-6.9 9.1.3 2.1 1.3 3.8 2.8 5.1l11 9.3c1.8 1.5 3.3 3.8 4.6 5.7 1.5 2.3 2.8 4.9 3.5 7.6 1.7 6.8-.8 13.4-5.4 18.4-.5.6-1.1 1-1.4 1.7-.2.6-.4 1.3-.6 2-.4 1.5-.5 3.1-.3 4.6.4 3.1 1.8 6.1 4.1 8.2 3.3 3 8 4 12.4 4.5 5.2.6 10.5.7 15.7.2 4.5-.4 9.1-1.2 13-3.4 5.6-3.1 9.6-8.9 10.5-15.2M76.4 46c.9 0 1.6.7 1.6 1.6 0 .9-.7 1.6-1.6 1.6-.9 0-1.6-.7-1.6-1.6-.1-.9.7-1.6 1.6-1.6zm-25.7 0c.9 0 1.6.7 1.6 1.6 0 .9-.7 1.6-1.6 1.6-.9 0-1.6-.7-1.6-1.6-.1-.9.7-1.6 1.6-1.6z"/> <path fill="#E0F7FA" d="M105.3 106.1c-.9-1.3-1.3-1.9-1.3-1.9l-.2-.3c-.6-.9-1.2-1.7-1.9-2.4-3.2-3.5-7.3-5.4-11.4-5.7 0 0 .1 0 .1.1l-.2-.1c-6.4 6.9-16 11.3-26.7 11.6-11.2-.3-21.1-5.1-27.5-12.6-.1.2-.2.4-.2.5-3.1.9-6 2.7-8.4 5.4l-.2.2s-.5.6-1.5 1.7c-.9 1.1-2.2 2.6-3.7 4.5-3.1 3.9-7.2 9.5-11.7 16.6-.9 1.4-1.7 2.8-2.6 4.3h109.6c-3.4-7.1-6.5-12.8-8.9-16.9-1.5-2.2-2.6-3.8-3.3-5z"/> <circle fill="#444" cx="76.3" cy="47.5" r="2"/> <circle fill="#444" cx="50.7" cy="47.6" r="2"/> <path fill="#444" d="M48.1 27.4c4.5 5.9 15.5 12.1 42.4 8.4-2.2-6.9-6.8-12.6-12.6-16.4C95.1 20.9 92 10 92 10c-1.4 5.5-11.1 4.4-11.1 4.4H62.1c-1.7-.1-3.4 0-5.2.3-12.8 1.8-22.6 11.1-25.7 22.9 10.6-1.9 15.3-7.6 16.9-10.2z"/> </svg>' template: '<svg class="user-avatar" xmlns:osb="http://www.openswatchbook.org/uri/2009/osb" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:cc="http://creativecommons.org/ns#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" width="210mm" height="297mm" viewBox="0 0 744.09448819 1052.3622047" id="svg2" version="1.1" inkscape:version="0.91 r13725" sodipodi:docname="rb icon.svg"> <defs id="defs4"> <linearGradient id="linearGradient4179" osb:paint="solid"> <stop style="stop-color:#000000;stop-opacity:1;" offset="0" id="stop4181" /> </linearGradient> </defs> <sodipodi:namedview id="base" pagecolor="#ffffff" bordercolor="#666666" borderopacity="1.0" inkscape:pageopacity="0.0" inkscape:pageshadow="2" inkscape:zoom="1.6394886" inkscape:cx="-607.16873" inkscape:cy="903.12934" inkscape:document-units="px" inkscape:current-layer="layer1" showgrid="false" showborder="false" showguides="false" inkscape:window-width="1536" inkscape:window-height="801" inkscape:window-x="-8" inkscape:window-y="-8" inkscape:window-maximized="1" /> <metadata id="metadata7"> <rdf:RDF> <cc:Work rdf:about=""> <dc:format>image/svg+xml</dc:format> <dc:type rdf:resource="http://purl.org/dc/dcmitype/StillImage" /> <dc:title></dc:title> </cc:Work> </rdf:RDF> </metadata> <g inkscape:label="Layer 1" inkscape:groupmode="layer" id="layer1"> <circle style="fill:#009cf0;fill-rule:evenodd;stroke:#000000;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;fill-opacity:1" id="path3336" cx="-602.85712" cy="69.505058" r="151.42857" /> <rect style="fill:#ffffff;fill-opacity:1" id="rect4158" width="193.80003" height="144.85301" x="-699.19604" y="24.652056" /> <rect style="fill:#ffffff;fill-opacity:1" id="rect4162" width="155.71428" height="155.71428" x="373.2374" y="322.82352" transform="matrix(-0.70710678,0.70710678,-0.70710678,-0.70710678,0,0)" /> <rect style="fill:#ffffff;fill-opacity:1" id="rect4173" width="19.512196" height="71.951218" x="-682.92682" y="-47.637794" ry="9.7560978" /> <text xml:space="preserve" style="font-style:normal;font-weight:normal;font-size:40px;line-height:125%;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#009cf0;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;" x="-635.36584" y="20.654888" id="text4195" sodipodi:linespacing="127%"><tspan sodipodi:role="line" id="tspan4197" x="-635.36584" y="20.654888" style="line-height:127%;-inkscape-font-specification:'sans-serif, Normal';font-family:sans-serif;font-weight:normal;font-style:normal;font-stretch:normal;font-variant:normal;font-size:60px;text-anchor:start;text-align:start;writing-mode:lr;fill:#009cf0;fill-opacity:1">R</tspan></text> <text xml:space="preserve" style="font-style:normal;font-weight:normal;font-size:40px;line-height:125%;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#009cf0;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" x="-597.46344" y="54.801228" id="text4199" sodipodi:linespacing="125%"><tspan sodipodi:role="line" id="tspan4201" x="-597.46344" y="54.801228" style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:60px;line-height:125%;font-family:sans-serif;-inkscape-font-specification:'sans-serif, Normal';text-align:start;writing-mode:lr-tb;text-anchor:start;fill:#009cf0;fill-opacity:1">B</tspan></text> <path style="fill:#009cf0;fill-opacity:1;stroke:none;stroke-width:0.67507702;stroke-opacity:1" d="m -505.412,36.002405 20.333,0 0,38.1298 -20.333,0 z" id="rect4207" inkscape:connector-curvature="0" /> <path style="fill:#009cf0;fill-opacity:1;stroke:none;stroke-width:0.67507702;stroke-opacity:1" d="m -719.52832,35.102183 20.337,0 0,38.1298 -20.337,0 z" id="rect4207-6" inkscape:connector-curvature="0" /> <rect style="fill:#ffffff;fill-opacity:1;stroke:#000000;stroke-opacity:1;stroke-width:1;stroke-miterlimit:4;stroke-dasharray:none" id="rect4307" width="47.575813" height="54.895168" x="-626.08392" y="115.31452" /> </g> </svg>' }; }); You can reproduce the error by replacing the original directive with the one above in the CodePen app. Original: https://codepen.io/team/AngularMaterial/pen/mVxgbg Broken: https://codepen.io/hack-r/pen/LRdVPN
It is tipical " ,' problem You have ' inside Your string representing svg, change all ' to " and it will work. First of them in svg is 'sans-serif, Normal'. Your code breaks because string starts on ' and ends on first ' which is inside svg string so next part of code has error syntax.
jquery svg shift color animation
I'm trying to make an svg animation where the color of the svg slides to an other color. My goal is to make kind of a running light. I'm not trying to fade the fill color of the whole svg as seen in a lot of examples. Here is an example photo with a few frames of the animation. svg animation I've tried a few different technics but couldn't find a good one. This is how i did it with css keyframes: $.keyframe.define([{ name: 'shift', '0%': {'background-position':'0px'}, '2%': {'background-position':'6px'}, '4%': {'background-position':'12px'}, '6%': {'background-position':'18px'}, '8%': {'background-position':'24px'}, '10%': {'background-position':'30px'}, '12%': {'background-position':'36px'}, '14%': {'background-position':'42px'}, '16%': {'background-position':'48px'}, '18%': {'background-position':'54px'}, '20%': {'background-position':'60px'}, '22%': {'background-position':'66px'}, '24%': {'background-position':'72px'}, '26%': {'background-position':'78px'}, '28%': {'background-position':'84px'}, '30%': {'background-position':'90px'}, '32%': {'background-position':'96px'}, '34%': {'background-position':'102px'}, '36%': {'background-position':'108px'}, '38%': {'background-position':'114px'}, '40%': {'background-position':'120px'}, '42%': {'background-position':'126px'}, '44%': {'background-position':'132px'}, '46%': {'background-position':'138px'}, '48%': {'background-position':'144px'}, '50%': {'background-position':'150px'}, '52%': {'background-position':'156px'}, '54%': {'background-position':'162px'}, '56%': {'background-position':'168px'}, '58%': {'background-position':'174px'}, '60%': {'background-position':'180px'}, '62%': {'background-position':'186px'}, '64%': {'background-position':'192px'}, '66%': {'background-position':'198px'}, '68%': {'background-position':'204px'}, '70%': {'background-position':'210px'}, '72%': {'background-position':'216px'}, '74%': {'background-position':'222px'}, '76%': {'background-position':'228px'}, '78%': {'background-position':'234px'}, '80%': {'background-position':'240px'}, '82%': {'background-position':'246px'}, '84%': {'background-position':'252px'}, '86%': {'background-position':'258px'}, '88%': {'background-position':'264px'}, '90%': {'background-position':'270px'}, '92%': {'background-position':'276px'}, '94%': {'background-position':'282px'}, '96%': {'background-position':'288px'}, '98%': {'background-position':'294px'}, '100%': {'background-position':'300px'} }]); $('.ledbar').playKeyframe({ name: 'shift', duration: "4s", timingFunction: 'linear', delay: 0, direction: 'reverse', fillMode: 'forwards', }); The oranje part moves over the green. The svg was drawn so that every rectangle was just 6px further than the other one. this didn't work becouse of the smooth transition between two keyframes. This made the animation flicker. Maybe if the animation was in steps it would work but i don't know how this works and i thing there has to be a better way. So i'm wondering how i could liniear shift the color of the svg in to an other color. I've looked at svg masks etc. but couldn't figuer it out. This is the svg file, maybe its useful: <?xml version="1.0" encoding="utf-8"?> <!-- Generator: Adobe Illustrator 16.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) --> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> <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" width="403.5px" height="13px" viewBox="0 0 403.5 13" enable-background="new 0 0 403.5 13" xml:space="preserve"> <g id="Button_2_-_Green"> </g> <g> <path fill="#80A993" d="M0.017,0L1.5,0.002L1.484,13H0L0.017,0z"/> <path fill="#80A993" d="M6.017,0L7.5,0.002L7.484,13H6L6.017,0z"/> <path fill="#80A993" d="M12.017,0L13.5,0.002L13.484,13H12L12.017,0z"/> <path fill="#80A993" d="M18.017,0L19.5,0.002L19.484,13H18L18.017,0z"/> <path fill="#80A993" d="M36.017,0L37.5,0.002L37.484,13H36L36.017,0z"/> <path fill="#80A993" d="M42.017,0L43.5,0.002L43.484,13H42L42.017,0z"/> <path fill="#80A993" d="M24.017,0L25.5,0.002L25.484,13H24L24.017,0z"/> <path fill="#80A993" d="M30.017,0L31.5,0.002L31.484,13H30L30.017,0z"/> <path fill="#80A993" d="M48.017,0L49.5,0.002L49.484,13H48L48.017,0z"/> <path fill="#80A993" d="M54.017,0L55.5,0.002L55.484,13H54L54.017,0z"/> <path fill="#80A993" d="M60.017,0L61.5,0.002L61.484,13H60L60.017,0z"/> <path fill="#80A993" d="M66.017,0L67.5,0.002L67.484,13H66L66.017,0z"/> <path fill="#80A993" d="M72.017,0L73.5,0.002L73.484,13H72L72.017,0z"/> <path fill="#80A993" d="M78.017,0L79.5,0.002L79.484,13H78L78.017,0z"/> <path fill="#80A993" d="M84.017,0L85.5,0.002L85.484,13H84L84.017,0z"/> <path fill="#80A993" d="M90.017,0L91.5,0.002L91.484,13H90L90.017,0z"/> <path fill="#80A993" d="M96.017,0L97.5,0.002L97.484,13H96L96.017,0z"/> <path fill="#80A993" d="M102.017,0l1.483,0.002L103.484,13H102L102.017,0z"/> <path fill="#80A993" d="M108.017,0l1.483,0.002L109.484,13H108L108.017,0z"/> <path fill="#80A993" d="M114.017,0l1.483,0.002L115.484,13H114L114.017,0z"/> <path fill="#80A993" d="M120.017,0l1.483,0.002L121.484,13H120L120.017,0z"/> <path fill="#80A993" d="M126.017,0l1.483,0.002L127.484,13H126L126.017,0z"/> <path fill="#80A993" d="M144.017,0l1.483,0.002L145.484,13H144L144.017,0z"/> <path fill="#80A993" d="M150.017,0l1.483,0.002L151.484,13H150L150.017,0z"/> <path fill="#80A993" d="M132.017,0l1.483,0.002L133.484,13H132L132.017,0z"/> <path fill="#80A993" d="M138.017,0l1.483,0.002L139.484,13H138L138.017,0z"/> <path fill="#80A993" d="M156.017,0l1.483,0.002L157.484,13H156L156.017,0z"/> <path fill="#80A993" d="M162.017,0l1.483,0.002L163.484,13H162L162.017,0z"/> <path fill="#80A993" d="M168.017,0l1.483,0.002L169.484,13H168L168.017,0z"/> <path fill="#80A993" d="M174.017,0l1.483,0.002L175.484,13H174L174.017,0z"/> <path fill="#80A993" d="M180.017,0l1.483,0.002L181.484,13H180L180.017,0z"/> <path fill="#80A993" d="M186.017,0l1.483,0.002L187.484,13H186L186.017,0z"/> <path fill="#80A993" d="M192.017,0l1.483,0.002L193.484,13H192L192.017,0z"/> <path fill="#80A993" d="M198.017,0l1.483,0.002L199.484,13H198L198.017,0z"/> <path fill="#80A993" d="M204.017,0l1.483,0.002L205.484,13H204L204.017,0z"/> <path fill="#80A993" d="M210.017,0l1.483,0.002L211.484,13H210L210.017,0z"/> <path fill="#80A993" d="M216.017,0l1.483,0.002L217.484,13H216L216.017,0z"/> <path fill="#80A993" d="M222.017,0l1.483,0.002L223.484,13H222L222.017,0z"/> <path fill="#80A993" d="M228.017,0l1.483,0.002L229.484,13H228L228.017,0z"/> <path fill="#80A993" d="M234.017,0l1.483,0.002L235.484,13H234L234.017,0z"/> <path fill="#80A993" d="M252.017,0l1.483,0.002L253.484,13H252L252.017,0z"/> <path fill="#80A993" d="M258.017,0l1.483,0.002L259.484,13H258L258.017,0z"/> <path fill="#80A993" d="M240.017,0l1.483,0.002L241.484,13H240L240.017,0z"/> <path fill="#80A993" d="M246.017,0l1.483,0.002L247.484,13H246L246.017,0z"/> <path fill="#80A993" d="M264.017,0l1.483,0.002L265.484,13H264L264.017,0z"/> <path fill="#80A993" d="M270.017,0l1.483,0.002L271.484,13H270L270.017,0z"/> <path fill="#80A993" d="M276.017,0l1.482,0.002L277.484,13H276L276.017,0z"/> <path fill="#80A993" d="M282.017,0l1.482,0.002L283.484,13H282L282.017,0z"/> <path fill="#80A993" d="M288.017,0l1.482,0.002L289.484,13H288L288.017,0z"/> <path fill="#80A993" d="M294.017,0l1.482,0.002L295.484,13H294L294.017,0z"/> <path fill="#80A993" d="M300.017,0l1.482,0.002L301.484,13H300L300.017,0z"/> <path fill="#80A993" d="M306.017,0l1.482,0.002L307.484,13H306L306.017,0z"/> <path fill="#80A993" d="M312.017,0l1.482,0.002L313.484,13H312L312.017,0z"/> <path fill="#80A993" d="M318.017,0l1.482,0.002L319.484,13H318L318.017,0z"/> <path fill="#80A993" d="M324.017,0l1.482,0.002L325.484,13H324L324.017,0z"/> <path fill="#80A993" d="M330.017,0l1.482,0.002L331.484,13H330L330.017,0z"/> <path fill="#80A993" d="M336.017,0l1.482,0.002L337.484,13H336L336.017,0z"/> <path fill="#80A993" d="M342.017,0l1.482,0.002L343.484,13H342L342.017,0z"/> <path fill="#80A993" d="M360.017,0l1.482,0.002L361.484,13H360L360.017,0z"/> <path fill="#80A993" d="M366.017,0l1.482,0.002L367.484,13H366L366.017,0z"/> <path fill="#80A993" d="M348.017,0l1.482,0.002L349.484,13H348L348.017,0z"/> <path fill="#80A993" d="M354.017,0l1.482,0.002L355.484,13H354L354.017,0z"/> <path fill="#80A993" d="M372.017,0l1.482,0.002L373.484,13H372L372.017,0z"/> <path fill="#80A993" d="M378.017,0l1.482,0.002L379.484,13H378L378.017,0z"/> <path fill="#80A993" d="M384.017,0l1.482,0.002L385.484,13H384L384.017,0z"/> <path fill="#80A993" d="M390.017,0l1.482,0.002L391.484,13H390L390.017,0z"/> <path fill="#80A993" d="M396.017,0l1.482,0.002L397.484,13H396L396.017,0z"/> <path fill="#80A993" d="M402.017,0l1.482,0.002L403.484,13H402L402.017,0z"/> </g> </svg>
Here is one way to do it: with an animated linear gradient. <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" width="403.5px" height="13px" viewBox="0 0 403.5 13" enable-background="new 0 0 403.5 13" xml:space="preserve"> <defs> <linearGradient id="orange-grad" gradientUnits="userSpaceOnUse"> <stop offset="0" stop-color="orange"/> <stop offset="0" stop-color="orange"> <animate attributeName="offset" attributeType="XML" begin="0s" dur="5s" fill="freeze" from="0" to="1" /> </stop> <stop offset="0" stop-color="#80A993"> <animate attributeName="offset" attributeType="XML" begin="0s" dur="5s" fill="freeze" from="0" to="1" /> </stop> <stop offset="1" stop-color="#80A993"/> </linearGradient> </defs> <g fill="url(#orange-grad)"> <path d="M0.017,0L1.5,0.002L1.484,13H0L0.017,0z"/> <path d="M6.017,0L7.5,0.002L7.484,13H6L6.017,0z"/> <path d="M12.017,0L13.5,0.002L13.484,13H12L12.017,0z"/> <path d="M18.017,0L19.5,0.002L19.484,13H18L18.017,0z"/> <path d="M36.017,0L37.5,0.002L37.484,13H36L36.017,0z"/> <path d="M42.017,0L43.5,0.002L43.484,13H42L42.017,0z"/> <path d="M24.017,0L25.5,0.002L25.484,13H24L24.017,0z"/> <path d="M30.017,0L31.5,0.002L31.484,13H30L30.017,0z"/> <path d="M48.017,0L49.5,0.002L49.484,13H48L48.017,0z"/> <path d="M54.017,0L55.5,0.002L55.484,13H54L54.017,0z"/> <path d="M60.017,0L61.5,0.002L61.484,13H60L60.017,0z"/> <path d="M66.017,0L67.5,0.002L67.484,13H66L66.017,0z"/> <path d="M72.017,0L73.5,0.002L73.484,13H72L72.017,0z"/> <path d="M78.017,0L79.5,0.002L79.484,13H78L78.017,0z"/> <path d="M84.017,0L85.5,0.002L85.484,13H84L84.017,0z"/> <path d="M90.017,0L91.5,0.002L91.484,13H90L90.017,0z"/> <path d="M96.017,0L97.5,0.002L97.484,13H96L96.017,0z"/> <path d="M102.017,0l1.483,0.002L103.484,13H102L102.017,0z"/> <path d="M108.017,0l1.483,0.002L109.484,13H108L108.017,0z"/> <path d="M114.017,0l1.483,0.002L115.484,13H114L114.017,0z"/> <path d="M120.017,0l1.483,0.002L121.484,13H120L120.017,0z"/> <path d="M126.017,0l1.483,0.002L127.484,13H126L126.017,0z"/> <path d="M144.017,0l1.483,0.002L145.484,13H144L144.017,0z"/> <path d="M150.017,0l1.483,0.002L151.484,13H150L150.017,0z"/> <path d="M132.017,0l1.483,0.002L133.484,13H132L132.017,0z"/> <path d="M138.017,0l1.483,0.002L139.484,13H138L138.017,0z"/> <path d="M156.017,0l1.483,0.002L157.484,13H156L156.017,0z"/> <path d="M162.017,0l1.483,0.002L163.484,13H162L162.017,0z"/> <path d="M168.017,0l1.483,0.002L169.484,13H168L168.017,0z"/> <path d="M174.017,0l1.483,0.002L175.484,13H174L174.017,0z"/> <path d="M180.017,0l1.483,0.002L181.484,13H180L180.017,0z"/> <path d="M186.017,0l1.483,0.002L187.484,13H186L186.017,0z"/> <path d="M192.017,0l1.483,0.002L193.484,13H192L192.017,0z"/> <path d="M198.017,0l1.483,0.002L199.484,13H198L198.017,0z"/> <path d="M204.017,0l1.483,0.002L205.484,13H204L204.017,0z"/> <path d="M210.017,0l1.483,0.002L211.484,13H210L210.017,0z"/> <path d="M216.017,0l1.483,0.002L217.484,13H216L216.017,0z"/> <path d="M222.017,0l1.483,0.002L223.484,13H222L222.017,0z"/> <path d="M228.017,0l1.483,0.002L229.484,13H228L228.017,0z"/> <path d="M234.017,0l1.483,0.002L235.484,13H234L234.017,0z"/> <path d="M252.017,0l1.483,0.002L253.484,13H252L252.017,0z"/> <path d="M258.017,0l1.483,0.002L259.484,13H258L258.017,0z"/> <path d="M240.017,0l1.483,0.002L241.484,13H240L240.017,0z"/> <path d="M246.017,0l1.483,0.002L247.484,13H246L246.017,0z"/> <path d="M264.017,0l1.483,0.002L265.484,13H264L264.017,0z"/> <path d="M270.017,0l1.483,0.002L271.484,13H270L270.017,0z"/> <path d="M276.017,0l1.482,0.002L277.484,13H276L276.017,0z"/> <path d="M282.017,0l1.482,0.002L283.484,13H282L282.017,0z"/> <path d="M288.017,0l1.482,0.002L289.484,13H288L288.017,0z"/> <path d="M294.017,0l1.482,0.002L295.484,13H294L294.017,0z"/> <path d="M300.017,0l1.482,0.002L301.484,13H300L300.017,0z"/> <path d="M306.017,0l1.482,0.002L307.484,13H306L306.017,0z"/> <path d="M312.017,0l1.482,0.002L313.484,13H312L312.017,0z"/> <path d="M318.017,0l1.482,0.002L319.484,13H318L318.017,0z"/> <path d="M324.017,0l1.482,0.002L325.484,13H324L324.017,0z"/> <path d="M330.017,0l1.482,0.002L331.484,13H330L330.017,0z"/> <path d="M336.017,0l1.482,0.002L337.484,13H336L336.017,0z"/> <path d="M342.017,0l1.482,0.002L343.484,13H342L342.017,0z"/> <path d="M360.017,0l1.482,0.002L361.484,13H360L360.017,0z"/> <path d="M366.017,0l1.482,0.002L367.484,13H366L366.017,0z"/> <path d="M348.017,0l1.482,0.002L349.484,13H348L348.017,0z"/> <path d="M354.017,0l1.482,0.002L355.484,13H354L354.017,0z"/> <path d="M372.017,0l1.482,0.002L373.484,13H372L372.017,0z"/> <path d="M378.017,0l1.482,0.002L379.484,13H378L378.017,0z"/> <path d="M384.017,0l1.482,0.002L385.484,13H384L384.017,0z"/> <path d="M390.017,0l1.482,0.002L391.484,13H390L390.017,0z"/> <path d="M396.017,0l1.482,0.002L397.484,13H396L396.017,0z"/> <path d="M402.017,0l1.482,0.002L403.484,13H402L402.017,0z"/> </g> </svg> Update: How to manually trigger and reverse direction Here I've used a different way of creating the graph. This variant uses a clipping path and slides an orange rect back and forth behind the clip. As requested by OP, I have added the ability to trigger the animation and switch directions by mousing over a couple of buttons. This example uses vanilla Javascript and a timeout function for basic animation. If you wanted, you could switch to using a window.requestAnimationFrame() approach if there is a lot going on on your page and the animation doesn't run smoothly. var graphPosition = 0; var graphStep = 6; var graphRunning = false; var orangeRect = document.getElementById("orange"); document.getElementById("forwards").addEventListener("mouseenter", function() { graphStep = 6; startGraphRunning(); }); document.getElementById("backwards").addEventListener("mouseenter", function() { graphStep = -6; startGraphRunning(); }); function startGraphRunning() { // If already running, do nothing if (graphRunning) return; graphRunning = true; // Perform the first step in the animation animationStep(); } function animationStep() { // Calculate new position of orang rectangle var newPos = graphPosition += graphStep; if (newPos < 0 || newPos > 408) { // Stop animation if we have reach min or max position graphRunning = false; } else { // Update x position of orange rect graphPosition = newPos; // We want right hand edge of rectangle to be at "graphPosition", // so we position rect at (graphPosition - rectangle-width) orangeRect.setAttribute("x", graphPosition - orangeRect.width.baseVal.value); } // If animation is still running, call this function again in 50mS if (graphRunning) setTimeout(animationStep, 50); } div { display: inline-block; padding: 10px; background-color: #eee; border: solid 1px black; margin: 20px 20px 0 0; } <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" width="403.5px" height="13px" viewBox="0 0 403.5 13" enable-background="new 0 0 403.5 13" xml:space="preserve"> <defs> <clipPath id="graph"> <path d="M0.017,0L1.5,0.002L1.484,13H0L0.017,0z"/> <path d="M6.017,0L7.5,0.002L7.484,13H6L6.017,0z"/> <path d="M12.017,0L13.5,0.002L13.484,13H12L12.017,0z"/> <path d="M18.017,0L19.5,0.002L19.484,13H18L18.017,0z"/> <path d="M36.017,0L37.5,0.002L37.484,13H36L36.017,0z"/> <path d="M42.017,0L43.5,0.002L43.484,13H42L42.017,0z"/> <path d="M24.017,0L25.5,0.002L25.484,13H24L24.017,0z"/> <path d="M30.017,0L31.5,0.002L31.484,13H30L30.017,0z"/> <path d="M48.017,0L49.5,0.002L49.484,13H48L48.017,0z"/> <path d="M54.017,0L55.5,0.002L55.484,13H54L54.017,0z"/> <path d="M60.017,0L61.5,0.002L61.484,13H60L60.017,0z"/> <path d="M66.017,0L67.5,0.002L67.484,13H66L66.017,0z"/> <path d="M72.017,0L73.5,0.002L73.484,13H72L72.017,0z"/> <path d="M78.017,0L79.5,0.002L79.484,13H78L78.017,0z"/> <path d="M84.017,0L85.5,0.002L85.484,13H84L84.017,0z"/> <path d="M90.017,0L91.5,0.002L91.484,13H90L90.017,0z"/> <path d="M96.017,0L97.5,0.002L97.484,13H96L96.017,0z"/> <path d="M102.017,0l1.483,0.002L103.484,13H102L102.017,0z"/> <path d="M108.017,0l1.483,0.002L109.484,13H108L108.017,0z"/> <path d="M114.017,0l1.483,0.002L115.484,13H114L114.017,0z"/> <path d="M120.017,0l1.483,0.002L121.484,13H120L120.017,0z"/> <path d="M126.017,0l1.483,0.002L127.484,13H126L126.017,0z"/> <path d="M144.017,0l1.483,0.002L145.484,13H144L144.017,0z"/> <path d="M150.017,0l1.483,0.002L151.484,13H150L150.017,0z"/> <path d="M132.017,0l1.483,0.002L133.484,13H132L132.017,0z"/> <path d="M138.017,0l1.483,0.002L139.484,13H138L138.017,0z"/> <path d="M156.017,0l1.483,0.002L157.484,13H156L156.017,0z"/> <path d="M162.017,0l1.483,0.002L163.484,13H162L162.017,0z"/> <path d="M168.017,0l1.483,0.002L169.484,13H168L168.017,0z"/> <path d="M174.017,0l1.483,0.002L175.484,13H174L174.017,0z"/> <path d="M180.017,0l1.483,0.002L181.484,13H180L180.017,0z"/> <path d="M186.017,0l1.483,0.002L187.484,13H186L186.017,0z"/> <path d="M192.017,0l1.483,0.002L193.484,13H192L192.017,0z"/> <path d="M198.017,0l1.483,0.002L199.484,13H198L198.017,0z"/> <path d="M204.017,0l1.483,0.002L205.484,13H204L204.017,0z"/> <path d="M210.017,0l1.483,0.002L211.484,13H210L210.017,0z"/> <path d="M216.017,0l1.483,0.002L217.484,13H216L216.017,0z"/> <path d="M222.017,0l1.483,0.002L223.484,13H222L222.017,0z"/> <path d="M228.017,0l1.483,0.002L229.484,13H228L228.017,0z"/> <path d="M234.017,0l1.483,0.002L235.484,13H234L234.017,0z"/> <path d="M252.017,0l1.483,0.002L253.484,13H252L252.017,0z"/> <path d="M258.017,0l1.483,0.002L259.484,13H258L258.017,0z"/> <path d="M240.017,0l1.483,0.002L241.484,13H240L240.017,0z"/> <path d="M246.017,0l1.483,0.002L247.484,13H246L246.017,0z"/> <path d="M264.017,0l1.483,0.002L265.484,13H264L264.017,0z"/> <path d="M270.017,0l1.483,0.002L271.484,13H270L270.017,0z"/> <path d="M276.017,0l1.482,0.002L277.484,13H276L276.017,0z"/> <path d="M282.017,0l1.482,0.002L283.484,13H282L282.017,0z"/> <path d="M288.017,0l1.482,0.002L289.484,13H288L288.017,0z"/> <path d="M294.017,0l1.482,0.002L295.484,13H294L294.017,0z"/> <path d="M300.017,0l1.482,0.002L301.484,13H300L300.017,0z"/> <path d="M306.017,0l1.482,0.002L307.484,13H306L306.017,0z"/> <path d="M312.017,0l1.482,0.002L313.484,13H312L312.017,0z"/> <path d="M318.017,0l1.482,0.002L319.484,13H318L318.017,0z"/> <path d="M324.017,0l1.482,0.002L325.484,13H324L324.017,0z"/> <path d="M330.017,0l1.482,0.002L331.484,13H330L330.017,0z"/> <path d="M336.017,0l1.482,0.002L337.484,13H336L336.017,0z"/> <path d="M342.017,0l1.482,0.002L343.484,13H342L342.017,0z"/> <path d="M360.017,0l1.482,0.002L361.484,13H360L360.017,0z"/> <path d="M366.017,0l1.482,0.002L367.484,13H366L366.017,0z"/> <path d="M348.017,0l1.482,0.002L349.484,13H348L348.017,0z"/> <path d="M354.017,0l1.482,0.002L355.484,13H354L354.017,0z"/> <path d="M372.017,0l1.482,0.002L373.484,13H372L372.017,0z"/> <path d="M378.017,0l1.482,0.002L379.484,13H378L378.017,0z"/> <path d="M384.017,0l1.482,0.002L385.484,13H384L384.017,0z"/> <path d="M390.017,0l1.482,0.002L391.484,13H390L390.017,0z"/> <path d="M396.017,0l1.482,0.002L397.484,13H396L396.017,0z"/> <path d="M402.017,0l1.482,0.002L403.484,13H402L402.017,0z"/> </mask> </defs> <g clip-path="url(#graph)"> <rect x="0" y="0" width="408" height="13" fill="green"/> <rect x="-408" y="0" width="408" height="13" fill="orange" id="orange"/> </g> </svg> <br /> <div id="forwards">Forwards</div> <div id="backwards">Backwards</div>