Creating a realistic bird animation that responds to touch events on mobile devices involves using HTML, CSS, and JavaScript. Below is a simplified example of how you could achieve this. This example will create a basic scene with a bird that you can drag around the screen, and when moved, it will flap its wings.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Bird in Garden</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="garden">
<div class="bird" id="bird">
<div class="wing left-wing"></div>
<div class="wing right-wing"></div>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
CSS (style.css)
body, html {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
overflow: hidden;
background-color: #f0f0f0;
}
.garden {
position: relative;
width: 100%;
height: 100%;
background-image: linear-gradient(to bottom, #87ceeb, #4682b4);
}
.bird {
position: absolute;
top: 50px;
left: 50%;
transform: translateX(-50%);
width: 100px;
height: 100px;
background-color: #ffff00;
border-radius: 50%;
cursor: grab;
}
.wing {
position: absolute;
width: 50px;
height: 20px;
background-color: #ffff00;
border-radius: 10px;
}
.left-wing {
top: 40px;
left: -20px;
transform-origin: right center;
}
.right-wing {
top: 40px;
right: -20px;
transform-origin: left center;
}
.flap {
animation: flap 0.5s ease-in-out;
}
@keyframes flap {
0% {
transform: rotate(0deg);
}
50% {
transform: rotate(30deg);
}
100% {
transform: rotate(0deg);
}
}
JavaScript (script.js)
document.addEventListener('DOMContentLoaded', function() {
const bird = document.getElementById('bird');
let isDown = false;
let offset = [0, 0];
bird.addEventListener('mousedown', function(event) {
if (event.button === 0) { // Left mouse button
isDown = true;
offset = [
bird.offsetLeft - event.clientX,
bird.offsetTop - event.clientY
];
event.preventDefault();
flapWings();
}
});
bird.addEventListener('touchstart', function(event) {
if (event.touches.length === 1) {
isDown = true;
const touch = event.touches[0];
offset = [
bird.offsetLeft - touch.clientX,
bird.offsetTop - touch.clientY
];
event.preventDefault();
flapWings();
}
});
document.addEventListener('mouseup', function() {
isDown = false;
});
document.addEventListener('touchend', function() {
isDown = false;
});
document.addEventListener('mousemove', function(event) {
event.preventDefault();
if (isDown) {
bird.style.left = `${event.clientX + offset[0]}px`;
bird.style.top = `${event.clientY + offset[1]}px`;
}
});
document.addEventListener('touchmove', function(event) {
if (event.touches.length === 1) {
event.preventDefault();
const touch = event.touches[0];
bird.style.left = `${touch.clientX + offset[0]}px`;
bird.style.top = `${touch.clientY + offset[1]}px`;
flapWings();
}
});
function flapWings() {
const leftWing = bird.querySelector('.left-wing');
const rightWing = bird.querySelector('.right-wing');
leftWing.classList.add('flap');
rightWing.classList.add('flap');
setTimeout(function() {
leftWing.classList.remove('flap');
rightWing.classList.remove('flap');
}, 500); // Remove flap class after animation
}
});
This code creates a simple bird that can be dragged around the screen. When the bird is moved, its wings will flap. The flapping animation is achieved through CSS keyframe animations. Note that this is a basic example and might need adjustments for a more realistic look and feel, especially concerning the bird's movement and wing flapping animation.
Also, keep in mind that this example uses both mouse and touch events to make it work on both desktop and mobile devices. The touchstart, touchmove, and touchend events are used for mobile devices, while mousedown, mousemove, and mouseup are used for desktop devices.