We can create a <canvas> element by using the document.createElement()
method:
<!DOCTYPE html> <html> <head> <style> canvas {//from w ww . ja v a2 s .c om border: 1px solid black; } </style> </head> <body> <button onclick="myFunction()">Test</button> <p>Click the button to create a CANVAS element and draw a red rectangle.</p> <script> function myFunction() { var x = document.createElement("CANVAS"); var ctx = x.getContext("2d"); ctx.fillStyle = "#FF0000"; ctx.fillRect(20, 20, 150, 100); document.body.appendChild(x); } </script> </body> </html>