Skip to content Skip to sidebar Skip to footer

How To Convert A Html5 Canvas Image To A Json Object?

hi i have to covert a set of images to a json object.But, as a first step i was trying to do it for a single image but i don't know whether the json object is created or not.please

Solution 1:

<canvas id="canvas" width="400" height="400">
</canvas>
<canvasid="c2"width="400"height="400"></canvas>var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
ctx.beginPath();
ctx.rect(5, 5, 300, 250);
ctx.stroke();
ctx.arc(150, 150, 100, 0, Math.PI, false);
ctx.stroke();

canvas.addEventListener("click", function (){
  var data = ctx.getImageData(0, 0, canvas.width, canvas.height);

    console.log(data);
    console.log(JSON.stringify(data));

  var c2 = document.getElementById("c2");
   var ctx2 = c2.getContext("2d");
    ctx2.putImageData(data, 0, 0);
}, false);

demo: http://jsfiddle.net/LcnbX/

ctx.getImageData() will return an object JSON.stringify(data) will generate json string for you(see the console)

Post a Comment for "How To Convert A Html5 Canvas Image To A Json Object?"