drawImage and resize to Canvas

前端 未结 1 1307
广开言路
广开言路 2020-12-29 21:00

I have an image which is 1836 x 3264 I want to drawImage() to canvas and resize to 739 x 1162.

After reading the documentation I thought this could be

相关标签:
1条回答
  • 2020-12-29 21:35

    What you have looks correct, so you might double-check for a typo somewhere.

    [additional thought: Is your image really 1836x3264 and not 3264x1836.]

    Here is working code and a Fiddle: http://jsfiddle.net/m1erickson/MLGr4/

    <!doctype html>
    <html>
    <head>
    <link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css -->
    <script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
    
    <style>
        body{ background-color: ivory; }
        canvas{border:1px solid red;}
    </style>
    
    <script>
    $(function(){
    
        var canvas=document.getElementById("canvas");
        var ctx=canvas.getContext("2d");
    
        img=new Image();
        img.onload=function(){
            canvas.width=400;
            canvas.height=300;
            ctx.drawImage(img,0,0,img.width,img.height,0,0,400,300);
        }
        img.src="http://www.onestopwebmasters.com/wp-content/uploads/2011/06/eitai-bridge.jpg";
    
    }); // end $(function(){});
    </script>
    
    </head>
    
    <body>
        <canvas id="canvas" width=100 height=100></canvas>
    </body>
    </html>
    
    0 讨论(0)
提交回复
热议问题