
A JavaScript library for interacting with SVG.
You need 3 files to get started:
You can also get it from the GitHub: PottisJS at GitHub. (patches welcome!)
PottisJS works with Firefox (3.5) and Safari (4.0). Sorry Opera!
In order to really do interactive stuff, animation is clearly needed, as seen on the SVG Open 2009 workshop. Three of the four teams there included some (JavaScript) animation in their prototype. That's something to wait for in PottisJS 2.0 :)
Initialize a new PottisJS library for targetSVG SVG element. If callback is given, callback(e) is called on the first click onto the targetSVG element. This is because some things don't work until all the external SVG files imported with importSVG have completely loaded, waiting for the mouse click gives them time to load.
var svg = document.getElementById("tutorial");
var pottis = new Pottis(svg);
<svg id="tutorial" version="1.1" width="900" height="600"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink" />
When element is clicked, callback(event) is fired.
When element is double-clicked, callback(event) is fired.
Make element draggable. If targetElement is given, then element works as a handle for dragging the targetElement.
// Draggable apple
pottis.addDrag(apple);
// Group moves when the basket is dragged.
var basketAndApples = pottis.group("basket_apples", [basket, green_apple, red_apple])
pottis.addDrag(basket, basketAndApples);
Make receiverElement droppable. Dropping draggables (made with addDrag) into receiverElement fires callback(receiver, dropped).
pottis.addDrop(basket, insertAppleIntoBasket);
When mouse leaves the element, callback(event) will be fired.
When mouse hovers over element, callback(event) will be fired.
When element is right-clicked, callback(event) will be fired.
Element will rotate when dragged. If targetElement is given, then element works as handle for rotating the targetElement.
// Element will rotate when dragged
pottis.addRotate(apple);
// Dragging basket will rotate apples
var apples = pottis.group("apples", [green_apple, red_apple])
pottis.addRotate(basket, apples);
Element will be scaled when dragged. If targetElement is given, then element works as a handle for scaling.
// Element will scale when dragged
pottis.addScale(apple);
// Dragging basket will scale a group
var basketAndApples = pottis.group("basket_apples", [basket, green_apple, red_apple])
pottis.addScale(basket, basketAndApples;
Removes drag from an element
Groups elements (array) into svg group element.
var basketAndApples = pottis.group("basket_apples", [basket, green_apple, red_apple]);
Hides element. See show.
Moves element above the peerElement in rendering order.
Moves element below the peerElement in rendering order.
Moves element to the bottom of all other elements in rendering order.
Moves element to the top of all other elements in rendering order.
Removes element from the DOM.
Shows element. See hide.
Rotate an element by angle (originally 0). By default, rotates relatively to element center. This can be changed with cx and cy, which specify the center point for rotate, relative to element top-left.
Increment element rotation by angleToAdd. Returns new new rotation angle.
Scale an element by sx and sy (originally 1 and 1). If sy is not specified, it's the same as sx. By default, scales relatively to element center. This can be changed with cx and cy, which specify the center point for scale, relative to element top-left.
Increment element scale. Returns the new scale.
Translate an element.
Add to existing translation of an element. Values can be negative. Returns the new translation.
Creates SVGImageElement with url as the source.
pottis.image("http://images.encyclopediadramatica.com/images/3/34/Longcat_buildings.jpg", "longcat_buildings");
Imports external SVG to the DOM and wraps import in a group groupId. Quite experimental!
pottis.importSVG("apple.svg", "apple");
pottis.use("apple", "fresh_apple");
Inserts a new shape into the SVG DOM. element can be any valid SVG element. See W3C specification for attributes.
pottis.shape("rect", { x: 10, y: 10, width: 200, height: 100, fill: "blue" });
Creates SVGTextElement with textContent as the text content.
pottis.text("Thanks for using PottisJS!", null, 200, 100);
Inserts an SVG use element into the SVG DOM. The use element references to a defs element with id defId.
pottis.use("apple", "another_fresh_apple");
Returns mouse coordinates inside of the parent element.
Retuns a random integer between [0, maxValue-1].
// Plots element in x: [0,499], y: [100,399]
pottis.use("apple", "red_apple", pottis.randomInt(500), pottis.randomInt(300)+100);