2017-10-15 22 views
0

J'ajoute svg to canvas et je veux définir des paramètres d'élément personnalisés. Je montre le paramètre personnalisé quand nous console le log getActiveObject() mais quand nous employons des valeurs de noeud de canvas.toJSON() Element Parameter ne change pas. méthodeLes paramètres personnalisés sont effacés canvas to Json

var canvas    = new fabric.Canvas('designcontainer'), 
/* Save additional attributes in Serialization */ 
var ElementParameters = { 
    ElementType:'', 
    imageType:'', 
    top:'', 
    left:'', 
    colors:'', 
    isBaseTier:'', 
    atLevel:'' 
}; 
fabric.Object.prototype.toObject = (function (toObject) { 
return function() {   
    return fabric.util.object.extend(toObject.call(this), { 
     ElementParameters:{ 
      ElementType:'', 
      imageType:'', 
      top:'', 
      left:'', 
      colors:'', 
      isBaseTier:'', 
      atLevel:'' 
     },   
    }); 
}; 
})(fabric.Object.prototype.toObject); 
/* End : Save additional attributes in Serialization */ 

var Designer = { 
addElement: function(e,p){ /* e = element, image src | p = parameters set for image */ 
    if(p.imageType == "svg"){ 
     if(p.ElementType == "caketier"){ 
      var group = []; 
      console.log('Before '); 
      console.log(ElementParameters); 
      $.extend(ElementParameters,p); 
      console.log('After '); 
      console.log(ElementParameters); 
      fabric.loadSVGFromURL(e,function(objects,options){ 
       var shape = fabric.util.groupSVGElements(objects,options); 
       var bound = shape.getBoundingRect(); 
       shape.set({ 
        left: p.left, 
        top: p.top, 
        width:bound.width+2, 
        height:bound.height, 
        angle:0, 
        centeredScaling:true, 
        ElementParameters:ElementParameters 
       }); 

       if(shape.paths && baseColor.length > 0){ 
        for(var i = 0;i<shape.paths.length;i++) shape.paths[i].setFill(baseColor[i]); 
       } 
       canvas.add(shape); 
       shape.setControlsVisibility(HideControls); 
       canvas.renderAll(); 
      },function(item, object) { 
       object.set('id',item.getAttribute('id')); 
       group.push(object); 
      }); 
     } 
    } 
} 
} 

$(".tierbox").on('click',function(){ 
    var i  = $(this).find('img'), 
     src  = i.attr('src'), 
     param = i.data('parameters'); 
    Designer.addElement(src,param); 
}); 

Maintenant, quand je l'appelle JSON.stringify (de JSON), élément nœud paramètre ne soit pas avec écrasera les valeurs définies dans shape.set().

+0

solutions Got remplacer fabric.Object.prototype.toObject = (function (toObject) {.. .} Pour fabric.Object.prototype.toObject = (function (toObject) { fonction de retour() {\t fabric.util.object.extend de retour (toObject.call (this), { \t ElementParameters: this.ElementParameters }) }; }) (fabric.Object.prototype.toObject); – Snehalk

Répondre

0

Remplacer fabric.Object.prototype.toObject = (function (toObject) {...} à

fabric.Object.prototype.toObject = (function (toObject) { 
    return function() {   
     return fabric.util.object.extend(toObject.call(this), { 
      ElementParameters:this.ElementParameters 
     }); 
    }; 
})(fabric.Object.prototype.toObject);