【发布时间】:2014-01-28 18:41:12
【问题描述】:
有没有办法让 Stylus 计算 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
如您所见,我设法在没有实际计算平方根的情况下解决了这个问题,而是使用了一个常数值。
但是当我查看Built-in Functions of Stylus 时,我找不到任何计算平方根的方法。
有什么方法可以让手写笔计算x 的平方根吗?
当然最好有一些不错的函数,比如LESS has built in.
【问题讨论】:
-
如何用手写笔教数学:stackoverflow.com/questions/21113212
标签: stylus