Difference between revisions of "Lua:Class:Canvas"

From Cheat Engine
Jump to navigation Jump to search
(Initial page creation.)
 
m (Added a fun RGB-Spectrum Code Example to showcase the drawing capabilites.)
 
Line 278: Line 278:
 
</pre>
 
</pre>
  
 +
<pre>
 +
function drawRGBSpectrum(canvas, x, y, width, height)
 +
  if canvas == nil then
 +
    return
 +
  end
 +
 +
  width = math.max(1, width or 256)
 +
  height = math.max(1, height or 32)
 +
 +
  for px = 0, width - 1 do
 +
    local hue = px / math.max(1, width - 1)
 +
    local r, g, b
 +
 +
    if hue < 1 / 6 then
 +
      r = 255
 +
      g = math.floor(hue * 6 * 255)
 +
      b = 0
 +
    elseif hue < 2 / 6 then
 +
      r = math.floor((2 / 6 - hue) * 6 * 255)
 +
      g = 255
 +
      b = 0
 +
    elseif hue < 3 / 6 then
 +
      r = 0
 +
      g = 255
 +
      b = math.floor((hue - 2 / 6) * 6 * 255)
 +
    elseif hue < 4 / 6 then
 +
      r = 0
 +
      g = math.floor((4 / 6 - hue) * 6 * 255)
 +
      b = 255
 +
    elseif hue < 5 / 6 then
 +
      r = math.floor((hue - 4 / 6) * 6 * 255)
 +
      g = 0
 +
      b = 255
 +
    else
 +
      r = 255
 +
      g = 0
 +
      b = math.floor((1 - hue) * 6 * 255)
 +
    end
 +
 +
    local color = r + (g * 0x100) + (b * 0x10000)
 +
 +
    canvas.Pen.Color = color
 +
    canvas.line(x + px, y, x + px, y + height)
 +
  end
 +
end
 +
 +
local form = createForm()
 +
form.Caption = "RGB Spectrum Canvas Example"
 +
form.Width = 400
 +
form.Height = 160
 +
 +
form.OnPaint = function(sender)
 +
  local canvas = sender.Canvas
 +
 +
  drawRGBSpectrum(canvas, 20, 40, 340, 40)
 +
 +
  canvas.Font.Color = 0x000000
 +
  canvas.textOut(20, 90, "RGB Spectrum")
 +
end
 +
 +
form.show()
 +
</pre>
 
{{LuaSeeAlso}}
 
{{LuaSeeAlso}}

Latest revision as of 09:50, 21 June 2026

<> Function

class Canvas : CustomCanvas

The Canvas class represents a drawing surface.

A Canvas inherits from CustomCanvas and Object. It provides access to drawing tools such as Brush, Pen, and Font, and offers methods for drawing shapes, text, images, pixels, and copied regions.

Inheritance[edit]

Class Inherits From Description
Canvas CustomCanvas A drawing surface used to render shapes, text, images, and pixels.
CustomCanvas Object Base class for canvas objects.

Properties[edit]

Property Type Description
Brush Brush The brush object used for filling shapes and areas.
Pen Pen The pen object used for drawing lines and outlines.
Font Font The font object used for drawing text.
Width Integer The width of the canvas.
Height Integer The height of the canvas.
Handle Integer The device context handle of the canvas.

Methods[edit]

Method Return Type Description
getBrush() Brush Returns the brush object of this canvas.
getPen() Pen Returns the pen object of this canvas.
getFont() Font Returns the font object of this canvas.
getWidth() Integer Returns the width of the canvas.
getHeight() Integer Returns the height of the canvas.
getPenPosition() Integer, Integer Returns the current pen position.
setPenPosition(x, y) void Sets the current pen position.
clear() void Clears the canvas.
line(sourcex, sourcey, destinationx, destinationy) void Draws a line from the source coordinates to the destination coordinates.
lineTo(destinationx, destinationy) void Draws a line from the current pen position to the destination coordinates.
moveTo(destinationx, destinationy) void Moves the current pen position to the destination coordinates.
rect(x1, y1, x2, y2) void Draws a rectangle.
fillRect(x1, y1, x2, y2) void Draws a filled rectangle.
roundRect(x1, y1, x2, y2, rx, ry) void Draws a rectangle with rounded corners.
drawFocusRect(x1, y1, x2, y2) void Draws a focus rectangle shape.
textOut(x, y, text) void Draws text at the specified coordinates.
textRect(rect, x, y, text) void Writes text within the given rectangle. The text supports some ANSI escape characters.
getTextWidth(text) Integer Returns the width of the given text.
getTextHeight(text) Integer Returns the height of the given text.
getPixel(x, y) Integer Returns the color value of the pixel at the specified coordinates.
setPixel(x, y, color) void Sets the color value of the pixel at the specified coordinates.
floodFill(x, y, color, filltype) void Fills an area starting at the specified coordinates.
ellipse(x1, y1, x2, y2) void Draws an ellipse within the specified rectangle.
gradientFill(x1, y1, x2, y2, startcolor, stopcolor, direction) void Fills a rectangle with a gradient.
copyRect(dest_x1, dest_y1, dest_x2, dest_y2, sourceCanvas, source_x1, source_y1, source_x2, source_y2) void Draws an image from one canvas to another. This is useful for double buffering.
draw(x, y, graphic) void Draws the image of a specific Graphic class object.
stretchDraw(rect, graphic) void Draws the image of a specific Graphic class object and stretches it to fit inside the given rectangle.
getClipRect() Table Returns a table containing the fields Left, Top, Right, and Bottom, which define the invalidated region of the graphical object.

Flood Fill Types[edit]

Value Description
fsSurface Fills until the specified color is reached. It fills all connected pixels except this color.
fsBorder Fills only connected pixels of the specified color.

Gradient Directions[edit]

Value Direction Description
0 Vertical Creates a vertical gradient.
1 Horizontal Creates a horizontal gradient.

Clip Rect Fields[edit]

Field Type Description
Left Integer The left edge of the invalidated region.
Top Integer The top edge of the invalidated region.
Right Integer The right edge of the invalidated region.
Bottom Integer The bottom edge of the invalidated region.

Examples[edit]

local form = createForm()
form.Caption = "Canvas Example"
form.Width = 400
form.Height = 300

form.OnPaint = function(sender)
  local canvas = sender.Canvas

  canvas.Pen.Color = 0x0000FF
  canvas.Brush.Color = 0xFFFFFF

  canvas.rect(20, 20, 180, 100)
  canvas.textOut(30, 40, "Hello Canvas")
end

form.show()
local form = createForm()
form.Caption = "Canvas Pixel Example"
form.Width = 300
form.Height = 200

form.OnPaint = function(sender)
  local canvas = sender.Canvas

  canvas.setPixel(50, 50, 0x0000FF)

  local color = canvas.getPixel(50, 50)
  canvas.textOut(20, 80, "Pixel color: " .. tostring(color))
end

form.show()
local form = createForm()
form.Caption = "Canvas Clip Rect Example"
form.Width = 400
form.Height = 300

form.OnPaint = function(sender)
  local canvas = sender.Canvas
  local clip = canvas.getClipRect()

  canvas.textOut(20, 20, "Clip Left: " .. tostring(clip.Left))
  canvas.textOut(20, 40, "Clip Top: " .. tostring(clip.Top))
  canvas.textOut(20, 60, "Clip Right: " .. tostring(clip.Right))
  canvas.textOut(20, 80, "Clip Bottom: " .. tostring(clip.Bottom))
end

form.show()
function drawRGBSpectrum(canvas, x, y, width, height)
  if canvas == nil then
    return
  end

  width = math.max(1, width or 256)
  height = math.max(1, height or 32)

  for px = 0, width - 1 do
    local hue = px / math.max(1, width - 1)
    local r, g, b

    if hue < 1 / 6 then
      r = 255
      g = math.floor(hue * 6 * 255)
      b = 0
    elseif hue < 2 / 6 then
      r = math.floor((2 / 6 - hue) * 6 * 255)
      g = 255
      b = 0
    elseif hue < 3 / 6 then
      r = 0
      g = 255
      b = math.floor((hue - 2 / 6) * 6 * 255)
    elseif hue < 4 / 6 then
      r = 0
      g = math.floor((4 / 6 - hue) * 6 * 255)
      b = 255
    elseif hue < 5 / 6 then
      r = math.floor((hue - 4 / 6) * 6 * 255)
      g = 0
      b = 255
    else
      r = 255
      g = 0
      b = math.floor((1 - hue) * 6 * 255)
    end

    local color = r + (g * 0x100) + (b * 0x10000)

    canvas.Pen.Color = color
    canvas.line(x + px, y, x + px, y + height)
  end
end

local form = createForm()
form.Caption = "RGB Spectrum Canvas Example"
form.Width = 400
form.Height = 160

form.OnPaint = function(sender)
  local canvas = sender.Canvas

  drawRGBSpectrum(canvas, 20, 40, 340, 40)

  canvas.Font.Color = 0x000000
  canvas.textOut(20, 90, "RGB Spectrum")
end

form.show()

See also[edit]

Lua
Script Engine