我正在尝试使用elm-0.15打印鼠标光标与窗口中心的距离。例如,将光标放置在窗口中心必须打印(0,0)。
我的代码是,
import Graphics.Element exposing (show)
import Mouse
import Signal
import Window
relativeMouse : (Int, Int) -> (Int, Int)
relativeMouse (ox, oy) (x,y) = (x - ox, y - oy)
center: (Int, Int) -> (Int, Int)
center (w,h) = (w/2, h/2)
main = Signal.map show <| relativeMouse (Signal.map center Window.dimensions Mouse.position)elm-make basics.elm抛出至少4个单独的type-mismatch errors
如何将多个信号(window.dimensions,Mouse.position)传递给elm 0.15中的一个函数(例如,relativeMouse)?
发布于 2015-07-01 18:58:41
注释是与您所拥有的内容不同的更改:
import Graphics.Element exposing (show)
import Mouse
import Signal
import Window
-- change type signature to match implementation
relativeMouse : (Int, Int) -> (Int, Int) -> (Int, Int)
relativeMouse (ox, oy) (x,y) = (x - ox, y - oy)
-- use // for integer division
center: (Int, Int) -> (Int, Int)
center (w, h) = (w // 2, h // 2)
-- 1) map center over Window.dimensions to get a signal of origin positions
-- 2) map2 relativeMouse over the signal of origin positions and
-- Mouse.position to get signal of relative mouse positions
-- 3) map show over the signal of relative mouse positions to display them
main = Signal.map show (Signal.map2 relativeMouse (Signal.map center Window.dimensions) Mouse.position)要回答最后一个问题:Signal.map2是用来映射两个信号上的一个函数的。对应的映射一直存在到map5。
信号导入线也可以更改为
import Signal exposing ((<~), (~))使用较短的信号映射语法,在这种情况下,主线将更改为
main = show <~ (relativeMouse <~ (center <~ Window.dimensions) ~ Mouse.position)https://stackoverflow.com/questions/31156558
复制相似问题