Utils.modulate(value, [a, a], [b, b], limit)
Translates a number between two ranges. When you enable limit, it will never return a value outside of the b range. The default value for limit is false.
译:在两个范围之间转换一个数字。当您启用限制时,它将永远不会返回一个值以外的“B”的范围内。限制的默认值是“假”。
Arguments(参数)
value— A variable, representing the input. 译:一个变量,表示输入。[a, a]— The first range of two numbers. 译:两个数的第一个范围。[b, b]— The second range of two numbers. 译:两个数的第二范围。limit— A boolean, set to false by default. (Optional) 译:一个布尔值,默认设置为假。(可选)
print Utils.modulate(0.5, [0, 1], [0, 100])
# Output: 50
print Utils.modulate(1, [0, 1], [0, 100])
# Output: 100
You can think of modulate as a way to convert numbers between different scales. Like going from Celcius to Farenheit, or Kilometers to Miles.
译:你可以认为调制是一种在不同尺度间转换数字的方法。喜欢从摄氏度到华氏或公里,英里。
# Convert to Celsius/Farenheit
Utils.modulate(celcius, [0, 100], [32, 212])
It's also useful for creating parallax effects on scroll. For example, you can move a layer from 0-10, when scrolling from 0-100.
译:它也有助于创建滚动的视差效果。例如,你可以移动一层 0-10 ,滚动时从 0-100。
scroll = new ScrollComponent
scrollHorizontal: false
layerA = new Layer
x: 100
# Modulate the scrolling distance
scroll.on Events.Move, ->
y = Utils.modulate(scroll.scrollY, [0,100], [0,10])
layerA.y = y