use Float for time consistently

This commit is contained in:
Daniel Barlow 2024-11-21 22:15:32 +00:00
parent f17332a047
commit 70a654c472

View File

@ -21,12 +21,11 @@ import Svg.Attributes as S exposing
, x1, y1 , x2, y2 , x1, y1 , x2, y2
, fill , fill
, stroke, strokeWidth, strokeOpacity) , stroke, strokeWidth, strokeOpacity)
import Time -- exposing(Posix) import Time
import Url.Parser exposing (Parser, (<?>), int, map, s, string) import Url.Parser exposing (Parser, (<?>), int, map, s, string)
import Url.Parser.Query as Query import Url.Parser.Query as Query
import Url exposing (Url) import Url exposing (Url)
type Route = Timeline (Maybe Int) (Maybe Int) type Route = Timeline (Maybe Int) (Maybe Int)
routeParser : Parser (Route -> a) a routeParser : Parser (Route -> a) a
@ -147,20 +146,20 @@ type alias Model =
{ centre: Coord { centre: Coord
, zoom: FineZoomLevel , zoom: FineZoomLevel
, drag: Drag , drag: Drag
, startTime : Int , startTime : Float
, duration : Int , duration : Float
, track: TrackState } , track: TrackState }
init : () -> Url -> Nav.Key -> (Model, Cmd Msg) init : () -> Url -> Nav.Key -> (Model, Cmd Msg)
init _ url navKey = init _ url navKey =
let (start, duration) = let (start, duration) =
case Url.Parser.parse routeParser url of case Url.Parser.parse routeParser url of
Just (Timeline (Just s) (Just d)) -> (s, d) Just (Timeline (Just s) (Just d)) -> (toFloat s, toFloat d)
_ -> (10,10) _ -> (10,10)
in in
((Model ((Model
(toCoord (Pos 0 0 Nothing)) (toCoord (Pos 0 0 Nothing))
(FineZoomLevel (1*8)) None 0 0 Loading), (FineZoomLevel (1*8)) None 0 0 Nothing Loading),
(fetchTrack start duration)) (fetchTrack start duration))
-- SUBSCRIPTIONS -- SUBSCRIPTIONS
@ -171,9 +170,9 @@ subscriptions model = Sub.none
fetchTrack start duration = Http.get fetchTrack start duration = Http.get
{ url = ("http://localhost:3000/points?start=" ++ { url = ("http://localhost:3000/points?start=" ++
String.fromInt start ++ String.fromInt (floor start) ++
"&duration=" ++ "&duration=" ++
String.fromInt duration) String.fromInt (ceiling duration))
, expect = Http.expectJson Loaded (D.list Point.decoder) , expect = Http.expectJson Loaded (D.list Point.decoder)
} }
@ -219,14 +218,14 @@ updateModel msg model =
drag = None, drag = None,
startTime = startTime =
let (delta, _) = subTuple start end let (delta, _) = subTuple start end
in model.startTime + delta * model.duration // portalWidth in model.startTime + toFloat delta * model.duration / portalWidth
} }
_ -> model _ -> model
TimeScale factor -> TimeScale factor ->
let fudge = factor let fudge = factor
len = model.duration - floor(fudge) len = model.duration - fudge
in { model | in { model |
startTime = model.startTime + floor(fudge / 2) startTime = model.startTime + fudge / 2
, duration = len , duration = len
} }
@ -240,8 +239,9 @@ updateModel msg model =
| track = Present trk | track = Present trk
, centre = toCoord (Point.centre trk) , centre = toCoord (Point.centre trk)
, zoom = FineZoomLevel ( 13 * 8) , zoom = FineZoomLevel ( 13 * 8)
, startTime = floor start , startTime = start
, duration = ceiling duration , duration = duration
-- , markedTime = Just (start + 300, duration - 600)
} }
Err (Http.BadBody e) -> { model | track = Debug.log e (Failure "e") } Err (Http.BadBody e) -> { model | track = Debug.log e (Failure "e") }
Err e -> { model | track = Debug.log "unknown error" (Failure "e") } Err e -> { model | track = Debug.log "unknown error" (Failure "e") }
@ -521,9 +521,9 @@ ifTrack model content =
case model.track of case model.track of
Present t -> Present t ->
let (dt, _) = dragDelta Graph model.drag let (dt, _) = dragDelta Graph model.drag
dpix = dt * model.duration // portalWidth dpix = toFloat dt * model.duration / portalWidth
start = toFloat (model.startTime + dpix) start = model.startTime + dpix
points = Point.subseq t start (toFloat model.duration) |> points = Point.subseq t start model.duration |>
Point.downsample 300 Point.downsample 300
in content points in content points
Failure f -> Debug.log f (div [] [ Html.text "failure", Html.text f]) Failure f -> Debug.log f (div [] [ Html.text "failure", Html.text f])