当前位置:首页 > 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)$

js实现心形线

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

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

相关文章

js实现驼峰

js实现驼峰

实现驼峰命名的几种方法 使用正则表达式和字符串替换 通过正则表达式匹配字符串中的特定模式(如下划线或短横线),并将其后的字母转换为大写,同时移除分隔符。 function toCamelCase(s…

js手势实现

js手势实现

手势识别实现方法 在JavaScript中实现手势识别通常涉及监听触摸事件(touchstart、touchmove、touchend)或鼠标事件(mousedown、mousemove、mouseu…

js实现图

js实现图

JavaScript 实现图的常用方法 在 JavaScript 中,图(Graph)可以通过多种方式实现,常见的包括邻接表(Adjacency List)和邻接矩阵(Adjacency Matrix…

js实现吸色

js实现吸色

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

js怎么实现异步

js怎么实现异步

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

js图片轮播的实现

js图片轮播的实现

基础图片轮播实现 使用HTML、CSS和JavaScript实现一个简单的图片轮播效果。HTML部分定义轮播容器和图片,CSS负责样式和过渡效果,JavaScript处理逻辑。 <div cl…