当前位置:首页 > JavaScript

js实现剪纸

2026-03-14 01:46:13JavaScript

使用Canvas实现剪纸效果

在JavaScript中实现剪纸效果可以通过Canvas API完成。以下是核心实现方法:

创建Canvas元素并获取上下文

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

绘制剪纸基础形状

ctx.fillStyle = 'red';
ctx.beginPath();
ctx.arc(400, 300, 200, 0, Math.PI * 2);
ctx.fill();

添加剪纸镂空效果

ctx.globalCompositeOperation = 'destination-out';
ctx.beginPath();
ctx.moveTo(400, 100);
ctx.lineTo(500, 500);
ctx.lineTo(300, 500);
ctx.closePath();
ctx.fill();

使用SVG实现剪纸效果

SVG更适合创建矢量剪纸图案:

创建SVG元素

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

添加剪纸路径

js实现剪纸

const path = document.createElementNS("http://www.w3.org/2000/svg", "path");
path.setAttribute('d', 'M400,300 a200,200 0 1,0 0.1,0 z');
path.setAttribute('fill', 'red');
svg.appendChild(path);

添加镂空效果

const hole = document.createElementNS("http://www.w3.org/2000/svg", "path");
hole.setAttribute('d', 'M400,100 L500,500 L300,500 Z');
hole.setAttribute('fill', 'white');
svg.appendChild(hole);

使用CSS Clip-path实现简单剪纸

对于简单的剪纸效果,CSS也能实现:

HTML结构

<div class="paper-cut"></div>

CSS样式

js实现剪纸

.paper-cut {
  width: 400px;
  height: 400px;
  background-color: red;
  clip-path: polygon(
    50% 0%, 
    80% 100%, 
    20% 100%
  );
}

复杂剪纸图案生成算法

对于需要程序化生成的复杂剪纸图案:

生成随机剪纸图案

function generateRandomPaperCut(ctx) {
  const centerX = 400;
  const centerY = 300;
  const radius = 200;
  const points = 12;

  ctx.beginPath();
  for(let i = 0; i < points; i++) {
    const angle = (i / points) * Math.PI * 2;
    const randomRadius = radius * (0.7 + Math.random() * 0.3);
    const x = centerX + Math.cos(angle) * randomRadius;
    const y = centerY + Math.sin(angle) * randomRadius;

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

动画剪纸效果

为剪纸添加动画效果:

旋转剪纸动画

let angle = 0;
function animate() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  ctx.save();
  ctx.translate(400, 300);
  ctx.rotate(angle);
  ctx.translate(-400, -300);

  // 绘制剪纸
  ctx.fillStyle = 'red';
  ctx.beginPath();
  ctx.arc(400, 300, 200, 0, Math.PI * 2);
  ctx.fill();

  ctx.globalCompositeOperation = 'destination-out';
  // 添加镂空图案
  ctx.restore();

  angle += 0.01;
  requestAnimationFrame(animate);
}
animate();

标签: 剪纸js
分享给朋友:

相关文章

vue实现js休眠

vue实现js休眠

Vue 中实现 JavaScript 休眠 在 Vue 中实现 JavaScript 休眠通常需要使用异步方式,以避免阻塞主线程。以下是几种常见方法: 使用 setTimeout 和 Pro…

js实现打印

js实现打印

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

js 实现继承

js 实现继承

原型链继承 通过让子类的原型对象指向父类的实例来实现继承。子类实例可以访问父类原型上的属性和方法。 function Parent() { this.name = 'parent'; } Par…

js实现瀑布流

js实现瀑布流

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

js实现驼峰

js实现驼峰

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

js实现游标

js实现游标

使用JavaScript实现游标 在JavaScript中,可以通过操作DOM元素的cursor样式属性来实现自定义游标效果。以下是几种常见的实现方法: 修改默认鼠标指针样式 通过CSS的curso…