Although most of the focus of elm-units is on physical/scientific units,
it's often useful to be able to safely convert back and forth between (for
example) Length values in the real world and on-screen lengths in
pixels.
This module provides a standard Pixels units type and basic functions for
constructing/converting values of type Quantity Int Pixels or
Quantity Float Pixels, which allows you to do things like represent
conversions between real-world and on-screen lengths as rates of change.
This in turn means that all the normal Quantity functions can be
used to convert between pixels and other units, or even do type-safe math
directly on pixel values.
Construct a quantity representing an integer number of on-screen pixels:
screenWidth =
Pixels.int 1920Construct a quantity representing a floating-point number of on-screen pixels:
lineWeight =
Pixels.float 1.5Convert an integer number of pixels back into a plain Int:
Pixels.int 1920
|> Quantity.multiplyBy 2
|> Pixels.toInt
--> 3840Convert a floating-point number of pixels back into a plain Float:
pixelDensity =
Pixels.float 96 |> Quantity.per (Length.inches 1)
Length.centimeters 1
|> Quantity.at pixelDensity
|> Pixels.toFloat
--> 37.795275590551185Generic version of Pixels.int/Pixels.float, for consistency with other
modules like Length. Note that passing an Int will give you a
Quantity Int Pixelswhile passing a Float will give you a
Quantity Float PixelsIf you pass a literal integer like 1920, you will get a generic Quantity
number Pixels which can be used as either an Int or Float number of
pixels.
Convert a Pixels value to a plain number of pixels. This is a generic
version of Pixels.toInt/Pixels.toFloat.
Shorthand for Pixels.pixels 1. Can be convenient to use with
Quantity.per.
Units type representing an on-screen speed of one pixel per second.
Units type representing an on-screen acceleration of one pixel per second squared.
Construct an on-screen speed from a number of pixels per second.
Convert an on-screen speed to a number of pixels per second.
elapsedTime =
Duration.milliseconds 16
dragDistance =
Pixels.float 2
dragSpeed =
dragDistance |> Quantity.per elapsedTime
dragSpeed |> Pixels.inPixelsPerSecond
--> 125Construct an on-screen acceleration from a number of pixels per second squared.
Convert an on-screen acceleration to a number of pixels per second squared.
Units type representing an on-screen area of one square pixel. For example, a 32x32 image has an area of 1024 square pixels.
Construct an on-screen area from a number of square pixels.
Convert an on-screen area to a number of square pixels.
area =
Pixels.int 1928 |> Quantity.times (Pixels.int 1080)
area |> Pixels.inSquarePixels
--> 2073600
Units type representing one on-screen pixel.