有没有办法让触控笔计算的平方根x?
您知道,就像JavaScript Math.sqrt(x)一样。
我正在创建一个以原点为中心的钻石。我目前正在使用以下代码:
vendor(prop, args...)
-webkit-{prop} args
-moz-{prop} args
-o-{prop} args
-ms-{prop} args
rotate(r)
vendor('transform', unquote('rotate('+r+')'))
rotate r
.diamond
display block
width 7ex
height @width
line-height @height
rotate(-45deg)
/* calc offset of a rotated square
* @width * @width = $offsetX * $offsetX + $offsetY * $offsetY
* @width * @width = ( $offset * $offset ) * 2 // for a square: $offsetX= $offsetY
* ( @width * @width ) / 2 = ( $offset * $offset )
* sqrt(( @width * @width ) / 2) = $offset
* @width * sqrt(2) = $offset
*/
$offset = @width / 1.41421356237 // =sqrt( 2 )
margin (- $offset) 0em 0ex (- $offset ) // center the diamond on its origin
padding 0ex 0em
background-color red
Run Code Online (Sandbox Code Playgroud)
如您所见,我设法在没有实际计算平方根的情况下解决了问题,而是改用了恒定值。但是当我看一下Stylus的内置函数时,我找不到任何计算平方根的方法。
在那里回答,您可以使用内置math函数,因此自定义sqrt看起来更好:
sqrt(x)
return math(x, 'sqrt')
Run Code Online (Sandbox Code Playgroud)