Is there a way to have pi in a CSS calc?

一个人想着一个人 提交于 2019-12-05 08:38:26

问题


I have an SVG circle animation for a progress bar where the stroke-dashoffset is animated from 0,radius to radius,0 (for 0% to 100%).

The equation for the length of the circumference of a circle is pi * d. Is there a way to use a CSS calc function where it can use a value of pi, rather than just a rounded value (like 3.14)?


回答1:


There's no such thing as a PI variable in CSS, unfortunately.

However..

You can make use of CSS variables to assign a number to it, downside to this is that it has a really, really bad browser support.

This would work like:

:root {
  --PI: 3.14159265358979; // enter the amount of digits you wish to use
}

.circle {
    width: calc(100 * var(--PI));
}

The best solution would be to use a preprocessor such as SASS or Less to assign the PI variable to, this would look like the following example in SASS:

$pi: 3.14159265358979 // amount of digits you wish to use

.circle {
    width: calc(100 * ${pi});
}

EDIT: As mentioned in the comments, some browsers (Safari + IE) round to 2 decimals, where Chrome and Firefox can round up to (at least) 4 decimals.




回答2:


No.

Consider that the value will be rounded anyway as computers cannot fully realize all numbers. Just use a lengthy approximation for pi:

3.141592653589793



回答3:


You can use something approximate, depending the accuracy you need:

22/7 = 3.14

377/120 = 3,142

355/113 = 3,141592



来源:https://stackoverflow.com/questions/40782381/is-there-a-way-to-have-pi-in-a-css-calc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!