Create line loading animation using css - html

Hey i have tried animation on a 2px solid line that fill white color from center to its end but failed because it just filled right to left and not gained from direction inverse.
Can any body tells me how to create the animation that work like this:
This is just the structure. The 2px height and 100px width just from the point + and fill the color from center to the ends in equal length from left and right and complete this type of animation. Hope I had explained my question with detail.
[----------+----------]
[---------+++---------]
[--------+++++--------]
[-------+++++++-------]
[------+++++++++------]
[-----+++++++++++-----]
[----+++++++++++++----]
[---+++++++++++++++---]
[--+++++++++++++++++--]
[-+++++++++++++++++++-]
[+++++++++++++++++++++]

something like below:
.line {
width:100px;
height:2px;
background:linear-gradient(red 0 0) center/0% 100% no-repeat;
animation:l 2s linear infinite alternate;
}
#keyframes l {
to {background-size:100% 100%}
}
<div class="line"></div>

Solution with stroke-dasharray
A line 100px long is drawn from the center with two rays.
Before starting animation stroke-dasharray: 0.50 0.50; both rays have
a dash equal to zero, and the maximum gap length is 50px. Therefore,
the line is initially invisible.
At the end of the animation, the gap of both rays become equal to
zero, and the dash takes on a maximum value of 100px, so the line
becomes fully visible
#pol{
fill:none;
stroke:red;
stroke-width:2;
stroke-dasharray:0,50 0,50;
animation:mid 2s linear infinite alternate;
}
#keyframes mid {
to {stroke-dasharray:0,0,100,0;}
}
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
width="200" height="200" viewBox="0 0 200 200" >
<polyline id="pol" stroke-dasharray="0,100" points="50,50 150,50" >
</polyline>
<text x="100" y="46" font-size="24px" text-anchor="middle" fill="black"> animate</text>
</svg>

The trick here for making from center to left is decreasing margin-left in animation
.wrapper {
position: relative;
width: 200px;
height: 2px;
background-color: #000000;
}
#inner-right,
#inner-left {
width: 0;
height: 100%;
background: white;
}
#inner-left {
margin-left: 50%;
animation: centerToLeft 2000ms ease forwards infinite;
}
#inner-right {
position: absolute;
top: 0;
left: 50%;
animation: centerToRight 2000ms ease forwards infinite;
}
#keyframes centerToRight {
to {
width: 50%;
}
}
#keyframes centerToLeft {
to {
margin-left: 0;
width: 50%;
}
}
<div class="wrapper">
<div id="inner-left"></div>
<div id="inner-right"></div>
</div>

Related

Can't figure a way to make the second animation of the second button work

So i'm making these out of a tutorial, but the solution in the video is kinda meh for me so i'm trying to make the two lines into one and then tranform to the X. I'm new to css animation like those so sorry if this is a really easy solution.
the first part seems to work correctly but the animation that should go the opposite way don't work. Any ideas? I tried messing with the delays and the order of things in the animation itself but that didn't help.
ps: and also sorry for typos english's not my mother language.
the full code on codepen: https://codepen.io/GuilhermeAFCruz/pen/RwJJerr
the HTML for the second button:
<button class="button-two" aria-controls="primary-navigation" aria-expanded="false">
<svg stroke="var(--button-color)" class="hamburger flex" viewBox="0 0 100 100" width="250px">
<line class="line top"
x1="85" y1="40"
x2="15" y2="40"
stroke-width="10"
stroke-linecap="round"
stroke-dasharray="80"
stroke-dashoffset="0"/>
<line class="line bottom"
x1="15" y1="60"
x2="85" y2="60"
stroke-width="10"
stroke-linecap="round"
stroke-dasharray="80"
stroke-dashoffset="0"/>
</svg>
</button>
The CSS :
html{
box-sizing: border-box;
}
body {
min-height: 100vh;
display: grid;
place-content: center;
background: hsl(48, 18%, 83%);
}
.examples{
padding: 3rem;
max-width: 1200px;
display: flex;
gap: clamp(2rem, 8vw, 4rem);
}
button{
background: transparent;
border: 10px solid var(--button-color, #000);
border-radius: 1rem;
}
.button-two{
--button-color:rgb(204, 0, 0);
}
.button-two .line{
animation: to-open-icon 1s forwards ;
transition: transform 300ms ease-in 1s ;
}
.button-two:hover .line{
animation: to-close-icon 1s forwards 300ms;
transition: transform 300ms ease-in ;
}
.button-two .line.top{
--rotation: -45deg;
transform-origin: center;
}
.button-two .line.bottom{
--rotation: 45deg;
transform-origin: center;
}
.button-two:hover .line.bottom{transform: translateY(-10px);}
.button-two:hover .line.top{transform: translateY(10px);}
#keyframes to-close-icon {
0%{stroke-dashoffset: 0;}
40%{stroke-dashoffset: 79.9;}
60%{stroke-dashoffset: 79.9; rotate: var(--rotation)}
100%{stroke-dashoffset: 0; rotate: var(--rotation);}
}
#keyframes to-open-icon {
0%{stroke-dashoffset: 0;rotate: var(--rotation) }
40%{stroke-dashoffset: 79.9; rotate: var(--rotation)}
60%{stroke-dashoffset: 79.9; }
100%{stroke-dashoffset: 0; }
}

Spinning Svg vs Html element - not spining on axis [duplicate]

This question already has answers here:
Why is blue circle not spinning in the center of itself [duplicate]
(2 answers)
Closed 2 years ago.
I am wanting to create a logo with a spinning icon in the centre. I have a few paths in it, and just want to spin one of the paths, that i have named on its central axis. I found many examples online, but couldn't get it to spin on its central axis.
I then have now simplified it to the following code pen.
If I spin a simple div, this works fine.
However if I try to spin a path inside an svg, it does not spin correctly.
What am I missing?
<html>
<head>
<style>
#keyframes spin {
from {transform:rotate(0deg);}
to {transform:rotate(360deg);}
}
rect {
animation: spin 2s infinite linear;
}
#rect{
width: 100px;
height: 100px;
animation: spin 2s infinite linear;
}
</style>
</head>
<body>
<div id="rect" style="background-color: blue; border: solid thin black;"></div>
<svg xmlns="http://www.w3.org/2000/svg">
<rect width='100' height='100' fill="green" stroke="black" />
</svg>
</body>
</html>
codepen
Add this selector with your <style>...</style> tag:
svg {
width: 100px;
height: 100px;
overflow: overlay;
}
Also, override the default transform-origin rule for rect:
rect {
...
transform-origin: unset;
}
<html>
<head>
<style>
#keyframes spin {
from {transform:rotate(0deg);}
to {transform:rotate(360deg);}
}
svg {
width: 100px;
height: 100px;
overflow: overlay;
}
rect {
animation: spin 2s infinite linear;
transform-origin: unset;
}
#rect {
width: 100px;
height: 100px;
animation: spin 2s infinite linear;
}
</style>
</head>
<body>
<div id="rect" style="background-color: blue; border: solid thin black;"></div>
<svg xmlns="http://www.w3.org/2000/svg">
<rect width='100' height='100' fill="green" stroke="black" />
</svg>
</body>
</html>

CSS - Animate object in curved path

I just want to animate my image through curved path. Like this way. ( I'm using position absolute for positioning. ) Did some research and found that css transform can do the job. It can be easily done by straight line. But curved path?
I tried to combine with css transform-origin + transform:rotate but I didn't get exact that I want. Clearly I want to move around 80% to the left curved and need to come to original position. I tried so many times adjusting my code but still no luck.
Fiddle
P.S
What is transform-origin really do here? Is it necessary?
Can someone explain me about how transform:rotate works here?
Here is my code
.sun{
width: 5.7%;
position: absolute;
top: -5%;
left: 57%;
animation: circle 10s linear infinite;
transform-origin: 0px 700px;
animation-fill-mode: forwards;
animation-direction: alternate;
}
#keyframes circle {
from {
transform:rotate(-60deg);
}
to {
transform:rotate(40deg);
}
}
<div class="sun">
<img src="sun.png" alt="">
</div>
Maybe make parent element move by rotate and children (in my case pseudoelement, whatever) make position absolute to the parent. And just use animation. Look at my solution. Maybe you will have to create some wrapper and use overflow: hidden, because it is square which is rotating. You can watch square's behavior by adding background-color.
#keyframes move-sun {
from {
transform: rotate(0deg);
}
to {
transform: rotate(90deg);
}
}
.sun {
position: relative;
width: 400px;
height: 400px;
margin: 200px;
transform: rotate(90deg);
animation: move-sun 10s;
}
.sun::before {
content: "";
position: absolute;
top: -25px;
left: -25px;
width: 50px;
height: 50px;
background-color: #ff0;
border-radius: 50%;
}
<div class="sun">
</div>
I realize this is an old question, but I just wanted to add another option. You could use 2 separate animations, one for the x-motion and one for the y-motion:
body {
background: #8DBECC;
}
.container {
position: relative;
height: 100%;
width: 100%;
}
.sun {
position: absolute;
width: 30px;
height: 200px;
animation: x-motion 3s ease-in-out 0s infinite alternate;
}
.sun:before {
content: '';
width: 30px;
height: 30px;
position: absolute;
top: 100%;
background: #F18C3E;
animation: y-motion 3s ease-in-out 0s infinite alternate;
border-radius: 15px;
}
#keyframes x-motion {
0% {
left: 0;
}
100% {
left: calc(100% - 30px);
}
}
#keyframes y-motion {
0%, 100% {
top: 100%;
}
50% {
top: 0%;
}
}
<html>
<body>
<div class="container">
<div class="sun">
</div>
</div>
</body>
</html>
https://jsfiddle.net/561pbt0r/27/
It might not be easy with CSS, but you can easily do this with SVG animation.
I modified a sample from this tutorial for your case:
<svg width="500" height="350" viewBox="0 0 350 350">
<path id="motionPath" fill="none" stroke="#000000" stroke-miterlimit="10" d="M100,100Q250,-50,400,100"/>
<g id="sun" transform="translate(-100, -300)">
<circle cx="100" cy="300" r="25" fill="yellow"/>
</g>
<animateMotion
xlink:href="#sun"
dur="3s"
begin="0s"
fill="freeze"
repeatCount="indefinite"
>
<mpath xlink:href="#motionPath" />
</animateMotion>
</svg>
When you want to apply some transform operation to an element, that transformation has a reference point from where it will be applied. That is the origin point and by default it is at the center of every element (i.e.: transform-origin(50% 50%)).
With this statement you can modify that origin whenever you need the transformation to apply from a different origin.
Here you can see an example when the rotation is done from the top left corner. Without the origin modification, it would rotate around its center.
Note: You can set the transform-origin even outside the element

SVG text with ‘stroke-dashoffset’ CSS animation not working in Firefox

The following animation works fine in Chrome and Opera, but it doesn't work in Mozilla Firefox. I can't figure out why.
Can someone please help me to fix the problem? What is wrong with my CSS?
#text-logo {
font-family: 'Shrikhand', cursive;
stroke-dashoffset: 100%;
stroke-dasharray: 100%;
-moz-animation: draw 8s forwards ease-in;
-webkit-animation: draw 8s forwards ease-in;
animation: draw 8s forwards ease-in;
background-clip: text;
}
#keyframes draw {
100% {
stroke-dashoffset: 0;
}
}
#-webkit-keyframes draw {
100% {
stroke-dashoffset: 0;
}
}
#-moz-keyframes draw {
100% {
stroke-dashoffset: 0;
}
}
<div class="draw_text">
<svg width="1092" height="220">
<text x="150" y="200" fill="#fff" stroke="#333" id="text-logo" stroke-width="2" font-size="95">WHO I AM ?</text>
</svg>
</div>
Units have to match in Firefox so if the base is a percentage unit then the animated value must be in percentages too.
There's no such thing as a -moz-animation or -moz-keyframes and any prefixed animations should be placed before the unprefixed version. I've fixed that too below.
#text-logo {
font-family: 'Shrikhand', cursive;
stroke-dashoffset: 100%;
stroke-dasharray: 100%;
-webkit-animation: draw 8s forwards ease-in;
animation: draw 8s forwards ease-in;
background-clip: text;
}
#-webkit-keyframes draw {
100% {
stroke-dashoffset: 0%;
}
#keyframes draw {
100% {
stroke-dashoffset: 0%;
}
}
}
<div class="draw_text">
<svg width="1092" height="220">
<text x="150" y="200" fill="#fff" stroke="#333" id="text-logo" stroke-width="2" font-size="95">WHO I AM ?</text>
</svg>
</div>
Setting stroke-dashoffset: 100% looks like a neat thing, but 100% of what? The canonical definition is:
If a percentage is used, the value represents a percentage of the current viewport …
… the percentage is calculated as the specified percentage of sqrt((actual-width)**2 + (actual-height)**2))/sqrt(2).
Firefox seems to not implement that. Setting px lengths makes it work:
#text-logo {
font-family: 'Shrikhand', cursive;
stroke-dashoffset: 1114px;
stroke-dasharray: 1114px;
-moz-animation: draw 8s forwards ease-in;
-webkit-animation: draw 8s forwards ease-in;
animation: draw 8s forwards ease-in;
background-clip: text;
}
#keyframes draw {
100% {
stroke-dashoffset: 0;
}
}
#-webkit-keyframes draw {
100% {
stroke-dashoffset: 0;
}
}
#-moz-keyframes draw {
100% {
stroke-dashoffset: 0;
}
}
<div class="draw_text">
<svg width="1092" height="220">
<text x="150" y="200" fill="#fff" stroke="#333" id="text-logo" stroke-width="2" font-size="95">WHO I AM ?</text>
</svg>
</div>

animating images in css 3D to move diagonally across the page

I am trying to animate some clouds on my website so they move diagonally across the page (starting off screen on the top left, ending off screen on the bottom right).
So far I have them moving from left to right but i'm not sure how to use translate3D to get them to move in this direction. Here's a site that shows what I want to achieve: http://futurewatercity.com/ (you have to click through to their map using the arrow)
and here is my code:
<div class="sky">
<div class="cloud cloud01"></div>
<div class="cloud cloud02"></div>
</div>
.cloud {
width: 512px;
height: 512px;
transform: scale(0.25);
position: absolute;
background: url(file://C:/Users/Sara/Documents/ExploreCanterbury/img/clouds01.png) 0 0 no-repeat;
}
.cloud01 {
top: 10px;
z-index: 100;
transform: scale(0.35);
animation: drift 25s linear infinite;
}
.cloud02 {
top: 10px;
z-index: 100;
transform: scale(0.5);
animation: drift 35s linear infinite;
}
#keyframes drift {
from {transform: translateX(-355px);}
to {transform: translateX(1350px);}
}