-
Notifications
You must be signed in to change notification settings - Fork 0
/
29-canvas_draw_image_with_angle.html
81 lines (64 loc) · 2.2 KB
/
29-canvas_draw_image_with_angle.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div><canvas id="test"></canvas></div>
<div>
<button id="btn">点击每次旋转90度</button>
<span id="degress">0</span>
</div>
<img src="file:///Users/laiye/Downloads/rotate-wrong4-2.jpg" width="500" />
<script>
window.onload = () => {
const $canvas = document.getElementById('test')
const ctx = $canvas.getContext('2d')
const width = 500
var degrees = 0
const $degress = document.getElementById('degress')
const drawImage = () => {
const image = new Image()
image.onload = () => {
const { naturalWidth, naturalHeight } = image
let scale = width / naturalWidth
let canvasWidth = width
let canvasHeight = naturalHeight * scale
const isRotate = degrees === 90 || degrees === 270
ctx.clearRect(0, 0, $canvas.width, $canvas.height)
if (isRotate) {
scale = width / naturalHeight
canvasHeight = naturalWidth * scale
}
// 调整画布的大小,则会重置canvas的所有配置,save需要在改变宽高之后进行
$canvas.width = canvasWidth
$canvas.height = canvasHeight
ctx.fillStyle = "red"
ctx.save()
ctx.translate(canvasWidth / 2, canvasHeight / 2)
ctx.rotate(degrees * Math.PI / 180)
if (isRotate) {
ctx.drawImage(image, -canvasHeight / 2, -canvasWidth / 2, canvasHeight, canvasWidth)
} else {
ctx.drawImage(image, -canvasWidth / 2, -canvasHeight / 2, canvasWidth, canvasHeight)
}
ctx.restore()
ctx.fillRect(0, 0, 40, 40)
}
image.onerror = (error) => {
console.error('error', error)
}
image.src = 'file:///Users/laiye/Downloads/rotate-wrong4-2.jpg'
}
document.getElementById('btn').addEventListener('click', e => {
degrees = (degrees + 90) % 360
$degress.textContent = degrees
drawImage()
})
drawImage()
}
</script>
</body>
</html>