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);
});
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.
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.
window. onload = function() { var canvas = document. getElementById("myCanvas"); var context = canvas. getContext("2d"); var imageObj = new Image(); imageObj.
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.
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.
Canvas - Images. To draw an image on a canvas, use the following method: drawImage(image,x,y)
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.
or just use 2 layers:
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With