开发者

printing just canvas element on a page

im a little stuck using the canvas element in html5, have scoured the net looking for a workable solution but to no avail!

the main issue is that I want to click a button and send just the canvas element on the page to the printer.

i have tried using toDataUrl() - but this just seems to be resulting in a blank white image which has none of the canvas content!

the other issue i am experiencing is that attempting to initiate any javascript func开发者_如何学JAVAtions using "onClick" attached to a form button seems to be being tempermental at best!

here is my current attempt - this works in the sense that it does open a new window and attempt to send it to printer and it does create a base64 string (tested this using the "dataUrl" output on the second lowest document.write line) but as previously mentioned the image appears to be completely blank! (the canvas itself is definitely filled, both with an imported image and some text)

function printCanv()  
{  
  var dataUrl = document.getElementById('copy').toDataURL(); //attempt to save base64 string to server using this var  
  document.getElementById('testBox').value = dataUrl; //load data into textarea  
  //attempt open window and add canvas etc  
  win = window.open();  
  self.focus();  
  win.document.open();  
  win.document.write('<'+'html'+'><'+'head'+'><'+'style'+'>');  
  win.document.write('body, td { font-family: Verdana; font-size: 10pt;}');  
  win.document.write('<'+'/'+'style'+'><'+'/'+'head'+'><'+'body'+'>');  
  ((image code is here but site will not let me post it for some reason?))  
  win.document.write(dataUrl);  
  win.document.write('<'+'/'+'body'+'><'+'/'+'html'+'>');  
  win.document.close();  
  win.print();  
  win.close();  
}  

note: the code from "win = window.open()" onwards is currently taken from a tutorial and not my own work!

it is currently being called using <body onload="printCanv";"> - for some reason I could not get this to work at all using a button (little chunk of code below is my attempt which seemed to fail)

<input type="button" id="test" value="click me" onClick="printCanv();"> </input>  

apologies is this help request is all over the place! i haven't posted to a site like this before and it didn't like me posting some html/script!

thanks in advance for any help you may be able to offer :)

Edit: draw function:

function copydraw() {  //function called "copydraw" (could be anything)
    var testimage3 = document.getElementById('copy').getContext('2d');  //declare variable for canvas overall (inc paths)
    var img3 = new Image();  //declare image variable called "img3"
    var testVar = document.getElementById('userIn').value; //take value from userin box.  only updating on browser refresh?
    img3.onload = function(){  //when image has loaded (img.onload) run this function
        testimage3.drawImage(img3,0,0);  //draw "img" to testimage
        testimage3.font = "30pt 'Arial'"; //font method varies font attrib (weight, size, face)
        testimage3.fillStyle = "#000000"; //sets fill color
        testimage3.fillText(testVar, 310, 360); //filltext method draws text (xup ydown)
        }  
    img3.src = 'images/liecakeA4.jpg';  //source of image
}

This function works perfectly, it draws the object and adds text from the variable, but for some reason it seems to be preventing me from outputting it as an image. I'm really confused!


I'm not sure exactly what's wrong with your code, I suspect in your current version calling printCanv in the body load event will mean you're trying to print the canvas before it's drawn. Running it off the button should work better, I'm not sure why that wasn't working for you as there's no reason in principle why it shouldn't work.

To arrive at a working version I've modified your code slightly:

function printCanvas(el) {  
    var dataUrl = document.getElementById(el).toDataURL(); //attempt to save base64 string to server using this var  
    var windowContent = '<!DOCTYPE html>';
    windowContent += '<html>'
    windowContent += '<head><title>Print canvas</title></head>';
    windowContent += '<body>'
    windowContent += '<img src="' + dataUrl + '">';
    windowContent += '</body>';
    windowContent += '</html>';
    var printWin = window.open('','','width=340,height=260');
    printWin.document.open();
    printWin.document.write(windowContent);
    printWin.document.close();
    printWin.focus();
    printWin.print();
    printWin.close();
}

This works for me in the Firefox 4.0 nightly.


One addition to the accepted-best-answer: It doesnt work here with Chrome 17.0.942.0 winvista, because the print-preview-dlg is shown within the website itself and printWin.close() will close the dlg too.

So printWin.close() must be delayed or initiated by the user, but those are no good solutions.

It would be best, if chrome printdlg could have a callback, sth. one knows, printing is done, and can close the window. If this is possible is another question.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜