-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconsoleweb.js
More file actions
183 lines (164 loc) · 4.71 KB
/
consoleweb.js
File metadata and controls
183 lines (164 loc) · 4.71 KB
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
(()=>{
let popupHtml2 = `
<div id="popup" style="display: none; position: fixed; width: 140px; height: 140px; background-color: #ffffff; ">
<div id="header" style="cursor: move; background-color: #f1f1f1; width: 100%; height: 30px; padding: 5px;">
<span style="font-size: 12px;">这里拖动</span>
</div>
<canvas id="joystick" width="150" height="150" style="margin:10px;"></canvas>
</div>`;
let body = (top.document.getElementsByTagName("main")|| top.document.getElementsByTagName("body"))[0];
let div = top.document.createElement('div');
div.innerHTML = popupHtml2;
body.appendChild(div);
let canvas = top.document.getElementById('joystick');
let ctx = canvas.getContext('2d');
let joystick = {
x: 70,
y: 55,
radius: 50,
isMoving: false,
angle: 0,
distance: 0,
};
function drawJoystick() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.beginPath();
ctx.arc(70, 55, joystick.radius, 0, Math.PI * 2);
ctx.fillStyle = '#ffffff';
ctx.fill();
ctx.strokeStyle = '#aaaaaa';
ctx.stroke();
ctx.closePath();
ctx.beginPath();
ctx.arc(joystick.x, joystick.y, 30, 0, Math.PI * 2);
ctx.fillStyle = '#aaaaaa';
ctx.fill();
ctx.closePath();
}
canvas.addEventListener('mousedown', (e) => {
let dx = e.clientX - (canvas.offsetLeft + top.document.getElementById('popup').offsetLeft) - 70;
let dy = e.clientY - (canvas.offsetTop + top.document.getElementById('popup').offsetTop) - 55;
if (dx * dx + dy * dy < joystick.radius * joystick.radius) {
joystick.isMoving = true;
}
});
canvas.addEventListener('mousemove', (e) => {
if (joystick.isMoving) {
let dx = e.clientX - (canvas.offsetLeft + top.document.getElementById('popup').offsetLeft) - 70;
let dy = e.clientY - (canvas.offsetTop + top.document.getElementById('popup').offsetTop) - 55;
joystick.angle = Math.atan2(dy, dx);
joystick.distance = Math.min(Math.sqrt(dx * dx + dy * dy), joystick.radius);
joystick.x = 70 + Math.cos(joystick.angle) * joystick.distance;
joystick.y = 55 + Math.sin(joystick.angle) * joystick.distance;
drawJoystick();
}
});
canvas.addEventListener('mouseup', () => {
joystick.isMoving = false;
joystick.x = 70;
joystick.y = 55;
joystick.angle = 0;
joystick.distance = 0;
drawJoystick();
});
// Draggable Popup
let popup = top.document.getElementById('popup');
let header = top.document.getElementById('header');
function openPopup() {
popup.style.display = 'block';
popup.style.left=10
popup.style.top=10
popup.style.zIndex = 1000;
drawJoystick();
}
function closePopup() {
popup.style.display = 'none';
}
header.onmousedown = function (event) {
popup.style.position = 'absolute';
popup.style.zIndex = 1000;
function moveAt(pageX, pageY) {
popup.style.left = pageX - popup.offsetWidth / 2 + 'px';
popup.style.top = pageY - header.offsetHeight / 2 + 'px';
}
moveAt(event.pageX, event.pageY);
function onMouseMove(event) {
moveAt(event.pageX, event.pageY);
}
top.document.addEventListener('mousemove', onMouseMove);
header.onmouseup = function () {
top.document.removeEventListener('mousemove', onMouseMove);
header.onmouseup = null;
};
};
header.ondragstart = function () {
return false;
};
class js {
constructor() { }
getInfo() {
return {
id: 'joystick',
name: '虚拟摇杆',
blocks: [{
opcode: 'show',
blockType: Scratch.BlockType.COMMAND,
text: '显示摇杆',
arguments: {}
},{
opcode: 'hide',
blockType: Scratch.BlockType.COMMAND,
text: '隐藏摇杆',
arguments: {}
},{
opcode: 'rad',
blockType: Scratch.BlockType.REPORTER,
text: '摇杆弧度',
arguments: {}
},{
opcode: 'ang',
blockType: Scratch.BlockType.REPORTER,
text: '摇杆角度',
arguments: {}
},{
opcode: 'distance',
blockType: Scratch.BlockType.REPORTER,
text: '摇杆距离',
arguments: {}
},{
opcode: 'x',
blockType: Scratch.BlockType.REPORTER,
text: '摇杆x',
arguments: {}
},{
opcode: 'y',
blockType: Scratch.BlockType.REPORTER,
text: '摇杆y',
arguments: {}
}]
};
}
show(){
openPopup()
}
hide(){
closePopup()
}
rad(){
return joystick.angle;
}
ang(){
return joystick.angle/Math.PI*180
}
distance(){
return joystick.distance
}
x(){
return joystick.x-70
}
y(){
return joystick.y-55
}
}
Scratch.extensions.register(new js())
})()