SVGKit
Introduction
All you need to work with SVGs at the basic, browser-independent level. Create a new SVG at runtime. Load SVG data from a URL. Conveniently handle quirks of three ways SVG can be included in browsers: object, embed, and inline. Handle different browsers and different SVG engines. Has convenience functions for DOM manipulation ala MochiKit. It manages the internal SVG DOM objects: HTMLElement, SVGDocument, SVGElement
Once you have an SVGKit object, adding content is simple. Inspired by MochiKit DOM, there is an all-caps function for every
tag defined in the SVG spec.
svg.append(CIRCLE( {'cx':50, 'cy':50, 'r':20, 'fill':'purple', 'fill-opacity':.3) );
It's annoying to have external javascript files in an SVG, especially a big library like MochiKit and SVGKit, but for anything more than a simple demo, it's worth it. You can also pick which functions you want to include and paste them into your SVG.
Live Demos
- svg live demo - Table of tests that you can modify and re-run client side.
- type tests - Tests browser support for different ways of including and getting at SVG
- SVGKit Interpreter - Play with an SVG interactively one line of code at a time.
Overview of Code
There is a single object SVGKit. You instantiate one for each SVG you want access too. You can use its constructor to grab an existing SVG, load an SVG from a file, or create a blank SVG to draw into. The goal of the code is to grab the svg element and be able to manipulate it independent of the browser/plugin and the technique used to include the SVG on the page. In many browsers this is just not possible for some or all methods.
Browse Code in SVN Repository
JavaScript access to SVG (not SVGKit specific, but a reference for everybody)
There are several different ways for JavaScript to access SVG content. The there are three major DOM elements you need to worry about, each of which has a corresponding member in a SVGKit object.svgDocument
(typeSVGDocument
) referrs to the document the<svg>
element is contained in. It has document-type methods likecreateElement
,getElementById
, andgetElementsByTagName
. If the SVG is inline in the XHTML or the JavaScript code lives in the SVG file, this will just bedocument
, otherwise you have to get it from the HTML element that the SVG is sitting in.svgElement
(typeSVGSVGElement
) referrs to the<svg>
element. It has the width and height of the entire SVG and contains all graphical elements inchildNodes
. It has element-type methods along with more graphical methods likesuspendRedraw
, andcreateSVGTransform
.htmlElement
(typeHTMLElement
) is the container that contains the SVG in an HTML document. This refers to an HTML element like<object>
,<embed>
,<iframe>
, or<svg>
. If the SVG is inline in the XHTML, this will just be the same assvgElement
. An<object>
and<embed>
element also has a width and height which may be different that theSVGElement
's, in which case you'll probably get a scroll bar.
Here's a summary of the most common ways to get access to the all important SVGSVGElement in JavaScript. Different browsers impliment different methods to varying degrees:
htmlElement |
svgDocument |
svgElement |
|
<svg> (inline) |
the svg tag |
document or svgElement.ownerDocument |
same as htmlElement |
<embed> |
the embed tag | htmlElement.getSVGDocument() |
svgDocument.rootElement or svgDocument.documentElement |
<object> |
the object tag | htmlElement.contentDocument |
svgDocument.rootElement or svgDocument.documentElement |
<iframe> |
the iframe tag | can never access | can never access |
inside svg | not applicable | document |
svgDocument.rootElement |
Constructors
SVGKit()
-- For JavaScript included inside an SVGSVGKit(node)
-- Already have an HTML element that contains an SVG like<object>
,<embed>
,<iframe>
, or<svg>
SVGKit(id)
-- Have the id for an HTML element (if your id ends in ".svg", pass in the node instead because strings ending in .svg will be treated as filenames.)SVGKit(filename, id, type, width, height)
-- Create a new HTML element contains the SVG infilename
(must end in ".svg")SVGKit(width, height, id, type)
-- Create a new SVG from scratch withwidth
,height
, andid
. type is'inline'
'embed'
or'object'
For <embed>
or <object>
-- The SVG won't necessarily be ready when the constructor
returns because the document loads in the background. All real graphical manipulation has to passed to svg.whenReady(callback)
, which will register an onload
callback to allow it to load in the
background.) If you are creating a new SVG, what really happens
is an <embed>
or <object>
tag is created referencing an svg file with no
graphical content. This means you have to manage a URI
to empty.svg. Another issue with<embed>
or <object>
is that if
they get hidden (for example the div they're contained in becomes
invisible) the browser will just forget about the SVG DOM and all of your hard
work. I'm thinking about registering an onhide
event which saves the
xml and then restores it on load. The answer probably is that <embed>
or <object>
just suck for all of this.
For inline <svg>
-- Creating a new SVG is as simple as creating a DOM
element. Loading an SVG from a file is difficult because an xml
loader must be used and the SVG extracted and properly recognized as
SVG content so you have access to the graphical DOM methods and your
content shows up. If you load an inline SVG from a file, you're going
to have to register your graphical manipulation with svg.whenReady(callback)
as above.
Overview of Functionality
- Content Manipulation: createSVGDOM, append, createUniqueID, getDefs, suspendRedraw, deleteContent, etc.
- Transformations: rotate, translate, scale. These can be applied to any element and do some rudimentary optimization. (If you translate the same element twice, combine them into a single translation.) I could suck all transformations into a single transformation matrix (like Inkscape), and while the would be efficient to render, it would be ugly to read.
- Output: toXML and emitXML.
To Do
- JSDoc Documentation
- Burst Canvas Animation
- http://code.google.com/p/cakejs/
- Parse comma or space seperated list of numbers (like those given in attributes of SVGs.)
- Parse the path attribute