-
Notifications
You must be signed in to change notification settings - Fork 0
/
canvas.js
178 lines (149 loc) · 4.25 KB
/
canvas.js
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import { globalScale, setScale, shapes } from "./globals.js";
document.addEventListener("DOMContentLoaded", function () {
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
const dpr = window.devicePixelRatio || 1;
canvas.width = window.innerWidth * dpr;
canvas.height = window.innerHeight * dpr;
canvas.style.width = `${window.innerWidth}px`;
canvas.style.height = `${window.innerHeight}px`;
ctx.scale(dpr, dpr);
let lastX = 0;
let lastY = 0;
let offsetX = 0;
let offsetY = 0;
const MIN_SCALE = 0.1;
const MAX_SCALE = 10;
const SCALE_FACTOR = 1.1;
shapes.addListener(draw);
let selectedShape = null;
let isPanning = false;
function screenToVirtual(screenX, screenY) {
return {
x: (screenX - offsetX) / globalScale,
y: (screenY - offsetY) / globalScale,
};
}
canvas.addEventListener("mousedown", function (e) {
const virtualCoord = screenToVirtual(e.offsetX, e.offsetY);
const realShapes = shapes.array;
for (let i = realShapes.length - 1; i >= 0; i--) {
if (realShapes[i].contains(virtualCoord.x, virtualCoord.y)) {
selectedShape = realShapes[i];
selectedShape.setIsDragging(true);
lastX = e.offsetX;
lastY = e.offsetY;
return;
}
}
// 如果没有选中形状,开始平移
isPanning = true;
lastX = e.offsetX;
lastY = e.offsetY;
});
canvas.addEventListener("mousemove", function (e) {
const currentX = e.offsetX;
const currentY = e.offsetY;
if (selectedShape && selectedShape.isDragging) {
const dx = (currentX - lastX) / globalScale;
const dy = (currentY - lastY) / globalScale;
selectedShape.translate(dx, dy);
draw();
} else if (isPanning) {
const dx = currentX - lastX;
const dy = currentY - lastY;
offsetX += dx;
offsetY += dy;
draw();
}
// 无论是否在拖拽或平移,都更新lastX和lastY
lastX = currentX;
lastY = currentY;
});
canvas.addEventListener("mouseup", function () {
if (selectedShape) {
selectedShape.setIsDragging(false);
selectedShape = null;
}
isPanning = false;
});
canvas.addEventListener("wheel", function (e) {
e.preventDefault();
handleTouchpadScroll(e);
});
function handleTouchpadScroll(e) {
// 反转滚动方向
offsetX -= e.deltaX;
offsetY -= e.deltaY;
draw();
}
// 添加键盘事件监听器
document.addEventListener("keydown", handleKeyPress);
function handleKeyPress(e) {
if (e.key === "-") {
zoomCanvas(false);
} else if (e.key === "=" || e.key === "+") {
zoomCanvas(true);
}
}
function zoomCanvas(zoomIn) {
const oldScale = globalScale;
let newScale = globalScale;
if (zoomIn) {
newScale *= SCALE_FACTOR;
} else {
newScale /= SCALE_FACTOR;
}
newScale = Math.min(Math.max(newScale, MIN_SCALE), MAX_SCALE);
const scaleRatio = newScale / oldScale;
// 使用lastX和lastY作为缩放中心
offsetX = lastX - scaleRatio * (lastX - offsetX);
offsetY = lastY - scaleRatio * (lastY - offsetY);
setScale(newScale);
if (globalScale !== oldScale) {
draw();
}
}
function isShapeVisible(
shape,
visibleLeft,
visibleTop,
visibleRight,
visibleBottom
) {
// 这个函数需要根据你的 Shape 类的具体实现来编写
// 这里只是一个示例
return (
shape.x >= visibleLeft &&
shape.x <= visibleRight &&
shape.y >= visibleTop &&
shape.y <= visibleBottom
);
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.save();
ctx.translate(offsetX, offsetY);
ctx.scale(globalScale, globalScale);
// 计算可见区域的虚拟坐标
const visibleLeft = -offsetX / globalScale;
const visibleTop = -offsetY / globalScale;
const visibleRight = visibleLeft + canvas.width / globalScale;
const visibleBottom = visibleTop + canvas.height / globalScale;
shapes.array.forEach((shape) => {
if (
isShapeVisible(
shape,
visibleLeft,
visibleTop,
visibleRight,
visibleBottom
)
) {
shape.draw(ctx);
}
});
ctx.restore();
}
draw();
});