当前位置:首页 > JavaScript

js实现心形线

2026-04-05 07:12:00JavaScript

使用 Canvas 绘制心形线

通过 HTML5 Canvas 和 JavaScript 可以绘制心形线。以下是基于极坐标方程的实现代码:

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

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

  // 心形线参数方程 (极坐标)
  for (let theta = 0; theta < 2 * Math.PI; theta += 0.01) {
    const r = 16 * Math.pow(Math.sin(theta), 3); // 心形线方程
    const x = canvas.width/2 + r * Math.cos(theta) * 10;
    const y = canvas.height/2 - r * Math.sin(theta) * 10;

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

  ctx.fillStyle = 'red';
  ctx.fill();
}

drawHeart();

使用 SVG 绘制心形线

通过 SVG 路径命令可以创建更精确的心形图形:

const svg = document.getElementById('heartSvg');
const path = document.createElementNS("http://www.w3.org/2000/svg", "path");

// 贝塞尔曲线绘制心形
path.setAttribute('d', `
  M 100,100
  C 100,50 25,25 100,150
  C 175,25 100,50 100,100
  Z
`);
path.setAttribute('fill', 'red');
svg.appendChild(path);

使用 CSS 绘制心形

纯 CSS 实现的心形效果(非数学精确曲线):

.heart {
  width: 100px;
  height: 100px;
  background-color: red;
  transform: rotate(45deg);
  position: relative;
}
.heart:before, .heart:after {
  content: '';
  width: 100px;
  height: 100px;
  background-color: red;
  border-radius: 50%;
  position: absolute;
}
.heart:before {
  top: -50px;
  left: 0;
}
.heart:after {
  top: 0;
  left: -50px;
}

参数方程说明

心形线的数学表达式(极坐标): $$ r = a(1 - \sin\theta) $$

笛卡尔坐标系下的参数方程: $$ x = a(2\cos t - \cos 2t) $$ $$ y = a(2\sin t - \sin 2t) $$

其中参数 $a$ 控制心形大小,$t$ 是角度参数(通常 $0 \leq t \leq 2\pi$)。

动画效果实现

为心形线添加跳动动画:

js实现心形线

let scale = 1;
let growing = true;

function animateHeart() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  ctx.save();
  ctx.translate(canvas.width/2, canvas.height/2);

  // 缩放动画
  scale = growing ? scale + 0.01 : scale - 0.01;
  if (scale > 1.2) growing = false;
  if (scale < 0.8) growing = true;
  ctx.scale(scale, scale);

  // 绘制心形
  ctx.beginPath();
  for (let theta = 0; theta < 2 * Math.PI; theta += 0.01) {
    const r = 16 * Math.pow(Math.sin(theta), 3);
    const x = r * Math.cos(theta) * 5;
    const y = -r * Math.sin(theta) * 5;
    ctx.lineTo(x, y);
  }
  ctx.fillStyle = 'red';
  ctx.fill();
  ctx.restore();

  requestAnimationFrame(animateHeart);
}

animateHeart();

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

相关文章

js 实现vue

js 实现vue

实现 Vue 的核心功能 在 JavaScript 中实现 Vue 的核心功能需要模拟数据绑定、虚拟 DOM 和响应式系统。以下是一个简化版的实现思路: 响应式系统 通过 Object.define…

js实现类

js实现类

JavaScript 实现类的几种方式 JavaScript 是一种基于原型的语言,但在 ES6 之后引入了 class 语法糖,使得实现类更加直观。以下是几种常见的实现方式: 使用构造函数 传统的…

js实现拷贝

js实现拷贝

实现文本拷贝 使用 document.execCommand 方法(已废弃但兼容性较好): function copyText(text) { const textarea = document…

js实现瀑布流

js实现瀑布流

实现瀑布流布局 瀑布流布局是一种常见的网页布局方式,常用于图片展示、商品列表等场景。以下是使用 JavaScript 实现瀑布流布局的几种方法。 纯 JavaScript 实现 通过计算元素的位置和…

js手势实现

js手势实现

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

js实现授权

js实现授权

授权流程设计 授权流程通常涉及前端与后端的交互,常见方案包括OAuth2.0、JWT等。以JWT为例的典型流程: 用户提交凭证(如用户名密码)到认证服务 服务端验证通过后生成包含用户信息的J…