GithubHelp home page GithubHelp logo

raphael.json's Introduction

Raphaël.JSON

Convert Raphaël 2.0 elements on a paper to JSON and back.

This plugin can be used to save the state of a paper for later re-use. It was originally forked from Jonathan Spies's raphael.serialize and later rewritten from scratch to work with Raphaël 2.0.

Licensed under the MIT license.

Example

<script type="text/javascript" src="raphael-min.js"></script>
<script type="text/javascript" src="raphael.json.js"></script>

<div id="holder"></div>

<script type="text/javascript">
	var paper = Raphael('holder');

	var rect = paper
		.rect(50, 50, 50, 50)
		.attr('fill', '#f00')
		.transform('s2')
		.rotate(10)
		;

	var json = paper.toJSON();

	paper.clear();

	paper = Raphael('holder');

	paper.fromJSON(json);
</script>

Callback

A callback function can be used to save and restore custom attributes.

var json = paper.toJSON(function(el, data) {
	data.id = el.node.id;

	return data;
});
paper.fromJSON(json, function(el, data) {
	el.node.id = data.id;

	return el;
});

Preserving sets

var paper = Raphael('holder');

// Create a set
var exampleSet = paper.set();

var rect = paper.rect(50, 50, 50, 50)
	.attr({ fill: 'red' })
	;

exampleSet.push(rect);

// Store a unique identifier in each of the set's elements
for ( i in exampleSet ) {
	exampleSet[i].setName = 'exampleSet';
}

// Serialize the paper
var json = paper.toJSON(function(el, data) {
	// Save the set identifier along with the other data
	data.setName = el.setName;

	return data;
});

// Start over
paper.clear();

exampleSet = null;

// Restore the paper to the previous state using serialized data
paper.fromJSON(json, function(el, data) {
	// Recreate the set using the identifier
	if ( !window[data.setName] ) window[data.setName] = paper.set();

	// Place each element back into the set
	window[data.setName].push(el);

	return el;
});

// The set is restored
console.log(exampleSet);

Raphaël.JSON and Raphaël.FreeTransform

Raphaël.JSON can be used together with Raphaël.FreeTransform to save and load drawings.

// Save
var json = paper.toJSON(function(el, data) {
    data.ft = {};

    if ( el.freeTransform != null ) {
        data.ft.attrs = el.freeTransform.attrs;

				paper.freeTransform(el).unplug();
    }

    return data;
});

// Start over
paper.clear();

// Load
paper.fromJSON(json, function(el, data) {
    if ( data.ft && data.ft.attrs ) {
        paper.freeTransform(el);

        el.freeTransform.attrs = data.ft.attrs;

        el.freeTransform.apply();
    }

    return el;
});

raphael.json's People

Contributors

aliasio avatar bruha avatar jberryman avatar wesleytodd avatar zcourts avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

raphael.json's Issues

Preserving function on elements

Ii can preserving the functions on my elements?

i've my raphael paper with much elements, and functions associated on these elements.

like ...

raphael.rect(0,0,200,100).drag(function1,function2,function3);

Animation into json

please can you guide me how i can convert my animated object into json object.

Preserving sets and their free transform

Hello,

I've noticed that there is no easy way to preserve a set and its free transform if it has one. I'm wondering if you've given this any thought? I've managed to accomplish this but it is fairly awkward looking code in my opinion.

Regards,
Dan H.

Raphael.JSON callback saving wrong values for transformation attribute

Hi ElbertF,

I am using Raphael.JSON callback in order to create a JSON representation of Raphael's different objects. The problem is that when i debug the final generated JSON created by the callback function the "transform" attribute does not match the values that are currently stored in the HTML, for example: after rotating, resizing etc an image, using firebug the "transform" attribute of the image in the HTML is:

transform="matrix(-1.6092,0.0554,-0.0554,-1.6092,1318.5385,773.6154)"

But in the generated JSON the value is:

"transform":"t1318.5384615014195,773.6153848834983s1.6102,1.6102,0,0r178.0288,0,0"

If i use the values that are stored in the JSON object for creating a SVG file(for example), the SVG file does not has any of the transformations made by the user, in the other hand, if i use the values that the HTML has the SVG file is generated properly(with all the transformations made by the user).

I checked Raphael.JSON pluggin, maybe this line needs to be chaged?:

transform: el.matrix.toTransformString()

Any idea/tip why is this happening?

Warm Regards,

Shirkavnd

Uncaught TypeError: Accessing selectionEnd on an input element that cannot have a selection.

Hi,

This has all been working great for me in Firefox 8, but I've just tried in Chrome 15 and I get this console error when executing:

paper.to_json();

The error in the console is:

Uncaught TypeError: Accessing selectionEnd on an input element that cannot have a selection.
derez cycle.js:79
derez cycle.js:79
derez cycle.js:79
derez cycle.js:79
derez cycle.js:79
derez cycle.js:79
derez cycle.js:79
derez cycle.js:79
derez cycle.js:79
derez cycle.js:79
derez cycle.js:79
derez cycle.js:79
derez cycle.js:79
derez cycle.js:79
derez cycle.js:79
derez cycle.js:79
derez cycle.js:79
derez cycle.js:79
decycle cycle.js:90
Raphael.fn.to_json raphael-json.js:18
(anonymous function) my-peripherals.js:157
f.event.dispatch jquery.min.js:3
f.event.add.h.handle.i jquery.min.js:3

Any ideas Elbert?

Thanks

documentation

  • README should be updated to reflect that project no longer depends on cycle.js.
  • A little note that this project is based on / inspired by https://github.com/jspies/raphael.serialize and a little information on the difference would be helpful. E.g. the code by jspies has a separate case for each of several shape elements, but Raphael.JSON seems to serialize everything and is much smaller. Are there tradeoffs or does Raphael.JSON work just as well?

Thanks for your work on this project!

Json Export

Hello,

I have the following code implemented.
http://jsfiddle.net/JQPurfect/gdmk2xg5/1/

All I'm trying to do is export the rectangle + arrow data to json. I already wrote the function that export data to .json however I cannot get it to work :).

Here are the formats I need:

Circle:
{"id":27,"localId":2,"type":"o-circle","centerX":153,"centerY":121,"radius":25,"hexColor":"#FFFF00","strokeWidth":2.0}'

Arrow:
{"id":28,"localId":3,"type":"o-arrow","data":"M15,252L207,136L167.94462449467756,146.491789367799M207,136L179.54462449467758,165.69178936779898","hexColor":"#FFFF00","strokeWidth":2.0}'

Text:
{"id":53,"localId":6,"type":"o-text","data":"ppppppppppppp","hexColor":"#FFFF00","strokeWidth":1.0,"posX":126,"posY":405}'

Freehand:
{"id":30,"localId":7,"type":"o-path","data":"M-12,54L-11,54L-9,54L-7,54L-3,54L4,56L17,57L38,60L63,64L83,67L96,68L109,70L117,71L122,72L126,72L129,72L131,72L132,72L135,72L137,72L140,73L142,73L147,73L152,74L159,74L165,74L170,74L171,74L173,74L174,74","hexColor":"#FFFF00","strokeWidth":2.0}'

Any help will be great.

Best,
S

json[i].node is undefined

Hi,

I've just updated to the latest version of this plug-in to see if it works in <ie9 (the old version I have didn't work in <ie9). However whenever the script is loaded at all it errors:

json[i].node is undefined

line 40: element.node.id = json[i].node.id;

The only thing I've changed is the function names to maintain legacy calls to the old version (named back to to_json and from_json which is what they were called originally).

Do you know why this is erroring or how to fix?

Thanks!

[enhancement] Add missing bower.json.

Hey, maintainer(s) of ElbertF/Raphael.JSON!

We at VersionEye are working hard to keep up the quality of the bower's registry.

We just finished our initial analysis of the quality of the Bower.io registry:

7530 - registered packages, 224 of them doesnt exists anymore;

We analysed 7306 existing packages and 1070 of them don't have bower.json on the master branch ( that's where a Bower client pulls a data ).

Sadly, your library ElbertF/Raphael.JSON is one of them.

Can you spare 15 minutes to help us to make Bower better?

Just add a new file bower.json and change attributes.

{
  "name": "ElbertF/Raphael.JSON",
  "version": "1.0.0",
  "main": "path/to/main.css",
  "description": "please add it",
  "license": "Eclipse",
  "ignore": [
    ".jshintrc",
    "**/*.txt"
  ],
  "dependencies": {
    "<dependency_name>": "<semantic_version>",
    "<dependency_name>": "<Local_folder>",
    "<dependency_name>": "<package>"
  },
  "devDependencies": {
    "<test-framework-name>": "<version>"
  }
}

Read more about bower.json on the official spefication and nodejs semver library has great examples of proper versioning.

NB! Please validate your bower.json with jsonlint before commiting your updates.

Thank you!

Timo,
twitter: @versioneye
email: [email protected]
VersionEye - no more legacy software!

paper.toJSON() does not work for id attributes

Draw any element and assign it an id:

var c = paper.circle(10,10,5);
c.node.id = "mycircle";

Run:

var json = paper.toJSON();
paper.clear();
paper.fromJSON(json);

The json object will be missing the id="mycircle" on the circle node.

Editing the element after receiving the json from server

I've following situation -

  1. I'm creating multiple shapes and giving them an ID. I push these shapes into an array so that I can update the attrs of the shapes later.
  2. I convert the drawing to json and save it on server (db).
  3. Later I fetch it back from server and unable to update the attrs of the shape.

JSON is Undefined in ie6 and 7

Hi, sorry if this has been covered already but I am trying to get 'toJSON()' working in IE6 and 7 but get the below error in the console of ie9 -> document mode: IE7 standards

Error:
SCRIPT5009: 'JSON' is undefined
raphael.serialize.js, line 26 character 3

I have Jquery .1.7.2, Raphiael 2.1 and your latest code all linking in that order at the very end of my javascript files

do you have a fix?

draggable elements not draggable anymore after save/load

Hi,

I am trying to make a Raphael diagram editor where I can save / load the diagrams. I am using your Json code for the saving and loading.

It saves and loads all the elements, but after I load them in again, their draggable functionality is gone. Any idea how I can restore that functionality after loading a previously saved version?
Any help would be greatly appreciated.

Edit: It looks like the data attributes of elements and onClick events are not saved as well. Any idea on how to save those?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.