2011-12-17 69 views
0

我试图找到一种方法来记录画布上的不同对象的状态(图像,形状,选择框),以便它可以全部序列化为XML或JSON。我试着将每个对象放入到一个全局数组:记录对象状态序列化

var shapeState = { //Example of a shape being recorded 
          number:increment, 
          id:thisCanvas, 
          type:'shape', 
          kind:shapeKind, 
          cheight:canvasHeight, 
          cwidth:canvasWidth, 
          height:shapeHeight, 
          width:shapeWidth, 
          color:shapeColor, 
          x:xPos, 
          y:yPos 
         } 

         totalState.push(shapeState); //State of shape data has been stored in global array 

但是这只能如果形状(或其他物体)是静态的。例如,如果我移动元素(点击'移动'按钮后),我如何更新xPos属性?形状对象已经放置在数组内,并且不能通过名称来识别。假设全球数组中有100多个形状,我将如何能够找到特定的一个更新?请帮忙。

这里是小提琴:http://jsfiddle.net/RymyY/5/

回答

0

这里有一个办法:

var AllShapeStates = {}; 

function Shape() { 
    var SerializedState = {}, 
     number, 
     thisCanvas, 
     etc; 
    AllShapeStates[UNIQUE_IDENTIFYING_ATTRIBUTE] = SerializedState; 
    //Shape Business 

    this.updateState = function(){ 
     SerializedState.number = number; 
     SerializedState.id = thisCanvas; 
     //etc 
    } 
} 

在这个模型中,当Shape被实例化,它增加了一个参考其SerializedState进入全球AllShapestates VAR;每当Shape更新它的状态时,它应该调用updateState,它更新它的SerializedState对象;

+0

@ Shad - 我不明白你的解决方案将如何超越一次观看的生活。如果用户决定退出该页面以访问其他网站并返回,则形状将消失。 我不太了解ASP.NET,但是`_VIEWSTATE]`变量可以用在这里吗? – dopatraman 2011-12-17 17:36:15