Issue
I have this code to add an image in my website but I want to resize it
var questionHeader = document.getElementById("a");
var img = new Image(),
canvas = document.createElement("canvas"),
ctx = canvas.getContext("2d");
img.onload = () => {
let width = Math.floor(img.naturalWidth * 0.1),
height = Math.floor(img.naturalHeight * 0.1);
canvas.width = width;
canvas.height = height;
ctx.drawImage(img, 0, 0, width, height);
};
img.src = "https://cdn-icons-png.flaticon.com/512/7951/7951990.png";
questionHeader.appendChild(img);
<div id="a"></div>
With this code I want to make the image smaller
let width = Math.floor(img.naturalWidth * 0.1),
height = Math.floor(img.naturalHeight * 0.1);
canvas.width = width;
canvas.height = height;
ctx.drawImage(img, 0, 0, width, height);
For some reason it's not working though. Anyone knows what the problem may be?
Solution
You can resize img by css class and you can set class for img with JavaScript by classname
Answered By - محمد شلیباوی
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.