How to fix canvas is stretched when using CSS but normal with width and height properties with JavaScript?

Spread the love

Sometimes, we want to fix canvas is stretched when using CSS but normal with width and height properties with JavaScript.

In this article, we’ll look at how to fix canvas is stretched when using CSS but normal with width and height properties with JavaScript.

How to fix canvas is stretched when using CSS but normal with width and height properties with JavaScript?

To fix canvas is stretched when using CSS but normal with width and height properties with JavaScript, we set the width and height with JavaScript.

For instance, we write

const canvas = document.getElementById("canv");
canvas.setAttribute("width", "438");
canvas.setAttribute("height", "462");

to get the canvas with getElementById.

Then we call setAttribute to set the width and height attribute values.

Conclusion

To fix canvas is stretched when using CSS but normal with width and height properties with JavaScript, we set the width and height with JavaScript.

Leave a Reply

Your email address will not be published. Required fields are marked *