Drawing on screen
In this tutorial you build a small clock in the top right corner: a dark box, the time in bold, and an accent line that pulses. On the way you meet the parts of draw you will use most. The rest of the page lists everything else draw can do.
Drawing needs no permission.
The rules
- Every
drawcall goes inside apainthandler. Anywhere else it raises an error. - Coordinates are overlay pixels, counted from the top left corner.
draw.screen_size()gives the width and height. - Nothing stays on screen by itself.
paintruns every frame and you draw the whole picture each time.
1. A box
events.on("paint", function()
draw.rect_filled(20, 20, 160, 32, color(17, 17, 17, 230), 4)
draw.rect(20, 20, 160, 32, ui.accent(), 1, 4)
end)draw.rect_filled(x, y, w, h, colour, rounding) fills a rectangle. draw.rect draws its outline, here 1 pixel thick with the same 4 pixel rounding. color(r, g, b, a) takes channels from 0 to 255. The fourth one is the opacity and is optional.
2. Text that fits
A box should grow with its text. draw.text_size measures text before you draw it:
events.on("paint", function()
local text = time.date("%H:%M:%S")
local tw, th = draw.text_size(text, "bold")
local w, h = tw + 24, th + 14
local sw = draw.screen_size()
local x, y = sw - w - 20, 20
draw.rect_filled(x, y, w, h, color(17, 17, 17, 230), 4)
draw.text(x + 12, y + 7, text, color(255, 255, 255), {font = "bold"})
end)time.date formats the local time with the usual strftime codes. The box now sits 20 pixels from the right edge whatever the width of the text.
3. Make it move
time.now() is a clock in seconds. Feed it to math.sin and you get a value that swings back and forth:
-- @name Clock
-- @description The time in the top right corner.
events.on("paint", function()
local text = time.date("%H:%M:%S")
local tw, th = draw.text_size(text, "bold")
local w, h = tw + 24, th + 14
local sw = draw.screen_size()
local x, y = sw - w - 20, 20
draw.rect_filled(x, y, w, h, color(17, 17, 17, 230), 4)
draw.text(x + 12, y + 7, text, color(255, 255, 255), {font = "bold"})
local pulse = 0.5 + 0.5 * math.sin(time.now() * 3)
local line = color.alpha(ui.accent(), math.floor(80 + 175 * pulse))
draw.gradient_h(x, y + h - 2, w, 2, line, color(0, 0, 0, 0))
end)color.alpha gives the same colour with another opacity, and draw.gradient_h fades from the left colour to the right one. That is the finished script. Save it as clock.lua and load it.
If you want to move something at a steady speed instead, multiply by time.delta(), the seconds since the last frame.
Colours
You can give a colour in four ways, and every function that takes a colour accepts all of them:
local a = color(143, 108, 240) -- r, g, b
local b = color(143, 108, 240, 128) -- with opacity
local c = "#8f6cf0ff" -- a hex string
local d = {143, 108, 240, 255} -- a tableA few helpers:
| Function | What it does |
|---|---|
color.hex("#8f6cf0") |
Reads a hex colour. |
color.hsv(h, s, v) |
Hue, saturation and value from 0 to 1. color.hsv(time.now() % 1, 1, 1) cycles through the rainbow. |
color.lerp(a, b, t) |
Blends from a to b. t = 0.5 is halfway. |
color.alpha(c, a) |
The same colour with another opacity. |
color.unpack(c) |
Gives back r, g, b, a. |
ui.accent() |
The menu's accent colour. Use it and your script matches the user's theme. |
Shapes
events.on("paint", function()
local white = color(255, 255, 255)
draw.line(40, 100, 140, 100, white, 2)
draw.circle(90, 160, 30, white)
draw.circle_filled(90, 160, 4, ui.accent())
draw.arc(90, 240, 30, -90, 270, white, 2)
draw.triangle(60, 330, 120, 330, 90, 290, color(255, 200, 60), true)
draw.polygon({160, 290, 220, 290, 240, 330, 140, 330}, color(0, 200, 255), true)
draw.polyline({160, 380, 190, 350, 220, 380, 250, 350}, white, false, 2)
draw.gradient_v(280, 100, 80, 60, color(60, 60, 60), color(10, 10, 10))
draw.gradient(280, 180, 80, 60, color(255, 0, 0), color(0, 255, 0), color(0, 0, 255), color(255, 255, 255))
end)| Function | Notes |
|---|---|
draw.line(x1, y1, x2, y2, col, thickness) |
Thickness is optional, 1 by default. |
draw.rect and draw.rect_filled |
Optional thickness and rounding. |
draw.circle and draw.circle_filled |
Optional segment count, 0 picks one for you. |
draw.arc(x, y, r, start, sweep, col, thickness) |
Degrees. 0 points right and angles go clockwise. The sweep can be negative. |
draw.triangle |
Pass true at the end to fill it. |
draw.polygon(points, col, filled, thickness) |
Points as a flat list {x1, y1, x2, y2, ...} or a list of vec2, up to 512. A filled polygon must be convex. |
draw.polyline(points, col, closed, thickness) |
An open line through the points, or closed with true. |
draw.gradient_h, draw.gradient_v |
Two colour gradients, left to right or top to bottom. |
draw.gradient |
Four corners: top left, top right, bottom right, bottom left. No rounding. |
Text
draw.text(x, y, text, col, opts) takes an optional table:
| Option | Values |
|---|---|
font |
A built in name: text, bold, small, esp, label, pixel or icons. Or a font from draw.load_font. |
size |
A pixel size. Leave it out for the font's own size. |
align |
"left", "center" or "right". The text is placed relative to x. |
outline |
true for a dark outline. |
shadow |
true for a drop shadow. |
One call draws at most 1024 bytes of text. draw.text_size(text, font, size) gives width and height for the same font and size.
To use your own font, put a TTF or OTF file in scripts\fonts\ and load it once at the top of the script. A script can load up to 8 fonts.
local big, err = draw.load_font("Inter.ttf", 24)
if not big then log.warn(err) end
events.on("paint", function()
draw.text(20, 100, "custom font", color(255, 255, 255), {font = big or "bold"})
end)Images
draw.load_image reads a PNG or JPEG from the scripts folder. The path is relative to the folder. Load images once at the top, not in paint.
local logo, err = draw.load_image("images/logo.png")
if not logo then log.warn("no logo: " .. err) end
events.on("paint", function()
if logo then
draw.image(logo, 20, 60, 64, 64)
draw.image(logo, 100, 60, 64, 64, color(255, 255, 255, 120), 45)
end
end)The two optional arguments of draw.image are a tint colour and a rotation in degrees around the centre.
Clipping
draw.clip_push(x, y, w, h) cuts off everything drawn after it outside the rectangle, until draw.clip_pop(). If you forget the pop, evasive closes it at the end of paint.
events.on("paint", function()
draw.clip_push(20, 20, 100, 20)
draw.text(20, 22, "this long line stops at 100 pixels", color(255, 255, 255))
draw.clip_pop()
end)The evasive look
Two functions draw pieces in the same style as evasive's own HUD:
local kills = 0
events.on("paint", function()
local y = draw.panel(20, 200, 180, 60, "Stats")
draw.text(28, y + 2, "kills", color(160, 160, 160))
draw.text(192, y + 2, tostring(kills), ui.accent(), {align = "right"})
draw.chip({x = 20, y = 280, label = "KILLS", value = tostring(kills)})
end)draw.panel(x, y, w, h, title) draws the frame, the accent line and the title row, and returns the y of the first row inside it. draw.chip draws a small label and value tag and returns its width and height. Its table also takes accent, meter (0 to 1), icon, label_color and value_color.
Limits
A script can queue up to 8192 draw commands per frame. Past that, further calls are dropped for the frame.
You may see render.draw_text, render.draw_rect and other render functions in older scripts. They are aliases of draw for compatibility. Use draw in new scripts.
Next
Reading players and entities draws things where players are.