当前位置:首页 > JavaScript

js实现浪漫

2026-02-01 04:53:59JavaScript

使用Canvas绘制动态星空

在HTML中创建Canvas元素,通过JavaScript绘制随机生成的星星和流星动画效果。星星可以设置闪烁效果,流星带有拖尾,模拟夜空的浪漫场景。

const canvas = document.getElementById('starCanvas');
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

class Star {
  constructor() {
    this.x = Math.random() * canvas.width;
    this.y = Math.random() * canvas.height;
    this.size = Math.random() * 3;
    this.blinkSpeed = Math.random() * 0.05;
    this.opacity = Math.random();
    this.direction = Math.random() > 0.5 ? 1 : -1;
  }

  update() {
    this.opacity += this.blinkSpeed * this.direction;
    if(this.opacity > 1 || this.opacity < 0) this.direction *= -1;
  }

  draw() {
    ctx.beginPath();
    ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
    ctx.fillStyle = `rgba(255, 255, 255, ${this.opacity})`;
    ctx.fill();
  }
}

const stars = Array(200).fill().map(() => new Star());

function animate() {
  ctx.fillStyle = 'rgba(10, 10, 30, 0.2)';
  ctx.fillRect(0, 0, canvas.width, canvas.height);

  stars.forEach(star => {
    star.update();
    star.draw();
  });

  requestAnimationFrame(animate);
}

animate();

创建爱心粒子动画

实现鼠标交互的爱心粒子效果,当鼠标移动时会产生爱心形状的粒子,并带有缓动动画和渐隐效果。

class HeartParticle {
  constructor(x, y) {
    this.x = x;
    this.y = y;
    this.size = Math.random() * 15 + 5;
    this.speedX = Math.random() * 3 - 1.5;
    this.speedY = Math.random() * 3 - 1.5;
    this.color = `hsl(${Math.random() * 60 + 330}, 100%, 50%)`;
    this.life = 100;
  }

  update() {
    this.x += this.speedX;
    this.y += this.speedY;
    this.life--;
  }

  draw() {
    ctx.save();
    ctx.globalAlpha = this.life / 100;
    ctx.fillStyle = this.color;
    ctx.beginPath();
    ctx.moveTo(this.x, this.y);
    ctx.bezierCurveTo(this.x, this.y - this.size/2, this.x + this.size, this.y - this.size/2, this.x, this.y + this.size);
    ctx.bezierCurveTo(this.x - this.size, this.y - this.size/2, this.x, this.y - this.size/2, this.x, this.y);
    ctx.fill();
    ctx.restore();
  }
}

const hearts = [];
canvas.addEventListener('mousemove', (e) => {
  for(let i = 0; i < 5; i++) {
    hearts.push(new HeartParticle(e.clientX, e.clientY));
  }
});

function animateHearts() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);

  for(let i = 0; i < hearts.length; i++) {
    hearts[i].update();
    hearts[i].draw();

    if(hearts[i].life <= 0) {
      hearts.splice(i, 1);
      i--;
    }
  }

  requestAnimationFrame(animateHearts);
}

animateHearts();

文字浪漫特效

实现文字逐字打印效果,配合打字音效和光标闪烁,适合用于表白或浪漫信息的展示。

const romanticText = "Every moment with you feels like a beautiful dream...";
const textElement = document.getElementById('romanticText');
let i = 0;

function typeWriter() {
  if (i < romanticText.length) {
    textElement.innerHTML += romanticText.charAt(i);
    i++;
    setTimeout(typeWriter, Math.random() * 100 + 50);

    // 添加光标效果
    const cursor = document.createElement('span');
    cursor.className = 'blinking-cursor';
    cursor.textContent = '|';
    textElement.appendChild(cursor);

    // 移除上一个光标
    const prevCursor = textElement.querySelector('.blinking-cursor:not(:last-child)');
    if(prevCursor) prevCursor.remove();
  }
}

// CSS样式
.blinking-cursor {
  animation: blink 1s infinite;
  color: #ff3366;
}

@keyframes blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0; }
}

背景音乐控制

添加浪漫背景音乐,并提供播放控制按钮,增强整体氛围。

const bgMusic = new Audio('romantic_music.mp3');
bgMusic.loop = true;

document.getElementById('playBtn').addEventListener('click', () => {
  bgMusic.play();
  document.getElementById('musicControls').style.display = 'none';
});

// 音量控制
document.getElementById('volume').addEventListener('input', (e) => {
  bgMusic.volume = e.target.value;
});

3D爱心渲染

使用Three.js创建3D旋转爱心,可添加纹理和光影效果。

js实现浪漫

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({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

// 创建爱心几何体
const heartShape = new THREE.Shape();
heartShape.moveTo(0, 0);
heartShape.bezierCurveTo(0, 0, -2, 2, -3, 0);
heartShape.bezierCurveTo(-5, -3, 0, -5, 0, -2);
heartShape.bezierCurveTo(0, -5, 5, -3, 3, 0);
heartShape.bezierCurveTo(2, 2, 0, 0, 0, 0);

const extrudeSettings = {
  depth: 0.5,
  bevelEnabled: true,
  bevelSegments: 2,
  steps: 2,
  bevelSize: 0.3,
  bevelThickness: 0.2
};

const geometry = new THREE.ExtrudeGeometry(heartShape, extrudeSettings);
const material = new THREE.MeshPhongMaterial({ 
  color: 0xff3366,
  specular: 0x111111,
  shininess: 30
});
const heart = new THREE.Mesh(geometry, material);
scene.add(heart);

// 添加光源
const light = new THREE.DirectionalLight(0xffffff, 1);
light.position.set(1, 1, 1);
scene.add(light);

camera.position.z = 5;

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

animate();

标签: 浪漫js
分享给朋友:

相关文章

js 进度条的实现

js 进度条的实现

使用 HTML 和 CSS 创建基础进度条 HTML 结构可以简单使用一个 div 元素作为容器,内部嵌套另一个 div 表示进度: <div class="progress-containe…

js实现vr

js实现vr

使用WebXR API实现VR体验 WebXR是浏览器中实现VR和AR体验的标准API,它取代了早期的WebVR。现代浏览器如Chrome、Edge和Firefox已支持WebXR。 // 初始化W…

js实现防洪

js实现防洪

防抖(Debounce)实现 防抖的核心思想是在事件触发后延迟执行回调函数,若在延迟时间内再次触发事件,则重新计时。适用于输入框搜索、窗口调整等场景。 function debounce(func,…

利用js实现

利用js实现

使用 JavaScript 实现 在 JavaScript 中,可以通过多种方式实现功能,具体取决于需求。以下是几种常见的方法: 方法一:使用原生 JavaScript // 示例代码 funct…

vue实现js休眠

vue实现js休眠

实现 JavaScript 休眠的方法 在 Vue 中实现 JavaScript 休眠(延迟执行)可以通过以下方式实现。由于 JavaScript 本身没有内置的 sleep 函数,通常使用 Prom…

节流js实现

节流js实现

节流(Throttle)的实现原理 节流是一种限制函数执行频率的技术,确保函数在一定时间间隔内最多执行一次。适用于高频触发事件(如滚动、输入、窗口调整等)的场景。 基础实现方式 使用时间戳判断是否执…