HLJ 发布于
2018-08-20 10:52:54

HTML5 Canvas drawImage()方法设置图像尺寸大小

要使用HTML5 Canvas设置图像的大小,我们可以向drawImage()方法,width和height添加两个额外的参数。
html代码:
<!DOCTYPE HTML>
<html>
  <head>
    <style>
      body {
        margin: 0px;
        padding: 0px;
      }
    </style>
  </head>
  <body>
    <canvas id="myCanvas" width="578" height="200"></canvas>
    <script>
      var canvas = document.getElementById('myCanvas');
      var context = canvas.getContext('2d');
      var x = 188;
      var y = 30;
      var width = 200;
      var height = 137;
      var imageObj = new Image();

      imageObj.onload = function() {
        context.drawImage(imageObj, x, y, width, height);
      };
      imageObj.src = 'https://www.html5canvastutorials.com/demos/assets/darth-vader.jpg';
    </script>
  </body>
</html>
当前文章内容为原创转载请注明出处:http://www.good1230.com/detail/2018-08-20/149.html
最后生成于 2023-06-18 18:37:50
此内容有帮助 ?
0