Creating a dynamic and visually appealing water-themed wallpaper with moving fishes, rising bubbles, and wobbly grass can be achieved using HTML5 Canvas and JavaScript. Below is a complete example that you can use and customize to fit your preferences.
Step 1: HTML Structure
First, set up the basic HTML structure with a <canvas> element where the animation will be rendered.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Water Lively Wallpaper</title>
<style>
body, html {
margin: 0;
padding: 0;
overflow: hidden; /* Hide scrollbars */
background: #001f3f; /* Deep blue background */
}
canvas {
display: block;
}
</style>
</head>
<body>
<canvas id="waterCanvas"></canvas>
<script src="waterWallpaper.js"></script>
</body>
</html>
Step 2: JavaScript Animation
Create a separate JavaScript file named waterWallpaper.js and include the following code. This script handles the animation of fishes, bubbles, and wobbly grass.
// waterWallpaper.js
const canvas = document.getElementById('waterCanvas');
const ctx = canvas.getContext('2d');
// Set canvas size to full window
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// Handle window resize
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
});
// Utility function to load images
function loadImage(src) {
return new Promise((resolve) => {
const img = new Image();
img.src = src;
img.onload = () => resolve(img);
});
}
// Fish class
class Fish {
constructor(image, x, y, speed, scale) {
this.image = image;
this.x = x;
this.y = y;
this.speed = speed;
this.scale = scale;
this.direction = Math.random() < 0.5 ? 1 : -1; // Left or right
}
update() {
this.x += this.speed * this.direction;
// Wrap around the screen
if (this.direction === 1 && this.x > canvas.width + 50) {
this.x = -50;
} else if (this.direction === -1 && this.x < -50) {
this.x = canvas.width + 50;
}
}
draw() {
ctx.save();
ctx.translate(this.x, this.y);
if (this.direction === -1) {
ctx.scale(-1, 1); // Flip the image for left direction
}
ctx.drawImage(this.image, -this.image.width * this.scale / 2, -this.image.height * this.scale / 2, this.image.width * this.scale, this.image.height * this.scale);
ctx.restore();
}
}
// Bubble class
class Bubble {
constructor(x, y, radius, speed, opacity) {
this.x = x;
this.y = y;
this.radius = radius;
this.speed = speed;
this.opacity = opacity;
}
update() {
this.y -= this.speed;
if (this.y < -this.radius) {
this.y = canvas.height + this.radius;
this.x = Math.random() * canvas.width;
}
}
draw() {
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
ctx.fillStyle = `rgba(255, 255, 255, ${this.opacity})`;
ctx.fill();
}
}
// Grass class
class Grass {
constructor(x, baseY, height, swayAmplitude, swaySpeed, phase) {
this.x = x;
this.baseY = baseY;
this.height = height;
this.swayAmplitude = swayAmplitude;
this.swaySpeed = swaySpeed;
this.phase = phase;
}
draw(time) {
const sway = Math.sin(time * this.swaySpeed + this.phase) * this.swayAmplitude;
ctx.beginPath();
ctx.moveTo(this.x + sway, this.baseY);
ctx.quadraticCurveTo(this.x + sway + 10, this.baseY - this.height / 2, this.x + sway, this.baseY - this.height);
ctx.quadraticCurveTo(this.x + sway - 10, this.baseY - this.height / 2, this.x + sway, this.baseY);
ctx.fillStyle = "#0a5c3c"; // Dark green color
ctx.fill();
}
}
// Initialize elements
const bubbleCount = 50;
const bubbles = [];
for (let i = 0; i < bubbleCount; i++) {
const x = Math.random() * canvas.width;
const y = Math.random() * canvas.height;
const radius = Math.random() * 3 + 1;
const speed = Math.random() * 1 + 0.5;
const opacity = Math.random() * 0.5 + 0.3;
bubbles.push(new Bubble(x, y, radius, speed, opacity));
}
// Load fish image
(async function init() {
// You can replace this URL with any fish image of your choice
const fishImage = await loadImage('https://i.imgur.com/1LrV3aI.png'); // Example fish image
const fishCount = 5;
const fishes = [];
for (let i = 0; i < fishCount; i++) {
const x = Math.random() * canvas.width;
const y = Math.random() * (canvas.height * 0.6) + canvas.height * 0.2; // Avoid grass area
const speed = Math.random() * 1 + 0.5;
const scale = Math.random() * 0.5 + 0.5;
fishes.push(new Fish(fishImage, x, y, speed, scale));
}
// Initialize grass
const grassCount = Math.floor(canvas.width / 20);
const grasses = [];
for (let i = 0; i < grassCount; i++) {
const x = i * 20 + Math.random() * 10;
const baseY = canvas.height - 20;
const height = Math.random() * 30 + 20;
const swayAmplitude = Math.random() * 5 + 5;
const swaySpeed = Math.random() * 0.002 + 0.001;
const phase = Math.random() * Math.PI * 2;
grasses.push(new Grass(x, baseY, height, swayAmplitude, swaySpeed, phase));
}
// Animation loop
function animate(time) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw bubbles
bubbles.forEach(bubble => {
bubble.update();
bubble.draw();
});
// Draw fishes
fishes.forEach(fish => {
fish.update();
fish.draw();
});
// Draw grass
grasses.forEach(grass => {
grass.draw(time);
});
requestAnimationFrame(animate);
}
requestAnimationFrame(animate);
})();
Step 3: Fish Image
The above script uses an external fish image hosted on Imgur. You can replace the URL 'https://i.imgur.com/1LrV3aI.png' with any fish image of your choice. Make sure the image has a transparent background (PNG format is preferred) for better visual integration.
Alternative Approach Without External Images:
If you prefer not to use external images, you can draw simple fish shapes using Canvas APIs. Here's how you can modify the Fish class to draw basic fish shapes instead of using images:
// Replace the Fish class with the following
class Fish {
constructor(x, y, speed, scale, color) {
this.x = x;
this.y = y;
this.speed = speed;
this.scale = scale;
this.direction = Math.random() < 0.5 ? 1 : -1; // Left or right
this.color = color;
}
update() {
this.x += this.speed * this.direction;
// Wrap around the screen
if (this.direction === 1 && this.x > canvas.width + 50) {
this.x = -50;
} else if (this.direction === -1 && this.x < -50) {
this.x = canvas.width + 50;
}
}
draw() {
ctx.save();
ctx.translate(this.x, this.y);
if (this.direction === -1) {
ctx.scale(-1, 1); // Flip the fish
}
ctx.scale(this.scale, this.scale);
// Draw body
ctx.beginPath();
ctx.ellipse(0, 0, 20, 10, 0, 0, Math.PI * 2);
ctx.fillStyle = this.color;
ctx.fill();
// Draw tail
ctx.beginPath();
ctx.moveTo(-20, 0);
ctx.lineTo(-30, 10);
ctx.lineTo(-30, -10);
ctx.closePath();
ctx.fill();
// Draw eye
ctx.beginPath();
ctx.arc(10, -3, 2, 0, Math.PI * 2);
ctx.fillStyle = "#000";
ctx.fill();
ctx.restore();
}
}
// Modify the initialization of fishes
(async function init() {
const fishCount = 7;
const fishes = [];
const colors = ["#FF6347", "#FFD700", "#ADFF2F", "#1E90FF", "#FF69B4"]; // Various colors
for (let i = 0; i < fishCount; i++) {
const x = Math.random() * canvas.width;
const y = Math.random() * (canvas.height * 0.6) + canvas.height * 0.2; // Avoid grass area
const speed = Math.random() * 1 + 0.5;
const scale = Math.random() * 0.7 + 0.5;
const color = colors[Math.floor(Math.random() * colors.length)];
fishes.push(new Fish(x, y, speed, scale, color));
}
// Initialize grass as before
// ... (rest of the initialization remains the same)
// Animation loop as before
// ... (rest of the animation loop remains the same)
})();
This modification allows you to have colorful, simple fish without relying on external images.
Final Notes
-
Performance Optimization: For better performance, especially with a large number of bubbles or fishes, consider optimizing the drawing methods and limiting the number of objects.
-
Customization: Feel free to customize colors, speeds, sizes, and counts of fishes, bubbles, and grass to achieve the desired visual effect.
-
Advanced Features: To enhance realism, you can add features like fish rotation based on movement direction, varying bubble sizes more dynamically, and more sophisticated grass animations.
Live Demo
To see the wallpaper in action, create the HTML and JavaScript files as described above and open the HTML file in a web browser. The animation should start automatically, displaying moving fishes, rising bubbles, and wobbly grass at the bottom.
Feel free to ask if you have any questions or need further customization!