Sketchily allows the easy integration of svg-edit with any rails application.
Currently supports and provides svg-edit-2.7.
Add this line to your application's Gemfile:
gem 'sketchily'And then execute:
$ bundleOr install it yourself:
$ gem install sketchilyAdd //= require sketchily to your application.js.
It should look similar to this:
//= require jquery
//= require jquery_ujs
//= require sketchilyNote: sketchily will automatically add the following files to your asset precompilation list:
sketchily.js
svg-edit/canvg/canvg.js
svg-edit/canvg/rgbcolor.js
svg-edit/extensions/ext-*.js
svg-edit/locale/lang.*.js
Other html files and images used by svg-edit will also be precompiled.
Sketchily adds new form elements which can be accessed by calling:
f.sketchilyor simplysketchilyfrom inside aform_for @my_object |f|sketchily_tagfrom inside aform_tag
This gem also adds a helper method that can be called to display the resulting SVG images (without an editor):
sketchily_showfrom any view template
- Both of these functions work exactly like the equivalent
hidden_fieldfunctions, except that svg-edit is displayed instead. - The
f.sketchilyformat accepts a method name and an options hash. - The
sketchilyformat (called without the form builder object) accepts an object name, a method name and an options hash. - The
sketchily_tagformat accepts a tag name, the svg string to be edited and an options hash.
Currently available options are:
-
widthtotal width of editor -
heighttotal height of editor -
bkgd_colorcanvas background color (3 or 6 hex digit html color format; not saved with image; see sketchily_show) -
bkgd_urlcanvas background image url (not saved with image; see bkgd_file option for sketchily_show) -
canvas_widthinitial canvas width -
canvas_heightinitial canvas height -
canvas_expansion0 if you want to disable scrolling -
hide_rulerstrue if you want to hide the canvas rulers -
hide_menutrue if you want svg-edit's menu to be hidden -
hide_image_tooltrue if you want to hide theimage toolbutton -
show_hyperlink_tooltrue if you want to show thehyperlink toolbutton (see explanation below) -
show_layers(true if you want the layer selector to display automatically when the editor is loaded) -
urloverride contents of the canvas with the svg file in the given url (careful: changes lost whenever editor is loaded) -
idoverride the default id (see explanation below) -
valueoverride the default value (the svg itself; can be useful when using form_for) -
indexoverride the default index (affects the tag name; can be useful when using form_for) -
extensionseditor plugins (e.g. 'plugin.js' will load '/assets/extensions/plugin.js') -
other standard html attributes for the input tag
The hyperlink tool is disabled by default, as embedded links and scripts do not work with the display method used by sketchily_show.
The sketchily_show helper will display svg's inside of tags, which are treated as static images by most browsers.
We consider this to be a necessary precaution when dealing with user-generated svg's.
Sketchily requires a unique id (by default, this is set in the same way as hidden_field) each time it is called in the same page.
However, some uses of form_for can generate repeated ids (e.g. multiple form_for @my_object.new in the same page).
In those cases, you need to generate and specify your own unique ids. A possible solution is to use one of the many uuid generator gems.
It is recommended that the database entries associated with sketchily form elements be of type text.
The following examples assume that the database table for @my_object has a sketch column of type text.
Example usage (haml):
= form_for @my_object do |f|
= f.sketchily :sketch, :hide_menu => true- This function takes the base64-encoded SVG string as an argument and an options hash.
- The SVG string can be directly read from the field used by the
sketchilyandsketchily_tagfunctions.
Currently available options are:
-
widthwidth of resulting image object -
heightheight of resulting image object -
bkgd_colorbackground color (3 or 6 hex digit html color format) -
bkgd_filebackground image file path (must be a local image file)
Passing only one of those options should keep the aspect ratio of the SVG constant in most browsers.
Example usage (haml):
= sketchily_show @my_object.sketch, :width => "500"See ConfigOptions for information about how to configure svg-edit. Please note that most common options, like width and height should be set directly on calls to sketchily's functions in ruby code.
Although more testing is needed, we currently believe sketchily supports any browsers that svg-edit supports, namely:
- Firefox 1.5+
- Opera 9.50+
- Safari 4+
- Chrome 1+
- IE 9+
- IE 6+ (with the Chrome Frame plugin)
- Fork it
- Create your feature branch (
git checkout -b my-new-feature) - Create specs for your feature
- Ensure that all specs pass
- Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create new pull request