Home >Web Front-end >HTML Tutorial >How to use svg object in Html5 canvas. _html/css_WEB-ITnose
Use drawImage in canvas and pass in the svg object as a parameter, but it cannot be displayed. I don’t know if drawImage does not support this type of object parameter. Solve. . .
See method
http://code.google.com/p/canvg/
http://canvg.googlecode .com/svn/trunk/examples/index.htm
I’ll post a piece of code for you:
Actually, I would like to know what are the acceptable image types for the first parameter of the drawImage method? ?
Test in browsers that support svg, such as Firefox16, Chrome
<html> <head> <title>test</title> <style> svg, canvas { margin:1em } </style> </head> <body> <p> <svg xmlns="http://www.w3.org/2000/svg" width="100px" height="100px" id="i"> FAIL <circle cx="50px" cy="50px" r="50px" fill="lime"/> </svg> <canvas width="100" height="100" id="c">FAIL</canvas> </p> <script type="text/javascript"> window.onload = function () { var ctx = document.getElementById("c").getContext('2d'); var svg_xml = (new XMLSerializer()).serializeToString(document.getElementById("i")); var img = new Image(); img.src = "data:image/svg+xml;base64," + window.btoa(svg_xml); img.onload = function () { ctx.drawImage(img, 0, 0); }; } </script> </body></html>
In fact, I would like to know the acceptable image types for the first parameter of the drawImage method. What are they? ?
You can use HTMLImageElement, HTMLCanvasElement or HTMLVideoElement as parameters