用javascript 写一个炫酷的界面
时间: 2023-10-05 10:04:21 浏览: 118
好的,以下是一个简单的用 JavaScript 写的炫酷界面的示例,它包括一个旋转的立方体和一些动态的文字效果。
```html
<!DOCTYPE html>
<html>
<head>
<title>炫酷界面示例</title>
<style type="text/css">
canvas {
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
}
#text {
color: #FFF;
font-size: 50px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
text-shadow: 0px 0px 10px #FFF, 0px 0px 20px #FFF, 0px 0px 30px #FFF, 0px 0px 40px #FF0000, 0px 0px 70px #FF0000, 0px 0px 80px #FF0000, 0px 0px 100px #FF0000, 0px 0px 150px #FF0000;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<div id="text">炫酷的文字效果!</div>
<script type="text/javascript">
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var width = canvas.width = window.innerWidth;
var height = canvas.height = window.innerHeight;
var centerX = width / 2;
var centerY = height / 2;
// 生成随机颜色
function randomColor() {
return "#" + Math.random().toString(16).slice(2, 8);
}
// 画立方体
function drawCube(x, y, size, angleX, angleY, angleZ) {
var halfSize = size / 2;
var vertices = [
{ x: -halfSize, y: -halfSize, z: halfSize },
{ x: halfSize, y: -halfSize, z: halfSize },
{ x: halfSize, y: -halfSize, z: -halfSize },
{ x: -halfSize, y: -halfSize, z: -halfSize },
{ x: -halfSize, y: halfSize, z: halfSize },
{ x: halfSize, y: halfSize, z: halfSize },
{ x: halfSize, y: halfSize, z: -halfSize },
{ x: -halfSize, y: halfSize, z: -halfSize }
];
// 旋转立方体
var cosX = Math.cos(angleX);
var sinX = Math.sin(angleX);
var cosY = Math.cos(angleY);
var sinY = Math.sin(angleY);
var cosZ = Math.cos(angleZ);
var sinZ = Math.sin(angleZ);
for (var i = 0; i < vertices.length; i++) {
var v = vertices[i];
var x1 = v.x;
var y1 = v.y;
var z1 = v.z;
// 旋转X轴
var y2 = y1 * cosX - z1 * sinX;
var z2 = z1 * cosX + y1 * sinX;
// 旋转Y轴
var x3 = x1 * cosY - z2 * sinY;
var z3 = z2 * cosY + x1 * sinY;
// 旋转Z轴
var x4 = x3 * cosZ - y2 * sinZ;
var y4 = y2 * cosZ + x3 * sinZ;
// 投影到2D平面上
var scale = 200 / (200 - y4);
var x2d = centerX + x4 * scale;
var y2d = centerY + y4 * scale;
if (i == 0) {
ctx.moveTo(x2d, y2d);
} else {
ctx.lineTo(x2d, y2d);
}
}
ctx.closePath();
ctx.stroke();
}
// 主循环
function loop() {
ctx.clearRect(0, 0, width, height);
ctx.lineWidth = 2;
ctx.strokeStyle = "#FFF";
// 旋转立方体
var angle = performance.now() / 1000 / 6;
var angleX = angle;
var angleY = angle * 1.1;
var angleZ = angle * 1.2;
// 画立方体
ctx.beginPath();
drawCube(centerX, centerY, 200, angleX, angleY, angleZ);
// 显示文字
var text = document.getElementById("text");
text.style.textShadow = "0px 0px 10px " + randomColor() +
", 0px 0px 20px " + randomColor() +
", 0px 0px 30px " + randomColor() +
", 0px 0px 40px " + randomColor() +
", 0px 0px 70px " + randomColor() +
", 0px 0px 80px " + randomColor() +
", 0px 0px 100px " + randomColor() +
", 0px 0px 150px " + randomColor();
requestAnimationFrame(loop);
}
// 启动循环
loop();
</script>
</body>
</html>
```
这个示例中,我们利用了 HTML5 的 Canvas 功能,画了一个旋转的立方体,并在其上方显示了动态的文字效果。每次执行 `loop` 函数时,我们都会清空画布,重新绘制立方体和更新文字效果。通过不断改变 `angleX`、`angleY` 和 `angleZ` 等参数,我们使立方体不断旋转,从而产生了炫酷的视觉效果。同时,我们也通过 `randomColor` 函数随机生成了一些颜色,让文字效果更加生动。
阅读全文