当前位置:首页 > JavaScript

js实现妖怪

2026-04-05 05:41:47JavaScript

JavaScript 实现妖怪效果

要实现妖怪效果,可以通过动画、变形和随机行为来模拟妖怪的运动和外观。以下是几种实现方式:

js实现妖怪

使用 Canvas 绘制妖怪

const canvas = document.getElementById('monsterCanvas');
const ctx = canvas.getContext('2d');

function drawMonster(x, y) {
    ctx.fillStyle = 'green';
    ctx.beginPath();
    ctx.arc(x, y, 30, 0, Math.PI * 2);
    ctx.fill();

    ctx.fillStyle = 'red';
    ctx.beginPath();
    ctx.arc(x - 10, y - 10, 5, 0, Math.PI * 2);
    ctx.arc(x + 10, y - 10, 5, 0, Math.PI * 2);
    ctx.fill();

    ctx.strokeStyle = 'black';
    ctx.beginPath();
    ctx.arc(x, y + 5, 10, 0, Math.PI);
    ctx.stroke();
}

function animate() {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    const x = Math.random() * canvas.width;
    const y = Math.random() * canvas.height;
    drawMonster(x, y);
    requestAnimationFrame(animate);
}

animate();

使用 CSS 动画创建妖怪效果

<div class="monster"></div>

<style>
.monster {
    width: 60px;
    height: 60px;
    background-color: purple;
    border-radius: 50%;
    position: absolute;
    animation: move 3s infinite alternate;
}

@keyframes move {
    0% { transform: translateX(0) rotate(0deg); }
    100% { transform: translateX(300px) rotate(360deg); }
}
</style>

使用 Three.js 创建 3D 妖怪

const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

const geometry = new THREE.SphereGeometry(1, 32, 32);
const material = new THREE.MeshBasicMaterial({ color: 0xff0000 });
const monster = new THREE.Mesh(geometry, material);
scene.add(monster);

camera.position.z = 5;

function animate() {
    requestAnimationFrame(animate);
    monster.rotation.x += 0.01;
    monster.rotation.y += 0.01;
    renderer.render(scene, camera);
}

animate();

实现妖怪随机行为

class Monster {
    constructor() {
        this.x = 0;
        this.y = 0;
        this.speed = 2;
    }

    move() {
        this.x += (Math.random() - 0.5) * this.speed;
        this.y += (Math.random() - 0.5) * this.speed;
    }
}

const monster = new Monster();
setInterval(() => {
    monster.move();
    console.log(`Monster position: (${monster.x}, ${monster.y})`);
}, 100);

这些方法可以根据需求组合使用,创造出更复杂的妖怪效果。

标签: 妖怪js
分享给朋友:

相关文章

js实现vue

js实现vue

Vue.js 简介 Vue.js 是一个渐进式 JavaScript 框架,用于构建用户界面。其核心库专注于视图层,易于与其他库或现有项目整合。 实现 Vue.js 的基本步骤 安装 Vue.j…

js实现吸色

js实现吸色

使用Canvas实现吸色功能 通过Canvas的getImageData方法获取像素颜色数据。创建一个Canvas元素,将目标图像绘制到Canvas上,通过鼠标事件获取坐标对应的颜色值。 const…

js怎么实现异步

js怎么实现异步

异步实现方法 JavaScript中实现异步操作主要通过以下几种方式: Promise Promise是ES6引入的异步解决方案,用于处理异步操作的成功或失败状态。 const promise =…

js实现滚动

js实现滚动

实现滚动效果的方法 在JavaScript中实现滚动效果可以通过多种方式完成,以下是一些常见的方法: 使用window.scrollTo() window.scrollTo()方法可以将页面滚动到指…

js实现上传文件

js实现上传文件

文件上传的基本实现 使用HTML的<input type="file">元素配合JavaScript的File API可以实现文件上传功能。 <input type="file"…

实现继承js

实现继承js

实现继承的方式 在JavaScript中,实现继承可以通过多种方式完成,每种方式都有其优缺点。以下是几种常见的实现继承的方法: 原型链继承 原型链继承是最基本的继承方式,通过将子类的原型指向父类的实…