Reputation: 125
I have an image animating in this path below and would like the path to be visible.
CSS:
div {
width:10px;
height:10px;
background:red;
position:relative;
-webkit-animation-name:Player1;
-webkit-animation-duration:100s;
-webkit-animation-timing-function:cubic-bezier(0.1,0.1,0.1,0.1);
-webkit-animation-delay:2s;
-webkit-animation-iteration-count:5;
-webkit-animation-direction:normal;
-webkit-animation-play-state:running
}
@-webkit-keyframes Player1 {
0% {
background:red;
left:20px;
top:20px
}
25% {
background:#ff0;
left:800px;
top:200px
}
50% {
background:blue;
left:950px;
top:500px
}
75% {
background:green;
left:0;
top:800px
}
100% {
background:red;
left:0;
top:0
}
}
HTML:
<div></div>
Is it possible to have a line linking them with only CSS code? And if not, would you please help me with the code?
Upvotes: 9
Views: 21222
Reputation: 111
You can animate the start point of a line in svg (also reversed) "stroke-dashoffset" like this example in codepen
@keyframes dash {
0% {
stroke-dashoffset: 500;
}
100% {
stroke-dashoffset: 0;
}
}
https://codepen.io/Yudo/full/OaEWXK
Upvotes: 2
Reputation: 41958
This is obviously what SVG (Structured Vector Graphics) are for.
<svg>
<polyline points="20,20 800,200 950,500 0,800 0,0"></polyline>
</svg>
Upvotes: 12
Reputation: 1084
You can calculate the the angle and distance between points and use CSS Transforms to push the line where you want it. Here's a jsfiddle. It's a little crude, but I think you'll get the idea.
var startPoint=[200,200],
endPoint=[300,300],
rise=endPoint[1]-startPoint[1],
run=endPoint[0]-startPoint[0],
slope=rise/run,
DEGREES=57.2957795,
width=Math.sqrt((rise*rise)+(run*run));
var line=document.getElementById('line');
line.style.top=startPoint[0]+'px';
line.style.left=startPoint[1]+'px';
line.style.width=width+"px";
line.style.transform= "rotate("+(Math.atan(slope)*DEGREES)+"deg)";
line.style.transformOrigin= "0 0";
Upvotes: 1