2

I have defined this function:

function viewCanvasImg() {
    //("stage" is a kineticjs stage class object)
    stage.toDataURL({ 
        callback: function(dataUrl) {
            document.getElementById('prova').src=dataUrl;
            window.open(dataUrl);
        }
    });
}

But when i call it by button it doesn't work. With shapes and draws all works fine but with images on canvas not.

SOLVED I noticed that the code didn't work only on local machine then i put on my webserver and all works fine.

sinsuren
  • 1,745
  • 2
  • 23
  • 26
Vrt
  • 33
  • 5

2 Answers2

0

Check the tutorial and see if it's working for you there:

http://www.html5canvastutorials.com/kineticjs/html5-canvas-stage-data-url-with-kineticjs/

Your code looks proper. If it's not working you're probably getting a Javascript error before the window.open is called. Check the console and console.log is your friend.

Also, what could be happening is stage is not defined in your function. So you might need to pass the stage object through the viewCanvasImg function.

Update

With images and toDataURL you have to becareful of the Same Origin Policy.

Read here for more information: Why does canvas.toDataURL() throw a security exception?

http://en.wikipedia.org/wiki/Same-origin_policy

This is the error KineticJS throws in the console:

Kinetic warning: Unable to get data URL. An attempt was made to break through the security policy of the user agent.

Community
  • 1
  • 1
projeqht
  • 3,160
  • 3
  • 18
  • 32
  • the function works when my canvas only contains draws or shapes but for my project i need that my canvas contain drag and droppable images and in this case my function doesn't work – Vrt Sep 12 '13 at 23:25
  • It should work fine, but there is an exception for images. See my updated answer. – projeqht Sep 13 '13 at 13:34
  • I've solved. The images were already in the same folder of my page. But i noticed that the code didn't work only on local machine then i put on my webserver and all works fine. – Vrt Sep 13 '13 at 14:44
0

I have a similar issue whilst opening a data image url in a new window; doesn't display on local.

Try right-click > Inspect Element in Chrome.

AEQ
  • 1,339
  • 1
  • 16
  • 20