不废话,直接上代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
img {
width: 900px;
height: 600px;
}
</style>
</head>
<body>
<div id="root">
<canvas id="canvas"></canvas>
</div>
<script>
const canvas = document.getElementById("canvas");
canvas.width = 900;
canvas.height = 600;
const ctx = canvas.getContext('2d');
const x = 0,
y = 0,
width = 900,
height = 600;
canvas.addEventListener("mousedown", (e) => {
console.log(e);
})
const body = document.querySelector('#root');
const img = new Image();
img.src = "https://img.zcool.cn/community/01f3045d9c929ba8012060be99585f.jpg@1280w_1l_2o_100sh.jpg"
img.onload = function () {
body.appendChild(img);
ctx.save();
ctx.translate(x + (width / 2), y + (height) / 2);
ctx.rotate(180 * (Math.PI / 180));
ctx.translate(-x - (width / 2), -y - (height / 2));
ctx.drawImage(img, x, y, width, height);
ctx.restore();
}
</script>
</body>
</html>