canvas 2d 目前支持预览,不支持真机调试

index.wxml

<canvas type="2d" id="canvas" bindtouchmove="move" bindtouchstart="start"  binderror="error" style="width:{{width}}px;height:{{height}}px;"></canvas>
<view class="btn">
<button bindtap="clearClick">重签</button>
<button bindtap="saveClick">完成签名</button>
</view>

index.wxss

page {
background-color: #e9f2f1;
}
.btn {
display: flex;
flex-direction: row;
justify-self: baseline;
margin: 15rpx; }
.btn button:first-child {
color: #3fa89a;
}
.btn button:last-child {
color: white;
background-color: #3fa89a;
}
button {
width: 200rpx;
border-radius: 5rpx;
box-shadow: 0px 0px 1px 1px #c5c5c5;
}
canvas {
background-color: white;
}

index.json

{
"usingComponents": {},
"pageOrientation": "landscape",
"navigationBarBackgroundColor":"#3fa89a",
"navigationBarTextStyle":"white",
"navigationBarTitleText":"手写签名"
}

index.js

const app = getApp()
Page({
data: {
canvas: '',
ctx: '',
pr:0,
width: 0,
height: 0,
first:true,
},
start(e) {
if(this.data.first){
this.clearClick();
this.setData({first:false})
}
this.data.ctx.beginPath(); // 开始创建一个路径,如果不调用该方法,最后无法清除画布
this.data.ctx.moveTo(e.changedTouches[0].x, e.changedTouches[0].y) // 把路径移动到画布中的指定点,不创建线条。用 stroke 方法来画线条
},
move(e) {
this.data.ctx.lineTo(e.changedTouches[0].x, e.changedTouches[0].y) // 增加一个新点,然后创建一条从上次指定点到目标点的线。用 stroke 方法来画线条
this.data.ctx.stroke()
},
error: function (e) {
console.log("画布触摸错误" + e);
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function () {
this.getSystemInfo()
this.createCanvas()
},
/**
* 初始化
*/
createCanvas() {
const pr = this.data.pr; // 像素比
const query = wx.createSelectorQuery();
query.select('#canvas').fields({ node: true, size: true }).exec((res) => {
const canvas = res[0].node;
const ctx = canvas.getContext('2d');
canvas.width = this.data.width*pr; // 画布宽度
canvas.height = this.data.height*pr; // 画布高度
ctx.scale(pr,pr); // 缩放比
ctx.lineGap = 'round';
ctx.lineJoin = 'round';
ctx.lineWidth = 4; // 字体粗细
ctx.font = '40px Arial'; // 字体大小,
ctx.fillStyle = '#ecf0ef'; // 填充颜色
ctx.fillText('签名区', res[0].width / 2 - 60, res[0].height / 2)
this.setData({ ctx, canvas })
})
},
// 获取系统信息
getSystemInfo() {
let that = this;
wx.getSystemInfo({
success(res) {
that.setData({
pr:res.pixelRatio,
width: res.windowWidth,
height: res.windowHeight - 75,
})
}
})
},
clearClick: function () {
//清除画布
this.data.ctx.clearRect(0, 0, this.data.width, this.data.height);
},
//保存图片
saveClick: function () {
wx.canvasToTempFilePath({
x: 0,
y: 0,
width: this.data.width,
height: this.data.height,
destWidth:this.data.width*this.data.pr,
destHeight: this.data.height*this.data.pr,
canvasId: 'canvas',
canvas: this.data.canvas,
fileType: 'png',
success(res) {
wx.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success(res) {
wx.showToast({
title: '保存成功',
icon: 'success'
})
}
})
}
})
}
})

 效果图

签名效果

PNG图

代码地址

 
 

微信小程序 canvas 手写签名(2d)的更多相关文章

  1. 微信小程序:手写日历组件

    一.前言 最近公司要做一个酒店入住的小程序,不可避免的一定会使用到日历,而小程序没有内置的日历组件.在网上看了一下也没有非常适合需求的日历,于是自己写了一个. 二.代码 1. 原理分析 写一个日历只需 ...

  2. 【微信小程序】手写索引选择器(城市列表,汽车品牌选择列表)

    摘要: 小程序索引选择器,点击跳转相应条目,索引可滑动,滑动也可跳转 场景:城市选择列表, 汽车品牌选择列表 所用组件: scroll-view(小程序原生) https://developers.w ...

  3. 技术博客--微信小程序canvas实现图片编辑

    技术博客--微信小程序canvas实现图片编辑 我们的这个小程序不仅仅是想给用户提供一个保存和查找的平台,还希望能给用户一个展示自己创意的舞台,因此我们实现了图片的编辑部分.我们对对图片的编辑集成了很 ...

  4. 原创:WeZRender:微信小程序Canvas增强组件

    WeZRender是一个微信小程序Canvas增强组件,基于HTML5 Canvas类库ZRender. 使用 WXML: <canvas style="width: 375px; h ...

  5. 微信小程序-canvas绘制文字实现自动换行

    在使用微信小程序canvas绘制文字时,时常会遇到这样的问题:因为canvasContext.fillText参数为 我们只能设置文本的最大宽度,这就产生一定的了问题.如果我们绘制的文本长度不确定或者 ...

  6. 微信小程序 canvas 字体自动换行(支持换行符)

    微信小程序 canvas 自动适配 自动换行,保存图片分享到朋友圈  https://github.com/richard1015/News 微信IDE演示代码https://developers.w ...

  7. 微信小程序--canvas画布实现图片的编辑

    技术:微信小程序   概述 上传图片,编辑图片大小,添加文字,改变文字颜色等 详细 代码下载:http://www.demodashi.com/demo/14789.html 概述 微信小程序--ca ...

  8. 微信小程序canvas生成并保存图片

    ---恢复内容开始--- 微信小程序canvas生成并保存图片,具体实现效果如下图     实现效果需要做以下几步工作 一.先获取用户屏幕大小,然后才能根据屏幕大小来定义canvas的大小 二.获取图 ...

  9. 微信小程序 - canvas实现多行文本 ,实现文本断行

    1.canvas绘制文本坑点 绘制的文本不管多长,永远只有一行,不会断行. 2.解决思路 根据每行文本字数来断行,超出的就向下排列. 由于 canvas绘制文本的语法如下: context.fillT ...

  10. 【微信小程序canvas】实现小程序手写板用户签名(附代码)

    工作中公司业务需要的微信小程序用户签字功能 先看效果图: wxml: <view class="wrapper"> <view class="handB ...

随机推荐

  1. sql 语句系列(字符串之父与子之间)[八百章之第十二章]

    前言 介绍字符串和其子字符串直接的使用. 判断含有子字母的字符串 select * from emp 在mysql中: select emp.ename from emp where emp.enam ...

  2. web开发可不可以是这样的?

    service不外乎就是数据校验,调用其它service,调用第三方api,读写数据库,既然这样,那我认为Service也可以做成可配置化的样子,配置项大致有 所需参数配置:参数列表,参数类型,参数长 ...

  3. 10个常用的JS工具库,80%的项目都在用

    高手区别于普通人的重要一点是,他们善于利用工具,把更多的时间留给了规划和思考.写代码也是同样的道理,工具用好了,你就有更多的时间来规划架构和攻克难点.今天就给大家分享一下当前最流行的 js 工具库,如 ...

  4. 鸿蒙HarmonyOS实战-ArkUI组件(Canvas)

    一.Canvas Canvas组件是一种图形渲染组件,它提供了一个画布(canvas),开发者可以在上面绘制各种图形.文本等.Canvas组件通常用于创建游戏.数据可视化等需要动态绘制图形的应用程序. ...

  5. 使用input标签的时候报错,提示Form elements must have labels: Element has no title attribute Element has no placeholder attribute

    使用input标签的时候报错,提示Form elements must have labels: Element has no title attribute Element has no place ...

  6. 力扣58(java)-最后一个单词的长度(简单)

    题目: 给你一个字符串 s,由若干单词组成,单词前后用一些空格字符隔开.返回字符串中 最后一个 单词的长度. 单词 是指仅由字母组成.不包含任何空格字符的最大子字符串. 示例 1: 输入:s = &q ...

  7. 实时化或成必然趋势?新一代 Serverless 实时计算引擎

    作者:高旸(吾与),阿里巴巴高级产品专家 本文由阿里巴巴高级产品专家高旸(吾与)分享,主要介绍新一代Serverless实时计算引擎的产品特性及核心功能. 一.实时计算 Flink 版 – 产品定位与 ...

  8. [Contract] Solidity 合约发布到测试网 ropsten 的作用

    当我们本地完成了一系列测试以后,接下来就是准备上线了. 关于合约部署可以参考这篇:Solidity 合约使用 truffle 部署到测试网和主网 你可能有一个疑问,在上主网之前,先上测试网的作用是什么 ...

  9. WPF 对接 Vortice 调用 WIC 加载图片

    本文将告诉大家如何通过 Vortice 库从底层的方式使用 WIC 层加载本地图片文件,解码为 IWICBitmap 图片,然后将 IWICBitmap 图片交给 WPF 进行渲染 本文的前置博客:W ...

  10. PostMan接口测试实用小点

    PostMan接口测试实用小点 1. 接口测试变量存取操作 在Postman中有很多地方可以存储一些变量,这里只介绍经常使用的环境变量.变量设置后,在UI界面可以通过{{变量名}}获取到对应值. 在环 ...