Drawing shapes and text on the screen (uses vita2d)
More...
Everything drawn with this library (and with images, ImGui, etc.) is drawn to a hidden buffer first. Nothing appears on the screen until you call draw.swapbuffers(), which shows the finished frame and clears the buffer for the next one. Because of this, a LifeLua app is usually one big loop:
local white = color.new(255, 255, 255)
while true do
draw.text(10, 10, "Hello World!", white)
draw.rect(10, 40, 100, 50, color.new(255, 0, 0))
draw.swapbuffers()
end
The screen is 960 × 544 pixels. (0, 0) is the top-left corner, x grows to the right and y grows downwards.
Every parameter written as color accepts either a color value made with color.new() or a plain number in 0xAABBGGRR order (see tocolor()).
|
| nil | draw.swapbuffers (color color) |
| |
| nil | draw.text (number x, number y, string text, color color, number size, font font) |
| |
| number | draw.textwidth (string text, number size, font font) |
| |
| number | draw.textheight (string text, number size, font font) |
| |
| nil | draw.rect (number x, number y, number width, number height, color color, color outline) |
| |
| nil | draw.circle (number x, number y, number radius, color color) |
| |
| nil | draw.radialcircle (number x, number y, number radius, color center, color edge, number smoothness) |
| |
| nil | draw.triangle (number x1, number y1, number x2, number y2, number x3, number y3, color c1, color c2, color c3) |
| |
| nil | draw.line (number start_x, number start_y, number end_x, number end_y, color color) |
| |
| nil | draw.gradientline (number start_x, number start_y, number end_x, number end_y, color start_color, color end_color) |
| |
| nil | draw.polyline (number x1, number y1, color color1, number x2, number y2, color color2,...) |
| |
| nil | draw.pixel (number x, number y, color color) |
| |
| nil | draw.gradientrect (number x, number y, number width, number height, color top_left, color top_right, color bottom_left, color bottom_right) |
| |
| nil | draw.vdoublegradientrect (number x, number y, number width, number height, color top_left, color top_right, color center_left, color center_right, color bottom_left, color bottom_right) |
| |
| nil | draw.hdoublegradientrect (number x, number y, number width, number height, color left_top, color center_top, color right_top, color left_bottom, color center_bottom, color right_bottom) |
| |
| nil | draw.enableclip (boolean enable) |
| |
| nil | draw.cliprect (number x1, number y1, number x2, number y2) |
| |
| nil | draw.clipcircle (number x, number y, number radius) |
| |
◆ draw.circle()
| nil draw.circle |
( |
number | x, |
|
|
number | y, |
|
|
number | radius, |
|
|
color | color ) |
Draws a filled circle
- Parameters
-
| x | X position of the center |
| y | Y position of the center |
| radius | Radius in pixels |
| color | Fill color |
◆ draw.clipcircle()
| nil draw.clipcircle |
( |
number | x, |
|
|
number | y, |
|
|
number | radius ) |
Sets a circular clipping area. Works like draw.cliprect(), but the visible area is a circle.
- Parameters
-
| x | X of the circle's center |
| y | Y of the circle's center |
| radius | Radius of the circle |
◆ draw.cliprect()
| nil draw.cliprect |
( |
number | x1, |
|
|
number | y1, |
|
|
number | x2, |
|
|
number | y2 ) |
Sets a rectangular clipping area, useful for cutting off long text or making scrollable areas. Only has an effect while clipping is turned on with draw.enableclip().
- Parameters
-
| x1 | Left edge of the area |
| y1 | Top edge of the area |
| x2 | Right edge of the area (this is a position, not a width) |
| y2 | Bottom edge of the area (this is a position, not a height) |
- Example:
draw.enableclip(true)
draw.cliprect(10, 10, 110, 30) -- a 100 × 20 area at (10, 10)
draw.text(10, 10, "This long text gets cut off at x = 110", white)
draw.enableclip(false)
◆ draw.enableclip()
| nil draw.enableclip |
( |
boolean | enable | ) |
|
Turns clipping on or off. While clipping is on, only the area set with draw.cliprect() or draw.clipcircle() can be drawn on; everything outside of it is cut off.
- Parameters
-
| enable | true to turn clipping on, false to turn it off |
◆ draw.gradientline()
| nil draw.gradientline |
( |
number | start_x, |
|
|
number | start_y, |
|
|
number | end_x, |
|
|
number | end_y, |
|
|
color | start_color, |
|
|
color | end_color ) |
Draws a 1 pixel wide line that fades from one color to another
- Parameters
-
| start_x | X of the start point |
| start_y | Y of the start point |
| end_x | X of the end point |
| end_y | Y of the end point |
| start_color | Color at the start point |
| end_color | Color at the end point |
◆ draw.gradientrect()
| nil draw.gradientrect |
( |
number | x, |
|
|
number | y, |
|
|
number | width, |
|
|
number | height, |
|
|
color | top_left, |
|
|
color | top_right, |
|
|
color | bottom_left, |
|
|
color | bottom_right ) |
Draws a rectangle with a different color in each corner, shaded between them
- Parameters
-
| x | Left position |
| y | Top position |
| width | Width in pixels |
| height | Height in pixels |
| top_left | Top-left color |
| top_right | Top-right color |
| bottom_left | Bottom-left color |
| bottom_right | Bottom-right color |
- Example:
-- Horizontal gradient from red to blue
draw.gradientrect(0, 0, 960, 60, red, blue, red, blue)
◆ draw.hdoublegradientrect()
| nil draw.hdoublegradientrect |
( |
number | x, |
|
|
number | y, |
|
|
number | width, |
|
|
number | height, |
|
|
color | left_top, |
|
|
color | center_top, |
|
|
color | right_top, |
|
|
color | left_bottom, |
|
|
color | center_bottom, |
|
|
color | right_bottom ) |
Draws a rectangle shaded in two halves side by side: the left half goes from the left colors to the center colors, the right half from the center colors to the right colors
- Parameters
-
| x | Left position |
| y | Top position |
| width | Width in pixels |
| height | Height in pixels |
| left_top | Top-left color |
| center_top | Top-middle color |
| right_top | Top-right color |
| left_bottom | Bottom-left color |
| center_bottom | Bottom-middle color |
| right_bottom | Bottom-right color |
◆ draw.line()
| nil draw.line |
( |
number | start_x, |
|
|
number | start_y, |
|
|
number | end_x, |
|
|
number | end_y, |
|
|
color | color ) |
Draws a 1 pixel wide line
- Parameters
-
| start_x | X of the start point |
| start_y | Y of the start point |
| end_x | X of the end point |
| end_y | Y of the end point |
| color | Line color |
◆ draw.pixel()
| nil draw.pixel |
( |
number | x, |
|
|
number | y, |
|
|
color | color ) |
Draws a single pixel
- Parameters
-
| x | X position |
| y | Y position |
| color | Pixel color |
◆ draw.polyline()
| nil draw.polyline |
( |
number | x1, |
|
|
number | y1, |
|
|
color | color1, |
|
|
number | x2, |
|
|
number | y2, |
|
|
color | color2, |
|
|
| ... ) |
Draws lines connecting several points, one after another. Each point is given as 3 values: x, y, color. At least 2 points are required. Each line fades between the colors of the 2 points it connects.
- Parameters
-
| x1 | X of the 1st point |
| y1 | Y of the 1st point |
| color1 | Color of the 1st point |
| x2 | X of the 2nd point |
| y2 | Y of the 2nd point |
| color2 | Color of the 2nd point |
| ... | More points, 3 values each |
- Example:
-- A white-blue-green zigzag
draw.polyline(10, 10, white, 50, 10, blue, 30, 70, green)
◆ draw.radialcircle()
| nil draw.radialcircle |
( |
number | x, |
|
|
number | y, |
|
|
number | radius, |
|
|
color | center, |
|
|
color | edge, |
|
|
number | smoothness ) |
Draws a filled circle with a gradient from its center to its edge
- Parameters
-
| x | X position of the center |
| y | Y position of the center |
| radius | Radius in pixels |
| center | Color in the center |
| edge | Color at the edge |
| smoothness | Number of segments used for the edge, higher is smoother (optional, default: 64, minimum: 3) |
◆ draw.rect()
| nil draw.rect |
( |
number | x, |
|
|
number | y, |
|
|
number | width, |
|
|
number | height, |
|
|
color | color, |
|
|
color | outline ) |
Draws a filled rectangle
- Parameters
-
| x | Left position |
| y | Top position |
| width | Width in pixels |
| height | Height in pixels |
| color | Fill color |
| outline | Color of a 1 pixel border around the rectangle (optional, no border if omitted) |
◆ draw.swapbuffers()
| nil draw.swapbuffers |
( |
color | color | ) |
|
Shows everything that was drawn since the last call on the screen, then clears the drawing buffer. Call this once at the end of every frame of your main loop.
- Parameters
-
| color | The color the next frame starts with (optional, default: black) |
◆ draw.text()
| nil draw.text |
( |
number | x, |
|
|
number | y, |
|
|
string | text, |
|
|
color | color, |
|
|
number | size, |
|
|
font | font ) |
Draws text on the screen
After the 4 required parameters, you can choose the font and the size in any of these ways:
draw.text(x, y, text, color): system font (PGF), size 1
draw.text(x, y, text, color, size): system font (PGF) at size
draw.text(x, y, text, color, font) or draw.text(x, y, text, color, font, size): a font loaded with font.load()
draw.text(x, y, text, color, size, font): same as above, with the size first
draw.text(x, y, text, color, true) / draw.text(x, y, text, color, true, size): system PVF font (false = system PGF font)
draw.text(x, y, text, color, size, true): same as above, with the size first
- Parameters
-
| x | Left position of the text |
| y | Top position of the text |
| text | The text to draw |
| color | Color of the text |
| size | Text scale, 1 is the normal size (optional, default: 1) |
| font | A font from font.load(), or a boolean to pick the system PVF (true) or PGF (false) font (optional) |
- Example:
local white = color.new(255, 255, 255)
local myfont = font.load("app0:font.ttf")
draw.text(10, 10, "System font", white)
draw.text(10, 40, "Twice as big", white, 2)
draw.text(10, 100, "Custom font", white, myfont)
◆ draw.textheight()
| number draw.textheight |
( |
string | text, |
|
|
number | size, |
|
|
font | font ) |
Gets how tall a text will be when drawn with draw.text()
Accepts the same size/font combinations as draw.textwidth().
- Parameters
-
| text | The text to measure |
| size | Text scale (optional, default: 1) |
| font | A font from font.load(), or true/false for the system PVF/PGF font (optional) |
- Returns
- The height in pixels
◆ draw.textwidth()
| number draw.textwidth |
( |
string | text, |
|
|
number | size, |
|
|
font | font ) |
Gets how wide a text will be when drawn with draw.text()
Accepts the same size/font combinations as draw.text(), e.g. draw.textwidth(text), draw.textwidth(text, size), draw.textwidth(text, font), draw.textwidth(text, font, size), draw.textwidth(text, true), etc.
- Parameters
-
| text | The text to measure |
| size | Text scale (optional, default: 1) |
| font | A font from font.load(), or true/false for the system PVF/PGF font (optional) |
- Returns
- The width in pixels
- Example:
-- Center a text horizontally
local text = "Centered!"
draw.text(480 - draw.textwidth(text) / 2, 10, text, color.new(255, 255, 255))
◆ draw.triangle()
| nil draw.triangle |
( |
number | x1, |
|
|
number | y1, |
|
|
number | x2, |
|
|
number | y2, |
|
|
number | x3, |
|
|
number | y3, |
|
|
color | c1, |
|
|
color | c2, |
|
|
color | c3 ) |
Draws a filled triangle. If you give 3 colors, each corner gets its own color and the triangle is shaded between them.
- Parameters
-
| x1 | X of the 1st corner |
| y1 | Y of the 1st corner |
| x2 | X of the 2nd corner |
| y2 | Y of the 2nd corner |
| x3 | X of the 3rd corner |
| y3 | Y of the 3rd corner |
| c1 | Color of the 1st corner (or of the whole triangle) |
| c2 | Color of the 2nd corner (optional, default: c1) |
| c3 | Color of the 3rd corner (optional, default: c1) |
◆ draw.vdoublegradientrect()
| nil draw.vdoublegradientrect |
( |
number | x, |
|
|
number | y, |
|
|
number | width, |
|
|
number | height, |
|
|
color | top_left, |
|
|
color | top_right, |
|
|
color | center_left, |
|
|
color | center_right, |
|
|
color | bottom_left, |
|
|
color | bottom_right ) |
Draws a rectangle shaded in two halves stacked vertically: the top half goes from the top colors to the center colors, the bottom half from the center colors to the bottom colors
- Parameters
-
| x | Left position |
| y | Top position |
| width | Width in pixels |
| height | Height in pixels |
| top_left | Top-left color |
| top_right | Top-right color |
| center_left | Middle-left color |
| center_right | Middle-right color |
| bottom_left | Bottom-left color |
| bottom_right | Bottom-right color |