Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 Canvas - How to draw rectangle over image in canvas

Actually, I am able to do it using img.onload function. Something I got from 'HTML5 Canvas - How to Draw a line over an image background?'. But I need to draw image without using img from onload function 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 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 does not support the HTML5 canvas 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 code:

 <!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>  
like image 441
Rani dubey Avatar asked Nov 13 '14 05:11

Rani dubey


People also ask

How do I draw a rectangle in canvas HTML?

The rect() method creates a rectangle. Tip: Use the stroke() or the fill() method to actually draw the rectangle on the canvas.

How do you add a rectangle to a canvas?

To draw the rectangle onto a canvas, you can use the fill() or stroke() methods. Note: To both create and render a rectangle in one step, use the fillRect() or strokeRect() methods.

Can you draw on an image in canvas?

The drawImage() method draws an image, canvas, or video onto the canvas. The drawImage() method can also draw parts of an image, and/or increase/reduce the image size.

Can we draw anything other than a rectangle in canvas?

Unlike SVG, <canvas> only supports two primitive shapes: rectangles and paths (lists of points connected by lines). All other shapes must be created by combining one or more paths. Luckily, we have an assortment of path drawing functions which make it possible to compose very complex shapes.


2 Answers

I tried this same approach of using window.onload() and it worked until I tried it using Microsoft Edge browser. A solution to that which worked for me was to attach an onload handler to the image, such as:

image.onload = function ()
{
    context.drawImage(image, 0, 0);
}

then it worked perfectly.

like image 88
JCDeen Avatar answered Sep 19 '22 13:09

JCDeen


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.

like image 41
Spencer Wieczorek Avatar answered Sep 16 '22 13:09

Spencer Wieczorek