Animated CSS play button - css
I'm trying to reproduce this piece of code (animated button using SVG and CSS):
https://codepen.io/jscottsmith/pen/azRObp/
To keep the code clean, I wanted to use xlink with that. My current code looks as follows:
#import "compass/css3";
body {background:violet}
#keyframes spin {
to { transform: rotate(360deg); }
}
.stroke-dotted {
opacity: 0;
stroke-dasharray: 4,5;
stroke-width: 1px;
transform-origin: 50% 50%;
animation: spin 4s infinite linear;
transition: opacity 1s ease,
stroke-width 1s ease;
}
.stroke-solid {
stroke-dashoffset: 0;
stroke-dashArray: 300;
stroke-width: 4px;
transition: stroke-dashoffset 1s ease,
opacity 1s ease;
}
.icon {
transform-origin: 50% 50%;
transition: transform 200ms ease-out;
}
#play:hover .stroke-dotted {
stroke-width: 4px;
opacity: 1;
}
#play:hover .stroke-solid {
opacity: 0;
stroke-dashoffset: 300;
}
#play:hover .icon {
transform: scale(1.05);
}
#PlayNow {
cursor: pointer;
position: absolute;
top: 50%;
left: 50%;
transform: translateY(-50%) translateX(-50%);
width:10em;
height:auto;
display:block;
}
<a href="#">
<svg xmlns="http://www.w3.org/2000/svg" id="PlayNow" viewBox="0 0 100 100" x="0px" y="0px" ><use xlink:href="#play" id="playTrailerButton" /></svg>
</a>
<svg xmlns="http://www.w3.org/2000/svg">
<symbol id="play" viewBox="0 0 100 100">
<path class="stroke-solid" fill="none" stroke="white" d="M49.9,2.5C23.6,2.8,2.1,24.4,2.5,50.4C2.9,76.5,24.7,98,50.3,97.5c26.4-0.6,47.4-21.8,47.2-47.7
C97.3,23.7,75.7,2.3,49.9,2.5"/>
<path class="stroke-dotted" fill="none" stroke="white" d="M49.9,2.5C23.6,2.8,2.1,24.4,2.5,50.4C2.9,76.5,24.7,98,50.3,97.5c26.4-0.6,47.4-21.8,47.2-47.7
C97.3,23.7,75.7,2.3,49.9,2.5"/>
<path class="icon" fill="white" d="M38,69c-1,0.5-1.8,0-1.8-1.1V32.1c0-1.1,0.8-1.6,1.8-1.1l34,18c1,0.5,1,1.4,0,1.9L38,69z"/>
</symbol>
</svg>
But the animation doesn't work.. Any ideas or hints - why? jsfiddle is here:
https://jsfiddle.net/4ztwr3c9/6
You have this:
#PlayNow {
cursor: pointer;
position: absolute;
top: 50%;
left: 50%;
transform: translateY(-50%) translateX(-50%);
width:10em;
height:auto;
display:block;
}
There is no element with the id #PlayNow. The example below has #PlayNow selector replaced by the original #play and your changes have been added, and I removed the Compass as well which I guess you can add it back, just be mindful of each change and retest so it's easier to debug and backtrack.
#keyframes spin {
to {
transform: rotate(360deg);
}
}
.stroke-dotted {
opacity: 0;
stroke-dasharray: 4, 5;
stroke-width: 1px;
transform-origin: 50% 50%;
animation: spin 4s infinite linear;
transition: opacity 1s ease, stroke-width 1s ease;
}
.stroke-solid {
stroke-dashoffset: 0;
stroke-dashArray: 300;
stroke-width: 4px;
transition: stroke-dashoffset 1s ease, opacity 1s ease;
}
.icon {
transform-origin: 50% 50%;
transition: transform 200ms ease-out;
}
#play:hover .stroke-dotted {
stroke-width: 4px;
opacity: 1;
}
#play:hover .stroke-solid {
opacity: 0;
stroke-dashoffset: 300;
}
#play:hover .icon {
transform: scale(1.05);
}
html {
height: 100%;
}
body {
height: 100%;
/*background-color: #30FF98;
background: radial-gradient(#71edb5, #30ff98);*/
}
#play {
cursor: pointer;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
/* Your Changes */
width:10em;
height:auto;
display:block;
}
body {background:violet}
<svg version="1.1" id="play" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" height="100px" width="100px" viewBox="0 0 100 100" enable-background="new 0 0 100 100" xml:space="preserve">
<path class="stroke-solid" fill="none" stroke="white" d="M49.9,2.5C23.6,2.8,2.1,24.4,2.5,50.4C2.9,76.5,24.7,98,50.3,97.5c26.4-0.6,47.4-21.8,47.2-47.7
C97.3,23.7,75.7,2.3,49.9,2.5"/>
<path class="stroke-dotted" fill="none" stroke="white" d="M49.9,2.5C23.6,2.8,2.1,24.4,2.5,50.4C2.9,76.5,24.7,98,50.3,97.5c26.4-0.6,47.4-21.8,47.2-47.7
C97.3,23.7,75.7,2.3,49.9,2.5"/>
<path class="icon" fill="white" d="M38,69c-1,0.5-1.8,0-1.8-1.1V32.1c0-1.1,0.8-1.6,1.8-1.1l34,18c1,0.5,1,1.4,0,1.9L38,69z"/>
</svg>
You can change/override some properties of a <use> element - provided these are not already set in your <symbol> element (see first example).
Since you need to change the dash-array values for different elements you can use css variables, that could be changed on hover.
body {
background: violet;
}
#keyframes spin {
to {
transform: rotate(360deg);
}
}
:root {
--fill: #fff;
--strokeWidth: 4px;
--scale: 1;
--strokeDashOffset: 10;
--strokeDashArray: 4 5;
--opacity: 0;
--strokeDashArraySolid: 300;
--strokeDashOffsetSolid: 0;
--opacitySolid: 1;
}
.PlayNow {
display: inline-block;
height: 10em;
}
.PlayNow2 {
--fill: #ccc;
--strokeWidth: 6px;
--scale: 1.5;
}
.PlayNow:hover {
--scale: 1.05;
--strokeDashOffset: 10;
--strokeDashArray: 4 5;
--strokeDashArraySolid: 300;
--strokeDashOffsetSolid: 300;
--opacity: 1;
--opacitySolid: 0;
}
#playTrailerButtonSimple:hover {
fill: green!important;
stroke: green!important;
stroke-dasharray: 10 5;
}
<a href="#">
<svg class="PlayNow PlayNowSimple" xmlns="http://www.w3.org/2000/svg" id="PlayNowSimple" viewBox="0 0 100 100">
<use href="#playSimple" id="playTrailerButtonSimple" stroke-width="4" fill="red" stroke="red"/>
</svg> Simple icon
</a>
<a href="#">
<svg class="PlayNow" xmlns="http://www.w3.org/2000/svg" id="PlayNow" viewBox="0 0 100 100">
<use href="#play" id="playTrailerButton" />
</svg>
</a>
<a href="#">
<svg class="PlayNow PlayNow2" xmlns="http://www.w3.org/2000/svg" id="PlayNow2" viewBox="0 0 100 100">
<use href="#play" id="playTrailerButton" />
</svg>
</a>
<svg xmlns="http://www.w3.org/2000/svg">
<symbol id="circle">
<path fill="none" style="stroke:var(--fill)" d="M49.9,2.5C23.6,2.8,2.1,24.4,2.5,50.4C2.9,76.5,24.7,98,50.3,97.5c26.4-0.6,47.4-21.8,47.2-47.7 C97.3,23.7,75.7,2.3,49.9,2.5" />
</symbol>
<symbol id="play" viewBox="0 0 100 100">
<use href="#circle" transform-origin="center" class="stroke-solid" style="stroke-width: var(--strokeWidth); stroke-dasharray: var(--strokeDashArraySolid); stroke-dashoffset: var(--strokeDashOffsetSolid); opacity:var(--opacitySolid); transition: stroke-dashoffset 1s ease, opacity 1s ease;" />
<use href="#circle" transform-origin="center" class="stroke-dotted" style="stroke-width: var(--strokeWidth); stroke-dasharray: var(--strokeDashArray); stroke-dashoffset: var(--strokeDashOffset); opacity:var(--opacity); animation: spin 4s infinite linear; transition: opacity 1s ease, stroke-width 1s ease;" />
<path class="icon" transform-origin="center" style="fill:var(--fill);transform:scale(var(--scale));transition:0.3s" d="M38,69c-1,0.5-1.8,0-1.8-1.1V32.1c0-1.1,0.8-1.6,1.8-1.1l34,18c1,0.5,1,1.4,0,1.9L38,69z" />
</symbol>
</svg>
<svg xmlns="http://www.w3.org/2000/svg">
<symbol id="circle2">
<path fill="none" d="M49.9,2.5C23.6,2.8,2.1,24.4,2.5,50.4C2.9,76.5,24.7,98,50.3,97.5c26.4-0.6,47.4-21.8,47.2-47.7 C97.3,23.7,75.7,2.3,49.9,2.5" />
</symbol>
<symbol id="playSimple" viewBox="0 0 100 100">
<use href="#circle2" transform-origin="center" class="stroke-solid" />
<path stroke-width="0" class="icon" transform-origin="center" d="M38,69c-1,0.5-1.8,0-1.8-1.1V32.1c0-1.1,0.8-1.6,1.8-1.1l34,18c1,0.5,1,1.4,0,1.9L38,69z" />
</symbol>
</svg>
External <use> reference
To some extent, you could also use external svg files like so:
<a href="#">
<svg class="svgBtn" xmlns="http://www.w3.org/2000/svg" id="PlayNow" viewBox="0 0 100 100" x="0px" y="0px">
<use href="button.svg#play" id="playTrailerButton" style="transition:0.3s"/>
</svg>
</a>
** Drawback:** Your animations/transitions won#t work in most browsers (Firefox can render them)
Related
how can i animate svg svg stroke line animation from left to right?
svg stroke animate start from right, but i want to stroke that animate form left to right. After animating stroke remain stands. can add any animate css class like fadeIn during the animation .cls-1 { fill: none; stroke: #00a139; stroke-miterlimit: 10; } svg { width: 100%; height: 100vh; margin-left: auto; margin-right: auto; display: block; background: #1e1e1e; } #Path_70 { stroke-dasharray: 1800; stroke-dashoffset: 1800; animation: dash 5s linear forwards; } #keyframes dash { to { stroke-dashoffset: 0; } } <svg id="ex6" xmlns="http://www.w3.org/2000/svg" viewBox="2080.831 303.745 1673.195 406.547"> <defs> </defs> <g id="Group_191" data-name="Group 191" transform="translate(2393 93)"> <path id="Path_70" data-name="Path 70" class="cls-1" d="M1700.935,169.155s-227.809,11.434-421.654,140.759c-174.322,116.314-275.519,110.6-373.713,41.794C786.235,268.022,551.495-57.262,63.3,9.47" transform="translate(-361.422 210.687)"/> </g> </svg>
You can make the stroke-dashoffset a negative value: .cls-1 { fill: none; stroke: #00a139; stroke-miterlimit: 10; } svg { width: 100%; height: 100vh; margin-left: auto; margin-right: auto; display: block; background: #1e1e1e; } #Path_70 { stroke-dasharray: 1800; stroke-dashoffset: -1800; animation: dash 5s linear forwards; } #keyframes dash { to { stroke-dashoffset: 0; } } <svg id="ex6" xmlns="http://www.w3.org/2000/svg" viewBox="2080.831 303.745 1673.195 406.547"> <defs> </defs> <g id="Group_191" data-name="Group 191" transform="translate(2393 93)"> <path id="Path_70" data-name="Path 70" class="cls-1" d="M1700.935,169.155s-227.809,11.434-421.654,140.759c-174.322,116.314-275.519,110.6-373.713,41.794C786.235,268.022,551.495-57.262,63.3,9.47" transform="translate(-361.422 210.687)"/> </g> </svg>
SVG Signature Animation Issues
I'm basically trying to use CSS to make this SVG signature animation happen for my own SVG signature: https://webdesign.tutsplus.com/tutorials/sign-on-the-dotted-line-animating-your-own-svg-signature--cms-23846 I've got it close, but for some reason it's drawing the outline of the SVG image, instead of the strokes of the SVG itself? I'm not sure what I'm doing wrong. Here's what I've got: .signature { max-width: 400px; margin: 0 auto; position: relative; overflow: auto; width: 100%; height: 0; padding-bottom: 20%; } svg { position: absolute; top: 0; left: 0; width: 100%; height: 100%; } path { fill: none; stroke: #2a3745; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; stroke-miterlimit: 10; } #keyframes write1 { 5% { stroke-dashoffset: 890; } 60% { stroke-dashoffset: 0; } } #keyframes write2 { 5%, 65% { stroke-dashoffset: 915; } 100% { stroke-dashoffset: 0; } } .stroke-A { stroke-dasharray: 890; animation: write1 4s 1 linear; } .stroke-mber { stroke-dasharray: 915; animation: write2 4s 1 linear; } <div class="signature"> <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 591.3 429.4" xml:space="preserve"> <g> <g> <path class="stroke-A" d="M1.9,277.9c53.5-7.3,104.9-28.2,148.7-59.7c44.1-31.7,79.6-74.2,104.2-122.5c13.8-27.1,24.2-55.8,31.5-85.4 c-1-0.1-2-0.3-2.9-0.4c-6.4,51.9-10.7,109.6,24.8,152.7c1.1,1.3,3.2-0.4,2.4-1.8c-9.3-16.7-25.7-27.6-42.3-36.3 c-19.1-10.1-39.2-18.9-59.5-26.3C169.2,83.5,125.8,75.6,84.4,87c-19.4,5.3-37.9,14.9-51.4,30c-1.3,1.4,0.8,3.6,2.1,2.1 c24.6-27.5,64.8-36.2,100.4-34.4c43.3,2.2,84.9,19.5,123.4,38.3c18.8,9.2,38.6,20.4,49.1,39.3c0.8-0.6,1.6-1.2,2.4-1.8 c-34.9-42.3-30.2-99.6-23.9-150.5c0.2-1.7-2.5-2-2.9-0.4c-13,52.7-36.2,103-70.3,145.4c-33,41-75.7,73.6-123.8,95 c-28,12.5-57.8,20.9-88.2,25.1C-0.8,275.2,0,278.1,1.9,277.9L1.9,277.9z"/> </g> </g> <g> <g> <path class="stroke-mber" d="M278.2,158.8c8.9-14.5,20.4-27.1,33.8-37.6c4.1-3.2,8.4-7.2,13.9-7.4c4.7-0.1,7.1,2.6,8.6,6.7c3.5,9.4,4.2,19.7,2.7,29.6 c1,0.1,2,0.3,2.9,0.4c-1.7-14.3,3.3-28.5,13.2-38.9c2.8-2.9,6.2-6.8,10.4-7.1c5.6-0.3,6.7,4.5,7.1,8.8c0.7,8.1,0.9,16.2,1.3,24.3 c0.1,1.7,2.4,2.1,2.9,0.4c2.2-7.2,10.3-19.4,18.8-12c2.4,2.1,4,4.5,7,5.9c3,1.4,6.4,1.8,9.6,1.5c15.1-1.5,21.9-16.6,25.7-29.4 c4.7-15.8,5-33.1,0.6-49c-1.1-3.9-4.3-6.9-8.6-4.7c-5,2.6-2.6,12-2.4,16.3c1.3,21.1,5.9,41.9,13.5,61.6c0.5,1.3,2.5,1.6,2.9,0 c1.8-7.5,3.8-15.4,9.6-20.9c6.2-5.9,21.6-9.4,25.3,1.4c2.4,6.9-3.5,13.9-9.3,16.8c-8.5,4.2-17.5,2.6-26.5,1.6 c-1.7-0.2-2.1,2.6-0.4,2.9c16.5,3.3,34.2,5.4,49.3-3.9c6-3.7,11.1-9.1,13.8-15.6c1.8-4.4,3.5-12.9-0.2-16.9 c-3.8-4.1-7.3,1.5-8.3,4.9c-1.4,5.2-0.2,10.8,2.4,15.3c6.2,10.7,19.9,14.8,30.9,9.1c11.2-5.8,17.3-20.8,8.4-31.1 c-0.9-1.1-3,0.1-2.5,1.5c3.7,9.5,7.4,19,11.1,28.5c0.9-0.4,1.8-0.8,2.7-1.2c-12.1-15.7,11.2-29.8,21.6-37.6 c9.1-6.9,16.1-15.5,19.3-26.5c6.2-21.3-3-43.8-21.1-56.1c-1.6-1.1-3.1,1.5-1.5,2.6c20.1,13.7,27.9,41.2,15.4,62.7 c-7.1,12.2-19.9,17.9-29.9,27.2c-8,7.4-13.9,19.6-6.4,29.3c0.9,1.2,3.4,0.6,2.7-1.2c-3.7-9.5-7.4-19-11.1-28.5 c-0.8,0.5-1.7,1-2.5,1.5c8.8,10.2-0.4,25-11.9,28c-12.1,3.1-25.8-6.3-25.3-19.3c0.1-1.9,2.2-10,4.5-4.6c1.2,3,0.2,7.4-0.8,10.3 c-2.3,6.6-7.6,12.1-13.5,15.6c-14.4,8.4-30.9,6.3-46.4,3.2c-0.1,1-0.3,2-0.4,2.9c10.9,1.3,22.8,3,32.1-4.6 c5.5-4.5,9.5-12.9,5.6-19.7c-4.7-8.3-16.8-8.5-24.3-4.3c-9.7,5.4-13,15.8-15.4,25.9c1,0,1.9,0,2.9,0 c-7.4-19.2-12.1-39.4-13.4-59.9c-0.1-2.3-1.8-17,3-14.8c3.6,1.7,4.1,13.1,4.5,16.4c1,9.7,0.5,19.5-1.7,29 c-2.1,8.9-5.2,19-11.6,25.8c-3.5,3.8-8.1,6.2-13.3,6.5c-5.5,0.4-8.9-1.8-12.7-5.5c-10.5-10.3-21.5,1.6-24.9,12.3 c1,0.1,2,0.3,2.9,0.4c-0.3-5.9-0.6-11.8-1-17.7c-0.2-4,0.1-8.6-1.4-12.4c-2.5-6.5-10.1-7.4-15.4-3.6 c-14.4,10.2-22.3,29.4-20.2,46.7c0.2,1.5,2.7,2.2,2.9,0.4c1.8-12.2,1.6-47.3-19.8-38.9c-4.5,1.8-8.5,5.5-12.3,8.5 c-4.4,3.5-8.5,7.3-12.5,11.3c-7.6,7.7-14.3,16.2-19.9,25.4C274.6,158.9,277.2,160.4,278.2,158.8L278.2,158.8z"/> </g> </g> </svg> </div>
As far as i can see it, the problem is your svg-file. Just go back to your illustration-tool and recreate your signature with just a colored path and no fill. Then your animation will just move along this path and not around it. I did just with the "A" in the example below. .signature { max-width: 400px; margin: 0 auto; position: relative; overflow: auto; width: 100%; height: 0; padding-bottom: 20%; } svg { position: absolute; top: 0; left: 0; width: 100%; height: 100%; } path { fill: none; stroke: #2a3745; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; stroke-miterlimit: 10; } #keyframes write1 { 5% { stroke-dashoffset: 890; } 60% { stroke-dashoffset: 0; } } #keyframes write2 { 5%, 65% { stroke-dashoffset: 915; } 100% { stroke-dashoffset: 0; } } .stroke-A { stroke-dasharray: 890; animation: write1 4s 1 linear; } .stroke-mber { stroke-dasharray: 915; animation: write2 4s 1 linear; } <div class="signature"> <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 591.3 429.4" style="enable-background:new 0 0 591.3 429.4;" xml:space="preserve"> <g> <g> <path class="stroke-A" d="M1.9,277.9c0,0,217-19.4,281.7-268.3c0,0-14.1,118,27,151.2c0,0-158.3-140.8-277.6-43.8"/> </g> </g> <g> <g> <path class="stroke-mber" d="M278.2,158.8c8.9-14.5,20.4-27.1,33.8-37.6c4.1-3.2,8.4-7.2,13.9-7.4c4.7-0.1,7.1,2.6,8.6,6.7c3.5,9.4,4.2,19.7,2.7,29.6 c1,0.1,2,0.3,2.9,0.4c-1.7-14.3,3.3-28.5,13.2-38.9c2.8-2.9,6.2-6.8,10.4-7.1c5.6-0.3,6.7,4.5,7.1,8.8c0.7,8.1,0.9,16.2,1.3,24.3 c0.1,1.7,2.4,2.1,2.9,0.4c2.2-7.2,10.3-19.4,18.8-12c2.4,2.1,4,4.5,7,5.9s6.4,1.8,9.6,1.5c15.1-1.5,21.9-16.6,25.7-29.4 c4.7-15.8,5-33.1,0.6-49c-1.1-3.9-4.3-6.9-8.6-4.7c-5,2.6-2.6,12-2.4,16.3c1.3,21.1,5.9,41.9,13.5,61.6c0.5,1.3,2.5,1.6,2.9,0 c1.8-7.5,3.8-15.4,9.6-20.9c6.2-5.9,21.6-9.4,25.3,1.4c2.4,6.9-3.5,13.9-9.3,16.8c-8.5,4.2-17.5,2.6-26.5,1.6 c-1.7-0.2-2.1,2.6-0.4,2.9c16.5,3.3,34.2,5.4,49.3-3.9c6-3.7,11.1-9.1,13.8-15.6c1.8-4.4,3.5-12.9-0.2-16.9 c-3.8-4.1-7.3,1.5-8.3,4.9c-1.4,5.2-0.2,10.8,2.4,15.3c6.2,10.7,19.9,14.8,30.9,9.1c11.2-5.8,17.3-20.8,8.4-31.1 c-0.9-1.1-3,0.1-2.5,1.5c3.7,9.5,7.4,19,11.1,28.5c0.9-0.4,1.8-0.8,2.7-1.2C536.3,104.9,559.6,90.8,570,83 c9.1-6.9,16.1-15.5,19.3-26.5c6.2-21.3-3-43.8-21.1-56.1c-1.6-1.1-3.1,1.5-1.5,2.6c20.1,13.7,27.9,41.2,15.4,62.7 c-7.1,12.2-19.9,17.9-29.9,27.2c-8,7.4-13.9,19.6-6.4,29.3c0.9,1.2,3.4,0.6,2.7-1.2c-3.7-9.5-7.4-19-11.1-28.5 c-0.8,0.5-1.7,1-2.5,1.5c8.8,10.2-0.4,25-11.9,28c-12.1,3.1-25.8-6.3-25.3-19.3c0.1-1.9,2.2-10,4.5-4.6c1.2,3,0.2,7.4-0.8,10.3 c-2.3,6.6-7.6,12.1-13.5,15.6c-14.4,8.4-30.9,6.3-46.4,3.2c-0.1,1-0.3,2-0.4,2.9c10.9,1.3,22.8,3,32.1-4.6 c5.5-4.5,9.5-12.9,5.6-19.7c-4.7-8.3-16.8-8.5-24.3-4.3c-9.7,5.4-13,15.8-15.4,25.9c1,0,1.9,0,2.9,0 c-7.4-19.2-12.1-39.4-13.4-59.9c-0.1-2.3-1.8-17,3-14.8c3.6,1.7,4.1,13.1,4.5,16.4c1,9.7,0.5,19.5-1.7,29 c-2.1,8.9-5.2,19-11.6,25.8c-3.5,3.8-8.1,6.2-13.3,6.5c-5.5,0.4-8.9-1.8-12.7-5.5c-10.5-10.3-21.5,1.6-24.9,12.3 c1,0.1,2,0.3,2.9,0.4c-0.3-5.9-0.6-11.8-1-17.7c-0.2-4,0.1-8.6-1.4-12.4c-2.5-6.5-10.1-7.4-15.4-3.6 c-14.4,10.2-22.3,29.4-20.2,46.7c0.2,1.5,2.7,2.2,2.9,0.4c1.8-12.2,1.6-47.3-19.8-38.9c-4.5,1.8-8.5,5.5-12.3,8.5 c-4.4,3.5-8.5,7.3-12.5,11.3c-7.6,7.7-14.3,16.2-19.9,25.4C274.6,158.9,277.2,160.4,278.2,158.8L278.2,158.8z"/> </g> </g> </svg> </div>
SVG icon animation leaves a pixel gap
I'm working on SVG animations with CSS and I've noticed that with my line drawing animations, any SVG rect (#clipboard-border and #clipboard-clip-border) stroke always excludes a bit of the top-left corner, which makes it an incomplete rectangle. I've tried adjusting the stroke-dasharray and stroke-dashoffset measurements within the CSS, as well as adjusting the sizes and pixel coordinated within the SVG code, but neither are the problem it seems. Help? html, body { width: 100%; height: 100%; background-color: #CECECE; } div { text-align: center; } svg { display: inline-block; width: 120px; margin: 3% auto; padding: 0px 100px; } /* --------------------- SVG RULES --------------------- */ /* All grey strokes */ #clipboard-border, .clipboard-content, .clipboard-borders, .mech-pencil-borders { fill: none; stroke: #4D5152; stroke-width: 6; stroke-miterlimit: 10; } /* All things white */ #clipboard-paper-fill, #mech-pencil-eraser-fill { fill: #F3F7F6; } /* All things green */ #mech-pencil-point-fill, #mech-pencil-top-fill { fill: #25B686; } /* All things blue */ #clipboard-fill { fill: #85D0D3; } /* All things yellow */ #clipboard-clip-fill, #mech-pencil-grip { fill: #FBFBCE; } #clipboard-knob-1, #clipboard-knob-2, #clipboard-knob-3, #mech-pencil-bottom-btn, #mech-pencil-top-btn { stroke-dasharray: 8px; stroke-dashoffset: 8px; animation: trace .5s ease-out forwards; } /* --------------------- ANIMATION KEYFRAMES --------------------- */ #keyframes trace { 100% { stroke-dashoffset: 0px; } } #keyframes fill-it { 100% { opacity: 1; } } #keyframes grow { 0% { transform: scale(0); } 30% { transform: scale(1.1); } 60% { transform: scale(.9); } } /* --------------------- SVG ANIMATION: INSIGHT & PLANNING ICON --------------------- */ #clipboard-clip-border { stroke-dasharray: 180px; stroke-dashoffset: 180px; animation: trace .2s ease-out forwards; } #clipboard-clip-fill { opacity: 0; animation: fill-it .2s .2s ease-in-out forwards; } #clipboard-border { stroke-dasharray: 640px; stroke-dashoffset: 640px; animation: trace 1.25s ease-in-out forwards; } #clipboard-fill, #mech-pencil-point-fill, #mech-pencil-top-fill { opacity: 0; animation: fill-it .25s 1.25s ease-in-out forwards; } #clipboard-paper-border { stroke-dasharray: 400px; stroke-dashoffset: 400px; animation: trace 1s ease-out forwards; } #clipboard-paper-fill, #mech-pencil-eraser-fill, #mech-pencil-grip { opacity: 0; animation: fill-it .75s 1s ease-in-out forwards; } #clipboard-content-line-1 { stroke-dasharray: 30px; stroke-dashoffset: 30px; animation: trace .5s ease-out forwards; } #clipboard-content-line-7, #clipboard-clip-detail { stroke-dasharray: 52px; stroke-dashoffset: 52px; animation: trace .5s ease-out forwards; } #clipboard-content-line, #clipboard-content-line-even, #mech-pencil-eraser-border { stroke-dasharray: 80px; stroke-dashoffset: 80px; animation: trace .75s ease-out forwards; } #mech-pencil-border-left, #mech-pencil-border-right { stroke-dasharray: 115px; stroke-dashoffset: 115px; animation: trace .75s ease-out forwards; } #mech-pencil-point-border { stroke-dasharray: 60px; stroke-dashoffset: 60px; animation: trace .5s ease-out forwards; } #mech-pencil-tip, #mech-pencil-top { stroke-dasharray: 10px; stroke-dashoffset: 10px; animation: trace .4s ease-out forwards; } /* --------------------- ANIMATION DELAYS --------------------- */ #clipboard-knob-1, #clipboard-knob-2 { animation-delay: .25s; } #clipboard-clip-detail, #clipboard-content-line, #clipboard-content-line-7, #clipboard-knob-2 { animation-delay: .5s; } #mech-pencil-bottom-btn, #mech-pencil-top-btn { animation-delay: 1.25s; } <div class="wrapper"> <!-- INSIGHT & PLANNING ICON --> <svg id="insight-planning" xmlns="http://www.w3.org/2000/svg" x="0px" y="0px" width="200px" height="200px" viewBox="0 0 200 200"> <g class="clipboard"> <rect id="clipboard-fill" x="15.015" y="11.44" width="132" height="182" /> <rect id="clipboard-paper-fill" x="30.753" y="11.44" width="100" height="151" /> <g class="clipboard-content"> <line id="clipboard-content-line-even" x1="46.491" y1="68.096" x2="115.738" y2="68.096" /> <line id="clipboard-content-line" x1="46.491" y1="80.687" x2="115.738" y2="80.687" /> <line id="clipboard-content-line-even" x1="46.491" y1="93.277" x2="115.738" y2="93.277" /> <line id="clipboard-content-line" x1="46.491" y1="105.867" x2="115.738" y2="105.867" /> <line id="clipboard-content-line-even" x1="46.491" y1="118.458" x2="115.738" y2="118.458" /> <line id="clipboard-content-line-7" x1="46.491" y1="131.048" x2="96.852" y2="131.048" /> <line id="clipboard-content-line-1" x1="115.738" y1="49.211" x2="90.557" y2="49.211" /> </g> <rect id="clipboard-border" x="15.015" y="11.44" width="132" height="182" /> </g> <g class="mech-pencil-fills"> <rect id="mech-pencil-grip" x="166.099" y="96.425" width="18" height="47" /> <rect id="mech-pencil-top-fill" x="166.099" y="30.325" width="18" height="66" /> <rect id="mech-pencil-eraser-fill" x="166.099" y="11.44" width="18" height="18" /> <polygon id="mech-pencil-point-fill" points="184.985,143.639 184.985,159.376 175.542,168.819 166.099,159.376 166.099,143.639" /> </g> <g class="mech-pencil-borders"> <line id="mech-pencil-border-left" x1="166.099" y1="143.639" x2="166.099" y2="30.325" /> <line id="mech-pencil-border-right" x1="184.985" y1="30.325" x2="184.985" y2="145" /> <rect id="mech-pencil-eraser-border" x="166.099" y="11.44" width="18" height="18" /> <polygon id="mech-pencil-point-border" points="184.985,143.639 184.985,159.376 175.542,168.819 166.099,159.376 166.099,143.639" /> <line id="mech-pencil-top" x1="175.542" y1="11.44" x2="175.542" y2="1.997" /> <line id="mech-pencil-tip" x1="175.542" y1="168.819" x2="175.542" y2="175.114" /> <line id="mech-pencil-bottom-btn" x1="175.542" y1="127.901" x2="175.542" y2="121.605" /> <line id="mech-pencil-top-btn" x1="175.542" y1="115.31" x2="175.542" y2="109.015" /> </g> <g class="clipboard-clip"> <rect id="clipboard-clip-fill" x="49.639" y="5.144" width="62" height="25" /> </g> <g class="clipboard-borders"> <polyline id="clipboard-paper-border" points="131.476,11.44 131.476,162.524 30.753,162.524 30.753,11.44" /> <rect id="clipboard-clip-border" x="49.639" y="5.144" width="62" height="25" /> <line id="clipboard-clip-detail" x1="59.081" y1="17.735" x2="103.148" y2="17.735" /> <line id="clipboard-knob-1" x1="65.376" y1="178.262" x2="71.672" y2="178.262" /> <line id="clipboard-knob-2" x1="77.967" y1="178.262" x2="84.262" y2="178.262" /> <line id="clipboard-knob-3" x1="90.557" y1="178.262" x2="96.852" y2="178.262" /> </g> </svg> </div> Also posted in Codepen.
Just add stroke-linecap: square; to the CSS declarations for the SVG object. svg { display: inline-block; width: 120px; margin: 3% auto; padding: 0px 100px; stroke-linecap: square; /* <-- Add this */ } Example: Here's an SVG with two paths (open, not closed). The path drawn with "butt" line endings has a bit missing in the top corner, but the other path (drawn with "square" line endings) doesn't have this issue. <svg width="250" height="100" viewBox="0 0 250 100"> <path d="M10 10h80v80h-80v-80" style="stroke:#000; stroke-width:10px; fill:none; stroke-linecap: square;"/> <text x="50" y="70" text-anchor="middle">Square</text> <path d="M160 10h80v80h-80v-80" style="stroke:#000; stroke-width:10px; fill:none; stroke-linecap: butt;"/> <text x="200" y="70" text-anchor="middle">Butt</text> </svg> P.S. I like your work, but next time please consider making a minimal, complete and verifiable example to illustrate the problem. That way people won't have to wade through reams of code to discover what's going wrong :-)
Change starting point of stroke animation around a polygon?
I have a hexagon shaped polygon with a stroke around it. I want to animate that stroke, which I've done, but I would love to have it start in a different place on the polygon. This is the CSS that I have right now which works, I just want to change where the animation starts from. .outline1 { fill: none; stroke: #0d72b9; stroke-miterlimit: 10; stroke-width: 3px; } g.icon .outline1 { stroke-dasharray: 808; stroke-dashoffset:808; transition:all 300ms ease-in-out; fill:transparent; } g.icon:hover .outline1{ stroke-dashoffset:0; cursor: pointer; } This is the svg <svg id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 764.87 535.94"> <g class="regional icon"> <polygon class="cls-3 outline1" points="113.52 134.89 229.99 67.64 346.47 134.89 346.47 269.38 229.99 336.62 113.52 269.38 113.52 134.89"/> </g> </svg> The animation starts in the upper left corner but I'd like it to start in the bottom left.
The stroke starts where the path/polygon starts...change the start point. svg { height: 250px; display: block; margin: 1em auto; border: 1px solid grey; } .outline1 { fill: none; stroke: #0d72b9; stroke-miterlimit: 10; stroke-width: 10px; } g.icon .outline1 { stroke-dasharray: 808; stroke-dashoffset: 808; transition: all 1000ms ease-in-out; fill: pink; } g.icon:hover .outline1 { stroke-dashoffset: 0; cursor: pointer; } <svg id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 764.87 535.94"> <g class="regional icon"> <polygon class="cls-3 outline1" points=" 113.52 269.38 113.52 134.89 229.99 67.64 346.47 134.89 346.47 269.38 229.99 336.62 113.52 269.38 " /> </g> </svg>
You just need to change your start point. Here it's a right way to do it. .outline1 { fill: none; stroke: #0d72b9; stroke-miterlimit: 10; stroke-width: 3px; } g.icon .outline1 { stroke-dasharray: 808; stroke-dashoffset:808; transition:all 300ms ease-in-out; fill:transparent; } g.icon:hover .outline1{ stroke-dashoffset:0; cursor: pointer; } <svg id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 764.87 535.94"> <g class="regional icon"> <polygon class="cls-3 outline1" points="113.52 202.135 113.52 134.89 229.99 67.64 346.47 134.89 346.47 269.38 229.99 336.62 113.52 269.38"/> </g> </svg>
For symmeterical shapes, it is much easier to add a CCS that directly rotate the shape. .outline1 { fill: none; stroke: #0d72b9; stroke-miterlimit: 10; stroke-width: 3px; } g.icon .outline1 { stroke-dasharray: 808; stroke-dashoffset:808; transition:all 300ms ease-in-out; fill:transparent; } g.icon:hover .outline1{ stroke-dashoffset:0; cursor: pointer; } .outline1 { -ms-transform: rotate(60deg); /* IE 9 */ -ms-transform-origin: 50% 50%; /* IE 9 */ -webkit-transform: rotate(60deg); /* Chrome, Safari, Opera */ -webkit-transform-origin: 50% 50%; /* Chrome, Safari, Opera */ transform: rotate(60deg); transform-origin: 50% 50%; } <svg id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 764.87 535.94"> <g class="regional icon"> <polygon class="cls-3 outline1" points="113.52 134.89 229.99 67.64 346.47 134.89 346.47 269.38 229.99 336.62 113.52 269.38 113.52 134.89"/> </g> </svg>
Animate svg path
I have animated a svg path but I need some help. I want to know if it's possible to animate the path at a certain speed and then an other speed. Here is a JSFIDDLE for more explanation. I want the line to go fast and then the text to be less fast. HTML <svg class="svg-path" version="1.1" id="Calque_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 2238.6 153.7" enable-background="new 0 0 2238.6 153.7" xml:space="preserve"> <path class="path" stroke-width="2" fill="none" stroke="#000000" d="M0,149.5c0.6,0,1629.7,0,1631,0c2.7,0,10.6,0,12.4,0c3.3,0,5.9,0,8.2,0c4.1,0,8.1,0,10.9,0 c3.8,0,5.3,0,8.7,0c3.9,0,3.6,0.1,8.1,0c6.3-0.2,8.2-0.9,12.7-2.6c4.5-1.7,8.3-4.1,11.4-7.1c3.1-3,5.5-6.6,7.1-10.7 c1.6-4.1,2.4-8.5,2.4-13.2c0-5.1-1-9.3-3.1-12.6c-2.1-3.3-4.8-6.1-8.2-8.3c-3.4-2.2-7.3-4.1-11.6-5.6c-4.3-1.5-8.8-3-13.3-4.4 c-4.6-1.4-9-3-13.3-4.8c-4.3-1.8-8.2-4-11.6-6.8c-3.4-2.8-6.1-6.2-8.2-10.3c-2.1-4.1-3.1-9.3-3.1-15.5c0-4.8,0.9-9.5,2.8-14 s4.5-8.5,8.1-11.9c3.5-3.4,7.9-6.2,13.1-8.3c5.2-2.1,11.1-3.1,17.8-3.1c7.5,0,19.1,0,25.5,0c7.5,0,22.4,0,35.9,0v140.5l44.4,0 L1788,9.6l66.3,110.3l64.5-109.8l-0.2,140.9h87.1c0,0,64.7,0,64.7-76.9c0-72-63.3-69.4-63.3-69.4s-60.7-2.7-60.7,69.2 c0,77,56.8,76.9,61,77c0.2,0,89.6,0,93.9,0V13.5l104.7,137.3l0-143.7l32.7,0 M1946.3,73.9 M2070.3,74.1"/> </svg> CSS svg{ &.svg-path{ position: absolute; top:25px; left: 0px; width: 100%; height: auto; } .path { stroke-dasharray: 3800; stroke-dashoffset: 0; animation: dash 3.5s linear reverse; } } #keyframes dash { to { stroke-dashoffset: 3800; } } Is that possible to do this with one svg path ?
use this example http://jsfiddle.net/wxx5o9ms/1/ svg.svg-path { position: absolute; top:25px; left: 0px; width: 100%; height: auto; } svg.svg-path path { stroke-dasharray: 3800; animation: dash 3.5s linear reverse; } #keyframes dash { 0% { stroke-dashoffset: 0; transition: 'stroke-dashoffset'; } 70% { stroke-dashoffset: 2000; transition: 'stroke-dashoffset'; } 100% { stroke-dashoffset: 3800; transition: 'stroke-dashoffset'; } }