Skip to content Skip to sidebar Skip to footer

How To Animate A Line Of A Rectangle's Diagonals?

I am trying add up an animation diagonally. The rectangle height and width I know (dynamically adding up). Now trying to animated a line from N to L, or O to M or other ways. I tri

Solution 1:

You can give your lines a stroke-dashoffset and animate it to 0. To calculate the values for stroke-dasharray and stroke-dashoffset I've used the getTotalLength() method to calculate the values for stroke-dasharray and stroke-dashoffset. I hope it helps.

svg{background:#efefef;width:100vh}
rect,line{stroke:black; fill:none}

#om{stroke-dasharray:94.34px;
stroke-dashoffset:94.34px;
animation: om 1s linear forwards;}


@keyframes om {
  to {
    stroke-dashoffset: 0;
  }
}
<svg viewBox = "0 0 100 70">  
  <rect x="10" y="10" width="80" height="50" />
  <line id="om" x1="90" y1="60" x2="10" y2="10" />
</svg>

And this is with both lines animated this time with m to o and l to n: just change the values for x1 to x2 and viceversa. The same for y. This will change the direction of the line.

svg{background:#efefef;width:100vh}
rect,line{stroke:black; fill:none}

#mo,#ln{stroke-dasharray:94.34px;
stroke-dashoffset:94.34px;
animation: om 1s linear forwards;}


@keyframes om {
  to {
    stroke-dashoffset: 0;
  }
}
<svg viewBox = "0 0 100 70">  
  <rect x="10" y="10" width="80" height="50" />
  <!--<line id="om" x1="90" y1="60" x2="10" y2="10" />
  <line id="nl" x1="90" y1="10" x2="10" y2="60" />-->
  <line id="mo" x2="90" y2="60" x1="10" y1="10" />
  <line id="ln" x2="90" y2="10" x1="10" y1="60" />
</svg>

I'm using the same animation for both #om and #nl


Solution 2:

Here is a simple idea with background coloration. You simply need to increase the background-size to draw the lines:

.box {
  width:200px;
  height:100px;
  border:2px solid;
  background:
    /*M - O*/
    linear-gradient(to top right,
      transparent calc(50% - 3px),red calc(50% - 2px),
      red calc(50% + 2px),transparent calc(50% + 3px)) top left,
      
    /*N - L*/  
    linear-gradient(to bottom right,
      transparent calc(50% - 3px),#000 calc(50% - 2px),
      #000 calc(50% + 2px),transparent calc(50% + 3px)) bottom left;
  background-repeat:no-repeat;
  background-size:0 0;
  transition:1s linear;
}
.box:hover {
  background-size:100% 100%;
}
<div class="box">

</div>

Change the background-position to change the animation start:

.box {
  width:200px;
  height:100px;
  border:2px solid;
  background:
    /*M - O*/
    linear-gradient(to top right,
      transparent calc(50% - 3px),red calc(50% - 2px),
      red calc(50% + 2px),transparent calc(50% + 3px)) bottom right,
      
    /*N - L*/
    linear-gradient(to bottom right,
      transparent calc(50% - 3px),#000 calc(50% - 2px),
      #000 calc(50% + 2px),transparent calc(50% + 3px)) top right;
  background-repeat:no-repeat;
  background-size:0 0;
  transition:1s linear;
}
.box:hover {
  background-size:100% 100%;
}
<div class="box">

</div>

UPDATE

Here is a version without the use of calc() that will work with IE. It will be a bit tricky to find the correct values and you will need a background-position animation which is also tricky:

.box {
  width:200px;
  height:100px;
  border:2px solid;
  background:
    /*M - O*/
    linear-gradient(to top right,
      transparent 176px,red 176px,
      red 181px,transparent 181px) left 200% top 200%,
      
    /*N - L*/
    linear-gradient(to bottom right,
      transparent 176px,black 176px,
      black 181px,transparent 181px) left -100% bottom -100%;
  background-repeat:no-repeat;
  background-size:200% 200%;
  transition:1s linear;
}
.box:hover {
  background-position:0 0,left 0 bottom 0;
}
<div class="box">

</div>

Check this answer for more details about the different values: https://stackoverflow.com/a/51734530/8620333


Post a Comment for "How To Animate A Line Of A Rectangle's Diagonals?"