js+css实现可修改参数的多边形地形起伏波动动画效果代码

代码语言:html

所属分类:动画

代码描述:js+css实现可修改参数的多边形地形起伏波动动画效果代码

代码标签: js css 参数 多边形 地形 起伏 波动 动画

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

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

<head>
  <meta charset="UTF-8">
  
  
  
  
<style>
@import url("https://fonts.googleapis.com/css2?family=Space+Mono&display=swap");

body {
	margin: 0;
	padding: 0;
	overflow: hidden;
	background-color: #87ceeb;
	font-family: "Space Mono", monospace;
}

canvas {
	display: block;
}

#controls-panel {
	position: absolute;
	top: 10px;
	left: 10px;
	background-color: rgba(255, 255, 255, 0.4);
	border-radius: 5px;
	transition: all 0.3s ease;
}

#controls {
	padding: 10px;
	display: none;
}

#toggle-controls {
	cursor: pointer;
	padding: 10px;
	font-size: 18px;
	background-color: rgba(255, 255, 255, 0.5);
	border: none;
	border-radius: 5px;
	width: 100%;
	text-align: left;
	font-family: "Space Mono", monospace;
	font-weight: 700;
}

label {
	display: inline-block;
	width: 140px;
	margin-bottom: 5px;
	font-size: 14px;
}

input[type="range"] {
	width: 150px;
}

input[type="color"] {
	vertical-align: middle;
}

select {
	font-family: "Space Mono", monospace;
	font-size: 14px;
	margin-bottom: 10px;
}
</style>



  
  
</head>

<body translate="no">
  <div id="controls-panel">
	<button id="toggle-controls">☰ Configurations</button>
	<div id="controls">
		<label for="scale">Scale: </label>
		<input type="range" id="scale" min="10" max="100" value="40"><br>
		<label for="height">Height: </label>
		<input type="range" id="height" min="50" max="300" value="200"><br>
		<label for="speed">Speed: </label>
		<input type="range" id="speed" min="0" max="0.05" step="0.001" value="0.01"><br>
		<label for="background-color">Background: </label>
		<input type="color" id="background-color" value="#87CEEB"><br>
		<label for="color-theme">Color Theme: </label>
		<select id="color-theme">
			<option value="heatmap">Heatmap</option>
			<option value="rainbow">Rainbow</option>
			<option value="monochrome">Monochrome</option>
			<option value="red">Red</option>
			<option value="green">Green</option>
			<option value="blue">Blue</option>
			<option value="temperature">Temperature</option>
			<option value="viridis">Viridis</option>
			<option value="magma">Magma</option>
			<option value="heat">Heat</option>
			<option value="brewer-ygb">Brewer YGB</option>
		</select>
	</div>
</div>

<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/p5.1.4.0.js"></script>
  
      <script >
let cols, rows;
let scl = 40;
let w = 2000;
let h = 1600;
let flying = 0;
let terrain = [];
let backgroundColor;
let colorTheme = "heatmap";

function setup() {
	createCanvas(windowWidth, windowHeight, WEBGL);
	cols = w / scl;
	rows = h / scl;

	for (let x = 0; x < cols; x++) {
		terrain[x] = [];
		for (let y = 0; y < rows; y++) {
			terrain[x][y] = 0;
		}
	}

	backgroundColor = color(135, 206, 235); // Sky blue in RGB

	// Add event listeners for controls
	document.getElementById("scale").addEventListener("input", updateScale);
	document.getElementById("speed").addEventListener("input", updateSpeed);
	document.getElementById("height").addEventListener("input", updateHeight);
	document
		.getElementById("background-color")
		.addEventListener("input", updateBackgroundColor);
	document
		.getElementById("color-theme")
		.addEventListener("change", updateColorTheme);

	// Add event listener for toggle button
	document
		.getElementById("toggle-controls")
		.addEventListener("click", toggleControls);

	// Set the initial background color input value
	document.getElementById("background-color").value = "#87CEEB";
}

function draw() {
	flying -= parseFloat(document.getElementById("speed").value);
	let yoff = flying;
	for (let y = 0; y < rows; y++) {
		let xoff = 0;
		for (let x = 0; x < cols; x++) {
			terrain[x][y] = map(
				noise(xoff, yoff),
				0,
				1,
				-100,
				parseFloat(document.getElementById("height").value)
			);
			xoff += 0.2;
		}
		yoff += 0.2;
	}

	background(backgroundColor);
	translate(0, 50);
	rotateX(PI / 3);

	translate(-w / 2, -h / 2);
	for (let y = 0; y < rows - 1; y++) {
		beginShape(TRIANGLE_STRIP);
		for (let x = 0; x < cols; x++) {
			let height = terrain[x][y];
			let terrainColor = getTerrainColor(height);
			fill(terrainColor);
			vertex(x * scl, y * scl, height);
			vertex(x * scl, (y + 1) * scl, terrain[x][y + 1]);
		}
		endShape();
	}
}

function getTerrainColor(height) {
	let normalizedHeight = map(
		height,
		-100,
		parseFloat(document.getElementById("height").value),
		0,
		1
	);

	switch (colorTheme) {
		case "heatmap":
			return getHeatmapColor(normalizedHeight);
		case "rainbow":
			return getRainbowColor(normalizedHeight);
		case "monochrome":
			return getMonochromeColor(normalizedHeight);
		case "red":
			return color(255 * normalizedHeight, 0, 0);
		case "green":
			return color(0, 255 * normalizedHeight, 0);
		case "blue":
			return color(0, 0, 255 * normalizedHeight);
		case "temperature":
			return getTemperatureColor(normalizedHeight);
		case "viridis":
			return getViridisColor(normalizedHeight);
		case "magma":
			return getMagmaColor(normalizedHeight);
		case "heat":
			return getHeatColor(normalizedHeight);
		case "brewer-ygb":
			return getBrewerYGBColor(nor.........完整代码请登录后点击上方下载按钮下载查看

网友评论0