{"id":317,"date":"2007-08-27T23:54:43","date_gmt":"2007-08-28T04:54:43","guid":{"rendered":"http:\/\/www.juixe.com\/techknow\/index.php\/2007\/08\/27\/running-with-shoes-2d-graphics-and-animation\/"},"modified":"2007-08-27T23:54:43","modified_gmt":"2007-08-28T04:54:43","slug":"running-with-shoes-2d-graphics-and-animation","status":"publish","type":"post","link":"http:\/\/juixe.com\/techknow\/index.php\/2007\/08\/27\/running-with-shoes-2d-graphics-and-animation\/","title":{"rendered":"Running with Shoes &#8211; 2D Graphics and Animation"},"content":{"rendered":"<p>I&#8217;ve already gone over the basic UI features of the <a href=\"http:\/\/www.juixe.com\/techknow\/index.php\/2007\/08\/27\/running-with-shoes-a-mini-gui\/\">Shoes<\/a> toolkit.  Shoes is a nice little domain specific language for developing GUI applications in Ruby.  Here I&#8217;ll go over some of the 2D graphics and animation capabilities in Shoes.<\/p>\n<p>Shoes has a light weight 2D support where you can draw lines, rectangles, and circles.  To create an application that draws a line we can execute the following bit of code.<\/p>\n<p>[source:ruby]<br \/>\nShoes.app :width => 200, :height => 200 do<br \/>\n  # line x1, y1, x2, y2<br \/>\n  line 20, 40, 60, 80<br \/>\nend<br \/>\n[\/source]<\/p>\n<p>To draw an oval insert the following code inside the shoes code block.<\/p>\n<p>[source:ruby]<br \/>\nShoes.app :width => 200, :height => 200 do<br \/>\n  # oval center_x, center_y, width, height<br \/>\n  oval 50, 100, 100, 50<br \/>\nend<br \/>\n[\/source]<\/p>\n<p>And to a simple rectangle, you can do so by using the rect method.<\/p>\n<p>[source:ruby]<br \/>\nShoes.app :width => 200, :height => 200 do<br \/>\n  # rect top_x, top_y, with, height<br \/>\n  rect 100, 100, 30, 10<br \/>\nend<br \/>\n[\/source]<\/p>\n<p>Shoes also supports the animate method which can be used, as it&#8217;s name applies, to repeatably call a block of code that animates some graphics.  The animate method takes a parameter, the number of frames per second.  For example, to draw a new image once a second we can call animate(1).  To draw an image twice a second call animate(2).  Here is a simple example of the animate method in action.<\/p>\n<p>[source:ruby]<br \/>\ntext_field = nil<br \/>\nShoes.app :width => 200, :height => 200 do<br \/>\n  text_field = text Time.new.to_s<br \/>\n  animate(1) do<br \/>\n    text_field.replace Time.new.to_s<br \/>\n  end<br \/>\nend<br \/>\n[\/source]<\/p>\n<p>The above piece of code creates a text field and updates the value of the text field with the current time once a second.  This way you can animate a simple text-based clock.<\/p>\n<p>Here is another animation example.  The following code draws a rectangle.  The rectangle is moved twice a second to a random location within the application window.  This code uses the fill and stroke methods, which accept arguments to define the fill and stroke color respectively.  You can also use the strokewith method to set the width of the pen stroke.  Also notice that the following example invokes the width and height methods which return the current width and height of the application window.<\/p>\n<p>[source:ruby]<br \/>\nmoving_box = nil<br \/>\nShoes.app :width => 200, :height => 200 do<br \/>\n  # fill red, green, blue, alpha<br \/>\n  fill 1.0, 0.5, 1.0, 1.0<br \/>\n  stroke 0.5, 0.5, 1.0, 1.0<br \/>\n  strokewidth 5<br \/>\n  moving_box = rect 10, 10, 20, 20<\/p>\n<p>  animate(2) do<br \/>\n    x = (0..width-20).rand<br \/>\n    y = (0..height-20).rand<br \/>\n    moving_box.move x, y<br \/>\n  end<br \/>\nend<br \/>\n[\/source]<\/p>\n<p>As you can see, it is easy to draw 2D graphics using Shoes.<\/p>\n<p>Technorati Tags: <a href=\"http:\/\/technorati.com\/tag\/ruby\" rel=\"tag\">ruby<\/a>, <a href=\"http:\/\/technorati.com\/tag\/shoes\" rel=\"tag\"> shoes<\/a>, <a href=\"http:\/\/technorati.com\/tag\/gui\" rel=\"tag\"> gui<\/a>, <a href=\"http:\/\/technorati.com\/tag\/ui\" rel=\"tag\"> ui<\/a>, <a href=\"http:\/\/technorati.com\/tag\/_why\" rel=\"tag\"> _why<\/a>, <a href=\"http:\/\/technorati.com\/tag\/tutorial\" rel=\"tag\"> tutorial<\/a>, <a href=\"http:\/\/technorati.com\/tag\/2d\" rel=\"tag\"> 2d<\/a>, <a href=\"http:\/\/technorati.com\/tag\/graphics\" rel=\"tag\"> graphics<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve already gone over the basic UI features of the Shoes toolkit. Shoes is a nice little domain specific language for developing GUI applications in Ruby. Here I&#8217;ll go over some of the 2D graphics and animation capabilities in Shoes. Shoes has a light weight 2D support where you can draw lines, rectangles, and circles. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":""},"categories":[19,22,3],"tags":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p902K-57","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/317"}],"collection":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/comments?post=317"}],"version-history":[{"count":0,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/317\/revisions"}],"wp:attachment":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/media?parent=317"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/categories?post=317"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/tags?post=317"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}