当前位置:首页 > JavaScript

js实现盖章

2026-04-06 22:01:13JavaScript

实现盖章效果的JavaScript方法

使用Canvas绘制印章

创建Canvas元素并通过JavaScript绘制印章图形和文字。以下是一个基础实现示例:

js实现盖章

// 创建Canvas元素
const canvas = document.createElement('canvas');
canvas.width = 200;
canvas.height = 200;
document.body.appendChild(canvas);

const ctx = canvas.getContext('2d');
ctx.fillStyle = 'red';

// 绘制圆形印章
ctx.beginPath();
ctx.arc(100, 100, 80, 0, Math.PI * 2);
ctx.lineWidth = 3;
ctx.stroke();

// 添加印章文字
ctx.font = '16px Arial';
ctx.textAlign = 'center';
ctx.fillText('专用章', 100, 100);

// 添加五角星
drawStar(ctx, 100, 100, 20, 5, 0);
function drawStar(ctx, cx, cy, spikes, outerRadius, innerRadius) {
    let rot = Math.PI/2*3;
    let x = cx;
    let y = cy;
    let step = Math.PI/spikes;

    ctx.beginPath();
    ctx.moveTo(cx, cy-outerRadius);
    for(let i=0; i<spikes; i++){
        x = cx + Math.cos(rot)*outerRadius;
        y = cy + Math.sin(rot)*outerRadius;
        ctx.lineTo(x,y);
        rot += step;

        x = cx + Math.cos(rot)*innerRadius;
        y = cy + Math.sin(rot)*innerRadius;
        ctx.lineTo(x,y);
        rot += step;
    }
    ctx.lineTo(cx, cy-outerRadius);
    ctx.closePath();
    ctx.fill();
}

使用CSS和HTML模拟印章

通过CSS创建印章视觉效果:

js实现盖章

<div class="stamp">
    <div class="stamp-circle"></div>
    <div class="stamp-text">公司公章</div>
    <div class="stamp-star">★</div>
</div>

<style>
.stamp {
    position: relative;
    width: 200px;
    height: 200px;
}

.stamp-circle {
    position: absolute;
    width: 180px;
    height: 180px;
    border: 3px solid red;
    border-radius: 50%;
    top: 10px;
    left: 10px;
}

.stamp-text {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    color: red;
    font-size: 24px;
    font-weight: bold;
}

.stamp-star {
    position: absolute;
    top: 40%;
    left: 50%;
    transform: translate(-50%, -50%);
    color: red;
    font-size: 30px;
}
</style>

使用SVG创建矢量印章

SVG实现可缩放的印章效果:

<svg width="200" height="200" viewBox="0 0 200 200">
    <circle cx="100" cy="100" r="80" fill="none" stroke="red" stroke-width="3"/>
    <text x="100" y="100" text-anchor="middle" fill="red" font-size="16" font-family="Arial">专用章</text>
    <path d="M100,30 L113,65 L150,65 L123,85 L135,120 L100,100 L65,120 L77,85 L50,65 L87,65 Z" fill="red"/>
</svg>

实现拖放盖章功能

结合以上方法实现可拖放的印章效果:

// 创建可拖动的印章
const stamp = document.createElement('div');
stamp.innerHTML = '<svg width="100" height="100" viewBox="0 0 200 200"><circle cx="100" cy="100" r="80" fill="none" stroke="red" stroke-width="3"/><text x="100" y="100" text-anchor="middle" fill="red" font-size="16" font-family="Arial">专用章</text><path d="M100,30 L113,65 L150,65 L123,85 L135,120 L100,100 L65,120 L77,85 L50,65 L87,65 Z" fill="red"/></svg>';
stamp.style.position = 'absolute';
stamp.style.cursor = 'move';
document.body.appendChild(stamp);

let isDragging = false;
let offsetX, offsetY;

stamp.addEventListener('mousedown', (e) => {
    isDragging = true;
    offsetX = e.clientX - stamp.getBoundingClientRect().left;
    offsetY = e.clientY - stamp.getBoundingClientRect().top;
    stamp.style.opacity = '0.7';
});

document.addEventListener('mousemove', (e) => {
    if (!isDragging) return;
    stamp.style.left = (e.clientX - offsetX) + 'px';
    stamp.style.top = (e.clientY - offsetY) + 'px';
});

document.addEventListener('mouseup', () => {
    isDragging = false;
    stamp.style.opacity = '1';
});

这些方法可以根据具体需求进行调整和组合,实现不同风格的印章效果。Canvas适合需要动态生成印章的场景,SVG适合需要矢量缩放的情况,而纯CSS方案则更简单轻量。

标签: js
分享给朋友:

相关文章

js实现拖拽

js实现拖拽

实现拖拽的基本步骤 通过监听鼠标事件(mousedown、mousemove、mouseup)实现拖拽功能。以下是核心代码逻辑: const draggableElement = document.…

js分组实现

js分组实现

分组实现方法 在JavaScript中,可以通过多种方式实现数组或对象的分组操作。以下是几种常见的方法: 使用Array.prototype.reduce() 通过reduce方法可以轻松实现数组分…

js实现论坛

js实现论坛

实现论坛的基本功能 使用JavaScript实现一个论坛需要结合前端和后端技术。前端可以使用React、Vue或Angular等框架,后端可以选择Node.js配合Express或Koa框架。数据库可…

js实现二叉树

js实现二叉树

二叉树的基本概念 二叉树是一种树形数据结构,每个节点最多有两个子节点,分别称为左子节点和右子节点。二叉树的常见操作包括插入、删除、遍历等。 二叉树的节点定义 在JavaScript中,二叉树的节点可…

链表实现js

链表实现js

链表的基本概念 链表是一种线性数据结构,由一系列节点组成,每个节点包含数据和一个指向下一个节点的指针(单向链表)或两个指针(双向链表)。与数组不同,链表在内存中非连续存储,插入和删除操作更高效,但随机…

实现继承js

实现继承js

实现继承的方式 在JavaScript中,实现继承可以通过多种方式完成,每种方式都有其优缺点。以下是几种常见的实现继承的方法: 原型链继承 原型链继承是最基本的继承方式,通过将子类的原型指向父类的实…