-
Notifications
You must be signed in to change notification settings - Fork 0
/
pizza.js
132 lines (116 loc) · 3.4 KB
/
pizza.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
const canvas = document.getElementById('pizzaCanvas')
const ctx = canvas.getContext('2d')
let pineappleSlices = []
function resizeCanvas() {
canvas.width = window.innerWidth
canvas.height = window.innerHeight
drawPizza()
drawPineappleSlices()
}
function dropPineapple(x, y, isLocal = true) {
const xPercent = x / canvas.width
const yPercent = y / canvas.height
pineappleSlices.push({ xPercent, yPercent })
drawPineappleSlices()
if (isLocal && typeof sendPineapplePosition === 'function') {
sendPineapplePosition(xPercent, yPercent)
}
}
function receivePineapple(xPercent, yPercent) {
pineappleSlices.push({ xPercent, yPercent })
drawPineappleSlices()
}
function drawPineappleSlices() {
pineappleSlices.forEach((slice) => {
const x = slice.xPercent * canvas.width
const y = slice.yPercent * canvas.height
// Draw pineapple body
ctx.beginPath()
ctx.ellipse(x, y + 10, 15, 20, 0, 0, 2 * Math.PI)
ctx.fillStyle = '#FFF700'
ctx.fill()
ctx.strokeStyle = '#DAA520'
ctx.lineWidth = 2
ctx.stroke()
// Draw pineapple pattern
for (let i = -2; i <= 2; i++) {
for (let j = -2; j <= 2; j++) {
ctx.beginPath()
ctx.arc(x + i * 6, y + 10 + j * 8, 2, 0, 2 * Math.PI)
ctx.fillStyle = '#DAA520'
ctx.fill()
}
}
// Draw pineapple crown
ctx.beginPath()
ctx.moveTo(x - 15, y)
ctx.lineTo(x - 10, y - 15)
ctx.lineTo(x - 5, y - 5)
ctx.lineTo(x, y - 20)
ctx.lineTo(x + 5, y - 5)
ctx.lineTo(x + 10, y - 15)
ctx.lineTo(x + 15, y)
ctx.closePath()
ctx.fillStyle = '#228B22'
ctx.fill()
ctx.strokeStyle = '#006400'
ctx.lineWidth = 1
ctx.stroke()
})
}
function drawPizza() {
const centerX = canvas.width / 2
const centerY = canvas.height / 2
const radius = Math.min(canvas.width, canvas.height) * 0.4
const slices = 8
const sliceAngle = (2 * Math.PI) / slices
const gapAngle = 0.02 // Adjust this value to increase/decrease the gap between slices
// Draw individual slices
for (let i = 0; i < slices; i++) {
const startAngle = i * sliceAngle + gapAngle / 2
const endAngle = (i + 1) * sliceAngle - gapAngle / 2
// Draw sauce (now the outermost layer)
ctx.beginPath()
ctx.moveTo(centerX, centerY)
ctx.arc(centerX, centerY, radius, startAngle, endAngle)
ctx.closePath()
ctx.fillStyle = '#B22222'
ctx.fill()
// Draw cheese
ctx.beginPath()
ctx.moveTo(centerX, centerY)
ctx.arc(centerX, centerY, radius * 0.95, startAngle, endAngle)
ctx.closePath()
ctx.fillStyle = '#FFA500'
ctx.fill()
// Draw slice outline
ctx.beginPath()
ctx.moveTo(centerX, centerY)
ctx.arc(centerX, centerY, radius, startAngle, endAngle)
ctx.closePath()
ctx.lineWidth = 2
ctx.strokeStyle = '#8B4513'
ctx.stroke()
}
// Draw pineapple slices
drawPineappleSlices()
}
window.addEventListener('resize', resizeCanvas)
resizeCanvas()
// Function to check if connection is open
function isConnected() {
return conn && conn.open
}
// Add click event listener to canvas
document
.getElementById('pizzaCanvas')
.addEventListener('click', function (event) {
if (isConnected()) {
const rect = canvas.getBoundingClientRect()
const x = event.clientX - rect.left
const y = event.clientY - rect.top
dropPineapple(x, y, true)
} else {
console.log('Not connected. Cannot drop pineapple.')
}
})