forked from qinyongliang/suspension-utools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuspend.html
190 lines (181 loc) · 5.78 KB
/
suspend.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
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
179
180
181
182
183
184
185
186
187
188
189
190
<!DOCTYPE html>
<html>
<style type="text/css">
body {
-webkit-app-region: drag;
-webkit-user-select: none;
margin: 0px;
width: 100%;
height: 100%;
overflow: hidden;
overflow-x: hidden;
overflow-y: hidden;
opacity: 1;
}
.body-hover {
transition: opacity 0.3s ease-in-out, transform 0.3s ease-in-out;
}
.body-hover:hover {
opacity: 0.8;
}
img {
width: 100vw;
height: auto;
border-radius: 6px;
overflow: hidden !important;
box-sizing: border-box;
border: 2px solid red;
}
</style>
<body scroll="no" class="body-hover">
<img draggable="false" />
</body>
<script type="application/javascript">
let lock = false;
let img = document.querySelector("img")
let imgKey = window.location.hash.substring(1);
img.src = localStorage[imgKey];
let filePath = localStorage[imgKey + "_file"];
localStorage.removeItem(imgKey);
localStorage.removeItem(imgKey + "_file");
window.rotateX = 0;
window.rotateY = 0;
function rotate(degree) {
if (lock) {
return;
}
lock = true;
//获得图片大小
let { width, height } = img;
//计算改变后窗口位置和大小
let R = Math.sqrt(width * width + height * height);
let xOffset = (R - width) / 2;
let yOffset = (R - height) / 2;
img.src = rotateImage(degree);
img.onload = () => {
window.moveBounds(yOffset - xOffset, xOffset - yOffset, height, width);
lock = false;
}
}
let keyCounter = {
lastKey: undefined,
key: undefined,
count: 3,
time: 0
}
let dowingKey = {};
document.addEventListener("keyup", event => {
delete dowingKey[event.key]
})
document.addEventListener("keydown", event => {
console.log(event.key);
//长按操作将加速
if (keyCounter.key === event.key && new Date().getTime() - keyCounter.time < 200) {
keyCounter.count++
} else {
keyCounter.key = event.key
keyCounter.count = 3;
keyCounter.time = 0;
}
keyCounter.time = new Date().getTime();
switch (event.key) {
case "c":
case "C":
if (dowingKey["Control"] || dowingKey["Meta"]) {
document.body.className = ""
//等待透明关闭,再拷贝当前图片
setTimeout(() => window.copyNowImage(), 100)
}
break
case "s":
case "S":
if (dowingKey["Control"] || dowingKey["Meta"]) {
document.body.className = ""
//等待透明关闭,再保存当前图片
setTimeout(() => window.saveNowImage(), 100)
}
break
case "q":
case "Q":
document.body.className = ""
//等待透明关闭,再去编辑图片
setTimeout(() => window.toEdit(), 100)
break;
case "Escape":
window.close();
break;
case "ArrowUp":
window.moveBounds(0, -keyCounter.count / 3);
break;
case "ArrowDown":
window.moveBounds(0, keyCounter.count / 3);
break;
case "ArrowLeft":
window.moveBounds(-keyCounter.count / 3, 0);
break;
case "ArrowRight":
window.moveBounds(keyCounter.count / 3, 0);
break;
case "+":
case "=":
window.resize(keyCounter.count / 2, img.height / img.width);
break;
case "-":
case "_":
if (img.width > 100) {
window.resize(0 - keyCounter.count / 2, img.height / img.width);
}
break;
//旋转相关
case "【":
case "[": {
rotate(-90);
break;
}
case "】":
case "]": {
rotate(90);
break;
}
//左右反转
case "Home":
case "End":
window.rotateY += 180;
window.document.body.style.webkitTransform = `rotateX(${window.rotateX}deg) rotateY(${window.rotateY}deg)`
break;
//上下反转
case "PageUp":
case "PageDown":
window.rotateX -= 180;
window.document.body.style.webkitTransform = `rotateX(${window.rotateX}deg) rotateY(${window.rotateY}deg)`
break;
}
dowingKey[event.key] = true
});
document.body.addEventListener("mousewheel", (e) => {
var delta = Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail)));
if (delta < 0 && img.width <= 100) {
return false;
}
window.resize(delta * 7, img.height / img.width);
return false;
}, false);
//重画图片
function rotateImage(degree) {
degree = degree % 360;
let img = document.querySelector("img");
let tmpImg = new Image()
tmpImg.src = img.src;
let canvas = document.createElement('canvas');
let cContext = canvas.getContext('2d');
let height = tmpImg.height;
let width = tmpImg.width;
canvas.width = height;
canvas.height = width;
cContext.translate(canvas.width / 2, canvas.height / 2);
cContext.rotate(degree * Math.PI / 180);
cContext.drawImage(tmpImg, -width / 2, -height / 2);
return canvas.toDataURL();
}
</script>
</html>