jrobertson / gtk2svg

Experimental gem to render SVG using GTK2
2 stars 0 forks source link

Gtk2SVG: An onmousedown example

require 'gtk2svg'

s =<<SVG
<svg width="400" height="110">
  <script>
    def hello()
      puts 'hello world'
    end
  </script>
  <rect width="300" height="100" onmousedown="hello()" style="fill:rgb(0,0,255);stroke-width:3;stroke:rgb(0,0,0)" />
</svg>
SVG

app = Gtk2SVG::Main.new s, irb: true

In the above example when the left mouse button is pressed while over the blue rectangle, the message 'hello world' is displayed in the command-line.


Experimenting with creating an SVG app using the Gtk2SVG gem

#!/usr/bin/env ruby

# file: draw3.rb

require 'gtk2svg'

s =<<SVG
<svg width="400" height="110">
  <rect width="300" height="100" style="fill:rgb(0,0,255);stroke-width:3;stroke:rgb(0,0,0)" />
  <rect x="100" y="100" width="100" height="100" style="fill:rgb(255,0,0);stroke-width:3;stroke:rgb(0,0,0)" />
</svg>
SVG

a = Gtk2SVG::Render.new(s).to_a

area = Gtk::DrawingArea.new

area.signal_connect("expose_event") do

  drawing = Gtk2SVG::DrawingInstructions.new area
  drawing.render a

end

Gtk::Window.new.add(area).show_all
Gtk.main

The above example creates a GTK2 application which can parse and render SVG. Below is a screenshot of the application.

Screenshot of the SVG rendered in a GTK2 application

Resources

gtk2 drawing svg gtk2svg gem