Canvas image is not the same size as the canvas or orginal image

ⅰ亾dé卋堺 提交于 2019-12-25 02:15:01

问题


I've put a canvas on top a background image. When I press the 'Try It' button the image appears in the canvas but it does not appear the same size as the parent or fit in the canvas. I only see the top left of the image.

The parent image and the canvas are the same size.

HTML

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="style.css">
        <script src="script.js"></script>
    </head>
    <body>
        <div class="bg">
            <img src="Assets/Images/background.png" alt="background">   
        </div>
        <section class="content">

            <p>Image to use:</p>
            <img id="taxi" src="Assets/Images/PV.png" width="250" height="300">

            <p>Canvas to fill:</p>
            <canvas id="myCanvas" width="250" height="300"
            style="border:1px solid #d3d3d3;">
            Your browser does not support the HTML5 canvas tag.</canvas>

            <p><button onclick="myCanvas()">Try it</button></p>
        </section>

    </body>
</html>

JS

function myCanvas() {
    var c = document.getElementById("myCanvas");
    var ctx = c.getContext("2d");
    var img = document.getElementById("taxi");
    ctx.drawImage(img,10,10);
  }

How do I get it so that the image in the canvas is the same size as the parent? Also, how do I get the image to disappear from the canvas when I press 'Try It' on the second press?


回答1:


I had the same issue and had to learn the hard way. Responsive Canvas and same same aspect Ratio Canvas is not as easy as one would hope.

What you need to do:

function myCanvas() {
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
var img = document.getElementById("taxi");
ctx.drawImage(img,10,10, 250, 300);
}

Let me explain why.

In your HTML, you have manually declared:

width="250" height="300"

So the image projected on the Canvas needs to be the same size. It is all controlled by the ctx.drawImage(img,10,10, 250, 300) function.

The first argument to the fn is the referenced data, this can be a video, image etc.

The second arguments is the X axis offset, the third argument is the Y axys offset(If you want no offset, just pass : ctx.drawImage(img,0,0, 250, 300);.

In this case, the 4th and 5th arguments are the size of the what was passed as first argument.

Here is a codepen, try changing the arguments and see what happens.

https://codepen.io/pen/?editors=1010

PS, you can pass many many more arguments to this method, but usually, this is all you need.

Just wanted to add, you can increase a smaller image to fit a Canvas, and the other way around, but if you declare the size of the Canvas with HTML attributes, and use smaller numbers in the drawImage fn, it will not fill the canvas. The HTML seems to have more specifity. This can be good or bad, but it is important to know.



来源:https://stackoverflow.com/questions/54572212/canvas-image-is-not-the-same-size-as-the-canvas-or-orginal-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!