-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
188 lines (159 loc) · 6.71 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
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
<!DOCTYPE html>
<html>
<head>
<title>Чпокер - пупырчатый релаксант v4</title>
<meta name="title" content="Чпокер - пупырчатый релаксант" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1,target-densitydpi=device-dpi, user-scalable=no" />
<script src="https://telegram.org/js/telegram-web-app.js"></script>
<style>
html, body {
width: 100vw;
height: 100vh;
margin: 0;
padding:0;
font-family: Verdana, sans-serif;
display: block;
overflow: hidden;
}
canvas {
user-select: none; /* Standard syntax */
width: 100vw;
height: 100vh;
background-color: #666;
--disabled-cursor: none;
}
</style>
</head>
<body oncontextmenu="return false;">
<canvas id="canvas"></canvas>
<script>
class Grid {
img = new Image()
snd = new Audio('chpocker.wav')
size = 44 // size of the sprite
piece = [ // pieces
// (first half is unchpocked)
{sx: 14, sy: 10},
{sx: 0, sy: 54},
{sx: 57, sy: 97},
{sx: 100, sy: 11},
// (second half is chpocked)
{sx: 43, sy: 53},
{sx: 171, sy: 54},
{sx: 142, sy: 96},
{sx: 86, sy: 140},
]
field = []
constructor() {
this.img.addEventListener('load', () => this.drawEverything());
this.img.src = 'chpocker.jpg';
}
chpock = (sx, sy) => {
const {x, y} = this.getFieldCoordinate(sx, sy);
if (this.field[x][y] < this.piece.length / 2) {
this.field[x][y] += this.piece.length / 2
this.snd.currentTime = 0;
this.snd.play();
grid.drawSprite(x, y)
}
}
// transform field coordinete to screen coordinate
getGridCoordinate = (x, y) => {
const dx = (y % 2 ? x - .5 : x) * this.size
const dy = y * this.size
return {dx, dy}
}
// transform screen coordinete to field coordinate
getFieldCoordinate = (sx, sy) => {
const y = Math.floor( sy / grid.size)
const add = y%2 === 0 ? 0 : grid.size/2
const x = Math.floor( (sx+add) / grid.size )
return {x,y}
}
// draw sprite
drawSprite = (x, y) => {
let rnd = null;
const {dx, dy} = this.getGridCoordinate(x, y);
if (this.field[x] === undefined) this.field[x] = [];
if (this.field[x][y] === undefined)
rnd = this.field[x][y] = Math.floor(Math.random() * Math.floor(this.piece.length)/1.98);
else
rnd = this.field[x][y]
ctx.drawImage(this.img, this.piece[rnd].sx, this.piece[rnd].sy, this.size, this.size, dx, dy, this.size, this.size);
}
// draw the cursor
drawCursor = (x, y) => {
const {dx, dy} = this.getGridCoordinate(x, y);
ctx.beginPath();
ctx.moveTo(dx + (this.size * 1 / 9), dy + (this.size / 2));
ctx.lineTo(dx + (this.size * 8 / 9), dy + (this.size / 2));
ctx.stroke();
ctx.beginPath();
ctx.moveTo(dx + (this.size / 2), dy + (this.size * 1 / 9))
ctx.lineTo(dx + (this.size / 2), dy + (this.size * 8 / 9))
ctx.stroke();
ctx.beginPath();
ctx.arc(dx + (this.size / 2), dy + (this.size / 2), this.size / 6, 0, 2 * Math.PI)
ctx.stroke();
ctx.beginPath();
ctx.arc(dx + (this.size / 2), dy + (this.size / 2), this.size / 4, 0, 2 * Math.PI)
ctx.stroke();
}
// draw whole screen
drawEverything = () => {
console.log('Grid draw start');
for (let y = 0; y < ctx.canvas.height / this.size + 1; y++)
for (let x = 0; x < ctx.canvas.width / this.size + 1; x++)
this.drawSprite(x, y);
console.log('Grid draw end');
}
// draw text in the middle of the screen
drawMessage(text) {
ctx.fillStyle = "#003300";
ctx.font = '20px sans-serif';
let textWidth = ctx.measureText(text).width;
ctx.fillText(text, (ctx.canvas.width / 2) - (textWidth / 2), ctx.canvas.height / 3);
}
// clean text from the middle of the screen
cleanMessage(text) {
const m = ctx.measureText(text)
let w = Math.ceil(m.width / this.size)
let xfrom = Math.floor((canvas.width - m.width) / 2 / this.size);
let yfrom = Math.floor(ctx.canvas.height / 3 / this.size);
for (let y = yfrom - 1; y <= yfrom + 1; y++)
for (let x = xfrom - 1; x <= xfrom + w; x++)
this.drawSprite(x, y);
}
}
const ctx = document.getElementById('canvas').getContext('2d');
const grid = new Grid()
window.addEventListener('resize', function () {
ctx.canvas.width = window.innerWidth;
ctx.canvas.height = window.innerHeight;
grid.drawEverything();
})
ctx.canvas.width = window.innerWidth;
ctx.canvas.height = window.innerHeight;
let action = { mousedown: false, mousex: null, mousey: null, selected: null, message: null }
canvas.addEventListener('mousedown', (e) => { e.preventDefault(); action.mousedown = true; return false; })
canvas.addEventListener('mouseup', (e) => { e.preventDefault(); action.mousedown = false; return false; })
canvas.addEventListener('touchstart', (e) => {
e.preventDefault();
for (i = 0; i < e.touches.length; i++)
grid.chpock(e.touches[i].clientX, e.touches[i].clientY)
})
canvas.addEventListener('touchend', (e) => { e.preventDefault(); action.mousedown = false; return false; })
canvas.addEventListener('mousemove', (e) => { e.preventDefault(); action.mousex = e.clientX - ctx.canvas.offsetLeft; action.mousey = e.clientY - ctx.canvas.offsetTop; return false; })
canvas.addEventListener('touchmove', (e) => { e.preventDefault(); action.mousex = e.clientX - ctx.canvas.offsetLeft; action.mousey = e.clientY - ctx.canvas.offsetTop; return false; })
canvas.addEventListener('mouseout', (e) => {e.preventDefault(); action.mousex = action.mousey = null; return false; })
canvas.addEventListener('touchcancel', (e) => {e.preventDefault(); action.mousex = action.mousey = null; return false; })
function render()
{
window.requestAnimationFrame(render);
if (action.mousedown) grid.chpock(action.mousex, action.mousey)
}
window.requestAnimationFrame(render);
</script>
</body>
</html>