Skip to content

Commit 6f714d9

Browse files
committed
Document canvas library
1 parent 8665ccf commit 6f714d9

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed

docs/Runtime Environment/Canvas.md

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
---
2+
sidebar_position: 3
3+
---
4+
Must be included via `require`.
5+
6+
---
7+
### `canvas.new`
8+
Creates a new canvas instance.
9+
#### Parameters
10+
1. Width.
11+
2. Height.
12+
13+
---
14+
### `canvas.bmp`
15+
Creates a new canvas instance based on a BMP image.
16+
#### Parameters
17+
1. The BMP image as a string.
18+
19+
---
20+
### `canvas.qrcode`
21+
Creates a new canvas instance containing a QR code.
22+
#### Parameters
23+
1. The string to be encoded.
24+
2. An optional table of options.
25+
#### Options
26+
- "ecl" specifies the error correction level between "low", "medium", "quartile", "high". defaults to "low".
27+
- "border" specifies the number of pixels around the QR code. defaults to 0.
28+
- "bg" specifies the background color. defaults to `0x000000` for black.
29+
- "fg" specifies the background color. defaults to `0xffffff` for white.
30+
31+
```pluto
32+
local canvas = require "canvas"
33+
local c = canvas.qrcode("Hello from Pluto!", { fg = 0xffffff, bg = 0x000000 })
34+
print(c:tobwstring(0xffffff))
35+
--> █▀▀▀▀▀█ ▄ █▄▄ █▀▀▀▀▀█
36+
--> █ ███ █ ▄█▄▄█ █ ███ █
37+
--> █ ▀▀▀ █ ▄▀ █▄ █ ▀▀▀ █
38+
--> ▀▀▀▀▀▀▀ ▀▄▀▄█ ▀▀▀▀▀▀▀
39+
--> ▀▀███ ▀███ █▀▀ ▀▄█▄▀▄
40+
--> █▄▀▄▀█▀█▀▀ ▀█▄▀▄▄██▀
41+
--> ▀▀▀▀▀ ▀ █▀█▀ ▄█▄▄▄ ▀
42+
--> █▀▀▀▀▀█ ▀ ▀██▀▄ ▄██
43+
--> █ ███ █ █▄ ▄█▄██▄▄ ▄
44+
--> █ ▀▀▀ █ █▄ ▀█▀ ▀▄▄█
45+
--> ▀▀▀▀▀▀▀ ▀ ▀ ▀ ▀ ▀
46+
```
47+
48+
---
49+
### `canvas.get`
50+
Gets the color of the pixel at the given coordinates.
51+
#### Parameters
52+
1. The canvas instance.
53+
2. The X coordinate.
54+
3. The Y coordinate.
55+
#### Returns
56+
The color as an integer.
57+
58+
---
59+
### `canvas.set`
60+
Sets the color of the pixel at the given coordinates.
61+
#### Parameters
62+
1. The canvas instance.
63+
2. The X coordinate.
64+
3. The Y coordinate.
65+
4. The color as an integer.
66+
67+
---
68+
### `canvas.fill`
69+
Sets every pixel to the given color.
70+
#### Parameters
71+
1. The canvas instance.
72+
2. The color as an integer.
73+
74+
---
75+
### `canvas.size`
76+
Get the size of a canvas.
77+
#### Parameters
78+
1. The canvas instance.
79+
#### Returns
80+
1. Width.
81+
1. Height.
82+
83+
---
84+
### `canvas.mulsize`
85+
Multiplies the size of a canvas.
86+
#### Parameters
87+
1. The canvas instance.
88+
2. The multiplier. Must be 2 or higher.
89+
90+
---
91+
### `canvas.tobmp`
92+
Convert a canvas to a BMP image.
93+
#### Parameters
94+
1. The canvas instance.
95+
#### Returns
96+
A string.
97+
98+
---
99+
### `canvas.topng`
100+
Convert a canvas to a PNG image.
101+
#### Parameters
102+
1. The canvas instance.
103+
#### Returns
104+
A string.
105+
```pluto
106+
local { base64, canvas } = require "*"
107+
local c = new canvas(5, 5)
108+
c:fill(0xff0000)
109+
print("data:image/png;base64,"..base64.encode(c:topng()))
110+
--> data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAIAAAACDbGyAAAAW0lEQVQIHQFQAK//AP8AAP8AAP8AAP8AAP8AAAD/AAD/AAD/AAD/AAD/AAAA/wAA/wAA/wAA/wAA/wAAAP8AAP8AAP8AAP8AAP8AAAD/AAD/AAD/AAD/AAD/AAD9fBjo3iScjwAAAABJRU5ErkJggg==
111+
```
112+
113+
---
114+
### `canvas.tobwstring`
115+
Convert a canvas to a black and white unicode string.
116+
#### Parameters
117+
1. The canvas instance.
118+
2. The color to be "white." Any other color will be "black."
119+
#### Returns
120+
A string.

0 commit comments

Comments
 (0)