Drawing an image in canvas using in JavaScript

Following is the code for drawing an image in canvas using JavaScript −


 Live Demo

<DOCTYPE html>
<html lang="en">
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
   body {
      font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
<h1>Drawing an image in canvas</h1>
<img class="flower" width="300" height="300"
BHGr2p_6EEE29uDDg" />
<canvas class="canvas1" width="300" height="300" style="border: 1px solid #d3d3d3;">
</canvas><br />
<button class="Btn">CLICK HERE</button>
<h3>Click on the above button to draw the image in canvas</h3>
   let resEle = document.querySelector(".result");
   let BtnEle = document.querySelector(".Btn");
   BtnEle.addEventListener("click", () => {
      var c = document.querySelector(".canvas1");
      var ctx = c.getContext("2d");
      var img = document.querySelector(".flower");
      ctx.drawImage(img, 10, 10);


On clicking the ‘CLICK HERE’ button −