Reference

Geometric primitives

Graphics.Vec2 — Type
Vec2(x, y) -> v

Create a Cartesian representation v of a vector (or point) in two dimensions.

source
Graphics.Point — Type
Point(x, y) -> p

Create a Cartesian representation p of a point in two dimensions. Point is an alias of Vec2.

source
Graphics.BoundingBox — Type
BoundingBox(xmin, xmax, ymin, ymax) -> bb

Create a representation bb of a rectangular region, specifying the coordinates of the horizontal (x) and vertical (y) edges.

source

Geometry API

Graphics.aspect_ratio — Function
aspect_ratio(bb::BoundingBox) -> r

Compute the ratio r of the height and width of bb.

Example

julia> bb = BoundingBox(Point(0, 0), Point(1920, 1080)); # landscape

julia> aspect_ratio(bb)
0.5625

julia> rationalize(ans)
9//16
source
Graphics.center — Function
center(obj) -> p::Point

Compute the center coordinate of obj.

Note

The fallback implementation of this function returns the center of bounding box, not the geometric center, or centroid.

source
Graphics.deform — Function
deform(bb::BoundingBox, Δl, Δr, Δt, Δb) -> bbnew

Add Δl (left), Δr (right), Δt (top), and Δb (bottom) to the edges of a BoundingBox. The sign of each value follows the positive direction of the axis. The "top" and "bottom" are representations when the y-axis is directed downward.

Example

julia> bb = BoundingBox(Point(1, 1), Point(10, 10));

julia> bbnew = deform(bb, 0.5, -1.0, -0.25, 1.0);

julia> xrange(bbnew), yrange(bbnew)
((1.5, 9.0), (0.75, 11.0))
source
Graphics.diagonal — Function
diagonal(obj) -> diag

Get the diagonal length of obj. The fallback implementation of this function returns the diagonal length of the bounding box of obj.

source
Graphics.isinside — Function
isinside(bb::BoundingBox, p::Point) -> tf::Bool
isinside(bb::BoundingBox, x, y) -> tf::Bool

Determine whether the point lies within bb.

source
Graphics.shift — Function
shift(bb::BoundingBox, Δx, Δy) -> bbnew

Shift center by (Δx, Δy), keeping width & height fixed.

source
Graphics.xmin — Function
xmin(obj) -> xmin

Get the minimum x coordinate of the bounding box of obj.

source
Graphics.xmax — Function
xmax(obj) -> xmax

Get the maximum x coordinate of the bounding box of obj.

source
Graphics.ymin — Function
ymin(obj) -> ymin

Get the minimum x coordinate of the bounding box of obj.

source
Graphics.ymax — Function
ymax(obj) -> ymax

Get the maximum y coordinate of the bounding box of obj.

source
Graphics.xrange — Function
xrange(obj) -> (xmin, xmax)

Get the horizontal range of the bounding box that minimally contains obj.

source
Graphics.yrange — Function
yrange(obj) -> (ymin, ymax)

Get the vertical range of the bounding box that minimally contains obj.

source

2d drawing contexts

Graphics.getgc — Function
getgc(obj) -> gc::GraphicContext

Get a GraphicsContext from something that might be drawable.

source

Coordinate systems

Graphics.set_coordinates — Function
set_coordinates(gc::GraphicsContext, device::BoundingBox, user::BoundingBox)
set_coordinates(gc::GraphicsContext, user::BoundingBox)

Set the device->user coordinate transformation of c so that device, expressed in "device coordinates" (pixels), is equivalent to user as expressed in "user coordinates". If device is omitted, it defaults to the full span of gc, BoundingBox(0, width(gc), 0, height(gc)).

source
Graphics.rotate — Function
rotate(p::Vec2, angle::Real, o::Vec2 = Vec2(0, 0)) -> pnew::Vec2

Rotate p around o by angle (in radians).

Note

The direction of rotation for positive angles is from the positive x-axis toward the positive y-axis. For example, the direction of rotation is "clockwise" when the x-axis is directed right and the y-axis is directed downward.

Example

julia> rotate(Vec2(2, 1), 0.5π, Vec2(1, 1))
Vec2(1.0, 2.0)
source
rotate(bb::BoundingBox, angle::Real, o::Point) -> bbnew

Rotate bb around o by angle (in radians), returning the BoundingBox that encloses the vertices of the rotated box.

source
rotate(gc::GraphicsContext, angle)

Rotate the user-space axes by angle (in radians). The rotation takes places after any existing transformation.

See also: rotate(p::Vec2, angle::Real, o::Vec2).

source
Graphics.scale — Function
scale(gc::GraphicsContext, sx, sy)

Scale the user-space x-axis and y-axis by sx and sy respectively. The scaling takes places after any existing transformation.

source
Graphics.translate — Function
translate(gc::GraphicsContext, Δx, Δy)

Translate the user-space origin by (Δx, Δy). The translation takes places after any existing transformation.

source
Graphics.user_to_device_distance! — Function
user_to_device_distance!(gc::GraphicsContext, d::Vector{Float64})

Transform a distance vector d from the user space to the device space. This function is similar to the device_to_user! except that the translation components will be cancelled.

source
Graphics.device_to_user_distance! — Function
device_to_user_distance!(gc::GraphicsContext, d::Vector{Float64})

Transform a distance vector d from the device space to the user space. This function is similar to the user_to_device! except that the translation components will be cancelled.

source

Lines

Graphics.set_line_width — Function
set_line_width(gc::GraphicsContext, w)

Set the current line width (in device-depended units). The actual width and aspect-ratio on the screen may be affected by the transformation.

source
Graphics.set_dash — Function
set_dash(gc::GraphicsContext, dashes::Vector{Float64}, offset)

Set the dash pattern. The dashes is a Vector of positive lengths. The odd-numbered elements represent the length of the "on" state, and the even-numbered elements represent the length of the "off" (blank) state. The offset specifies an offset at which the stroke begins. If dashes is empty, dashing is disabled.

source

Colors and painting (drawing attributes)

Graphics.set_source — Function
set_source(gc::GraphicsContext, src)

Set the source pattern to src. If the src is a Color, it is used as the source color.

source
Graphics.set_source_rgb — Function
set_source_rgb(gc::GraphicsContext, r, g, b)

Set the source pattern to an opaque color RGB(r, g, b). The color components are in the range [0, 1], not [0, 255].

See also: set_source_rgba.

source
Graphics.set_source_rgba — Function
set_source_rgba(gc::GraphicsContext, r, g, b, a)

Set the source pattern to a transparent color RGBA(r, g, b, a). The color and alpha components are in the range [0, 1], not [0, 255].

See also: set_source_rgb.

source
Graphics.save — Function
save(gc::GraphicsContext)

Save the copy of current context gc. The context is saved onto an internal stack, for example.

See also: restore

Warning

The function name save conflicts with the save in the FileIO package, which is likely to be used with the Graphics package.

source
Graphics.restore — Function
restore(gc::GraphicsContext)

Restore the context saved by a preceding save. The state at the time of the call is overwritten with the restored context, and the restored context is removed from an internal stack, for example.

source

Clipping

Graphics.clip_preserve — Function
clip_preserve(gc::GraphicsContext)

Set a new clipping region based on the current path and fill within the context. Unlike the clip function, this function preserves the path within the context.

source
Graphics.inner_canvas — Function
inner_canvas(gc::GraphicsContext, device::BoundingBox, user::BoundingBox)
inner_canvas(gc::GraphicsContext, x, y, w, h, l, r, t, b)

Create a rectangular drawing area inside device (represented in device-coordinates), giving it user-coordinates user. Any drawing that occurs outside this box is clipped.

x, y, w, and h are an alternative parametrization of device, and l, r, t, b parametrize user.

See also: set_coordinates.

source

Paths

Graphics.line_to — Function
line_to(gc::GraphicsContext, x, y)

Add a line to the current path from the current point to the position (x, y). The current point will be moved to (x, y).

See also: rel_line_to.

source
Graphics.rel_line_to — Function
rel_line_to(gc::GraphicsContext, Δx, Δy)

Add a line to the current path from the current point of (x, y) to the position (x + Δx, y + Δy). The current point will be moved to (x + Δx, y + Δy).

See also: line_to.

source
Graphics.rel_move_to — Function
rel_move_to(gc::GraphicsContext, Δx, Δy)

Begin a new sub-path. The current point will be moved to (x + Δx, y + Δy), where (x, y) is the previous current point.

See also: move_to.

source
Graphics.new_path — Function
new_path(gc::GraphicsContext)

Clear the current path.

Note

Depending on the backend, the new path may actually begin when a path element is added.

source
Graphics.close_path — Function
close_path(gc::GraphicsContext)

Add a line to the current path from the current point to the beginning of the current sub-path and closes the sub-path. The current point will be changed to the joined point.

There is a difference between closing a subpath and drawing a line to the equivalent coordinate. This difference might be visualized as a difference in drawing stroke endpoints.

source
Graphics.arc — Function
arc(gc::GraphicsContext, xc, yc, radius, angle1, angle2)

Add a circular arc with the specified radius to the current path. The arc is centered at (xc, yc), begins at angle1 and ends at angle2. The angle1 and angle2 are in radians. The arc will be drawn in the direction of increasing angles.

source

High-level paths

Graphics.rectangle — Function
rectangle(gc::GraphicsContext, x, y, width, height)
rectangle(gc::GraphicsContext, user::BoundingBox)

Add a sub-path rectangle to the current path. The x and y specify the (typically the upper left) corner coordinate of the rectangle, and the width and height specify the size.

You can also specify the position and size by a BoundingBox in user-space coordinate.

source
Graphics.circle — Function
circle(gc::GraphicsContext, x, y, r)

Add a sub-path circle to the current path. The x and y specify the center coordinate of the circle, and the r specifies the radius.

source
Graphics.polygon — Function
polygon(gc::GraphicsContext, verts::Matrix, idx::Vector)

Add a closed sub-path polygon with the given vertices. The verts is a collection of vertex coordinates in the following matrix form:

[x1 x2 x3 ... xn;
 y1 y2 y3 ... yn]

The idx is a vector of vertex indices, i.e. the matrix column numbers.

Tip

You can reuse the vertex coordinates by specifying the same index in idx. This is useful when drawing meshes.

source
polygon(gc::GraphicsContext, points::AbstractVector)

Add a closed sub-path polygon with the given vertices to .

source

Fill and stroke

Base.fill — Function
fill(gc::GraphicsContext)

Fill the current path according to the current fill rule. The current path will be cleared from the context.

See also: fill_preserve.

source
Graphics.fill_preserve — Function
fill_preserve(gc::GraphicsContext)

Fill the current path according to the current fill rule. Unlike the fill function, this function preserves the current path within the context.

source
Graphics.paint — Function
paint(gc::GraphicsContext)

Paint the current source everywhere within the current clipping region.

source
Graphics.stroke — Function
stroke(gc::GraphicsContext)

Stroke the current path according to the current stroke style. The current path will be cleared from the context.

Note

The stroke function ignores the current transformation. If you want to apply the current transformation to the stroke, use stroke_transformed.

See also: stroke_preserve.

source
Graphics.stroke_preserve — Function
stroke_preserve(gc::GraphicsContext)

Stroke the current path according to the current stroke style. Unlike the stroke function, this function preserves the current path within the context.

Note

The stroke_preserve function ignores the current transformation. If you want to apply the current transformation to the stroke, use stroke_transformed_preserve.

source
Graphics.stroke_transformed — Function
stroke_transformed(gc::GraphicsContext)

Stroke the current path according to the current stroke style and the current transformation.

See also: stroke.

source