D
D
DarkRover2019-12-05 01:06:01
Canvas
DarkRover, 2019-12-05 01:06:01

Why is canvas.drawImage() stretching my image?

Shouldn't it be supposed to output it just like a normal img tag?
HTML

<canvas id="display">
    <p>Ваш браузер не поддерживает элемент canvas!</p>
    <script src="logic.js"></script>
</canvas>
<img src="images/ava.png">

css
#display {
    background: whitesmoke;
    width: 320px;
    height: 320px;
}

JS
const spr = new Image();
spr.src = "images/ava.png";
ctx.drawImage(spr, 0, 0);

5de82d3b3cf29846328817.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
SagePtr, 2019-12-05
@DarkRover

Give the canvas tag the width and height attributes, otherwise it will not know what size it actually is, and will draw in a 300x150 viewport, and then stretch it to the size you specify using CSS.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question