在 Vue 中,通常将 Three.js 的相关代码封装成一个组件,然后在 created 或 mounted 生命周期中进行初始化、动画等操作。
以下是将上述 Three.js 代码改写成 Vue 组件的示例:
<template>
<div id="wrap"></div>
</template>
<script>
export default {
data() {
return {
$wrap: null,
areawidth: window.innerWidth,
areaheight: window.innerHeight,
canvassize: 500,
length: 30,
radius: 5.6,
rotatevalue: 0.035,
acceleration: 0,
animatestep: 0,
toend: false,
pi2: Math.PI * 2,
group: null,
mesh: null,
ringcover: null,
ring: null,
camera: null,
scene: null,
renderer: null,
};
},
mounted() {
this.init();
this.animate();
},
methods: {
init() {
this.$wrap = document.getElementById("wrap");
this.camera = new THREE.PerspectiveCamera(65, 1, 1, 10000);
this.camera.position.z = 150;
this.scene = new THREE.Scene();
this.group = new THREE.Group();
this.scene.add(this.group);
this.mesh = new THREE.Mesh(
new THREE.TubeGeometry(
new THREE.Curve.create(
function() {},
(percent) => {
const x = this.length * Math.sin(this.pi2 * percent);
const y = this.radius * Math.cos(this.pi2 * 3 * percent);
let z, t;
t = percent % 0.25 / 0.25;
t = percent % 0.25 - (2 * (1 - t) * t * -0.0185 + t * t * 0.25);
if (Math.floor(percent / 0.25) == 0 || Math.floor(percent / 0.25) == 2) {
t *= -1;
}
z = this.radius * Math.sin(this.pi2 * 2 * (percent - t));
return new THREE.Vector3(x, y, z);
}
)(),
200,
1.1,
2,
true
),
new THREE.MeshBasicMaterial({
color: 0x5287FF,
})
);
this.group.add(this.mesh);
this.ringcover = new THREE.Mesh(
new THREE.PlaneGeometry(50, 15, 1),
new THREE.MeshBasicMaterial({
color: 0xd1684e,
opacity: 0,
transparent: true,
})
);
this.ringcover.position.x = this.length + 1;
this.ringcover.rotation.y = Math.PI / 2;
this.group.add(this.ringcover);
this.ring = new THREE.Mesh(
new THREE.RingGeometry(4.3, 5.55, 32),
new THREE.MeshBasicMaterial({
color: 0xffffff,
opacity: 0,
transparent: true,
})
);
this.ring.position.x = this.length + 1.1;
this.ring.rotation.y = Math.PI / 2;
this.group.add(this.ring);
// fake shadow
for (let i = 0; i < 10; i++) {
const plain = new THREE.Mesh(
new THREE.PlaneGeometry(this.length * 2 + 1, this.radius * 3, 1),
new THREE.MeshBasicMaterial({
color: 0xffffff,
transparent: true,
opacity: 0.13,
})
);
plain.position.z = -2.5 + i * 0.5;
this.group.add(plain);
}
this.renderer = new THREE.WebGLRenderer({
antialias: true,
});
this.renderer.setPixelRatio(window.devicePixelRatio);
this.renderer.setSize(this.canvassize, this.canvassize);
this.renderer.setClearColor("#ffffff");
this.$wrap.appendChild(this.renderer.domElement);
// this.$body.addEventListener('mousedown', start, false);
// this.$body.addEventListener('touchstart', start, false);
// this.$body.addEventListener('mouseup', back, false);
// this.$body.addEventListener('touchend', back, false);
},
animate() {
this.mesh.rotation.x += this.rotatevalue + this.acceleration;
this.render();
requestAnimationFrame(this.animate);
},
easing(t, b, c, d) {
if ((t /= d / 2) < 1) return (c / 2) * t * t + b;
return (c / 2) * ((t -= 2) * t * t + 2) + b;
},
start() {
this.toend = true;
},
back() {
this.toend = false;
},
tilt(percent) {
this.group.rotation.y = percent * 0.5;
},
render() {
let progress;
this.animatestep = Math.max(0, Math.min(240, this.toend ? this.animatestep + 1 : this.animatestep - 4));
this.acceleration = this.easing(this.animatestep, 0, 1, 240);
if (this.acceleration > 0.35) {
progress = (this.acceleration - 0.35) / 0.65;
this.group.rotation.y = (-Math.PI / 2) * progress;
this.group.position.z = 50 * progress;
progress = Math.max(0, (this.acceleration - 0.97) / 0.03);
this.mesh.material.opacity = 1 - progress;
this.ringcover.material.opacity = this.ring.material.opacity = progress;
this.ring.scale.x = this.ring.scale.y = 0.9 + 0.1 * progress;
}
this.renderer.render(this.scene, this.camera);
},
},
};
</script>
上述代码将 Three.js 的相关操作封装在 Vue 组件的方法中,并在 mounted 生命周期中调用 init 方法进行初始化,并在 animate 方法中调用 render 方法实现动画效果。由于服务端渲染环境中不存在 window 对象,因此应将代码中的 window 变量改为全局变量,或在方法中使用 this.$isServer 进行判断,以避免服务端渲染时的报错。