Class: GD2::Canvas
- Inherits:
-
Object
show all
- Defined in:
- lib/gd2/canvas.rb
Defined Under Namespace
Classes: Arc, Ellipse, FilledEllipse, FilledPolygon, FilledRectangle, FilledWedge, Line, NoColorSelectedError, NoFontSelectedError, OpenPolygon, Point, Polygon, Rectangle, Text, TextCircle, Wedge
Constant Summary
collapse
- STYLED =
-2
- BRUSHED =
-3
- STYLED_BRUSHED =
-4
- TILED =
-5
- TRANSPARENT =
Line styles only; not a color index
-6
- ANTI_ALIASED =
-7
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#affine_transform(a, b, c, d, tx, ty) ⇒ Object
-
#arc(cx, cy, width, height, range) ⇒ Object
-
#cartesian ⇒ Object
-
#circle(cx, cy, diameter, filled = false) ⇒ Object
-
#ellipse(cx, cy, width, height, filled = false) ⇒ Object
-
#fill ⇒ Object
-
#fill_to(border) ⇒ Object
-
#initialize(image) ⇒ Canvas
constructor
A new instance of Canvas.
-
#line(x1, y1, x2, y2) ⇒ Object
-
#line_to(x, y) ⇒ Object
-
#location ⇒ Object
-
#move(x, y) ⇒ Object
-
#move_to(x, y) ⇒ Object
-
#point(x, y) ⇒ Object
-
#polygon(points, filled = false, open = false) ⇒ Object
-
#rectangle(x1, y1, x2, y2, filled = false) ⇒ Object
-
#rotate(angle) ⇒ Object
-
#scale(sx, sy = sx) ⇒ Object
-
#text(string, angle = 0.0) ⇒ Object
-
#text_circle(top, bottom, radius, text_radius, fill_portion) ⇒ Object
-
#translate(tx, ty) ⇒ Object
-
#wedge(cx, cy, width, height, range, filled = false, chord = false) ⇒ Object
Constructor Details
#initialize(image) ⇒ Canvas
Returns a new instance of Canvas.
230
231
232
233
234
235
236
|
# File 'lib/gd2/canvas.rb', line 230
def initialize(image)
@image = image
self.thickness = 1
self.anti_aliasing = false
@transformation_matrix = Matrix.identity(3)
move_to(0, 0)
end
|
Instance Attribute Details
#anti_aliasing ⇒ Object
Also known as:
anti_aliasing?
Returns the value of attribute anti_aliasing.
218
219
220
|
# File 'lib/gd2/canvas.rb', line 218
def anti_aliasing
@anti_aliasing
end
|
#brush ⇒ Object
Returns the value of attribute brush.
216
217
218
|
# File 'lib/gd2/canvas.rb', line 216
def brush
@brush
end
|
#color ⇒ Object
Returns the value of attribute color.
216
217
218
|
# File 'lib/gd2/canvas.rb', line 216
def color
@color
end
|
#dont_blend ⇒ Object
Returns the value of attribute dont_blend.
216
217
218
|
# File 'lib/gd2/canvas.rb', line 216
def dont_blend
@dont_blend
end
|
#font ⇒ Object
Returns the value of attribute font.
218
219
220
|
# File 'lib/gd2/canvas.rb', line 218
def font
@font
end
|
#style ⇒ Object
Returns the value of attribute style.
216
217
218
|
# File 'lib/gd2/canvas.rb', line 216
def style
@style
end
|
#thickness ⇒ Object
Returns the value of attribute thickness.
216
217
218
|
# File 'lib/gd2/canvas.rb', line 216
def thickness
@thickness
end
|
#tile ⇒ Object
Returns the value of attribute tile.
216
217
218
|
# File 'lib/gd2/canvas.rb', line 216
def tile
@tile
end
|
Returns the value of attribute transformation_matrix.
216
217
218
|
# File 'lib/gd2/canvas.rb', line 216
def transformation_matrix
@transformation_matrix
end
|
Instance Method Details
274
275
276
277
278
279
280
281
282
283
|
# File 'lib/gd2/canvas.rb', line 274
def affine_transform(a, b, c, d, tx, ty)
old_matrix = @transformation_matrix
begin
@transformation_matrix = Matrix[[a, b, 0], [c, d, 0], [tx, ty, 1]] *
@transformation_matrix
yield
ensure
@transformation_matrix = old_matrix
end
end
|
#arc(cx, cy, width, height, range) ⇒ Object
368
369
370
|
# File 'lib/gd2/canvas.rb', line 368
def arc(cx, cy, width, height, range)
Arc.new(point(cx, cy), width, height, range).draw(@image, line_pixel)
end
|
#cartesian ⇒ Object
305
306
307
308
309
|
# File 'lib/gd2/canvas.rb', line 305
def cartesian
affine_transform(1, 0, 0, -1, 0, @image.height - 1) { |*block_args|
yield(*block_args)
}
end
|
#circle(cx, cy, diameter, filled = false) ⇒ Object
382
383
384
|
# File 'lib/gd2/canvas.rb', line 382
def circle(cx, cy, diameter, filled = false)
ellipse(cx, cy, diameter, diameter, filled)
end
|
#ellipse(cx, cy, width, height, filled = false) ⇒ Object
377
378
379
380
|
# File 'lib/gd2/canvas.rb', line 377
def ellipse(cx, cy, width, height, filled = false)
(filled ? FilledEllipse : Ellipse).new(point(cx, cy), width, height).
draw(@image, filled ? fill_pixel : line_pixel)
end
|
#fill ⇒ Object
342
343
344
345
|
# File 'lib/gd2/canvas.rb', line 342
def fill
::GD2::GD2FFI.send(:gdImageFill, @image.image_ptr, @point.x.to_i, @point.y.to_i, fill_pixel.to_i)
self
end
|
#fill_to(border) ⇒ Object
347
348
349
350
351
352
|
# File 'lib/gd2/canvas.rb', line 347
def fill_to(border)
::GD2::GD2FFI.send(:gdImageFillToBorder, @image.image_ptr, @point.x.to_i, @point.y.to_i,
@image.color2pixel(border), pixel.to_i)
self
end
|
#line(x1, y1, x2, y2) ⇒ Object
331
332
333
|
# File 'lib/gd2/canvas.rb', line 331
def line(x1, y1, x2, y2)
Line.new(point(x1, y1), point(x2, y2)).draw(@image, line_pixel)
end
|
#line_to(x, y) ⇒ Object
335
336
337
338
339
340
|
# File 'lib/gd2/canvas.rb', line 335
def line_to(x, y)
point2 = point(x, y)
Line.new(@point, point2).draw(@image, line_pixel)
@point = point2
self
end
|
#location ⇒ Object
327
328
329
|
# File 'lib/gd2/canvas.rb', line 327
def location
@point.transform(transformation_matrix.inverse).coordinates
end
|
#move(x, y) ⇒ Object
320
321
322
323
324
325
|
# File 'lib/gd2/canvas.rb', line 320
def move(x, y)
@point.transform!(Matrix[[1, 0, 0], [0, 1, 0], [x, y, 1]] *
@transformation_matrix)
self
end
|
#move_to(x, y) ⇒ Object
315
316
317
318
|
# File 'lib/gd2/canvas.rb', line 315
def move_to(x, y)
@point = point(x, y)
self
end
|
#point(x, y) ⇒ Object
311
312
313
|
# File 'lib/gd2/canvas.rb', line 311
def point(x, y)
Point.new(x, y).transform!(transformation_matrix)
end
|
#polygon(points, filled = false, open = false) ⇒ Object
359
360
361
362
363
364
365
366
|
# File 'lib/gd2/canvas.rb', line 359
def polygon(points, filled = false, open = false)
points = points.map { |(x, y)| point(x, y) }
if filled
FilledPolygon.new(points).draw(@image, fill_pixel)
else
(open ? OpenPolygon : Polygon).new(points).draw(@image, line_pixel)
end
end
|
#rectangle(x1, y1, x2, y2, filled = false) ⇒ Object
354
355
356
357
|
# File 'lib/gd2/canvas.rb', line 354
def rectangle(x1, y1, x2, y2, filled = false)
(filled ? FilledRectangle : Rectangle).new(point(x1, y1), point(x2, y2)).
draw(@image, filled ? fill_pixel : line_pixel)
end
|
#rotate(angle) ⇒ Object
297
298
299
300
301
302
303
|
# File 'lib/gd2/canvas.rb', line 297
def rotate(angle)
cos = Math.cos(angle)
sin = Math.sin(angle)
affine_transform(cos, sin, -sin, cos, 0, 0) { |*block_args|
yield(*block_args)
}
end
|
#scale(sx, sy = sx) ⇒ Object
291
292
293
294
295
|
# File 'lib/gd2/canvas.rb', line 291
def scale(sx, sy = sx)
affine_transform(sx, 0, 0, sy, 0, 0) { |*block_args|
yield(*block_args)
}
end
|
#text(string, angle = 0.0) ⇒ Object
386
387
388
|
# File 'lib/gd2/canvas.rb', line 386
def text(string, angle = 0.0)
Text.new(get_font, @point, angle, string).draw(@image, pixel)
end
|
#text_circle(top, bottom, radius, text_radius, fill_portion) ⇒ Object
390
391
392
393
|
# File 'lib/gd2/canvas.rb', line 390
def text_circle(top, bottom, radius, text_radius, fill_portion)
TextCircle.new(get_font, @point, radius, text_radius, fill_portion,
top, bottom).draw(@image, pixel)
end
|
#translate(tx, ty) ⇒ Object
285
286
287
288
289
|
# File 'lib/gd2/canvas.rb', line 285
def translate(tx, ty)
affine_transform(1, 0, 0, 1, tx, ty) { |*block_args|
yield(*block_args)
}
end
|
#wedge(cx, cy, width, height, range, filled = false, chord = false) ⇒ Object
372
373
374
375
|
# File 'lib/gd2/canvas.rb', line 372
def wedge(cx, cy, width, height, range, filled = false, chord = false)
(filled ? FilledWedge : Wedge).new(point(cx, cy), width, height,
range, chord).draw(@image, filled ? fill_pixel : line_pixel)
end
|