-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
129 lines (112 loc) · 3.3 KB
/
index.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Canvas鼠标滚轮缩放和拖放</title>
<style>
canvas {
border: solid 1px #000;
display: block;
cursor: move;
}
</style>
</head>
<body>
<script src="./scripts/gl-matrix-min.js"></script>
<canvas id="canvas"></canvas>
<script>
class Scene {
maxScale = 8;
minScale = 0.4;
scaleStep = 0.2;
scale = 1;
preScale = 1;
matrix = new Float32Array([
1, 0, 0,
0, 1, 0,
0, 0, 1,
]);
constructor(id, options = {
width: 600,
height: 400
}) {
this.canvas = document.querySelector('#' + id)
this.width = options.width;
this.height = options.height;
this.canvas.width = options.width;
this.canvas.height = options.height;
this.ctx = this.canvas.getContext('2d');
this.onMousedown = this.onMousedown.bind(this);
this.onMousemove = this.onMousemove.bind(this);
this.onMouseup = this.onMouseup.bind(this);
this.onMousewheel = this.onMousewheel.bind(this);
this.canvas.addEventListener('mousewheel', this.onMousewheel);
this.canvas.addEventListener('mousedown', this.onMousedown);
}
onMousewheel(e) {
e.preventDefault();
const { clientX, clientY, deltaY } = e;
const zoom = 1 + (deltaY < 0 ? this.scaleStep : -this.scaleStep);
this.scale = parseFloat((this.scale * zoom).toFixed(2));
if (this.scale < this.minScale) {
this.scale = this.minScale;
return;
} else if(this.scale > this.maxScale) {
this.scale = this.maxScale;
return;
}
const x = clientX * (1 - zoom);
const y = clientY * (1 - zoom);
const t = new Float32Array([
zoom, 0, 0,
0, zoom, 0,
x, y, 1,
]);
this.matrix = this.refresh(this.matrix, t);
}
onMousedown(e) {
if (e.button === 0) {
// 鼠标左键
window.addEventListener('mousemove', this.onMousemove);
window.addEventListener('mouseup', this.onMouseup);
}
}
onMousemove(e) {
const { movementX, movementY } = e;
const t = new Float32Array([
1, 0, 0,
0, 1, 0,
movementX, movementY, 1,
]);
this.matrix = this.refresh(this.matrix, t);
}
onMouseup() {
window.removeEventListener('mousemove', this.onMousemove);
window.removeEventListener('mouseup', this.onMouseup);
}
refresh(o, t) {
const out = new Float32Array([
0, 0, 0,
0, 0, 0,
0, 0, 0,
]);
const calc = glMatrix.mat3.multiply(out, t, o);
this.ctx.save();
this.ctx.clearRect(0, 0, this.width, this.height);
this.ctx.transform(calc[0], calc[3], calc[1], calc[4], calc[6], calc[7]);
this.draw();
this.ctx.restore();
return calc;
}
draw() {
this.ctx.fillStyle = 'red';
this.ctx.fillRect(0, 0, 50, 50);
}
}
let scene = new Scene('canvas');
scene.draw();
</script>
</body>
</html>