Difference between revisions of "Lua:Class:Canvas"
Jump to navigation
Jump to search
m |
m (Syntax Highlighting.) |
||
| Line 222: | Line 222: | ||
===Examples=== | ===Examples=== | ||
| − | < | + | <syntaxhighlight lang="lua" line> |
local form = createForm() | local form = createForm() | ||
form.Caption = "Canvas Example" | form.Caption = "Canvas Example" | ||
| Line 239: | Line 239: | ||
form.show() | form.show() | ||
| − | </ | + | </syntaxhighlight> |
| − | < | + | <syntaxhighlight lang="lua" line> |
local form = createForm() | local form = createForm() | ||
form.Caption = "Canvas Pixel Example" | form.Caption = "Canvas Pixel Example" | ||
| Line 257: | Line 257: | ||
form.show() | form.show() | ||
| − | </ | + | </syntaxhighlight> |
| − | < | + | <syntaxhighlight lang="lua" line> |
local form = createForm() | local form = createForm() | ||
form.Caption = "Canvas Clip Rect Example" | form.Caption = "Canvas Clip Rect Example" | ||
| Line 276: | Line 276: | ||
form.show() | form.show() | ||
| − | </ | + | </syntaxhighlight> |
| − | < | + | <syntaxhighlight lang="lua" line> |
function drawRGBSpectrum(canvas, x, y, width, height) | function drawRGBSpectrum(canvas, x, y, width, height) | ||
if canvas == nil then | if canvas == nil then | ||
| Line 339: | Line 339: | ||
form.show() | form.show() | ||
| − | </ | + | </syntaxhighlight> |
| + | |||
{{LuaSeeAlso}} | {{LuaSeeAlso}} | ||
Latest revision as of 15:38, 25 June 2026
{} Class
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.
Contents
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]
1 local form = createForm()
2 form.Caption = "Canvas Example"
3 form.Width = 400
4 form.Height = 300
5
6 form.OnPaint = function(sender)
7 local canvas = sender.Canvas
8
9 canvas.Pen.Color = 0x0000FF
10 canvas.Brush.Color = 0xFFFFFF
11
12 canvas.rect(20, 20, 180, 100)
13 canvas.textOut(30, 40, "Hello Canvas")
14 end
15
16 form.show()
1 local form = createForm()
2 form.Caption = "Canvas Pixel Example"
3 form.Width = 300
4 form.Height = 200
5
6 form.OnPaint = function(sender)
7 local canvas = sender.Canvas
8
9 canvas.setPixel(50, 50, 0x0000FF)
10
11 local color = canvas.getPixel(50, 50)
12 canvas.textOut(20, 80, "Pixel color: " .. tostring(color))
13 end
14
15 form.show()
1 local form = createForm()
2 form.Caption = "Canvas Clip Rect Example"
3 form.Width = 400
4 form.Height = 300
5
6 form.OnPaint = function(sender)
7 local canvas = sender.Canvas
8 local clip = canvas.getClipRect()
9
10 canvas.textOut(20, 20, "Clip Left: " .. tostring(clip.Left))
11 canvas.textOut(20, 40, "Clip Top: " .. tostring(clip.Top))
12 canvas.textOut(20, 60, "Clip Right: " .. tostring(clip.Right))
13 canvas.textOut(20, 80, "Clip Bottom: " .. tostring(clip.Bottom))
14 end
15
16 form.show()
1 function drawRGBSpectrum(canvas, x, y, width, height)
2 if canvas == nil then
3 return
4 end
5
6 width = math.max(1, width or 256)
7 height = math.max(1, height or 32)
8
9 for px = 0, width - 1 do
10 local hue = px / math.max(1, width - 1)
11 local r, g, b
12
13 if hue < 1 / 6 then
14 r = 255
15 g = math.floor(hue * 6 * 255)
16 b = 0
17 elseif hue < 2 / 6 then
18 r = math.floor((2 / 6 - hue) * 6 * 255)
19 g = 255
20 b = 0
21 elseif hue < 3 / 6 then
22 r = 0
23 g = 255
24 b = math.floor((hue - 2 / 6) * 6 * 255)
25 elseif hue < 4 / 6 then
26 r = 0
27 g = math.floor((4 / 6 - hue) * 6 * 255)
28 b = 255
29 elseif hue < 5 / 6 then
30 r = math.floor((hue - 4 / 6) * 6 * 255)
31 g = 0
32 b = 255
33 else
34 r = 255
35 g = 0
36 b = math.floor((1 - hue) * 6 * 255)
37 end
38
39 local color = r + (g * 0x100) + (b * 0x10000)
40
41 canvas.Pen.Color = color
42 canvas.line(x + px, y, x + px, y + height)
43 end
44 end
45
46 local form = createForm()
47 form.Caption = "RGB Spectrum Canvas Example"
48 form.Width = 400
49 form.Height = 160
50
51 form.OnPaint = function(sender)
52 local canvas = sender.Canvas
53
54 drawRGBSpectrum(canvas, 20, 40, 340, 40)
55
56 canvas.Font.Color = 0x000000
57 canvas.textOut(20, 90, "RGB Spectrum")
58 end
59
60 form.show()
See Also[edit]
Main Pages