class Chitra::Context

Included Modules

Defined in:

context.cr
elements/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

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

def background(r, g, b, a = 1.0) #

Fill background color

ctx = Chitra.new
# white background
ctx.background 1, 0, 0

[View source]
def background(hexcol : String) #

Fill background color(Hex Color)

ctx = Chitra.new
# white background
ctx.background "#ffffff"

[View source]
def background(gray, a = 1.0) #

Fill background color(Gray scale)

ctx = Chitra.new
# white background
ctx.background 1

[View source]
def 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

Refer https://www.cairographics.org/operators/ for more details.


[View source]
def debug : Bool #

[View source]
def debug=(debug : Bool) #

[View source]
def elements : Array(Chitra::Element) #

[View source]
def enable_debug #

Enable debug messages to verify if any property applied to an element is not maching

ctx = Chitra.new
ctx.enable_debug

[View source]
def fill(r, g, b, a = 1.0) #

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

[View source]
def fill(hexcol : String) #

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"

[View source]
def fill(gray, a = 1.0) #

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

[View source]
def fill_opacity(a) #

Set fill opacity. Opacity values are between 0 and 1.

# 50% Opacity
#                opacity
ctx.fill_opacity 0.5

[View source]
def font(font_name, font_size) #

Set Font family and size

ctx.font "Times", 30

[View source]
def font_size(font_size) #

Set Font size

ctx.font_size 30

[View source]
def font_weight(wt) #

Set Font weight

ctx.font_weight "Heavy"

[View source]
def 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

[View source]
def 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

[View source]
def 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

[View source]
def height #

Get height of the canvas

#                         width height
ctx = Chitra.new 1600, 900
puts ctx.height # 900

[View source]
def hyphen_char(value : String) #

Set Hyphenation char. Default is -

ctx.hyphen_char "*"

[View source]
def hyphenation(value : Bool) #

Enable/Disable text hyphenation. Default is disabled

ctx.hyphenation true

[View source]
def image(path, x, y) #

Draw image

ctx = Chitra.new
ctx.image "logo.png", 100, 100

[View source]
def image_height(path) #

Get height of the image

ctx = Chitra.new
h = ctx.image_height "logo.png"

[View source]
def image_size(path) #

Get width and height of the image

ctx = Chitra.new
w, h = ctx.image_size "logo.png"

[View source]
def image_width(path) #

Get width of the image

ctx = Chitra.new
w = ctx.image_width "logo.png"

[View source]
def line(x1, y1, x2, y2) #

Draw a line

ctx = Chitra.new
x y w h
ctx.line 100, 100, 500, 500

[View source]
def line_cap(value) #

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

[View source]
def line_dash(values : Array(Int32), offset = 0) #

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

[View source]
def line_dash(*values, offset = 0) #

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

[View source]
def line_height(value) #

[View source]
def line_join(value) #

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

[View source]
def markup(txt, x, y) #

Draw text for given x and y values.

markup "Hello <b>World</b>", 100, 100

[View source]
def markup_box(txt, x, y, w, h = 0.0) #

[View source]
def 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"

[View source]
def new_page #

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

[View source]
def no_fill #

Disable fill

ctx.no_fill

[View source]
def no_stroke #

Disable Stroke.

ctx.stroke_width 0
# OR
ctx.no_stroke

[View source]
def 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

[View source]
def polygon(points : Array(Float64), 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

[View source]
def polygon(*points, 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

[View source]
def rainbow(txt : String | Array(String), x, y, colors = RAINBOW_COLORS, join_str = "") #

[View source]
def rainbow_box(txt : String | Array(String), x, y, w, h = 0.0, colors = RAINBOW_COLORS, join_str = "") #

[View source]
def rect(x, y, w, h = 0.0) #

Draw rectangle shape

ctx = Chitra.new
x y w h
ctx.rect 100, 100, 500, 500

[View source]
def restore_state(saved_context) #

Restore Context state

ctx.restore_state

[View source]
def rotate(angle, center_x = 0, center_y = 0) #

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

[View source]
def save(file : String) #

Save the output to a file.

ctx = Chitra.new
#        filename
ctx.save "hello.png"

[View source]
def save_state(saved_context) #

Save Context state

ctx.save_state
ctx.fill 0, 0, 1

[View source]
def saved_state(&block) #

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

[View source]
def scale(scale_x, scale_y = 0) #

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


[View source]
def size : Chitra::Size #

[View source]
def size=(size : Chitra::Size) #

[View source]
def stroke(r, g, b, a = 1.0) #

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

[View source]
def stroke(hexcol : String) #

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"

[View source]
def stroke(gray, a = 1.0) #

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

[View source]
def stroke_opacity(a) #

Set stroke opacity. Opacity values are between 0 and 1.

# 50% Opacity
#                  opacity
ctx.stroke_opacity 0.5

[View source]
def stroke_width(value : Int32) #

Set Stroke/Line width. Default value is 1.

ctx.stroke_width 10

[View source]
def text(txt, x, y) #

Draw text for given x and y values.

text "Hello World", 100, 100

[View source]
def text_align(value : String) #

Set Text align for text boxes

ctx.text_align "center"

[View source]
def text_box(txt, x, y, w, h = 0.0) #

[View source]
def translate(x, y) #

Translate the canvas to given x and y

ctx = Chitra.new
ctx.translate 100, 100
ctx.rect 0, 0, 500, 500

[View source]
def width #

Get width of the canvas

#                         width height
ctx = Chitra.new 1600, 900
puts ctx.width # 1600

[View source]