the left edge of the canvas in screen coordinates
the bottom edge of the canvas in screen coordinates
the simulation coordinate corresponding to screen_left
the simulation coordinate corresponding to screen_bottom
canvas pixels per simulation space unit along x axis
canvas pixels per simulation space unit along y axis
Returns an AffineTransform that maps simulation coordinates to screen coordinates using the mapping defined by this CoordMap.
the AffineTransform equivalent of this CoordMap
Translates a point in screen coordinates to simulation coordinates.
horizontal position in screen coordinates, or GenericVector in screen coordinates
Optional
scr_y: numbervertical position in screen coordinates
the equivalent position in simulation coordinates
Translates the given screen coordinates rectangle into simulation coordinates.
the rectangle in screen coordinates
the equivalent rectangle in simulation coordinates
Translates a point from simulation coordinates to screen coordinates.
the point in simulation coordinates to translate
the point translated to screen coordinates
Translates the given simulation coordinates rectangle into screen coordinates.
the rectangle in simulation coordinates
the equivalent rectangle in screen coordinates
Static
makeCreates a CoordMap that fits a simulation coordinates rectangle inside a screen coordinates rectangle in accordance with alignment options and aspect ratio. Calculates the origin and scale, which define the coordinate mapping.
The mapping is calculated so that the given simulation rectangle transforms to be the largest possible rectangle that fits inside the given screen rectangle, subject to various alignment options. The alignment options are similar to typical word processor alignment options such as left, center, right, or full justification. In the following diagrams the simulation rectangle is the smaller bold bordered rectangle, inside the larger screen rectangle.
┌──────────────────────────────────────────────────┐
│┏━━━━━━━━━━━━━━━━┓ │
│┃ ┃ │
│┃ ┃ │
│┃ x: left ┃ │
│┃ ┃ │
│┃ ┃ │
│┗━━━━━━━━━━━━━━━━┛ │
└──────────────────────────────────────────────────┘
┌──────────────────────────────────────────────────┐
│ ┏━━━━━━━━━━━━━━━━┓ │
│ ┃ ┃ │
│ ┃ ┃ │
│ ┃ x: middle ┃ │
│ ┃ ┃ │
│ ┃ ┃ │
│ ┗━━━━━━━━━━━━━━━━┛ │
└──────────────────────────────────────────────────┘
┌──────────────────────────────────────────────────┐
│ ┏━━━━━━━━━━━━━━━━┓│
│ ┃ ┃│
│ ┃ ┃│
│ ┃ x: right ┃│
│ ┃ ┃│
│ ┃ ┃│
│ ┗━━━━━━━━━━━━━━━━┛│
└──────────────────────────────────────────────────┘
Both horizontal and vertical dimensions (x and y) have alignments. One of x or y will determine the scale and will fully span the screen rectangle; the alignment option only affects the other axis. In the diagrams above, the alignment of the y axis is ignored; the alignment only matters for the x placement.
Suppose the first diagram above had LEFT
horizontal alignment and
TOP
vertical alignment, but then the screen rectangle changed to be tall
and narrow; then we would see the first picture below. Other vertical alignment
options are shown as well.
┌──────────────────┐ ┌──────────────────┐ ┌──────────────────┐
│┏━━━━━━━━━━━━━━━━┓│ │ │ │ │
│┃ ┃│ │ │ │ │
│┃ x: left ┃│ │ │ │ │
│┃ y: top ┃│ │ │ │ │
│┃ ┃│ │┏━━━━━━━━━━━━━━━━┓│ │ │
│┃ ┃│ │┃ ┃│ │ │
│┗━━━━━━━━━━━━━━━━┛│ │┃ x: left ┃│ │ │
│ │ │┃ y: middle ┃│ │ │
│ │ │┃ ┃│ │ │
│ │ │┃ ┃│ │┏━━━━━━━━━━━━━━━━┓│
│ │ │┗━━━━━━━━━━━━━━━━┛│ │┃ ┃│
│ │ │ │ │┃ x: left ┃│
│ │ │ │ │┃ y: bottom ┃│
│ │ │ │ │┃ ┃│
│ │ │ │ │┃ ┃│
│ │ │ │ │┗━━━━━━━━━━━━━━━━┛│
└──────────────────┘ └──────────────────┘ └──────────────────┘
FULL
ensures that for the chosen axis the simulation and screen rectangles
coincide. When both x and y have FULL
, then the simulation and screen rectangles
will coincide but the aspect ratio is altered, so an image from the simulation may
appear squashed or stretched (see definition of aspect ratio below). For example, the
square simulation rectangle from our earlier examples is stretched out here:
┌──────────────────────────────────────────────────┐
│┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓│
│┃ ┃│
│┃ ┃│
│┃ x:full, y:full ┃│
│┃ ┃│
│┃ ┃│
│┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛│
└──────────────────────────────────────────────────┘
When only one of the axes has FULL
, the simulation rectangle
might not entirely fit into the screen rectangle as the following example shows, but
the aspect ratio is preserved.
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ ┃
┃ ┃
┃ ┃
┃ ┃
┃ ┃
┌┃────────────────────────────────────────────────┃┐
│┃ ┃│
│┃ ┃│
│┃ x:full, y:middle ┃│
│┃ ┃│
│┃ ┃│
└┃────────────────────────────────────────────────┃┘
┃ ┃
┃ ┃
┃ ┃
┃ ┃
┃ ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
The aspect ratio is the ratio of 'pixels per simulation unit along y axis' divided
by 'pixels per simulation unit along x axis'. The default aspect ratio is 1.0, so x
and y are treated identically with distance being measured the same in each direction.
An aspect ratio other than 1.0 will squash or stretch the image. Note that aspect
ratio is ignored when both x and y axes have FULL
specified.
The simulation rectangle, screen rectangle, alignment options, and aspect ratio are only used to initially determine the coordinate transformation; they are not stored by the CoordMap.
the screen space rectangle to fit the sim rect into
the simulation space rectangle to be fit into the screenRect
Optional
horizAlign: LEFT | MIDDLE | RIGHT | FULL | VALUEhorizontal alignment option; default is HorizAlign.MIDDLE
Optional
verticalAlign: TOP | MIDDLE | BOTTOM | FULL | VALUEvertical alignment option; default isVerticalAlign.MIDDLE
Optional
aspectRatio: numberthe ratio of 'pixels per simulation unit along y axis' divided by 'pixels per simulation unit along x axis'; default is 1.0
the CoordMap corresponding to the given options
if simRect is empty (has zero area), or invalid alignment options are given.
Generated using TypeDoc
Provides the mapping between screen (canvas) coordinates and simulation coordinates; this is an immutable object.
Screen coordinates corresponds to pixels on an HTML canvas; the vertical coordinate increases going down, with zero usually being the top of the canvas.
Simulation coordinates the vertical coordinate increases going up; units can be any size.
To create a CoordMap you specify translation and scaling factors for going between screen and simulation coordinates. The CoordMap constructor maps the bottom-left point on the canvas to the given bottom-left point in simulation space and then uses the given scaling factors.
The static method CoordMap.make calculates the translation and scaling factors in order to fit a certain rectangle in simulation coords into another rectangle in screen coords.
From David Flanagan, JavaScript: The Definitive Guide, 6th Edition page 869:
Note however that a canvas actually has two coordinate systems:
Essentially the display coordinates can be used to stretch a canvas to fit the screen as desired. Here we ignore display coordinates and regard screen coordinates to be what is called model coordinates in the above quote.
See also Coordinate System When Drawing An Image in DisplayShape.