Here's a complete HTML5 program that simulates a car driving down an infinite winding road using canvas and JavaScript:
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Infinite Winding Road</title>
<style>
body {
margin: 0;
padding: 0;
overflow: hidden;
background: #87CEEB;
}
canvas {
display: block;
}
</style>
</head>
<body>
<canvas id="gameCanvas"></canvas>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// Set canvas size
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// Game variables
let roadOffset = 0;
let carX = canvas.width / 2;
let carY = canvas.height * 0.7;
let carAngle = 0;
let speed = 5;
let time = 0;
// Road segments
const roadSegments = [];
const segmentHeight = 10;
const numSegments = Math.ceil(canvas.height / segmentHeight) + 20;
// Initialize road segments
for (let i = 0; i < numSegments; i++) {
roadSegments.push({
curve: Math.sin(i * 0.1) * 100,
width: 200
});
}
// Car object
const car = {
width: 30,
height: 50,
wheelOffset: 10
};
function drawSky() {
// Sky gradient
const gradient = ctx.createLinearGradient(0, 0, 0, canvas.height * 0.6);
gradient.addColorStop(0, '#87CEEB');
gradient.addColorStop(1, '#98D8E8');
ctx.fillStyle = gradient;
ctx.fillRect(0, 0, canvas.width, canvas.height * 0.6);
}
function drawGround() {
ctx.fillStyle = '#228B22';
ctx.fillRect(0, canvas.height * 0.6, canvas.width, canvas.height * 0.4);
}
function drawRoad() {
// Draw road segments from bottom to top
for (let i = 0; i < numSegments; i++) {
const segment = roadSegments[i];
const y = canvas.height - (i * segmentHeight) + roadOffset;
if (y < -segmentHeight || y > canvas.height) continue;
// Calculate perspective
const perspective = 1 - (canvas.height - y) / canvas.height;
const roadWidth = segment.width * (1 - perspective * 0.7);
const centerX = canvas.width / 2 + segment.curve * perspective;
// Draw road
ctx.fillStyle = '#333333';
ctx.fillRect(centerX - roadWidth / 2, y, roadWidth, segmentHeight + 1);
// Draw center line
if (Math.floor((i + roadOffset / segmentHeight) / 5) % 2 === 0) {
ctx.fillStyle = '#FFFF00';
ctx.fillRect(centerX - 2, y, 4, segmentHeight + 1);
}
// Draw road edges
ctx.fillStyle = '#FFFFFF';
ctx.fillRect(centerX - roadWidth / 2 - 5, y, 5, segmentHeight + 1);
ctx.fillRect(centerX + roadWidth / 2, y, 5, segmentHeight + 1);
}
}
function drawCar() {
ctx.save();
ctx.translate(carX, carY);
ctx.rotate(carAngle);
// Car body
ctx.fillStyle = '#FF0000';
ctx.fillRect(-car.width / 2, -car.height / 2, car.width, car.height);
// Car roof
ctx.fillStyle = '#CC0000';
ctx.fillRect(-car.width / 2 + 5, -car.height / 2 + 10, car.width - 10, car.height - 30);
// Windows
ctx.fillStyle = '#4444FF';
ctx.fillRect(-car.width / 2 + 7, -car.height / 2 + 12, car.width - 14, 15);
// Wheels
ctx.fillStyle = '#000000';
ctx.fillRect(-car.width / 2 - 3, -car.height / 2 + car.wheelOffset, 6, 10);
ctx.fillRect(car.width / 2 - 3, -car.height / 2 + car.wheelOffset, 6, 10);
ctx.fillRect(-car.width / 2 - 3, car.height / 2 - car.wheelOffset - 10, 6, 10);
ctx.fillRect(car.width / 2 - 3, car.height / 2 - car.wheelOffset - 10, 6, 10);
ctx.restore();
}
function updateRoad() {
roadOffset += speed;
// When a segment goes off screen, move it to the top with new curve
if (roadOffset >= segmentHeight) {
roadOffset -= segmentHeight;
roadSegments.shift();
roadSegments.push({
curve: Math.sin(time * 0.1) * 100 + Math.cos(time * 0.05) * 50,
width: 200
});
}
// Update car position based on road curve
const carSegmentIndex = Math.floor((canvas.height - carY) / segmentHeight);
if (carSegmentIndex >= 0 && carSegmentIndex < roadSegments.length) {
const segment = roadSegments[carSegmentIndex];
const perspective = 1 - (canvas.height - carY) / canvas.height;
const targetX = canvas.width / 2 + segment.curve * perspective;
// Smooth car movement
carX += (targetX - carX) * 0.1;
// Calculate car angle based on road curve
if (carSegmentIndex < roadSegments.length - 1) {
const nextSegment = roadSegments[carSegmentIndex + 1];
const nextX = canvas.width / 2 + nextSegment.curve * perspective;
carAngle = Math.atan2(0, nextX - targetX) * 0.3;
}
}
time += 0.1;
}
function gameLoop() {
// Clear canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw scene
drawSky();
drawGround();
drawRoad();
drawCar();
// Update
updateRoad();
requestAnimationFrame(gameLoop);
}
// Handle window resize
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
carX = canvas.width / 2;
carY = canvas.height * 0.7;
});
// Start the game
gameLoop();
</script>
</body>
</html>
This program creates a complete driving simulation with the following features:
Visual Elements:
A gradient sky background
Green ground/grass on the sides
A dark gray road with white edge lines
Yellow dashed center line
A red car with windows and wheels
Animation Features:
The road continuously scrolls downward creating an infinite driving effect
The road curves left and right using sine and cosine functions for smooth, natural curves
The car follows the road's curves automatically
The car tilts slightly when turning for more realistic movement
Perspective effect makes the road narrow toward the horizon
Technical Implementation:
Uses HTML5 Canvas for rendering
Implements a segment-based road system where each segment can have different curve values
Smooth interpolation for car movement
Efficient rendering by only drawing visible road segments
Responsive design that adapts to window resizing
You can adjust various parameters to customize the experience:
speed: Controls how fast the road scrolls
roadSegments[i].curve: Controls how much each road segment curves
car.width/height: Changes the car size
Road width and colors can be easily modified
The simulation runs at 60 FPS using requestAnimationFrame for smooth animation. The winding road pattern is generated procedurally using trigonometric functions, creating an endless variety of curves.