Certainly! Below is a complete HTML file that creates a "big world" using the <canvas> element. It adds multiple circles representing persons that patrol the area by moving randomly within the canvas boundaries.
Features:
- Canvas Setup: A large canvas where the action takes place.
- Person Circles: Each person is represented by a circle with random initial positions and velocities.
- Patrolling Behavior: Persons move continuously and change direction upon hitting the canvas edges to stay within bounds.
Full Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Patrolling Persons in a Big World</title>
<style>
body {
margin: 0;
overflow: hidden; /* Hide scrollbars */
background-color: #f0f0f0;
}
canvas {
display: block; /* Removes scrollbar in some browsers */
background-color: #ffffff;
}
</style>
</head>
<body>
<canvas id="worldCanvas"></canvas>
<script>
// Get the canvas and context
const canvas = document.getElementById('worldCanvas');
const ctx = canvas.getContext('2d');
// Set canvas size to window size
function resizeCanvas() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
window.addEventListener('resize', resizeCanvas);
resizeCanvas();
// Number of persons
const NUM_PERSONS = 100; // Adjust as needed for performance
// Person properties
class Person {
constructor(x, y, radius, color, speed) {
this.x = x;
this.y = y;
this.radius = radius;
this.color = color;
// Random initial velocity
const angle = Math.random() * 2 * Math.PI;
this.vx = Math.cos(angle) * speed;
this.vy = Math.sin(angle) * speed;
}
update() {
this.x += this.vx;
this.y += this.vy;
// Bounce off the walls
if (this.x + this.radius > canvas.width || this.x - this.radius < 0) {
this.vx *= -1;
this.x = Math.max(this.radius, Math.min(this.x, canvas.width - this.radius));
}
if (this.y + this.radius > canvas.height || this.y - this.radius < 0) {
this.vy *= -1;
this.y = Math.max(this.radius, Math.min(this.y, canvas.height - this.radius));
}
// Optionally, add some random wandering
const changeDirection = Math.random();
if (changeDirection < 0.01) { // 1% chance to change direction each frame
const angle = Math.random() * 2 * Math.PI;
const speed = Math.sqrt(this.vx * this.vx + this.vy * this.vy);
this.vx = Math.cos(angle) * speed;
this.vy = Math.sin(angle) * speed;
}
}
draw(ctx) {
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, 2 * Math.PI, false);
ctx.fillStyle = this.color;
ctx.fill();
ctx.closePath();
}
}
// Initialize persons
const persons = [];
for (let i = 0; i < NUM_PERSONS; i++) {
const radius = 5 + Math.random() * 5; // Radius between 5 and 10
const x = Math.random() * (canvas.width - 2 * radius) + radius;
const y = Math.random() * (canvas.height - 2 * radius) + radius;
const color = getRandomColor();
const speed = 1 + Math.random() * 1.5; // Speed between 1 and 2.5
persons.push(new Person(x, y, radius, color, speed));
}
// Utility function to get random color
function getRandomColor() {
const letters = '0123456789ABCDEF';
let color = '#';
for (let j = 0; j < 6; j++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
// Animation loop
function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (let person of persons) {
person.update();
person.draw(ctx);
}
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>
How It Works:
-
Canvas Initialization:
- The
<canvas> element covers the entire browser window.
- The canvas resizes dynamically when the window size changes.
-
Person Class:
- Each person has properties like position (
x, y), radius, color, and velocity (vx, vy).
- The
update method moves the person based on its velocity and handles bouncing off the canvas edges.
- A small probability allows the person to change direction randomly, simulating patrolling behavior.
- The
draw method renders the person as a circle on the canvas.
-
Creating Persons:
- A specified number of persons (
NUM_PERSONS) are created with random initial positions, colors, and speeds.
- Adjust
NUM_PERSONS based on performance needs.
-
Animation Loop:
- The
animate function clears the canvas and updates/draws each person.
requestAnimationFrame ensures smooth animations by syncing with the browser's refresh rate.
Customization:
- Number of Persons: Change the
NUM_PERSONS constant to add or remove the number of patrolling circles.
- Colors & Sizes: Modify the
getRandomColor function or the radius calculation for different visual effects.
- Speed & Movement: Adjust the speed range or the probability within the
update method to change how the persons move.
Running the Code:
- Save the Code: Copy the above code into a text editor and save it with a
.html extension, e.g., patrolling_persons.html.
- Open in Browser: Double-click the saved file or open it via your browser's "Open File" option.
- Enjoy the Animation: You should see a canvas filling your browser window with multiple colored circles moving around randomly.
Feel free to tweak the parameters to better suit your needs or to add more complex behaviors!