<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>canvas绘制图像</title>
<style>
body{margin: 0;overflow: hidden}
#test{background: antiquewhite;}
</style>
</head>
<body>
<canvas id="test"></canvas>
<script>
const cvs=document.getElementById('test');
cvs.width=window.innerWidth;
cvs.height=window.innerHeight;
const ctx=cvs.getContext('2d');
const img=new Image();
img.src='./images/dog.jpg';
img.onload=function(){
/*图像尺寸*/
const {width,height}=img;
/*绘图+移动 drawImage(image, x, y) */
/*ctx.drawImage(
img,
50,100
);*/
/*绘图+移动+缩放 drawImage(image, x, y,width,height) */
/*ctx.drawImage(
img,
0,0,
width*2,height*2
)*/
/*绘图+裁剪+移动+缩放 drawImage(image, x1, y1,w1,h1,x2,y2,w2,h2) */
ctx.drawImage(
img,
//裁剪
width/2,height/2,
width/2,height/2,
//位移+缩放
100,100,
width,height
)
};
</script>
</body>
</html>