canvas实现可调节温度风力湿度云层密度的下雨天气动画效果代码

代码语言:html

所属分类:动画

代码描述:canvas实现可调节温度风力湿度云层密度的下雨天气动画效果代码

代码标签: canvas 调节 温度 风力 湿度 云层 密度 下雨 天气 动画

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  

<link type="text/css" rel="stylesheet" href="//repo.bfw.wiki/bfwrepo/css/all.6.7.0.css">
<style>
body {
	margin: 0;
	overflow: hidden;
	display: flex;
	flex-direction: column;
	align-items: center;
	background-color: #1e1e2f;
	color: #fff;
	font-family: "Poppins", Arial, sans-serif;
}

canvas {
	display: block;
	background: linear-gradient(to bottom, #4a90e2, #1e1e2f);
}

.controls {
	position: fixed;
	top: 10px;
	left: 50%;
	transform: translateX(-50%);
	z-index: 10;
	display: flex;
	gap: 20px;
	align-items: center;
	background: rgba(30, 30, 30, 0.8);
	padding: 10px 20px;
	border-radius: 10px; /* Rounded corners */
}

.slider-group {
	display: flex;
	flex-direction: column;
	align-items: center;
}

.slider-group label {
	color: #fff; /* White text */
	text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.8); /* Add text shadow for contrast */
	font-size: 1rem; /* Slightly increase label size */
	margin-bottom: 5px;
}

.temperature-display {
	font-size: 1.2em;
	padding: 10px;
	background: rgba(30, 30, 30, 0.8);
	border-radius: 8px;
	display: flex;
	flex-direction: column;
	align-items: center;
	gap: 10px;
	text-align: center;
	color: #fff; /* Ensure text is white for visibility */
}

.weather-icon {
	font-size: 2em;
	transition: opacity 0.5s ease-in-out;
}

.weather-icon.hidden {
	opacity: 0;
}

.additional-info {
	font-size: 1em;
	margin-top: 5px;
}

.temperature-display,
.slider-group label,
.additional-info {
	font-family: "Poppins", Arial, sans-serif;
	font-weight: 500; /* Medium weight for better readability */
}

input[type="range"] {
	appearance: none;
	width: 100px;
	height: 6px;
	background: #ddd;
	border-radius: 5px;
	outline: none;
	transition: background 0.3s ease;
}

input[type="range"]::-webkit-slider-thumb {
	appearance: none;
	width: 20px;
	height: 20px;
	background: #4a90e2; /* Blue handle */
	border: 2px solid #fff;
	border-radius: 50%; /* Circular handle */
	cursor: pointer;
	transition: transform 0.2s ease;
}

input[type="range"]::-webkit-slider-thumb:hover {
	transform: scale(1.2); /* Slightly enlarge on hover */
}
</style>


  
  
</head>


  <body>
	<div class="controls">
		<div class="slider-group">
			<label for="temperature">Temperature (°C)</label>
			<input type="range" id="temperature" min="-10" max="30" value="0">
		</div>
		<div class="slider-group">
			<label for="precipitation">Precipitation Intensity</label>
			<input type="range" id="precipitation" min="0" max="100" value="50">
		</div>
		<div class="slider-group">
			<label for="wind">Wind Speed</label>
			<input type="range" id="wind" min="-5" max="5" value="0">
		</div>
		<div class="slider-group">
			<label for="cloudiness">Cloudiness (%)</label>
			<input type="range" id="cloudiness" min="0" max="100" value="50">
			<span id="cloudinessValue">50%</span>
		</div>

		<div class="temperature-display">
			<i class="weather-icon fas fa-cloud-showers-heavy" id="weatherIcon"></i>
			<span id="temperatureDisplay">Temperature: 0°C / 32°F</span>
			<span id="precipitationInfo" class="additional-info">Precipitation: 50%</span>
			<span id="windInfo" class="additional-info">Wind: 0 m/s (Calm)</span>
		</div>
	</div>
	<canvas id="weatherCanvas"></canvas>


  
      <script  >
// =============================
// Initialization and Constants
// =============================
const canvas = document.getElementById("weatherCanvas");
const ctx = canvas.getContext("2d");

const temperatureSlider = document.getElementById("temperature");
const precipitationSlider = document.getElementById("precipitation");
const windSlider = document.getElementById("wind");
const cloudinessSlider = document.getElementById("cloudiness");

const temperatureDisplay = document.getElementById("temperatureDisplay");
const precipitationInfo = document.getElementById("precipitationInfo");
const windInfo = document.getElementById("windInfo");
const weatherIcon = document.getElementById("weatherIcon");

canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

let particles = [];
let cloudParticles = [];
let temperature = 0;
let intensity = 50;
let windSpeed = 0;
let cloudiness = 50;

const icons = {
  rain: "fa-cloud-showers-heavy",
  snow: "fa-snowflake",
  mixed: "fa-cloud-sun-rain",
  sunny: "fa-sun" };


// =============================
// Utility Functions
// =============================
function celsiusToFahrenheit(celsius) {
  return Math.round(celsius * 9 / 5 + 32);
}

function getWindDirection(speed) {
  if (speed === 0) return "Calm";
  if (speed > 0 && speed <= 2) return "E";
  if (speed > 2 && speed <= 4) return "NE";
  if (speed > 4) return "N";
  if (speed < 0 && speed >= -2) return "W";
  if (speed < -2 && speed >= -4) return "NW";
  if (speed < -4) return "S";
  return "Variable";
}

// =============================
// UI Update Functions
// =============================
function updateTemperatureDisplay() {
  const fahrenheit = celsiusToFahrenheit(temperature);
  temperatureDisplay.textContent = `Temperature: ${temperature}°C / ${fahrenheit}°F`;

  let condition;
  if (temperature > 20 && intensity === 0) {
    condition = "sunny"; // Sunny weather if warm and no precipitation
  } else if (temperature > 2) {
    condition = "rain";
  } else if (temperature < -2) {
    condition = "snow";
  } else {
    condition = "mixed";
  }

  updateWeatherIcon(condition);
}

function updatePrecipitationInfo() {
  precipitationInfo.textContent = `Precipitation: ${intensity}%`;
}

function updateWindInfo() {
  const direction = getWindDirection(windSpeed);
  const windText =
  windSpeed !== 0 ? `${Math.abs(windSpeed)} m/s (${direction})` : "Calm";
  windInfo.textContent = `Wind: ${windText}`;
}

function updateCanvasBackground() {
  if (temperature > 20 && intensity === 0) {
    // Sunny
    canvas.style.background = "linear-gradient(to bottom, #87ceeb, #f0e68c)"; // Bright sunny sky
  } else if (temperature > 10 && temperature <= 20) {
    // Mild
    canvas.style.background = "linear-gradient(to bottom, #6ab3f1, #c2d8ef)"; // Cool blue sky
  } else if (temperature > 0 &.........完整代码请登录后点击上方下载按钮下载查看

网友评论0