简体   繁体   中英

Redraw canvas using localStorage object in FabricJs

My canvas id is "canvas-panel" I want to redraw my canvas using the localStorage object value when go to the next page. I am getting canvas object using localStorage object like below:

var myCanvas = JSON.parse(localStorage.getItem('canvas'));

JSON formate:

{
"objects":[

  {
     "type":"image",
     "originX":"left",
     "originY":"top",
     "left":42,
     "top":311,
     "width":100,
     "height":100,
     "fill":"rgb(0,0,0)",
     "stroke":null,
     "strokeWidth":1,
     "strokeDashArray":null,
     "strokeLineCap":"butt",
     "strokeLineJoin":"miter",
     "strokeMiterLimit":10,
     "scaleX":1,
     "scaleY":1,
     "angle":0,
     "flipX":false,
     "flipY":false,
     "opacity":1,
     "shadow":null,
     "visible":true,
     "clipTo":null,
     "backgroundColor":"",
     "fillRule":"nonzero",
     "globalCompositeOperation":"source-over",
     "src":"http://stg.souqueen.com/media/catalog/product/cache/1/image/300x300/9df78eab33525d08d6e5fb8d27136e95/1/9/19b_1024i1.PNG",
     "filters":[

     ],
     "crossOrigin":"",
     "alignX":"none",
     "alignY":"none",
     "meetOrSlice":"meet"
  }
],
"background":""
}

I am getting all canvas object with the help of this thread Select All the objects on canvas using Fabric.js

You have got your json. So now on next page you can import the objects using this :

canvas.loadFromJSON(myCanvas, canvas.renderAll.bind(canvas))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM