class Chitra::Context
- Chitra::Context
- Reference
- Object
Included Modules
Defined in:
context.crelements/background.cr
elements/composite.cr
elements/image.cr
elements/line.cr
elements/markup.cr
elements/new_page.cr
elements/oval.cr
elements/polygon.cr
elements/properties.cr
elements/rect.cr
elements/rotate.cr
elements/scale.cr
elements/text.cr
elements/translate.cr
helpers.cr
Instance Method Summary
-
#background(r, g, b, a = 1.0)
Fill background color
ctx = Chitra.new # white background ctx.background 1, 0, 0
-
#background(hexcol : String)
Fill background color(Hex Color)
ctx = Chitra.new # white background ctx.background "#ffffff"
-
#background(gray, a = 1.0)
Fill background color(Gray scale)
ctx = Chitra.new # white background ctx.background 1
-
#composite(op : String)
Apply Composite
ctx.group_start ctx.rect 10, 10, 100, 100 ctx.composite "in" ctx.rect 50, 50, 100, 100 ctx.group_end
Other available Composite options are: Clear, Source, Over, In, Out, Atop, Dest, DestOver, DestIn, DestOut, DestAtop, Xor, Add, Saturate, Multiply, Screen, Overlay, Darken, Lighten, ColorDodge, ColorBurn, HardLight, SoftLight, Difference, Exclusion, HslHue, HslSaturation, HslColor, HslLuminosity - #debug : Bool
- #debug=(debug : Bool)
- #elements : Array(Chitra::Element)
-
#enable_debug
Enable debug messages to verify if any property applied to an element is not maching
ctx = Chitra.new ctx.enable_debug
-
#fill(r, g, b, a = 1.0)
Set fill color.
-
#fill(hexcol : String)
Set fill color in hex format.
-
#fill(gray, a = 1.0)
Set fill color.
-
#fill_opacity(a)
Set fill opacity.
-
#font(font_name, font_size)
Set Font family and size
ctx.font "Times", 30
-
#font_size(font_size)
Set Font size
ctx.font_size 30
-
#font_weight(wt)
Set Font weight
ctx.font_weight "Heavy"
-
#group_end
End a Group
ctx.group_start ctx.rect 10, 10, 100, 100 ctx.composite "in" ctx.rect 50, 50, 100, 100 ctx.group_end
-
#group_start
Start a Group
ctx.group_start ctx.rect 10, 10, 100, 100 ctx.composite "in" ctx.rect 50, 50, 100, 100 ctx.group_end
-
#grouped(&block)
Grouped code run as block
ctx.grouped do ctx.rect 10, 10, 100, 100 ctx.composite "In" ctx.rect 50, 50, 100, 100 end
-
#height
Get height of the canvas
# width height ctx = Chitra.new 1600, 900 puts ctx.height # 900
-
#hyphen_char(value : String)
Set Hyphenation char.
-
#hyphenation(value : Bool)
Enable/Disable text hyphenation.
-
#image(path, x, y)
Draw image
ctx = Chitra.new ctx.image "logo.png", 100, 100
-
#image_height(path)
Get height of the image
ctx = Chitra.new h = ctx.image_height "logo.png"
-
#image_size(path)
Get width and height of the image
ctx = Chitra.new w, h = ctx.image_size "logo.png"
-
#image_width(path)
Get width of the image
ctx = Chitra.new w = ctx.image_width "logo.png"
-
#line(x1, y1, x2, y2)
Draw a line
ctx = Chitra.new x y w h ctx.line 100, 100, 500, 500
-
#line_cap(value)
Set Line cap style Allowed values are butt, round and square.
-
#line_dash(values : Array(Int32), offset = 0)
Set line dash pattern.
-
#line_dash(*values, offset = 0)
Set line dash pattern.
- #line_height(value)
-
#line_join(value)
Set Line join style Possible values are: miter, round, bevel.
-
#markup(txt, x, y)
Draw text for given x and y values.
- #markup_box(txt, x, y, w, h = 0.0)
-
#new_drawing
Reset the drawing to clean and empty canvas
ctx = Chitra.new ctx.fill 0 ctx.rect 0, 0, width, height ctx.save "slide1.png" ctx.new_drawing ctx.fill 0, 0, 1 ctx.rect 0, 0, width, height ctx.save "slide2.png"
-
#new_page
Creates a new page.
-
#no_fill
Disable fill
ctx.no_fill
-
#no_stroke
Disable Stroke.
-
#oval(x, y, w = 2.0, h = 0.0)
Draw oval shape
ctx = Chitra.new x y w h ctx.oval 100, 100, 500, 500
-
#polygon(points : Array(Float64), close = true)
Draw polygon shape.
-
#polygon(*points, close = true)
Draw polygon shape.
- #rainbow(txt : String | Array(String), x, y, colors = RAINBOW_COLORS, join_str = "")
- #rainbow_box(txt : String | Array(String), x, y, w, h = 0.0, colors = RAINBOW_COLORS, join_str = "")
-
#rect(x, y, w, h = 0.0)
Draw rectangle shape
ctx = Chitra.new x y w h ctx.rect 100, 100, 500, 500
-
#restore_state(saved_context)
Restore Context state
ctx.restore_state
-
#rotate(angle, center_x = 0, center_y = 0)
Rotate the canvas in clockwise to given angle.
-
#save(file : String)
Save the output to a file.
-
#save_state(saved_context)
Save Context state
ctx.save_state ctx.fill 0, 0, 1
-
#saved_state(&block)
Use Saved state as block that applies save_state and restore_state automatically.
-
#scale(scale_x, scale_y = 0)
Scale the canvas with the given x and y scale factors.
- #size : Chitra::Size
- #size=(size : Chitra::Size)
-
#stroke(r, g, b, a = 1.0)
Set Stroke color(Gray scale).
-
#stroke(hexcol : String)
Set Stroke color in hex format.
-
#stroke(gray, a = 1.0)
Set stroke/line color(Gray scale).
-
#stroke_opacity(a)
Set stroke opacity.
-
#stroke_width(value : Int32)
Set Stroke/Line width.
-
#text(txt, x, y)
Draw text for given x and y values.
-
#text_align(value : String)
Set Text align for text boxes
ctx.text_align "center"
- #text_box(txt, x, y, w, h = 0.0)
-
#translate(x, y)
Translate the canvas to given x and y
ctx = Chitra.new ctx.translate 100, 100 ctx.rect 0, 0, 500, 500
-
#width
Get width of the canvas
# width height ctx = Chitra.new 1600, 900 puts ctx.width # 1600
Instance methods inherited from module Chitra::TextProperties
align
align,
align=(align)
align=,
font
font,
font=(font)
font=,
hyphen_char
hyphen_char,
hyphen_char=(hyphen_char)
hyphen_char=,
hyphenation
hyphenation,
hyphenation=(hyphenation)
hyphenation=,
line_height
line_height,
line_height=(line_height)
line_height=,
reset_text_properties
reset_text_properties
Instance methods inherited from module Chitra::ShapeProperties
fill
fill,
fill=(fill)
fill=,
line_cap
line_cap,
line_cap=(line_cap)
line_cap=,
line_dash
line_dash,
line_dash=(line_dash)
line_dash=,
line_join
line_join,
line_join=(line_join)
line_join=,
no_fill
no_fill,
no_fill=(no_fill)
no_fill=,
reset_shape_properties
reset_shape_properties,
stroke
stroke,
stroke=(stroke)
stroke=,
stroke_width
stroke_width,
stroke_width=(stroke_width)
stroke_width=
Instance Method Detail
Fill background color
ctx = Chitra.new
# white background
ctx.background 1, 0, 0
Fill background color(Hex Color)
ctx = Chitra.new
# white background
ctx.background "#ffffff"
Fill background color(Gray scale)
ctx = Chitra.new
# white background
ctx.background 1
Apply Composite
ctx.group_start
ctx.rect 10, 10, 100, 100
ctx.composite "in"
ctx.rect 50, 50, 100, 100
ctx.group_end
Other available Composite options are: Clear, Source, Over, In, Out, Atop, Dest, DestOver, DestIn, DestOut, DestAtop, Xor, Add, Saturate, Multiply, Screen, Overlay, Darken, Lighten, ColorDodge, ColorBurn, HardLight, SoftLight, Difference, Exclusion, HslHue, HslSaturation, HslColor, HslLuminosity
Refer https://www.cairographics.org/operators/ for more details.
Enable debug messages to verify if any property applied to an element is not maching
ctx = Chitra.new
ctx.enable_debug
Set fill color. Color values are between 0 and 1. Default color is Black/RGBA(0, 0, 0, 1)
# r g b
ctx.fill 0, 0, 1
# Fill with 50% opacity
r g b a
ctx.fill 0, 0, 1, 0.5
Set fill color in hex format. Default color is Black/Hex(#000000)
# Black color
# hex_color
ctx.fill "#000000"
# White color
ctx.fill "#ffffff"
# Fill black with 50% opacity
# 00 - 0% and FF - 100% opacity
gray a
ctx.fill "#00000080"
Set fill color. Color values are between 0 and 1. Default color is Black/RGBA(0, 0, 0, 1)
# Black color
# gray
ctx.fill 0
# White color
ctx.fill 1
# Fill black with 50% opacity
gray a
ctx.fill 0, 0.5
Set fill opacity. Opacity values are between 0 and 1.
# 50% Opacity
# opacity
ctx.fill_opacity 0.5
End a Group
ctx.group_start
ctx.rect 10, 10, 100, 100
ctx.composite "in"
ctx.rect 50, 50, 100, 100
ctx.group_end
Start a Group
ctx.group_start
ctx.rect 10, 10, 100, 100
ctx.composite "in"
ctx.rect 50, 50, 100, 100
ctx.group_end
Grouped code run as block
ctx.grouped do
ctx.rect 10, 10, 100, 100
ctx.composite "In"
ctx.rect 50, 50, 100, 100
end
Get height of the canvas
# width height
ctx = Chitra.new 1600, 900
puts ctx.height # 900
Enable/Disable text hyphenation. Default is disabled
ctx.hyphenation true
Get width and height of the image
ctx = Chitra.new
w, h = ctx.image_size "logo.png"
Set Line cap style Allowed values are butt, round and square. Default value is butt.
ctx.line_cap "round"
ctx.line 100, 100, 500, 100
Set line dash pattern. line_dash 0
disables
the dash.Symmetric dash pattern with one value
to this function.
ctx.line_dash [2]
Asymmetric pattern
ctx.line_dash [2, 4, 10]
To set the offset value to start the pattern
ctx.line_dash [2, 4, 10], offset: 1
Set line dash pattern. line_dash 0
disables
the dash.Symmetric dash pattern with one value
to this function.
ctx.line_dash 2
Asymmetric pattern
ctx.line_dash 2, 4, 10
To set the offset value to start the pattern
ctx.line_dash 2, 4, 10, offset: 1
Set Line join style Possible values are: miter, round, bevel. Default value is miter.
ctx.stroke_width 14
ctx.line_join "round"
ctx.rect 100, 100, 500, 500
Reset the drawing to clean and empty canvas
ctx = Chitra.new
ctx.fill 0
ctx.rect 0, 0, width, height
ctx.save "slide1.png"
ctx.new_drawing
ctx.fill 0, 0, 1
ctx.rect 0, 0, width, height
ctx.save "slide2.png"
Creates a new page.
ctx = Chitra.new 200
ctx.scale 2
ctx.rect 40, 40, 40, 40
ctx.new_page
ctx.rect 40, 40, 40, 40
Draw oval shape
ctx = Chitra.new
x y w h
ctx.oval 100, 100, 500, 500
Draw polygon shape. By default closes the path
ctx = Chitra.new
x y w h
ctx.polygon [50, 450, 50, 50, 450, 50, 100, 100], close: true
Draw polygon shape. By default closes the path
ctx = Chitra.new
x y w h
ctx.polygon 50, 450, 50, 50, 450, 50, 100, 100, close: true
Draw rectangle shape
ctx = Chitra.new
x y w h
ctx.rect 100, 100, 500, 500
Rotate the canvas in clockwise to given angle. Negative angle rotates the canvas in anti-clockwise direction.
ctx = Chitra.new 200
ctx.rotate 45
ctx.rect 40, 40, 40, 40
Rotate center by default is (0, 0). Change this by adding arguments to rotate function
ctx = Chitra.new 200
ctx.rotate 45, 100, 100
ctx.rect 50, 50, 100, 100
Save the output to a file.
ctx = Chitra.new
# filename
ctx.save "hello.png"
Use Saved state as block that applies save_state and restore_state automatically.
ctx.fill 1, 0, 0
ctx.saved_state do
ctx.fill 0, 0, 1
# Blue rect
ctx.rect 100, 100, 200, 200
end
# Red Rect
ctx.rect 200, 200, 200, 200
Scale the canvas with the given x and y scale factors. direction.
ctx = Chitra.new 200
ctx.scale 2
ctx.rect 40, 40, 40, 40
Different scaling for x and y
ctx = Chitra.new 200
ctx.scale 2, 1
ctx.rect 40, 40, 40, 40
Change the center of Scale
Set Stroke color(Gray scale). Color values are between 0 and 1. Default color is Black/RGBA(0, 0, 0, 1)
# r g b
ctx.stroke 0, 0, 1
# Stroke with 50% opacity
r g b a
ctx.stroke 0, 0, 1, 0.5
Set Stroke color in hex format. Default color is Black/Hex(#000000)
# Black color
# hex_color
ctx.stroke "#000000"
# White color
ctx.stroke "#ffffff"
# Fill black with 50% opacity
# 00 - 0% and FF - 100% opacity
ctx.stroke "#00000080"
Set stroke/line color(Gray scale). Color values are between 0 and 1. Default color is Black/RGBA(0, 0, 0, 1)
# Black color
# gray
ctx.stroke 0
# White color
ctx.stroke 1
# Fill black with 50% opacity
gray a
ctx.stroke 0, 0.5
Set stroke opacity. Opacity values are between 0 and 1.
# 50% Opacity
# opacity
ctx.stroke_opacity 0.5
Translate the canvas to given x and y
ctx = Chitra.new
ctx.translate 100, 100
ctx.rect 0, 0, 500, 500