A
A
Arion2011-06-19 09:03:38
JavaScript
Arion, 2011-06-19 09:03:38

How to save html5 canvas/svg to png on javascript

There is canvas:

window.onload = function(){
    var canvas = document.getElementById("myCanvas");
    var context = canvas.getContext("2d");
 
    var destX = 69;
    var destY = 50;
 
    var imageObj = new Image();
    imageObj.onload = function(){
        context.drawImage(imageObj, destX, destY);
    };
    imageObj.src = "http://otherdomain.ru/example.jpg";
};

or svg:

<svg version="1.1" width="1042" height="594" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
  <image x="2" y="2" width="50" height="50" xlink:href="http://otherdomain.ru/example.jpg">
  </image>
</svg>

how to save one of these options to png using javascript.

Please note that the image is loaded from another domain. In the case of Canvas, the toDataUrl method throws a "security error".

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
yui_room9, 2011-06-19
@yui_room9

There are several options for
Canvas
1) Use a proxy for the image, then it will be from your server
2) Take the image, redraw it pixel by pixel on the Canvas, do the toDataURL
SVG
1) Here the library redraws the SVG on the Canvas http://code.google.com/p/ canvg/ , and with Canvas already toDataURL

P
Pavlo Ponomarenko, 2011-06-19
@TheShock

I doubt you can fool the browser's security policy.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question