Files
animated_wallpaper/assets/index.html
T

137 lines
3.8 KiB
HTML

<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Isometric Tiles</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { overflow: hidden; background: #000; }
canvas { display: block; }
</style>
</head>
<body>
<canvas id="c"></canvas>
<script>
var c = document.getElementById('c');
var ctx = c.getContext('2d');
var W, H;
var TILE = 32;
function resize() {
W = c.width = window.innerWidth;
H = c.height = window.innerHeight;
}
window.addEventListener('resize', resize);
resize();
var offX = 0, offY = 0;
var speedX = 0, speedY = 0;
var targetSpeedX = -2, targetSpeedY = -1;
var angle = Math.PI / 6;
function project(x, y, z) {
var fov = 600;
var scale = fov / (600 + z);
var cosA = Math.cos(angle);
var sinA = Math.sin(angle);
var isoX = x * cosA - y * cosA;
var isoY = x * sinA + y * sinA - z;
var sx = isoX * scale + W / 2;
var sy = isoY * scale + H / 2;
return [sx, sy, scale];
}
function getHeight(x, y) {
var h = 0;
h += Math.sin(x * 0.05) * 8;
h += Math.cos(y * 0.05) * 8;
h += Math.sin(x * 0.1 + y * 0.05) * 4;
h += Math.cos(y * 0.1 - x * 0.05) * 3;
return h;
}
var tiles = [];
function generateTiles() {
tiles = [];
var rangeX = Math.ceil(W / TILE) + 4;
var rangeY = Math.ceil(H / TILE) + 4;
for (var x = -rangeX; x <= rangeX; x++) {
for (var y = -rangeY; y <= rangeY; y++) {
var wx = x * TILE + offX;
var wy = y * TILE + offY;
var h = getHeight(wx, wy);
tiles.push({ x: wx, y: wy, h: h });
}
}
}
function drawTiles() {
tiles.sort(function(a, b) {
return ((a.x + a.y) - (b.x + b.y));
});
for (var i = 0; i < tiles.length; i++) {
var t = tiles[i];
var wx = t.x, wy = t.y, h = t.h;
var x1 = wx, y1 = wy + TILE, z1 = h;
var x2 = wx + TILE, y2 = wy + TILE, z2 = getHeight(wx + TILE, wy + TILE);
var x3 = wx + TILE, y3 = wy, z3 = getHeight(wx + TILE, wy);
var x4 = wx, y4 = wy, z4 = h;
var p1 = project(x1, y1, z1);
var p2 = project(x2, y2, z2);
var p3 = project(x3, y3, z3);
var p4 = project(x4, y4, z4);
if (p1[1] < -100 || p1[1] > H + 100) continue;
if (p4[1] < -100 || p4[1] > H + 100) continue;
var water = h < 2;
var baseR = water ? 0 : 0;
var baseG = water ? 80 : 140;
var baseB = water ? 180 : 0;
var lighting = 0.4 + Math.max(0, Math.sin(angle) * 0.5 + 0.3);
var r = Math.floor(baseR * lighting);
var g = Math.floor(baseG * lighting);
var b = Math.floor(baseB * lighting);
ctx.fillStyle = 'rgb(' + r + ',' + g + ',' + b + ')';
ctx.beginPath();
ctx.moveTo(p1[0], p1[1]);
ctx.lineTo(p2[0], p2[1]);
ctx.lineTo(p3[0], p3[1]);
ctx.lineTo(p4[0], p4[1]);
ctx.closePath();
ctx.fill();
ctx.strokeStyle = water ? 'rgba(0, 200, 255, 0.5)' : 'rgba(0, 255, 100, 0.5)';
ctx.lineWidth = 0.5;
ctx.stroke();
}
}
function frame() {
ctx.fillStyle = '#001020';
ctx.fillRect(0, 0, W, H);
speedX += (targetSpeedX - speedX) * 0.05;
speedY += (targetSpeedY - speedY) * 0.05;
offX += speedX;
offY += speedY;
generateTiles();
drawTiles();
requestAnimationFrame(frame);
}
frame();
</script>
</body>
</html>