当前位置:网站首页>uniapp H5 签名横版生成图片
uniapp H5 签名横版生成图片
2022-08-08 05:40:00 【玖伍玖伍】
需求 在线签名生成图片
保存生成base64图片。详细代码
<template>
<view>
<view class="main-content" @touchmove.stop.prevent="">
<!-- 签字canvas -->
<canvas class="mycanvas" id="mycanvas" canvas-id="mycanvas" @touchstart="touchstart" @touchmove="touchmove" @touchend="touchend"></canvas>
<canvas class="mycanvas" :style="{ 'z-index': -1, width: `${screenWidth}px`, height: `${(screenWidth * screenWidth) / screenHeight}px` }" id="rotatCanvas" canvas-id="rotatCanvas"></canvas>
<cover-view class="button-line">
<cover-view class="save-button" @tap="finish">保存</cover-view>
<cover-view class="clear-button" @tap="clear">清除</cover-view>
</cover-view>
</view>
</view>
</template>
<script> export default {
data() {
return {
ctx: '', //绘图图像 points: [], //路径点集合 screenWidth: '', screenHeight: '', img: '', }; }, mounted() {
this.createCanvas(); uni.getSystemInfo({
success: e => {
this.screenWidth = e.screenWidth; this.screenHeight = e.screenHeight; } }); }, methods: {
//创建并显示画布 createCanvas() {
this.showCanvas = true; this.ctx = uni.createCanvasContext('mycanvas', this); //创建绘图对象 //设置画笔样式 this.ctx.lineWidth = 2; this.ctx.lineCap = 'round'; this.ctx.lineJoin = 'round'; }, //触摸开始,获取到起点 touchstart(e) {
let startX = e.changedTouches[0].x; let startY = e.changedTouches[0].y; let startPoint = {
X: startX, Y: startY }; this.points.push(startPoint); //每次触摸开始,开启新的路径 this.ctx.beginPath(); }, //触摸移动,获取到路径点 touchmove(e) {
let moveX = e.changedTouches[0].x; let moveY = e.changedTouches[0].y; let movePoint = {
X: moveX, Y: moveY }; this.points.push(movePoint); //存点 let len = this.points.length; if (len >= 2) {
this.draw(); //绘制路径 } }, // 触摸结束,将未绘制的点清空防止对后续路径产生干扰 touchend() {
this.points = []; }, /* *********************************************** # 绘制笔迹 # 1.为保证笔迹实时显示,必须在移动的同时绘制笔迹 # 2.为保证笔迹连续,每次从路径集合中区两个点作为起点(moveTo)和终点(lineTo) # 3.将上一次的终点作为下一次绘制的起点(即清除第一个点) ************************************************ */ draw() {
let point1 = this.points[0]; let point2 = this.points[1]; this.points.shift(); this.ctx.moveTo(point1.X, point1.Y); this.ctx.lineTo(point2.X, point2.Y); this.ctx.stroke(); this.ctx.draw(true); }, //清空画布 clear() {
this.ctx.clearRect(0, 0, this.screenWidth, this.screenHeight); this.ctx.draw(true); }, //完成绘画并保存到本地 finish() {
uni.canvasToTempFilePath({
canvasId: 'mycanvas', success: res => {
this.rotat(res.tempFilePath); }, complete: com => {
} }, this ); }, // 将图片选装 rotat(e) {
let rotatCtx = uni.createCanvasContext('rotatCanvas', this); //创建绘图对象 // 重新定位中心点 rotatCtx.translate(0, (this.screenWidth * this.screenWidth) / this.screenHeight); // 将画布逆时针旋转90度 rotatCtx.rotate((270 * Math.PI) / 180); // 将签字图片绘制进入Canvas rotatCtx.drawImage(e, 0, 0, (this.screenWidth * this.screenWidth) / this.screenHeight, this.screenWidth); // 保存后旋转后的结果 rotatCtx.draw(true); setTimeout(() => {
// 生成图片并回调 uni.canvasToTempFilePath({
canvasId: 'rotatCanvas', success: val => {
console.log(val.tempFilePath) //base64图片地址 uni.setStorageSync('img', val.tempFilePath) uni.navigateBack() }, complete: com => {
// console.log(com); } }, ); }, 500); } } }; </script>
<style lang="scss" scoped> .main-content {
width: 100vw; height: 100vh; background-color: red; position: fixed; top: 0rpx; left: 0rpx; z-index: 9999; } .mycanvas {
width: 100vw; height: 100vh; background-color: #efefef; position: fixed; left: 0rpx; top: 0rpx; z-index: 2; } .button-line {
transform: rotate(90deg); position: fixed; bottom: 170rpx; left: -100rpx; width: 340rpx; height: 50rpx; display: flex; align-items: center; justify-content: space-between; z-index: 999; } .button-style {
color: #ffffff; width: 150rpx; height: 60rpx; text-align: center; line-height: 60rpx; border-radius: 10rpx; } .save-button {
@extend .button-style; background-color: #02b340; } .clear-button {
@extend .button-style; background-color: #ffa500; } .cancel-button {
@extend .button-style; background-color: #e10b2b; } </style>
边栏推荐
- 【Win10】Several sleep problems and countermeasures
- 0字典树/字符串中等 LeetCode676. 实现一个魔法字典
- Day7:面试必考选择题
- VSCode已经设置过为中文但变成英文的解决办法
- Checkerboard Coloring Problem
- 分页组件的使用
- 如何保存页面的当前的状态
- 说说Redis分布式锁的原理和实现蚂【蚁金服三面】
- Rust development - Struct usage example
- Talk about the principle and implementation of Redis distributed lock [Ant Financial Services Three Sides]
猜你喜欢

文件操作 - IO

一小时掌握vim基础用法

Day8:面试必考编程题(细心OJ)

Unity-CharacterController(角色控制器)

10 must-have free tools for self-media people to operate quickly and efficiently

Sqlmap + dnslog injection of repetition

Preprocessing Notes

Unity-CharacterController (Character Controller)

为什么互联网大厂一边疯狂裁员,一边不停招聘?

Leetcode sword 】 refers to the Offer (special commando) summary
随机推荐
说说Redis分布式锁的原理和实现蚂【蚁金服三面】
Unity鼠标光标使用学习
tracepoint: 定义函数及调用示例
数字IC设计中为什么要避免锁存器(Latches)
数据库分库分表,何时分?怎样分?
RecycleView配合Adapter调用notifyDataSetChanged闪屏?
Postman显示验证码图片(base64字符串)
断言失败——[UITableView _configureCellForDisplay: forIndexPath:]
sqlmap+dnslog注入复现
MySQL5
阿里云的数据库怎么提高访问速度的本地的打开的方式是www.zgysffm.com怎样的?
卷积神经网络 图像识别,卷积神经网络 图像处理
Filter 过滤器的使用
【js基础】闭包的几种情况(代码)
使用 Zap 和 W3af 进行 Web 应用程序漏洞评估
Leetcode78. Subset
Hard Disk Basics
Lecture 84 Biweekly t4 6144 and Lecture 305 t4 6138
clue binary tree
如何批量导入文件,并全部自定义重命名为相同文件名