rasem is a pure ruby gem that allows you to describe your SVG images in ruby code.
Have you ever wanted to visualize some data in a nice image?
Do you want to draw a complex image that is easier to describe using a code?
Would you like to power your HTML 5 site with nicely drawn SVG images?
Do you need to generate nice images from your ruby code?
Did you feed up with using DIVs and Javascript to draw simple graphs?
Is your server overwhelmed with sending PNG images that can be described in a few bytes using SVG?
Rasem allows you to generate SVG images that can be easily exported to other formats and embed in an HTML 5 page.
Draw basic elements: line, rectangle, rouned rectangle, circle, ellipse, polygon and polyline.
Use SVG styles such as stroke-width, opacity and fill.
Create SVG groups for better editing in SVG editors.
Coming features:
Include all SVG standard elements such as gradients, filters and paths
Create default rake and thor tasks to convert your .rasem files to .svg.
Embed other images in your SVG image
Output to more formats such as PNG.
Rasem is still in alpha release. Please report your bugs so that we can work on it. You are welcome to contribute to the project by adding more features and fixing bugs. The end goal of the project is to fully support SVG standard and make it easy to use.
geminstallrasem
There are two main methods to generate images with Rasem.
Create a file with extension .rasem. Here is a sample file test.rasem
set_width100set_height100circle20, 20, 5circle50, 50, 5line20, 20, 50, 50
Save this file and execute the command
rasemtest.rasem
A file test.svg will be generated.
You can generate the image from your ruby code. This has several uses such as sending the image on the fly from a Rails application.
Here is a simple example
require'rasem'img = Rasem::SVGImage.new(100,100) docircle20, 20, 5circle50, 50, 5line20, 20, 50, 50endputsimg.output
img.output is the SVG code generated for this images.
The following examples are for .rasem files. These are actually ruby scripts that are executed to generate the image. So, you can write arbitrary ruby code in .rasem files as you will see.
simple_graph.rasem
nodes = [[10,10], [20,20], [10,20]] radius = 3with_style:fill=>"white", :stroke=>"black"dofornodeinnodescirclenode.first, node.last, radiusendendlinenodes[0].first, nodes[0].last, nodes[1].first, nodes[1].last, :stroke=>"blue"
Here is a more sophisticated example.
tictactoe.rasem
set_width150set_height150board = [['x', 'o', 'x'], ['o', '-', 'o'], ['x', 'o', 'x']] defdraw_x(x,y) group:stroke=>"red"dolinex-20, y-20, x+20, y+20linex-20, y+20, x+20, y-20endenddefdraw_o(x,y) circlex, y, 20, :stroke=>"blue", :fill=>"white"endgroup:stroke=>"black"dorectangle0, 0, 150, 150, :stroke_width=>2, :fill=>"white"line50, 0, 50, 150line100, 0, 100, 150line0, 50, 150, 50line0, 100, 150, 100endboard.each_with_indexdo|row, row_index|row.each_with_indexdo|cell, column_index|ifcell=="x"draw_xrow_index*50+25, column_index*50+25elsifcell=="o"draw_orow_index*50+25, column_index*50+25endendend
Here are some examples that show how you can generate images from your ruby code.
You can generate SVG and store it to file
img = Rasem::SVGImage.new(100, 100)
img.line(0, 0, 100, 100)
img.close
File.open("test.svg", w") do |f|
f << img.output
endYou can use pass a block to SVGImage.new to make things more compact
img = Rasem::SVGImage.new(100, 100) doline(0, 0, 100, 100) end# Image is closed automatically# Write to file
You can make your code even more compact by passing the file as a last argument to SVGImage.new
File.open("test.svg", "w") do|f|Rasem::SVGImage.new(100, 100, f) do|f|line(0, 0, 100, 100) endend
The previous example has another advantage. It does not need to buffer the whole image to memory before writing to file. Rasem is smart enough to detect that the output can be passed directly to the file, hence, saving unnecessary memory buffers.
Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet
Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it
Fork the project
Start a feature/bugfix branch
Commit and push until you are happy with your contribution
Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.
Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
Copyright © 2011 Ahmed Eldawy. See LICENSE.txt for further details.