---
title: "HTML5 Canvas - How to draw rectangle over image in canvas"  
description: "HTML5 Canvas - How to draw rectangle over image in canvas"  
author: "Anonymous User"  
published: 2014-12-31  
updated: 2014-12-31  
canonical: https://www.mindstick.com/forum/12832/html5-canvas-how-to-draw-rectangle-over-image-in-canvas  
category: ".net"  
tags: ["jquery", "javascript", "html", "html5", "html5 canvas"]  
reading_time: 2 minutes  

---

# HTML5 Canvas - How to draw rectangle over image in canvas

I need to draw [image](https://www.mindstick.com/articles/23551/an-investigate-distinctive-seafood-restaurant-for-your-image-stamp-character) without using img from onload [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) which is like this:\

```
    var imagePaper = new Image();        imagePaper.onload = function(){            context.drawImage(imagePaper,100, 20, 500,500);        };      imagePaper.src = "images/main_timerand3papers.png";}
```

But I want to able draw rectangle over canvas using simply attaching img src above canvas,just as:\

```
<body>  <img id="scream" src="my.jpg" alt="The Scream" width="220" height="277"><p>Canvas:</p><canvas id="myCanvas" width="500" height="500" >
```

Your [browser](https://www.mindstick.com/blog/155254/which-is-the-best-internet-browser) does not [support](https://yourviews.mindstick.com/view/286/modi-support-for-sportsperson) the [HTML5 canvas](https://www.mindstick.com/articles/1534/stylish-css3-progress-bars) tag.</canvas>But I'm unable to draw line over img in canvas,either img is drawing or shape is hiding behind. This is my [full](https://www.mindstick.com/articles/12893/experience-the-full-power-of-suitecrm) [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii):

```
 <!DOCTYPE html>   <head>   <title>Playing YouTube video on HTML5 canvas</title>   <meta name="viewport" content="user-scalable=no, initial-scale=1.0, maximum-scale=1.0, width=device-width" />   <style type="text/css">  body {        margin: 0px;        padding: 0px;      }  </style>  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> </head>   <body>  <img id="scream" src="my.jpg" alt="The Scream" width="220" height="277"><p>Canvas:</p><canvas id="myCanvas" width="500" height="500" >Your browser does not support the HTML5 canvas tag.</canvas>  <script>  var c=document.getElementById("myCanvas");var ctx=c.getContext("2d");var img=document.getElementById("scream");ctx.drawImage(img,10,10);  var canvas = document.getElementById('myCanvas');      var context = canvas.getContext('2d');      context.beginPath();      context.rect(188, 50, 200, 100);      context.fillStyle = 'yellow';      context.fill();      context.lineWidth = 7;      context.strokeStyle = 'black';      context.stroke();  </script>   </body>   </html>  
```

## Replies

### Reply by Anonymous User

Because you are not waiting for the image to load first. In your script add an window.onload()\

```
<script>window.onload = function() {  var c=document.getElementById("myCanvas");  var ctx=c.getContext("2d");  var img=document.getElementById("scream");    ctx.drawImage(img,10,10);    var canvas = document.getElementById('myCanvas');  var context = canvas.getContext('2d');  context.beginPath();  context.rect(188, 50, 200, 100);  context.fillStyle = 'yellow';  context.fill();  context.lineWidth = 7;  context.strokeStyle = 'black';  context.stroke();}</script>
```

What is happening is it's trying to draw the image before it loaded, doing window.onload will call the code once the entire document is loaded (such as images). Otherwise it may display no image or draw it out of line.


---

Original Source: https://www.mindstick.com/forum/12832/html5-canvas-how-to-draw-rectangle-over-image-in-canvas

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
