Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

drawing over an Image in HTML5 Canvas while preserving the image

In HTML5 Canvas, what's the simplest way to draw and move a line over an Image (already on the canvas), preserving the image underneath? (e.g. have a vertical line track the mouse X position)

My current canvas:

$(document).ready(function() {
  canvas = document.getElementById("myCanvas");
  context = canvas.getContext("2d");
  imageObj = new Image();

    imageObj.onload = function() { 
    context.drawImage(imageObj, 0,0);  
  }
  imageObj.src = "http://example.com/some_image.png";
  $('#myCanvas').click(doSomething);
});
like image 886
Asaf Bartov Avatar asked Nov 13 '13 23:11

Asaf Bartov


People also ask

Can you draw on an image in canvas?

The function we use for drawing an image onto a canvas is the drawImage() function. This function draws an image, canvas, or video onto the canvas. It can also draw parts of an image, and/or increase/reduce the image size.

Which built in HTML5 object is used to draw on the canvas?

The HTML <canvas> element is used to draw graphics, on the fly, via JavaScript. The <canvas> element is only a container for graphics. You must use JavaScript to actually draw the graphics.

How can I display and write on an image in canvas?

window. onload = function() { var canvas = document. getElementById("myCanvas"); var context = canvas. getContext("2d"); var imageObj = new Image(); imageObj.

How to use images with HTML5 canvas?

To use images with HTML5 canvas, use the drawImage () method. This method draws the given image onto the canvas. You can try to run the following code to learn how to use images with HTML Canvas. Here, the image is a reference to an image or canvas object. x and y form the coordinate on the target canvas where our image should be placed.

What is the use of drawimage in HTML5?

The HTML5 drawImage () method is used to draw an image, canvas or video on the canvas. It also draws parts of an image. You can also use it to increase or decrease image size. To specify the image, canvas, or video.

How do I draw an image on a canvas?

Canvas - Images. To draw an image on a canvas, use the following method: drawImage(image,x,y)

What is the use of drawimage () method?

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. Note: You cannot call the drawImage () method before the image has loaded.


2 Answers

or just use 2 layers:

  1. background layer has image and do not change,
  2. top layer has line, what you can clear and redraw it lots of time without affecting background layer.
like image 160
ViliusL Avatar answered Oct 23 '22 17:10

ViliusL


May this is not an actual answer, just in case you need it (in the future). Working with canvas would be better (and easier) with some library. I have tried EaselJS of CreateJS and find myself loving it. You can have a look at it EaselJS (I have done an example allow drawing and dragging image using EaselJS long time before)

like image 44
Shinigamae Avatar answered Oct 23 '22 17:10

Shinigamae