前端H5用Canvas画布做类似银行签名的操作

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>签名页面</title>
</head>
<body>
<canvas id="signatureCanvas" width="400" height="200"></canvas>
<button id="clearButton">清除签名</button>
<button id="saveButton">保存签名</button>
<img id="signatureImage" src="" alt="签名预览">
<script src="js/signatureCanvas.js"></script>
</body>
</html>
// 获取Canvas元素
const canvas = document.getElementById("signatureCanvas");
const ctx = canvas.getContext("2d");

// 设置绘制参数
ctx.strokeStyle = "#000"; // 线条颜色
ctx.lineWidth = 2; // 线条宽度

// 标记签名是否开始
let drawing = false;

// 监听鼠标按下事件
canvas.addEventListener("mousedown", (e) => {
    drawing = true;
    ctx.beginPath();
    ctx.moveTo(e.clientX - canvas.getBoundingClientRect().left, e.clientY - canvas.getBoundingClientRect().top);
});

// 监听鼠标移动事件
canvas.addEventListener("mousemove", (e) => {
    if (drawing) {
        ctx.lineTo(e.clientX - canvas.getBoundingClientRect().left, e.clientY - canvas.getBoundingClientRect().top);
        ctx.stroke();
    }
});

// 监听鼠标松开事件
canvas.addEventListener("mouseup", () => {
    drawing = false;
});

// 清除签名
document.getElementById("clearButton").addEventListener("click", () => {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    document.getElementById("signatureImage").src = "";
});

// 保存签名
document.getElementById("saveButton").addEventListener("click", () => {
    const signatureImage = document.getElementById("signatureImage");
    signatureImage.src = canvas.toDataURL("image/png");
});

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下使用 Canvas 绘制渐变色立方体的示例代码: ```html <!DOCTYPE html> <html> <head> <title>Canvas Gradient Cube</title> <style> canvas { border: 1px solid #000; } </style> </head> <body> <canvas id="myCanvas" width="400" height="400"></canvas> <script> var canvas = document.getElementById("myCanvas"); var context = canvas.getContext("2d"); // 绘制立方体的六个面 drawFace(100, 100, 100, 200, 200, 200, "#ff0000", "#00ff00", "#0000ff"); // 前面 drawFace(0, 100, 100, 100, 200, 200, "#0000ff", "#00ff00", "#ff0000"); // 左面 drawFace(200, 100, 100, 300, 200, 200, "#00ff00", "#0000ff", "#ff0000"); // 右面 drawFace(100, 0, 100, 200, 100, 200, "#ff0000", "#0000ff", "#00ff00"); // 上面 drawFace(100, 200, 100, 200, 300, 200, "#ff0000", "#00ff00", "#0000ff"); // 下面 drawFace(100, 100, 0, 200, 200, 100, "#00ff00", "#ff0000", "#0000ff"); // 后面 // 绘制一个立方体面 function drawFace(x1, y1, z1, x2, y2, z2, color1, color2, color3) { // 计算面的中心点 var centerX = (x1 + x2) / 2; var centerY = (y1 + y2) / 2; var centerZ = (z1 + z2) / 2; // 开始绘制面 context.beginPath(); // 绘制面的左下角点 context.moveTo(projectX(x1, z1), projectY(y1, z1)); // 绘制面的其它三个点 context.lineTo(projectX(x2, z1), projectY(y1, z1)); context.lineTo(projectX(x2, z2), projectY(y2, z2)); context.lineTo(projectX(x1, z2), projectY(y2, z2)); // 使用渐变色填充面 var gradient = context.createLinearGradient(0, 0, 0, canvas.height); gradient.addColorStop(0, color1); gradient.addColorStop(0.5, color2); gradient.addColorStop(1, color3); context.fillStyle = gradient; context.fill(); // 结束绘制面 context.closePath(); } // 将 3D 坐标投影到 2D 平面上 function projectX(x, z) { return canvas.width / 2 + (x - canvas.width / 2) * (200 / (200 - z)); } function projectY(y, z) { return canvas.height / 2 - (y - canvas.height / 2) * (200 / (200 - z)); } </script> </body> </html> ``` 上述代码使用了三个颜色值,可以根据需要进行调整。同时,也可以根据需要修改立方体的大小和位置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值