前端立方体怎么画

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta http-equiv="X-UA-Compatible" content="IE=edge">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Document</title>

<style>

.box{

width: 400px;

height: 400px;

background: chartreuse;

margin: 100px auto;

transform-style: preserve-3d;

transform: rotateX(50deg) rotateY(50deg) rotateZ(50deg);

position: relative;

animation: run 3s linear infinite;

}

.box div:nth-child(1){

width: 400px;

height: 400px;

background: rgb(237, 228, 100);

position: absolute;

}

.box div:nth-child(2){

width: 400px;

height: 400px;

background: rgb(77, 216, 65);

position: absolute;

transform: translateZ(400px);

}

.box div:nth-child(3){

width: 400px;

height: 400px;

background: rgb(82, 211, 228);

transform: translateZ(200px) translateX(200px) rotateY(90deg);

position: absolute;

}

.box div:nth-child(4){

width: 400px;

height: 400px;

background: rgb(61, 77, 228);

transform: rotateY(90deg);

position: absolute;

transform: translateZ(200px) translateX(-200px) rotateY(90deg);

}

.box div:nth-child(5){

width: 400px;

height: 400px;

background: rgb(168, 98, 214);

transform: translateZ(200px) translatey(200px) rotateX(90deg);

position: absolute;

}

.box div:nth-child(6){

width: 400px;

height: 400px;

background: rgb(207, 56, 127);

transform: translateZ(200px) translatey(-200px) rotateX(90deg);

position: absolute;

}

@keyframes run {

0%{}

350%{transform: translate(100px,170px);}

70%{transform: translate(200px,100px)}

100%{transform: translate(500px,370px);}

}

</style>

</head>

<body>

<div class="box">

<div>1</div>

<div>2</div>

<div>3</div>

<div>4</div>

<div>5</div>

<div>6</div>

</div>

</body>

</html>

以下使用 Canvas 绘制渐变色立方体的示例代码: ```html <!DOCTYPE html> <html> <head> <title>Canvas Gradient Cube</title> <style> canvas { border: 1px solid #000; } </style> </head> <body> <canvas id="myCanvas" width="400" height="400"></canvas> <script> var canvas = document.getElementById("myCanvas"); var context = canvas.getContext("2d"); // 绘制立方体的六个面 drawFace(100, 100, 100, 200, 200, 200, "#ff0000", "#00ff00", "#0000ff"); // 前面 drawFace(0, 100, 100, 100, 200, 200, "#0000ff", "#00ff00", "#ff0000"); // 左面 drawFace(200, 100, 100, 300, 200, 200, "#00ff00", "#0000ff", "#ff0000"); // 右面 drawFace(100, 0, 100, 200, 100, 200, "#ff0000", "#0000ff", "#00ff00"); // 上面 drawFace(100, 200, 100, 200, 300, 200, "#ff0000", "#00ff00", "#0000ff"); // 下面 drawFace(100, 100, 0, 200, 200, 100, "#00ff00", "#ff0000", "#0000ff"); // 后面 // 绘制一个立方体面 function drawFace(x1, y1, z1, x2, y2, z2, color1, color2, color3) { // 计算面的中心点 var centerX = (x1 + x2) / 2; var centerY = (y1 + y2) / 2; var centerZ = (z1 + z2) / 2; // 开始绘制面 context.beginPath(); // 绘制面的左下角点 context.moveTo(projectX(x1, z1), projectY(y1, z1)); // 绘制面的其它三个点 context.lineTo(projectX(x2, z1), projectY(y1, z1)); context.lineTo(projectX(x2, z2), projectY(y2, z2)); context.lineTo(projectX(x1, z2), projectY(y2, z2)); // 使用渐变色填充面 var gradient = context.createLinearGradient(0, 0, 0, canvas.height); gradient.addColorStop(0, color1); gradient.addColorStop(0.5, color2); gradient.addColorStop(1, color3); context.fillStyle = gradient; context.fill(); // 结束绘制面 context.closePath(); } // 将 3D 坐标投影到 2D 平面上 function projectX(x, z) { return canvas.width / 2 + (x - canvas.width / 2) * (200 / (200 - z)); } function projectY(y, z) { return canvas.height / 2 - (y - canvas.height / 2) * (200 / (200 - z)); } </script> </body> </html> ``` 上述代码使用了三个颜色值,可以根据需要进行调整。同时,也可以根据需要修改立方体的大小和位置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值