$ (calc)
Evaluate an arithmetic expression, with custom properties as variables.
$(1 + 2) is 3; $(@i * 10)px is 10px, 20px, …
Syntax
- $(<expression>)
- $(<expression>)<unit>
- $<name>
The expression is numbers, operators and math functions.
Functions such as @i or @r inside it are evaluated first.
Expressions
In each cell, @i is that cell's index:
@grid: 5 / 60vmin _1px;
background: #000;
color: #fff;
@content: $(@i * 2);
Functions such as mix and clamp work as in GLSL. smoothstep(a, b, x) eases from 0 to 1
as x goes from a to b:
@grid: 9 / 60vmin _1px;
background: #000;
border-radius: 50%;
scale: $(1 - smoothstep(0, 5, @dr)); Units
A unit right after $() is appended to the result.
A unit on a number inside the expression is written out as CSS math for the browser:
$((@i - 6) * 30deg) is calc(-5 * 30deg) in the first cell.
Inside a call deg, turn and rad are read as angles: $(cos(60deg)) is .5.
@grid: 12x1 / 60vmin;
@place: center;
@size: 40% 3px;
background: #000;
margin: auto;
transform-origin: 0 50%;
rotate: $(360 / @I * @i)deg; Variables
Custom properties drop their -- prefix and any unit,
so --step: 10% is step = 10:
@grid: 1x10 / 60vmin;
background: #000;
--step: 10%;
width: $(step * @i)%;
The cell variables such as x, i and dr read too,
so $(i * 2) is $(@i * 2). Inside a repeat, such as @m or circle*5 in @svg,
n is the step. A custom property of the same name comes first.
Without parentheses, $name is the custom property --name.
Arithmetic is evaluated, and anything else passes through:
@grid: 5 / 60vmin _1px;
--ink: @p(#000, #e6437d);
--n: @i * @i;
background: $ink;
color: #fff;
@content: $n; Live values
A live value such as @t has no number when the doodle is generated, so the expression around it becomes CSS math:
@grid: 12x1 / 60vmin;
@place: center;
@size: 40% 3px;
background: #000;
margin: auto;
transform-origin: 0 50%;
rotate: $(@t / 100 * @i)deg; Reference
Expressions
| Part | Value |
|---|---|
| operators | + - * / % ^, comparisons, and, or, not, and parentheses. Expressions has the full list and precedence. |
| adjacent values | multiply: 2x, 2π, (1+2)(3) |
| functions | the Math functions, gcd(), match(), pick(k: …) and the GLSL math mod(), fract(), clamp(), mix(), step(), smoothstep(), radians(), degrees() and atan(y, x), the angle of the point (x, y). For a random number write @r, as in $(@r() * 2). A function defined with --name() can also be called as name(…), unless a built-in has the name. |
| variables | the custom properties declared above the $ on :doodle, :container or the cell, or in an enclosing @svg, without their --. Under them come the cell variables and a repeat's n, nx, ny and N. |
| units | a variable drops its unit, so --w: 10px gives w = 10. A unit on a number is written out as CSS math, except deg, turn and rad inside a call, which are radians. |
| unknown names | read as 0; there are no errors |
| result | rounded to 12 significant digits, and anything closer to zero than 1e-9 is 0 |
Forms
| Form | Value |
|---|---|
$(…)<unit> | the result with the unit appended as written, as in $(1+1)px, $(…)deg or $(…)% |
$<name> | the value of --name, as is $(name). Arithmetic is evaluated; anything else, a list included, passes through as written. With --s: 50, $s% is 50%. With no --x, $x is the cell variable x. |
| with a live value | CSS math for the browser: $(@t / 10)deg is calc((var(--cssd-utime) / 10) * 1deg). Live values lists what carries over. |
$<unit>(…) | an older writing of $(…)<unit>, which warns, unless a function --<unit>() is defined |
@calc() | an older name of $() |