当前位置:首页 > JavaScript

js实现心形线

2026-03-02 06:11:40JavaScript

实现心形线的 JavaScript 方法

心形线(Cardioid)是一种极坐标方程曲线,可以通过以下数学公式描述:

  • 极坐标方程:$r = a(1 + \cos\theta)$
  • 参数方程:
    $x = a(2\cos t - \cos 2t)$
    $y = a(2\sin t - \sin 2t)$

使用 Canvas 绘制心形线

const canvas = document.createElement('canvas');
document.body.appendChild(canvas);
const ctx = canvas.getContext('2d');
canvas.width = 400;
canvas.height = 400;

// 心形线参数方程
function cardioid(t, scale) {
  return {
    x: scale * (16 * Math.pow(Math.sin(t), 3)),
    y: scale * (13 * Math.cos(t) - 5 * Math.cos(2*t) - 2 * Math.cos(3*t) - Math.cos(4*t))
  };
}

// 绘制心形
ctx.beginPath();
ctx.strokeStyle = 'red';
ctx.fillStyle = 'pink';

for (let t = 0; t < 2 * Math.PI; t += 0.01) {
  const point = cardioid(t, 10);
  const x = point.x + canvas.width / 2;
  const y = -point.y + canvas.height / 2; // 反转Y轴

  if (t === 0) {
    ctx.moveTo(x, y);
  } else {
    ctx.lineTo(x, y);
  }
}

ctx.closePath();
ctx.fill();
ctx.stroke();

使用 SVG 绘制心形线

const svgNS = "http://www.w3.org/2000/svg";
const svg = document.createElementNS(svgNS, "svg");
svg.setAttribute("width", "400");
svg.setAttribute("height", "400");
document.body.appendChild(svg);

let pathData = "";
for (let t = 0; t <= 2 * Math.PI; t += 0.01) {
  const x = 200 + 16 * Math.pow(Math.sin(t), 3) * 10;
  const y = 200 - (13 * Math.cos(t) - 5 * Math.cos(2*t) - 2 * Math.cos(3*t) - Math.cos(4*t)) * 10;

  if (t === 0) {
    pathData += `M ${x} ${y}`;
  } else {
    pathData += ` L ${x} ${y}`;
  }
}

const path = document.createElementNS(svgNS, "path");
path.setAttribute("d", pathData);
path.setAttribute("fill", "pink");
path.setAttribute("stroke", "red");
svg.appendChild(path);

使用 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();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

// 创建心形几何体
const heartShape = new THREE.Shape();
heartShape.moveTo(0, 0);
for (let t = 0; t < 2 * Math.PI; t += 0.01) {
  const x = 16 * Math.pow(Math.sin(t), 3);
  const y = 13 * Math.cos(t) - 5 * Math.cos(2*t) - 2 * Math.cos(3*t) - Math.cos(4*t);
  heartShape.lineTo(x, y);
}

const geometry = new THREE.ExtrudeGeometry(heartShape, {
  depth: 1,
  bevelEnabled: true,
  bevelSize: 0.5
});
const material = new THREE.MeshPhongMaterial({ color: 0xff69b4 });
const heart = new THREE.Mesh(geometry, material);
scene.add(heart);

camera.position.z = 30;

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

数学公式变体

经典心形线公式: $r = 1 - \cos\theta$
或参数方程:
$x = \cos t \cdot (1 + \cos t)$
$y = \sin t \cdot (1 + \cos t)$

更复杂的心形公式(如上文代码所用): $x = 16\sin^3 t$
$y = 13\cos t - 5\cos 2t - 2\cos 3t - \cos 4t$

js实现心形线

标签: js心形线
分享给朋友:

相关文章

js实现跳转

js实现跳转

使用 window.location 跳转 通过修改 window.location.href 或直接使用 window.location 实现页面跳转,适用于普通跳转或带参数的 URL。 // 方…

js实现

js实现

实现 JavaScript 功能的方法 在 JavaScript 中实现特定功能通常涉及多个步骤。以下是常见的实现方法: 基础语法和变量声明 使用 let 或 const 声明变量: let co…

js实现打印

js实现打印

使用window.print()方法实现打印 在JavaScript中,可以通过调用window.print()方法直接触发浏览器的打印功能。这会打开系统的打印对话框,用户可以选择打印机和设置打印选项…

js实现图片上传

js实现图片上传

图片上传的基本实现 使用HTML的<input type="file">元素配合JavaScript的File API可以实现图片上传功能。HTML部分需要创建一个文件选择输入框和一个用于…

js实现pdf在线预览

js实现pdf在线预览

使用PDF.js实现PDF在线预览 PDF.js是由Mozilla开发的一个开源JavaScript库,可以在网页中直接渲染PDF文件。以下是实现PDF在线预览的步骤: 引入PDF.js库 在H…

js 实现分页

js 实现分页

实现分页的基本逻辑 分页功能通常需要后端返回数据总量或总页数,前端根据当前页码和每页条数截取对应数据。以下是一个基于JavaScript的简单分页实现方案: 前端分页实现 假设已有从后端获取的完整…