Class: Toybox.Graphics.Dc

Inherits:
Toybox.Lang.Object show all

Overview

This class represents a device context.

It provides methods to perform drawing operations on the device.

Note:

You should never directly instantiate a Dc object, or attempt to render to the screen outside of an onUpdate call.

Example:

Draws a blue rectangle using direct pixel parameters.

using Toybox.Graphics;
function onUpdate(dc) {
    dc.setColor(Graphics.COLOR_BLUE, Graphics.COLOR_BLACK);
    dc.fillRectangle(100, 100, 100, 100);
}

Example:

Draws a red circle using direct pixel parameters.

using Toybox.Graphics;
function onUpdate(dc) {
    dc.setColor(Graphics.COLOR_RED, Graphics.COLOR_BLACK);
    dc.fillCircle(50, 100, 75);
}

Example:

Draws "Hello World" in the center of the screen using calls to the Device Context (dc).

using Toybox.Graphics;
function onUpdate(dc) {
    dc.setColor(Graphics.COLOR_BLACK, Graphics.COLOR_TRANSPARENT);
    dc.drawText(
        dc.getWidth() / 2,                      // gets the width of the device and divides by 2
        dc.getHeight() / 2,                     // gets the height of the device and divides by 2
        Graphics.FONT_LARGE,                    // sets the font size
        "Hello World",                          // the String to display
        Graphics.TEXT_JUSTIFY_CENTER            // sets the justification for the text
                );
}

Example:

Clears the screen of device with background color (Graphics.COLOR_BLACK).

using Toybox.Graphics;
function onUpdate(dc) {
    dc.setColor(Graphics.COLOR_BLACK, Graphics.COLOR_BLACK);
    dc.clear();
}

Since:

1.0.0

Instance Method Summary collapse

Instance Method Details

clear()

Erase the screen using the background color.

Note:

Starting form version 3.1.0, COLOR_TRANSPARENT will also be honored as background color, which will cause the value of pixels in the clip region to be replaced by COLOR_TRANSPARENT. For example, this can be used to clear the transparent overlay layer so animation background is visible.

Since:

1.0.0

clearClip()

Reset the drawable area to the full area of the Dc.

Since:

2.3.0

drawArc(x, y, r, attr, degreeStart, degreeEnd)

Draw an arc.

  • 0 degrees: 3 o'clock position.

  • 90 degrees: 12 o'clock position.

  • 180 degrees: 9 o'clock position.

  • 270 degrees: 6 o'clock position.

Parameters:

Since:

1.2.0

drawBitmap(x, y, bitmap)

Draw a bitmap to the screen.

Parameters:

Since:

1.0.0

Throws:

drawCircle(x, y, radius)

Draw a circle around a point.

Parameters:

Since:

1.0.0

drawEllipse(x, y, a, b)

Draw an ellipse around a point.

Parameters:

Since:

1.0.0

drawLine(x1, y1, x2, y2)

Draw a line between two points.

Parameters:

Since:

1.0.0

drawPoint(x, y)

Draw a point on the screen.

Parameters:

Since:

1.0.0

drawRectangle(x, y, width, height)

Draw a rectangle.

Parameters:

Since:

1.0.0

drawRoundedRectangle(x, y, width, height, radius)

Draw a rounded rectangle.

Parameters:

Since:

1.0.0

drawText(x, y, font, text, justification)

Draw text at the given location.

This method is not supported for anti-aliased fonts (including most built in fonts) for a BufferedBitmap that has a palette.

Parameters:

  • x(Toybox.Lang.Number)

    The x location of the text

  • y(Toybox.Lang.Number)

    The y location of the text

  • font(Toybox.WatchUi.FontResource)

    The font to use. This can be a custom font loaded from resources or a Graphics.FONT_* value

  • text(Toybox.Lang.String)

    The String to render. This can be a String loaded from resources or a hard coded String

  • justification

    Mask of Graphics.TEXT_JUSTIFY_* constants. This may either be a single Graphics.TEXT_JUSTIFY_* constant, or a combination of one vertical and one horizontal justification value as a bit mask (e.g., Graphics.TEXT_JUSTIFY_CENTER | Graphics.TEXT_JUSTIFY_VCENTER).

Since:

1.0.0

Throws:

fillCircle(x, y, radius)

Fill a circle with the foreground color.

Parameters:

Since:

1.0.0

fillEllipse(x, y, a, b)

Fill an ellipse with the foreground color.

Parameters:

Since:

1.0.0

fillPolygon(pts)

Fill a polygon with the foreground color.

Parameters:

Since:

1.0.0

fillRectangle(x, y, width, height)

Fill a rectangle with the foreground color.

Parameters:

Since:

1.0.0

fillRoundedRectangle(x, y, width, height, radius)

Fill a rounded rectangle with the foreground color.

Parameters:

Since:

1.0.0

getFontHeight(font)Toybox.Lang.Number

Get the height of a font.

Parameters:

Returns:

Since:

1.0.0

getHeight()Toybox.Lang.Number

Get the height of the display region that is available to the app.

Returns:

Since:

1.0.0

getTextDimensions(text, font)Toybox.Lang.Array

Get the width and height of a String.

This takes new lines into account when determining the height. The width is the maximum width for a given line of the String. If a String has two newline characters (\\n) in it, the height would be for three lines and the width would be the width of the longest String.

Parameters:

Returns:

Since:

1.0.0

getTextWidthInPixels(text, font)Toybox.Lang.Number

Get the width of a String.

Parameters:

Returns:

Since:

1.0.0

getWidth()Toybox.Lang.Number

Get the width of the display region that is available to the app.

Returns:

Since:

1.0.0

setAntiAlias(enabled)

Enable anti-aliased drawing for primitives This method is not supported for a BufferedBitmap that has a palette.

Parameters:

Supported Devices:

Since:

3.2.0

Throws:

setClip(x, y, width, height)

Apply a clipping region to the Dc.

Pixels outside of the region will not be affected by any operations.

Parameters:

Since:

2.3.0

setColor(foreground, background)

Set the current foreground and background colors.

Parameters:

  • foreground(Toybox.Lang.Number)

    Graphics.COLOR_* constant or 24-bit integer of the form 0xRRGGBB.

  • background(Toybox.Lang.Number)

    Graphics.COLOR_* constant or 24-bit integer of the form 0xRRGGBB.

Since:

1.0.0

setPenWidth(width)

Set the width of a line.

Parameters:

Since:

1.0.0


Generated Oct 21, 2020 12:46:17 PM