Just noticed this little @codepen demo got featured:

https://codepen.io/thebabydino/pen/XJNpeYL

Detailed explanation for the how behind the mathy solution in this thread ๐Ÿ‘‰ https://mastodon.social/@anatudor/116594229219732729

#CSS #cssGradient #code #coding #web #dev #webDev #webDevelopment #frontend #Maths #cssMaths

That is, we need to add:
i*2*ฮฑ_dx

If k > 1, then we need to add half a turn as well, that is:
round(down, k)*180ยฐ

Adding these to the angular progress along the edge, we get the conic-gradient angle producing a point that moves at constant speed along the perimeter.

#CSS #cssGradient #code #coding #web #dev #webDev #webDevelopment #frontend #Maths #cssMaths

We know the distance from the middle of the rectangle to the middle of the current edge.

It's half the length of the other edge:
j*50cqw + i*50cqh

Absolute value of the absolute distance of the current progress point to the middle of the edge it's on:
(p - .5)*(j*100cqh + i*100cqw)

These form a right triangle out of which we can get the angle of their hypotenuse with the axis perpendicular onto the current edge.

#CSS #code #coding #web #dev #webDev #webDevelopment #frontend #Maths #cssMaths

Progress along a vertical edge is:
mod(k, 1)/f
Progress along a horizontal one is:
(mod(k, 1) - f)/(1 - f)

Progress along a generic edge is:
p = j*mod(k, 1)/f + i*(mod(k, 1) - f)/(1 - f)

Length traveled along the current generic edge:
j*mod(k, 1)/f*100cqh +
i*(mod(k, 1) - f)/(1 - f)*100cqw

Signed relative distance from middle of the current generic edge: p - .5

We get the absolute value of this difference & its sign.

#CSS #coding #web #dev #webDev #webDevelopment #frontend #cssMaths #code

We also compute its complementary j = 1 - i.
Idea https://css-tricks.com/logical-operations-with-css-variables/

Basically, we have:
i = max(0, sign(mod(k, 1) - f))
j = 1 - i

i = 0, j = 1 on vertical edge (incl. corner)
i = 1, j = 0 on horizontal edge (after corner)

Now we compute things for going along vertical & horizontal edges separately and use i & j to switch between them. That is, multiply vertical results with j and horizontal ones with i.

#CSS #code #coding #web #dev #webDev #webDevelopment #frontend #Maths #cssMaths

Logical Operations with CSS Variables | CSS-Tricks

Very often, while using switch variables (a variable that's either 0 or 1, a concept that's explained in a greater detail in in this post), I wish I could

CSS-Tricks

Moving further, we'll be computing the progress of mod(k, 1). Starting from the top left corner, once we go across half a perimeter, the cycle of going along a vertical edge, then along a horizontal one repeats.

There are 2 cases:
mod(k, 1) <=f (going along a vertical edge)
mod(k, 1) > 1 (going along a horizontal one)

These two cases repeat once more after the halfway point (bottom left corner since we're starting from the top right).

#CSS #code #coding #web #dev #frontend #Maths #cssMaths

If progress at the bottom left corner (= at half the perimeter starting from the top right corner) is 1, then progress at the bottom right corner is the height 100cqh over half the perimeter (one height 100cqh plus one width 100cqw):

f = 100cqh/(100cqh + 100cqw)

Dividing lengths is supported by Safari & Chrome + we have a Firefox fallback https://frontendmasters.com/blog/count-auto-fill-columns/#getting-the-number-of-columns

#CSS #cssGradient #code #coding #web #dev #webDev #webDevelopment #frontend #Maths #cssMaths

Anyway, what we want to animate here linearly isn't a conic-gradient() angle, it's the progress around the perimeter.

This is k, going from 0 to 2. When k is 0, we're at the top right corner. When it's 1, we're at the bottom left corner, having gone across half the perimeter.

What about the progress value at the bottom right? We'll be calling that f, but how much is it?

#CSS #cssGradient #code #coding #web #dev #webDev #webDevelopment #frontend #Maths #cssMaths

We can also compute half the perimeter as:

100cqh + 100cqw

---

By the way, this container technique is also what I've used to compute the aspect ratio of images of unknown dimensions (and therefore aspect ratio) in order to determine along which edge they should get a blur extension to square - check out the article! https://frontendmasters.com/blog/non-square-image-blur-extensions/#unknown-aspect-ratio

#CSS #code #coding #web #dev #webDev #webDevelopment #frontend #Maths #cssMaths

Idea behind the mathy solution: card's dimensions are given by its text content. It has an absolutely positioned child .back covering its entire area, but placed behind. This .back is sized explicitly relative to its parent, so its pseudos know its dimensions if we make it a container: they're 100cqw and 100cqh. These allow us to compute the angles of the rectangle diagonal with the horizontal ฮฑ_dx & with the vertical ฮฑ_dy as:

ฮฑ_dx = atan2(100cqh, 100cqw)
ฮฑ_dy = 90ยฐ - ฮฑ_dx

#CSS #code #cssMaths