pixelflow_canvas 0.7.4 → 0.7.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/LICENSE +674 -0
- data/README.md +3 -41
- data/docs/.gitignore +15 -0
- data/docs/Gemfile +7 -0
- data/docs/Gemfile.lock +92 -0
- data/{LICENSE.txt → docs/LICENSE} +6 -6
- data/docs/README.md +174 -0
- data/docs/_config.yml +11 -0
- data/docs/advanced_use.md +37 -0
- data/docs/drawing_things.md +296 -0
- data/docs/images/4x6.png +0 -0
- data/docs/images/6x10.png +0 -0
- data/docs/images/7x13.png +0 -0
- data/docs/images/7x13B.png +0 -0
- data/docs/images/8x13.png +0 -0
- data/docs/images/8x13B.png +0 -0
- data/docs/images/9x15.png +0 -0
- data/docs/images/9x15B.png +0 -0
- data/docs/images/code/0ba813cd6b7e0ae0.png +0 -0
- data/docs/images/code/30c5ac94e5fa22ba.png +0 -0
- data/docs/images/code/3871051301832e44.png +0 -0
- data/docs/images/code/40acf0284459aa0e.png +0 -0
- data/docs/images/code/4eb9d4f97e05c739.png +0 -0
- data/docs/images/code/584e2ee3b7526718.png +0 -0
- data/docs/images/code/7fa15cea8c83a542.png +0 -0
- data/docs/images/code/809d9ce7d7a8ce6c.png +0 -0
- data/docs/images/code/85abc3576f6d78b1.png +0 -0
- data/docs/images/code/ec32331b354119d2.png +0 -0
- data/docs/images/code/ef667cfe7ff65c27.png +0 -0
- data/docs/index.md +126 -0
- data/docs/palettes.md +3192 -0
- data/docs/render-docs.rb +98 -0
- data/lib/pixelflow_canvas/palettes.yaml +257 -0
- data/lib/pixelflow_canvas/version.rb +1 -1
- data/lib/pixelflow_canvas.rb +28 -12
- metadata +33 -9
- data/sig/pixelflow_canvas.rbs +0 -4
- data/test-anaglyph.rb +0 -15
- data/test-sirds.rb +0 -46
- data/test-triangle.rb +0 -25
- data/test-turtle.rb +0 -14
- data/test.rb +0 -36
@@ -0,0 +1,296 @@
|
|
1
|
+
---
|
2
|
+
title: Drawing things
|
3
|
+
layout: page
|
4
|
+
nav_order: 2
|
5
|
+
---
|
6
|
+
|
7
|
+
<style>
|
8
|
+
img.full {
|
9
|
+
width: 100%;
|
10
|
+
max-width: 100%;
|
11
|
+
margin: 0;
|
12
|
+
padding: 0;
|
13
|
+
image-rendering: pixelated;
|
14
|
+
border-radius: 1em;
|
15
|
+
box-shadow: 0 0 1em rgba(0, 0, 0, 0.2);
|
16
|
+
padding: 1em;
|
17
|
+
}
|
18
|
+
</style>
|
19
|
+
|
20
|
+
# Drawing things
|
21
|
+
|
22
|
+
The `Canvas` class provides a number of methods for drawing things on the canvas. These methods are used to draw pixels, lines, rectangles, circles, ellipses, arcs, Bézier curves, triangles, and text.
|
23
|
+
|
24
|
+
## Drawing and reading pixels
|
25
|
+
|
26
|
+
The `set_pixel` method sets the color of a single pixel on the canvas.
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
canvas.set_pixel(160, 90, 10) # in palette mode
|
30
|
+
canvas.set_pixel(160, 90, 0, 255, 0) # in RGB mode
|
31
|
+
```
|
32
|
+
|
33
|
+
To read the color of a pixel, use the `get_pixel` method.
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
color = canvas.get_pixel(160, 90)
|
37
|
+
```
|
38
|
+
|
39
|
+
Depending on the color mode, the `get_pixel` method returns either a palette index or an array of RGB values.
|
40
|
+
|
41
|
+
## Setting the current color
|
42
|
+
|
43
|
+
The `set_color` method sets the color that will be used by all subsequent drawing operations.
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
canvas.set_color(10)
|
47
|
+
```
|
48
|
+
|
49
|
+
You can omit the color argument in the `set_pixel` method if you have previously set the color using the `set_color` method.
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
canvas.set_color(10)
|
53
|
+
canvas.set_pixel(160, 90)
|
54
|
+
```
|
55
|
+
|
56
|
+
## Drawing lines
|
57
|
+
|
58
|
+
Use the `draw_line` method to draw a line between two points.
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
canvas.set_color(10)
|
62
|
+
canvas.draw_line(10, 10, 100, 50)
|
63
|
+
```
|
64
|
+
|
65
|
+
<!-- code begin
|
66
|
+
Pixelflow::Canvas.new(64, 18, :palette) do
|
67
|
+
set_color(15)
|
68
|
+
fill_rect(0, 0, 63, 17)
|
69
|
+
set_color(8)
|
70
|
+
draw_line(0, 0, 63, 17)
|
71
|
+
end
|
72
|
+
code end --><img class='full' src='images/code/0ba813cd6b7e0ae0.png'>
|
73
|
+
|
74
|
+
## Drawing rectangles
|
75
|
+
|
76
|
+
Use the `draw_rect` and `fill_rect` methods to draw rectangles. Specify the coordinates of the upper-left corner and the lower-right corner of the rectangle.
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
canvas.set_color(3)
|
80
|
+
canvas.fill_rect(10, 10, 100, 50)
|
81
|
+
canvas.set_color(10)
|
82
|
+
canvas.draw_rect(10, 10, 100, 50)
|
83
|
+
```
|
84
|
+
|
85
|
+
<!-- code begin
|
86
|
+
Pixelflow::Canvas.new(64, 18, :palette) do
|
87
|
+
set_color(15)
|
88
|
+
fill_rect(0, 0, 63, 17)
|
89
|
+
set_color(7)
|
90
|
+
fill_rect(0, 0, 63, 17)
|
91
|
+
set_color(8)
|
92
|
+
draw_rect(0, 0, 63, 17)
|
93
|
+
end
|
94
|
+
code end --><img class='full' src='images/code/584e2ee3b7526718.png'>
|
95
|
+
|
96
|
+
## Drawing circles
|
97
|
+
|
98
|
+
Use the `draw_circle` and `fill_circle` methods to draw circles. Specify the center and the radius of the circle.
|
99
|
+
|
100
|
+
```ruby
|
101
|
+
canvas.set_color(3)
|
102
|
+
canvas.fill_circle(160, 90, 50)
|
103
|
+
canvas.set_color(10)
|
104
|
+
canvas.draw_circle(160, 90, 50)
|
105
|
+
```
|
106
|
+
|
107
|
+
<!-- code begin
|
108
|
+
Pixelflow::Canvas.new(64, 19, :palette) do
|
109
|
+
set_color(15)
|
110
|
+
fill_rect(0, 0, 63, 18)
|
111
|
+
set_color(7)
|
112
|
+
fill_circle(32, 9, 9)
|
113
|
+
set_color(8)
|
114
|
+
draw_circle(32, 9, 9)
|
115
|
+
end
|
116
|
+
code end --><img class='full' src='images/code/809d9ce7d7a8ce6c.png'>
|
117
|
+
|
118
|
+
## Drawing ellipses
|
119
|
+
|
120
|
+
Use the `draw_ellipse` and `fill_ellipse` methods to draw ellipses. Specify the center, the horizontal radius, and the vertical radius of the ellipse.
|
121
|
+
|
122
|
+
```ruby
|
123
|
+
canvas.set_color(3)
|
124
|
+
canvas.fill_ellipse(160, 90, 50, 30)
|
125
|
+
canvas.set_color(10)
|
126
|
+
canvas.draw_ellipse(160, 90, 50, 30)
|
127
|
+
```
|
128
|
+
<!-- code begin
|
129
|
+
Pixelflow::Canvas.new(64, 19, :palette) do
|
130
|
+
set_color(15)
|
131
|
+
fill_rect(0, 0, 63, 18)
|
132
|
+
set_color(7)
|
133
|
+
fill_ellipse(32, 9, 16, 9)
|
134
|
+
set_color(8)
|
135
|
+
draw_ellipse(32, 9, 16, 9)
|
136
|
+
end
|
137
|
+
code end --><img class='full' src='images/code/3871051301832e44.png'>
|
138
|
+
|
139
|
+
## Drawing circular arcs
|
140
|
+
|
141
|
+
Use the `draw_arc` and `fill_arc` methods to draw arcs. Specify the center, the radius, the start angle, and the end angle of the arc.
|
142
|
+
|
143
|
+
```ruby
|
144
|
+
canvas.set_color(3)
|
145
|
+
canvas.fill_arc(160, 90, 50, 0, 180)
|
146
|
+
canvas.set_color(10)
|
147
|
+
canvas.draw_arc(160, 90, 50, 0, 180)
|
148
|
+
```
|
149
|
+
|
150
|
+
<!-- code begin
|
151
|
+
Pixelflow::Canvas.new(64, 18, :palette) do
|
152
|
+
set_color(15)
|
153
|
+
fill_rect(0, 0, 63, 17)
|
154
|
+
set_color(7)
|
155
|
+
fill_arc(32, 0, 18, 30, 150)
|
156
|
+
set_color(8)
|
157
|
+
draw_arc(32, 0, 18, 30, 150)
|
158
|
+
end
|
159
|
+
code end --><img class='full' src='images/code/4eb9d4f97e05c739.png'>
|
160
|
+
|
161
|
+
## Drawing Bézier curves
|
162
|
+
|
163
|
+
Use the `draw_quadratic_bezier` and `draw_cubic_bezier` methods to draw quadratic and cubic Bézier curves, respectively.
|
164
|
+
|
165
|
+
```ruby
|
166
|
+
canvas.set_color(10)
|
167
|
+
canvas.draw_quadratic_bezier(10, 10, 50, 50, 100, 10)
|
168
|
+
canvas.draw_cubic_bezier(10, 10, 50, 50, 100, 50, 150, 10)
|
169
|
+
```
|
170
|
+
|
171
|
+
<!-- code begin
|
172
|
+
Pixelflow::Canvas.new(64, 18, :palette) do
|
173
|
+
set_color(15)
|
174
|
+
fill_rect(0, 0, 63, 17)
|
175
|
+
set_color(8)
|
176
|
+
draw_quadratic_bezier(0, 17, 16, -8, 63, 17)
|
177
|
+
end
|
178
|
+
code end --><img class='full' src='images/code/ec32331b354119d2.png'>
|
179
|
+
|
180
|
+
<!-- code begin
|
181
|
+
Pixelflow::Canvas.new(64, 18, :palette) do
|
182
|
+
set_color(15)
|
183
|
+
fill_rect(0, 0, 63, 17)
|
184
|
+
set_color(8)
|
185
|
+
draw_cubic_bezier(0, 17, 16, -8, 47, 30, 63, 0)
|
186
|
+
end
|
187
|
+
code end --><img class='full' src='images/code/40acf0284459aa0e.png'>
|
188
|
+
|
189
|
+
## Drawing triangles
|
190
|
+
|
191
|
+
Use the `draw_triangle` and `fill_triangle` methods to draw triangles. Specify the coordinates of the three vertices of the triangle.
|
192
|
+
|
193
|
+
```ruby
|
194
|
+
canvas.set_color(3)
|
195
|
+
canvas.fill_triangle(10, 10, 100, 50, 50, 100)
|
196
|
+
canvas.set_color(10)
|
197
|
+
canvas.draw_triangle(10, 10, 100, 50, 50, 100)
|
198
|
+
```
|
199
|
+
|
200
|
+
<!-- code begin
|
201
|
+
Pixelflow::Canvas.new(64, 18, :palette) do
|
202
|
+
set_color(15)
|
203
|
+
fill_rect(0, 0, 63, 17)
|
204
|
+
set_color(7)
|
205
|
+
fill_triangle(10, 0, 52, 6, 31, 17)
|
206
|
+
set_color(8)
|
207
|
+
draw_triangle(10, 0, 52, 6, 31, 17)
|
208
|
+
end
|
209
|
+
code end --><img class='full' src='images/code/85abc3576f6d78b1.png'>
|
210
|
+
|
211
|
+
## Flood filling
|
212
|
+
|
213
|
+
The `flood_fill` method fills an area of the canvas with the current color. Specify the starting point of the fill operation.
|
214
|
+
|
215
|
+
```ruby
|
216
|
+
canvas.set_color(10)
|
217
|
+
canvas.draw_circle(110, 90, 50)
|
218
|
+
canvas.draw_circle(210, 90, 40)
|
219
|
+
canvas.flood_fill(160, 90)
|
220
|
+
```
|
221
|
+
|
222
|
+
<!-- code begin
|
223
|
+
Pixelflow::Canvas.new(64, 18, :palette) do
|
224
|
+
set_color(15)
|
225
|
+
fill_rect(0, 0, 63, 17)
|
226
|
+
set_color(8)
|
227
|
+
draw_circle(25, 12, 12)
|
228
|
+
draw_circle(39, 12, 8)
|
229
|
+
set_color(7)
|
230
|
+
flood_fill(25, 14)
|
231
|
+
flood_fill(39, 14)
|
232
|
+
end
|
233
|
+
code end --><img class='full' src='images/code/ef667cfe7ff65c27.png'>
|
234
|
+
|
235
|
+
## Drawing text
|
236
|
+
|
237
|
+
Use the `draw_text` method to draw text on the canvas. Specify the position, the text to draw, the font name, and the scale factor.
|
238
|
+
|
239
|
+
```ruby
|
240
|
+
canvas.set_color(10)
|
241
|
+
canvas.draw_text(10, 10, "Hello, world!", "4x6", 2)
|
242
|
+
```
|
243
|
+
|
244
|
+
<!-- code begin
|
245
|
+
Pixelflow::Canvas.new(64, 14, :palette) do
|
246
|
+
set_color(15)
|
247
|
+
fill_rect(0, 0, 63, 13)
|
248
|
+
set_color(8)
|
249
|
+
draw_text(7, 4, "Hello, world!", "4x6", 1)
|
250
|
+
end
|
251
|
+
code end --><img class='full' src='images/code/30c5ac94e5fa22ba.png'>
|
252
|
+
|
253
|
+
|
254
|
+
Use `text_width` to get the width and height of a given text string in pixels.
|
255
|
+
|
256
|
+
```ruby
|
257
|
+
width = canvas.text_width("Hello, world!", "4x6", 2)
|
258
|
+
```
|
259
|
+
|
260
|
+
There are several built-in fonts available:
|
261
|
+
|
262
|
+
<!-- fonts start -->
|
263
|
+
|
264
|
+
#### 4x6
|
265
|
+
|
266
|
+
<img src='images/4x6.png' alt='4x6'>
|
267
|
+
|
268
|
+
#### 6x10
|
269
|
+
|
270
|
+
<img src='images/6x10.png' alt='6x10'>
|
271
|
+
|
272
|
+
#### 7x13
|
273
|
+
|
274
|
+
<img src='images/7x13.png' alt='7x13'>
|
275
|
+
|
276
|
+
#### 7x13B
|
277
|
+
|
278
|
+
<img src='images/7x13B.png' alt='7x13B'>
|
279
|
+
|
280
|
+
#### 8x13
|
281
|
+
|
282
|
+
<img src='images/8x13.png' alt='8x13'>
|
283
|
+
|
284
|
+
#### 8x13B
|
285
|
+
|
286
|
+
<img src='images/8x13B.png' alt='8x13B'>
|
287
|
+
|
288
|
+
#### 9x15
|
289
|
+
|
290
|
+
<img src='images/9x15.png' alt='9x15'>
|
291
|
+
|
292
|
+
#### 9x15B
|
293
|
+
|
294
|
+
<img src='images/9x15B.png' alt='9x15B'>
|
295
|
+
|
296
|
+
<!-- fonts end -->
|
data/docs/images/4x6.png
ADDED
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
data/docs/index.md
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
---
|
2
|
+
title: Getting started
|
3
|
+
layout: home
|
4
|
+
nav_order: 0
|
5
|
+
---
|
6
|
+
|
7
|
+
<style>
|
8
|
+
img.full {
|
9
|
+
width: 100%;
|
10
|
+
max-width: 100%;
|
11
|
+
margin: 0;
|
12
|
+
padding: 0;
|
13
|
+
image-rendering: pixelated;
|
14
|
+
}
|
15
|
+
</style>
|
16
|
+
|
17
|
+
# Pixelflow Canvas (Ruby driver)
|
18
|
+
|
19
|
+
A virtual CRT for old school graphics programming in Visual Studio Code.
|
20
|
+
|
21
|
+
The Pixelflow Canvas is a simple library for creating pixel graphics, animations or interactive experiments in Ruby. It provides a canvas that you can draw on using a fixed palette of 256 colors or 24-bit RGB values. The canvas can be displayed in Visual Studio Code using the Pixelflow Canvas extension.
|
22
|
+
|
23
|
+
{: .info }
|
24
|
+
This is the documentation for the pixelflow_canvas rubygem which requires the [»Pixelflow Canvas«](https://marketplace.visualstudio.com/items?itemName=gymnasiumsteglitz.pixelflow-canvas) Visual Studio Code extension to work. You can find the documentation for the VS Code extension [here](https://specht.github.io/pixelflow_canvas_vscode/).
|
25
|
+
|
26
|
+
## Creating a canvas
|
27
|
+
|
28
|
+
There are two ways to work with a canvas.
|
29
|
+
|
30
|
+
### Working with an instance
|
31
|
+
|
32
|
+
Specify the width, height, and color mode (either `:palette` or `:rgb`) to create a new canvas:
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
require 'pixelflow_canvas'
|
36
|
+
|
37
|
+
canvas = Pixelflow::Canvas.new(32, 18, :palette)
|
38
|
+
canvas.set_pixel(16, 9, 10)
|
39
|
+
```
|
40
|
+
|
41
|
+
This should produce the following output:
|
42
|
+
|
43
|
+
<!-- code begin
|
44
|
+
Pixelflow::Canvas.new(32, 18, :palette) do
|
45
|
+
set_pixel(16, 9, 10)
|
46
|
+
end
|
47
|
+
code end --><img class='full' src='images/code/7fa15cea8c83a542.png'>
|
48
|
+
|
49
|
+
### Pass a block to the constructor
|
50
|
+
|
51
|
+
This method allows you to omit the `canvas` variable on every method call:
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
Pixelflow::Canvas.new(320, 180, :palette) do
|
55
|
+
set_pixel(160, 90, 10)
|
56
|
+
end
|
57
|
+
```
|
58
|
+
|
59
|
+
You can also use the `perform` method to run a block on an existing canvas:
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
canvas = Pixelflow::Canvas.new(320, 180, :palette)
|
63
|
+
canvas.perform do
|
64
|
+
set_pixel(160, 90, 10)
|
65
|
+
end
|
66
|
+
```
|
67
|
+
|
68
|
+
## Modes of operation
|
69
|
+
|
70
|
+
The following modes of operation affect how the canvas is working and how it is displayed.
|
71
|
+
|
72
|
+
### Color modes
|
73
|
+
|
74
|
+
There are two color modes: `:palette` and `:rgb`. In palette mode, the canvas uses a fixed palette of 256 colors and each pixel is represented by an index (0...255) into this palette. In RGB mode, each pixel is represented by a 24-bit RGB value.
|
75
|
+
|
76
|
+
The default color mode is `:rgb`. It can be changed by calling `set_color_mode`.
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
canvas.set_color_mode(:palette)
|
80
|
+
```
|
81
|
+
|
82
|
+
### Double buffering
|
83
|
+
|
84
|
+
There are two drawing modes: `:direct` and `:buffered`. In direct mode, every pixel gets drawn immediately. In buffered mode, the canvas is drawn to an off-screen buffer and then copied to the screen when `flip` is called. Double buffering is useful for animations to prevent flickering.
|
85
|
+
|
86
|
+
The default drawing mode is `:direct`. It can be changed by calling `set_draw_mode`.
|
87
|
+
|
88
|
+
```ruby
|
89
|
+
canvas.set_draw_mode(:buffered)
|
90
|
+
```
|
91
|
+
|
92
|
+
{: .info }
|
93
|
+
If you get a black canvas in buffered mode, make sure to call `flip` at the end of your drawing code.
|
94
|
+
|
95
|
+
### Composition modes
|
96
|
+
|
97
|
+
There are three composition modes: `:copy`, `:add`, `:subtract` and `:multiply`.
|
98
|
+
|
99
|
+
- `:copy` copies the source pixel to the destination pixel
|
100
|
+
- `:add` adds the source pixel to the destination pixel
|
101
|
+
- `:subtract` subtracts the source pixel from the destination pixel
|
102
|
+
- `:multiply` multiplies the source pixel with the destination pixel
|
103
|
+
|
104
|
+
The default composition mode is `:copy`. It can be changed by calling `set_composition_mode`.
|
105
|
+
|
106
|
+
```ruby
|
107
|
+
canvas.set_composition_mode(:add)
|
108
|
+
```
|
109
|
+
|
110
|
+
{: .info }
|
111
|
+
Composition modes other than `:copy` require the canvas to be in RGB mode.
|
112
|
+
|
113
|
+
### Interpolation modes
|
114
|
+
|
115
|
+
There are two interpolation modes: `:nearest` and `:bilinear`. This mode only affects the display of the canvas in the Visual Studio Code extension.
|
116
|
+
|
117
|
+
The default interpolation mode is `:nearest`. It can be changed by calling `set_interpolation_mode`.
|
118
|
+
|
119
|
+
```ruby
|
120
|
+
canvas.set_interpolation_mode(:bilinear)
|
121
|
+
```
|
122
|
+
<!--
|
123
|
+
- Using masks
|
124
|
+
- Saving the canvas
|
125
|
+
- Event polling
|
126
|
+
-->
|