Although most of the focus of elm-units is on physical/scientific units,
it's very 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 number 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.
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.
Units type representing an on-screen area of one square pixel. For example, a 32x32 image has an area of 1024 square pixels.
Construct a quantity representing a given number of on-screen pixels:
screenWidth =
pixels 1920Note 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, the result can be used as either an
Int or Float number of pixels.
Convert a Pixels value to a plain number of pixels.
pixelDensity =
pixels 96 |> Quantity.per (Length.inches 1)
Length.centimeters 1
|> Quantity.at pixelDensity
|> inPixels
--> 37.795275590551185Shorthand for Pixels.pixels 1. Can be convenient to use with
Quantity.per.
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 2
dragSpeed =
dragDistance |> Quantity.per elapsedTime
dragSpeed |> 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.
Construct an on-screen area from a number of square pixels.
Convert an on-screen area to a number of square pixels.
area =
pixels 1920 |> Quantity.times (pixels 1080)
area |> inSquarePixels
--> 2073600
Units type representing one on-screen pixel.