| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- /*
- * Licensed under the MIT license:
- * http://www.opensource.org/licenses/mit-license.php
- *
- */
- (function() {
- Raphael.fn.toJSON = function(callback) {
- var
- data,
- elements = new Array,
- paper = this
- ;
- for ( var el = paper.bottom; el != null; el = el.next ) {
- data = callback ? callback(el, new Object) : new Object;
-
- if ( data ) elements.push({
- data: data,
- type: el.type,
- attrs: el.attrs,
- transform: el.matrix.toTransformString(),
- id: el.id
- });
- }
- return JSON.stringify(elements);
- }
- Raphael.fn.fromJSON = function(json, callback) {
- var
- el,
- paper = this
- ;
- if ( typeof json === 'string' ) {
- json = JSON.parse(json);
- }
- for ( var i in json ) {
- if ( json.hasOwnProperty(i) ) {
- el = paper[json[i].type]()
- .attr(json[i].attrs)
- .transform(json[i].transform);
- el.id = json[i].id;
- if ( callback ) el = callback(el, json[i].data);
- if ( el ) paper.set().push(el);
- }
- }
- }
- })();
|