Skip to content
This repository was archived by the owner on Apr 8, 2020. It is now read-only.
This repository was archived by the owner on Apr 8, 2020. It is now read-only.

a bug with Raphael.Json  #123

Open
Open

Description

@bcw104

1.do some transform and export by Raphael.Json.
2.then import by Raphael.json, it can't init transform correctly.

you can test the bug in here: http://jsfiddle.net/bcw104/MvNvL/11/

Activity

  1. dhardtke commented on Apr 14, 2014

    @dhardtke

    I have the same issue...

  2. bcw104 commented on Apr 15, 2014

    @bcw104
    Author

    @dhardtke I found a solution by change the raphael.json.js, do not use matrix.toTransformString(), use el.transform().toString():

    (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(),
                    transform: el.transform().toString(),
                    id: el.id
                });
            }
    
            var cache = [];
            var o = JSON.stringify(elements, function (key, value) {
                //http://stackoverflow.com/a/11616993/400048
                if (typeof value === 'object' && value !== null) {
                    if (cache.indexOf(value) !== -1) {
                        // Circular reference found, discard key
                        return;
                    }
                    // Store value in our collection
                    cache.push(value);
                }
                return value;
            });
            cache = null;
            return o;
        }
    
        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);
                    if (json[i].transform != null) {
                        el.transform(json[i].transform);
                    }
    
                    el.id = json[i].id;
    
                    if (callback) el = callback(el, json[i].data);
    
    //              if ( el ) paper.set().push(el);
                }
            }
        }
    })();
  3. dhardtke commented on Apr 15, 2014

    @dhardtke

    Maybe you should fork raphael.json and create a pull request for this? :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions