js实现骷髅
JavaScript实现骷髅效果
在JavaScript中实现骷髅效果通常涉及图形绘制或动画技术。以下是几种常见的实现方式:
使用Canvas绘制骷髅
const canvas = document.getElementById('skullCanvas');
const ctx = canvas.getContext('2d');
// 绘制骷髅头
ctx.beginPath();
ctx.arc(100, 100, 80, 0, Math.PI * 2); // 头部
ctx.moveTo(140, 160);
ctx.arc(100, 160, 40, 0, Math.PI); // 下巴
ctx.moveTo(70, 80);
ctx.arc(65, 80, 5, 0, Math.PI * 2); // 左眼
ctx.moveTo(135, 80);
ctx.arc(130, 80, 5, 0, Math.PI * 2); // 右眼
ctx.moveTo(100, 110);
ctx.arc(100, 110, 10, 0, Math.PI); // 鼻子
ctx.moveTo(60, 140);
ctx.lineTo(140, 140); // 嘴巴
ctx.stroke();
使用CSS和HTML创建简单骷髅
<div class="skull">
<div class="head"></div>
<div class="eyes">
<div class="eye left"></div>
<div class="eye right"></div>
</div>
<div class="mouth"></div>
</div>
.skull {
position: relative;
width: 200px;
height: 200px;
}
.head {
width: 150px;
height: 150px;
border-radius: 50%;
background: white;
border: 2px solid black;
}
.eye {
width: 20px;
height: 20px;
background: black;
border-radius: 50%;
position: absolute;
}
.left { top: 50px; left: 40px; }
.right { top: 50px; left: 90px; }
.mouth {
width: 60px;
height: 20px;
border-bottom: 3px solid black;
border-radius: 0 0 50% 50%;
position: absolute;
top: 100px;
left: 45px;
}
使用Three.js创建3D骷髅
import * as THREE from 'three';
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer();
// 创建骷髅几何体
const headGeometry = new THREE.SphereGeometry(1, 32, 32);
const eyeGeometry = new THREE.SphereGeometry(0.1, 16, 16);
const headMaterial = new THREE.MeshBasicMaterial({color: 0xffffff});
const eyeMaterial = new THREE.MeshBasicMaterial({color: 0x000000});
const head = new THREE.Mesh(headGeometry, headMaterial);
const leftEye = new THREE.Mesh(eyeGeometry, eyeMaterial);
const rightEye = new THREE.Mesh(eyeGeometry, eyeMaterial);
leftEye.position.set(-0.3, 0.2, 0.9);
rightEye.position.set(0.3, 0.2, 0.9);
scene.add(head);
scene.add(leftEye);
scene.add(rightEye);
camera.position.z = 5;
function animate() {
requestAnimationFrame(animate);
head.rotation.y += 0.01;
renderer.render(scene, camera);
}
animate();
使用SVG创建矢量骷髅

<svg width="200" height="200" viewBox="0 0 200 200">
<circle cx="100" cy="100" r="80" fill="white" stroke="black" stroke-width="2"/>
<path d="M60,140 A40,40 0 0 0 140,140" stroke="black" stroke-width="2" fill="none"/>
<circle cx="70" cy="80" r="5" fill="black"/>
<circle cx="130" cy="80" r="5" fill="black"/>
</svg>
这些方法可以根据项目需求选择使用,从简单的2D绘制到复杂的3D渲染都能实现骷髅效果。






