im got a little stuck using canvas element in html5, scanned a network looking for a workable solution, but to no avail!
The main problem is that I want to click a button and send to the website only the canvas element on the page.
I tried using toDataUrl () - but this seems to result in a blank white image that doesn't have the contents of the canvas!
Another problem I'm experiencing is that trying to trigger any javascript functions with an "onClick" attached to a form button seems to be milder at best!
here is my current attempt - it works in the sense that it opens a new window and tries to send it to the printer, and it creates a base64 line (tested this using the output "dataUrl" in the second lower document. recording line), but as already mentioned earlier, the image looks completely blank! (the canvas itself is definitely filled, both with the imported image, and with 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 ()" is currently taken from a tutorial, not my own work!
it is currently being called using <body onload="printCanv";">
- for some reason I couldn't get this to work at all with a button (the small part of the code below is my attempt, which seemed to fail)
<input type="button" id="test" value="click me" onClick="printCanv();"> </input>
apologies - this help request is everywhere! I didnโt post to the site as it was before, and I didnโt like to post some html / script!
in advance for any help you can 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 fine, it draws an object and adds text from a variable, but for some reason it seems to stop me from displaying it as an image. I'm really confused!