-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
114 lines (106 loc) · 3.08 KB
/
index.html
File metadata and controls
114 lines (106 loc) · 3.08 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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Circles</title>
<style>
html, body {
margin: 0;
padding: 0;
height: 100%;
}
#container {
position: relative;
width: 100%;
height: 100%;
}
#canvas {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
#credit {
position: absolute;
bottom: 0;
right: 0;
margin: 10px;
font-size: 12px;
color: gray;
}
</style>
</head>
<body>
<div id="container">
<canvas id="canvas"></canvas>
<div id="credit">Created with ChatGPT</div>
</div>
<script>
// Set up canvas
const canvas = document.getElementById("canvas");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
const ctx = canvas.getContext("2d");
// Create initial circle
let circles = [];
createCircle();
// Add event listener for clicks on canvas
canvas.addEventListener("click", function(event) {
for (let i = 0; i < circles.length; i++) {
const circle = circles[i];
const dx = event.clientX - circle.x;
const dy = event.clientY - circle.y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance <= circle.radius) {
createCircle();
break;
}
}
});
// Function to create a new circle
function createCircle() {
const radius = 20 + Math.random() * 30;
const x = radius + Math.random() * (canvas.width - 2 * radius);
const y = radius + Math.random() * (canvas.height - 2 * radius);
const dx = Math.random() * 10 - 5;
const dy = Math.random() * 10 - 5;
const color = getRandomColor();
const circle = {x: x, y: y, radius: radius, dx: dx, dy: dy, color: color};
circles.push(circle);
}
// Function to get a random color
function getRandomColor() {
const letters = "0123456789ABCDEF";
let color = "#";
for (let i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
// Function to update and draw circles
function drawCircles() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (let i = 0; i < circles.length; i++) {
const circle = circles[i];
circle.x += circle.dx;
circle.y += circle.dy;
if (circle.x - circle.radius < 0 || circle.x + circle.radius > canvas.width) {
circle.dx = -circle.dx;
}
if (circle.y - circle.radius < 0 || circle.y + circle.radius > canvas.height) {
circle.dy = -circle.dy;
}
ctx.beginPath();
ctx.arc(circle.x, circle.y, circle.radius, 0, 2 * Math.PI);
ctx.fillStyle = circle.color;
ctx.fill();
}
}
// Set up game loop
setInterval(function() {
drawCircles();
}, 20);
</script>
</body>
</html>