@jerad
To draw a picture on a canvas using JavaScript in HTML5, follow these steps:
1
|
<canvas id="myCanvas"></canvas> |
1 2 |
var canvas = document.getElementById("myCanvas"); var ctx = canvas.getContext("2d"); |
1 2 |
var image = new Image(); image.src = "path/to/image.jpg"; |
1 2 3 |
image.onload = function() { ctx.drawImage(image, x, y, width, height); }; |
Note: Replace x, y, width, height
with the desired position and size for the image.
Here's an example of the complete code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<canvas id="myCanvas"></canvas> <button onclick="drawImageOnCanvas()">Draw Image</button> <script> function drawImageOnCanvas() { var canvas = document.getElementById("myCanvas"); var ctx = canvas.getContext("2d"); var image = new Image(); image.src = "path/to/image.jpg"; image.onload = function() { ctx.drawImage(image, x, y, width, height); }; } </script> |
Replace "path/to/image.jpg"
with the actual image file path you want to draw.