国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

微信小程序canvas實現(xiàn)簡易手寫簽名版(uni-app)

這篇具有很好參考價值的文章主要介紹了微信小程序canvas實現(xiàn)簡易手寫簽名版(uni-app)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

微信小程序可以通過canvas實現(xiàn)手寫簽名的效果,本文中使用的是微信小程序Canvas 2D接口
本示例中繪制的是橫屏簽名的效果,效果圖如下:
微信小程序?qū)崿F(xiàn)手寫簽名,JS,微信小程序,小程序,前端
這里我們需要調(diào)整canvas的物理寬高,默認物理寬高為300*150px,物理寬高調(diào)整通過css樣式即可,本文中需要根據(jù)屏幕高度進行動態(tài)調(diào)整,使用的是行內(nèi)樣式
頁面布局:

<template>
	<view class="sign-page" :style="{paddingTop: top + 'px'}">
		<view class="canvas-box">
			<view class="left-pane">
				<view class="f28 text-gray6 left-text">請簽字確認</view>
				<view class="right-box">
					<view class="left-button" @click="clearContext">
						<text class="ic ic-delete text-gray6"></text>
						<text class="f30 text-gray6 ml5">清空</text>
					</view>
					<button class="right-button" @click="confirm">完成</button>
				</view>
			</view>
			<!-- canvas的物理寬高可通過樣式調(diào)整 -->
			<canvas
				class="canvas"
				disable-scroll 
				type="2d" 
				id="myCanvas"
				@touchstart="handleTouchstart"
				@touchmove="handleTouchmove"
				:style="{
					width: canvasWidth + 'px', 
					height: canvasHeight + 'px'}">
			</canvas>
			
			<view class="right-pane">
				<view class="dis-flex back-button" @click="back">
					<text class="ic ic-left text-gray6"></text>
					<text class="text-gray6 ml15">取消</text>
				</view>
				<view class="title">
					<text class="text text-gray6">{{title || '檢測人員'}}</text>
				</view>
			</view>
		</view>
		<canvas
			class="canvas2"
			disable-scroll 
			type="2d" 
			id="myCanvas2"
			:style="{
				width: canvasHeight + 'px', 
				height: canvasWidth + 'px'}">
		</canvas>
	</view>
</template>

js代碼:canvas的物理寬高調(diào)整后,canvas的邏輯寬高也需要進行調(diào)整,默認邏輯寬高是300*150px,(小程序Canvas 2D接口支持修改邏輯寬高),具體參考本文中的initCanvas方法


<script>
	export default {
		data() {
			return {
				canvasWidth: 300,
				canvasHeight: 150,
				top: 0,
				canvas: null,
				title: ''
			}
		},
		onLoad() {
			const menuData = uni.getMenuButtonBoundingClientRect()
			uni.getSystemInfo({
			  success: (res) => {
			    let navPadding = menuData.top - res.statusBarHeight
			    // 頂部高度 = 狀態(tài)欄高度 + 膠囊按鈕行高度 + 膠囊按鈕上下的padding
			    let navHeight = res.statusBarHeight + navPadding * 2 + menuData.height
			    // 設(shè)置canvas的物理寬高
			    this.canvasWidth = res.windowWidth - 100
				this.canvasHeight = res.windowHeight - navHeight - 20
				this.top = navHeight
			  }
			})
		},
		onReady() {
			this.initCanvas()
		},
		methods: {
			initCanvas() {
				uni.createSelectorQuery()
					.select('#myCanvas')
					.fields({ node: true, size: true })
					.exec((res) => {
						// 修改canvas的邏輯寬高
						// 如果不修改canvas的邏輯寬高,僅通過樣式修改canvas的寬高,會導(dǎo)致繪圖時比例不對,
						// 如將物理寬度改為600,但邏輯寬度還是300,假設(shè)畫圖時的起點x是100,那么實際看到的繪圖起點是200
						const canvas = res[0].node
						this.canvas = canvas
						this.ctx = canvas.getContext('2d')
						// canvas.width = this.canvasWidth
						// canvas.height = this.canvasHeight
						// 注意:按照上面方式調(diào)整,雖然邏輯寬高和物理寬高保持一致了,但是會發(fā)現(xiàn)畫出來的線會有鋸齒不夠清晰
						// 因為不同設(shè)備上物理像素與邏輯像素是不一致的
						// 因此canvas的邏輯寬高等于物理寬高分別*dpr
						const dpr = wx.getSystemInfoSync().pixelRatio
						canvas.width = this.canvasWidth * dpr
						canvas.height = this.canvasHeight * dpr
						// 假設(shè)dpr等于2,,那么canvas的物理寬度是600,邏輯寬度就是1200,
						// 假設(shè)畫圖時的起點x是100,那么實際看到的繪圖起點是50,此時只需要將繪圖內(nèi)容進行等比例放大即可
						this.ctx.scale(dpr, dpr)
					})
			},
			handleTouchstart(e) {
				this.lineBegin(e.touches[0].x, e.touches[0].y)
			},
			handleTouchmove(e) {
				this.lineTo(e.touches[0].x, e.touches[0].y)
			},
			lineBegin(x, y) {
				this.ctx.beginPath()
				// 新版Canvas 2D接口,直接修改屬性即可
				this.ctx.lineCap = 'round'
				this.ctx.lineWidth = 5
				this.startX = x
				this.startY = y
				this.ctx.moveTo(this.startX, this.startY)
			},
			lineTo(x, y) {
				this.ctx.lineTo(x, y)
				this.ctx.stroke()
				this.ctx.moveTo(x, y)
			},
			clearContext() {
				this.ctx.clearRect(0, 0, this.canvasWidth, this.canvasHeight)
			},
			confirm() {
				uni.canvasToTempFilePath({
					canvas: this.canvas, 
					success: (res) => {
						this.rotateImage(res.tempFilePath)
					}
				})
			},
			// 橫屏簽名,但是canvas的方向是垂直的,導(dǎo)出的圖片也是豎屏,需要將圖片進行旋轉(zhuǎn)
			rotateImage(filePath) {
				uni.createSelectorQuery()
					.select('#myCanvas2')
					.fields({ node: true, size: true })
					.exec((res) => {
						// 首先繪制一個寬高與上面canvas相反的canvas
						const canvas = res[0].node
						this.canvas2 = canvas
						this.ctx2 = canvas.getContext('2d')
						
						const dpr = wx.getSystemInfoSync().pixelRatio
						canvas.width = this.canvasHeight * dpr
						canvas.height = this.canvasWidth * dpr
						this.ctx2.scale(dpr, dpr)
						// 繪制上述導(dǎo)出的簽名圖片到新的canvas上
						const img = this.canvas2.createImage()
						img.src = filePath
						img.onload = () => {
							// 簽名圖片旋轉(zhuǎn)繪畫解析在下方
							this.ctx2.translate(0, this.canvasWidth);
							this.ctx2.rotate(270 * Math.PI / 180)
							this.ctx2.drawImage(img, 0, 0, this.canvasWidth, this.canvasHeight)
							
							uni.canvasToTempFilePath({
								canvas: this.canvas2, 
								success: (res) => {
									this.handleUploadFile(res.tempFilePath)
								}
							}) 
						}						
					})
			},
			handleUploadFile(filePath) {
				uni.uploadFile({
		  			url: config.requestUrl + '/biz/file/upload/annex', 
			        filePath,
	  		        name: 'file',
					header: {
						'Authorization': getToken()
					},
				  	success: (res) => {
						// 調(diào)用接口成功
						if(res.statusCode == 200) {
							// 解析服務(wù)器返回數(shù)據(jù)
							const data = JSON.parse(res.data)
							if(data.code == 200) {
								const responseUrl = config.requestUrl + data.filePath
								const eventChannel = this.getOpenerEventChannel()
								eventChannel.emit('getSignImage', {filePath: responseUrl, fileId: data.fileId});
								this.back()
							}
						} else {
							uni.hideLoading()
						}
				  },
				  fail: (res) => {
				    uni.hideLoading()
				  }
				})
			},
			back() {
				uni.navigateBack()
			}
		}
	}
</script>	

由于簽名的方向是橫向的,但是canvas本身是豎向,導(dǎo)出的簽名圖片也為豎向,那么我們需要將導(dǎo)出的圖片旋轉(zhuǎn)為橫向后重新繪制到canvas上然后導(dǎo)出。簽名圖旋轉(zhuǎn)繪制的效果圖如下:

圖中黑色部分為canvas區(qū)域,豎向的簽名圖片為剛剛我們導(dǎo)出的圖片,繪制到canvas上的效果如下圖,如果我們要將其橫向繪制到面板上,此時需要將繪畫內(nèi)容沿左上角順時針旋轉(zhuǎn)270deg,此時可以發(fā)現(xiàn)旋轉(zhuǎn)后圖片的覆蓋區(qū)域在canvas向上移動canvasWdth的位置,那么旋轉(zhuǎn)前將canvas的繪畫起點重置到(0,canvasWidth)即可保證繪畫內(nèi)容旋轉(zhuǎn)后剛好覆蓋在canvas上
微信小程序?qū)崿F(xiàn)手寫簽名,JS,微信小程序,小程序,前端
需要注意的點是,后面繪畫旋轉(zhuǎn)后的canvas在實際頁面中我們是不需要看到的,需要通過樣式將其隱藏,如果需要使用canvasToTempFilePath方法導(dǎo)出圖片的話,不能使用display:none的隱藏canvas,否則會報錯no image found,
可以通過定位和visiblity(opacity)屬性隱藏。

頁面樣式:文章來源地址http://www.zghlxwxcb.cn/news/detail-519113.html

<style lang="scss" scoped>
	.sign-page {
		min-height: 100vh;
		background-color: #f5f5f5;
		.canvas-box {
			position: relative;
			width: 100%;
		}
		.left-pane {
			width: 100rpx;
			.left-text {
				position: absolute;
				top: 0;
				line-height: 100rpx;
				transform: translateX(100rpx) rotate(90deg) ;
				transform-origin: 0 0;
			}
			.right-box {
				position: absolute;
				display: flex;
				align-items: center;
				bottom: 0;
				transform: translateX(100rpx) rotate(90deg)  translateX(-100%) translateX(100rpx);
				transform-origin: 0 0;
				.left-button {
					line-height: 100rpx;
					margin-right: 30rpx;
				}
				.right-button {
					font-size: 30rpx;
					color: #fff;
					width: 140rpx;
					height: 60rpx;
					line-height: 60rpx;
					background-color: green;
				}
			}
		}
		
		.canvas {
			margin: 0 auto;
			background-color: #fff;
			border: 2rpx dashed #d9d9d9;
			transform-origin: center center;
		}
		.canvas2 {
		/*設(shè)置display:none會導(dǎo)致wx.canvasToTempFilePath報錯no image found*/
			/*display: none;*/
			position: absolute;
			opacity: 0;
		}
		.right-pane {
			position: absolute;
			width: 100rpx;
			height: 100%;
			right: 0;
			top: 0;
			.back-button {
				position: relative;
				z-index: 5;
				white-space: nowrap;
				line-height: 100rpx;
				align-items: center;
				transform: translateX(100rpx) rotate(90deg);
				transform-origin: 0 0;
			}
			.title {
				position: absolute;
				top: 0;
				z-index: 4;
				width: 100rpx;
				height: 100%;
				white-space: nowrap;
				display: flex;
				justify-content: center;
				align-items: center;
				line-height: 100rpx;
				text-align: center;
				.text {
					display: inline-block;
					transform: rotate(90deg);
				}
			}
		}
	}
</style>

到了這里,關(guān)于微信小程序canvas實現(xiàn)簡易手寫簽名版(uni-app)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔相關(guān)法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • 微信小程序手寫簽名

    wxml js wxss 開啟橫屏 json

    2024年02月08日
    瀏覽(18)
  • uni-app微信小程序-利用canvas給圖片添加水印

    uni-app微信小程序-利用canvas給圖片添加水印

    選擇圖片 → 將圖片繪制到 canvas 中并繪制水印 →將添加水印的圖片繪制到 canvas 中 → 將 canvas 畫布轉(zhuǎn)換為圖片地址 → 上傳/展示操作 注意:微信小程序在選擇照片或者喚起相機之前需要獲取相應(yīng)的 權(quán)限 利用 uni.getSetting 查看用戶是否調(diào)用相機的權(quán)限(有就選擇圖片,沒有就

    2024年02月06日
    瀏覽(25)
  • uni-app 微信小程序 圖文生成圖片 wxml-to-canvas

    uni-app 微信小程序 圖文生成圖片 wxml-to-canvas

    在做的小程序要增加一個將文字與圖片生成圖片不可修改的功能,第一次做,在網(wǎng)上找了不少資料。參考了wxml-to-canvas | 微信開放文檔? ,又看了一些相關(guān)事例,嘗試寫了一下。 ? 需要準備的文件及配置項: 1、先把代碼片段下載到本地 2、創(chuàng)建wxcomponents目錄,把代碼片段中的

    2024年02月09日
    瀏覽(24)
  • uni-app+vue2 微信小程序 使用canvas繪制折線圖/波形圖

    uni-app+vue2 微信小程序 使用canvas繪制折線圖/波形圖

    ? 接口返回一個數(shù)組,每一項均是一個數(shù)字,代表著y坐標,x坐標需自己處理。 我的數(shù)據(jù)是1024個浮點數(shù),在-10到10之間 波形圖需要xy軸縮放功能,用c3的 transform: scale()是不行的,至少會失真。 然后背景的格子,我這里是每個格子要求100個點,初始縮放下是11個格子,10條線(

    2024年04月26日
    瀏覽(25)
  • 微信小程序?qū)崿F(xiàn)canvas畫圓形微信頭像

    微信小程序?qū)崿F(xiàn)canvas畫圓形微信頭像

    1.需要獲取用戶信息,拿到用戶頭像圖片 2.實例化一個canvas對象,繪制出圓形并將頭像固定到圓形中心位置 3.實現(xiàn)效果 ? ? ?

    2024年02月15日
    瀏覽(22)
  • 微信小程序--canvas畫布實現(xiàn)圖片的編輯

    微信小程序--canvas畫布實現(xiàn)圖片的編輯

    上傳圖片,編輯圖片大小,添加文字,改變文字顏色等 微信小程序--canvas畫布實現(xiàn)圖片的編輯 軟件環(huán)境:微信開發(fā)者工具 官方下載地址:微信開發(fā)者工具下載地址與更新日志 | 微信開放文檔 1、基本需求。 實現(xiàn)上傳圖片 實現(xiàn)圖片編輯 實現(xiàn)添加文字 實現(xiàn)導(dǎo)出圖片 2、案例目錄

    2024年02月05日
    瀏覽(94)
  • 微信小程序使用 canvas 2d 實現(xiàn)簽字板組件

    微信小程序使用 canvas 2d 實現(xiàn)簽字板組件

    本文是在微信小程序中使用 canvas 2d 來實現(xiàn)簽字板功能; 效果圖: 代碼: 1、wxml 2、js 3、總結(jié) canvas 的寬度和高度可以寫死,也可以根據(jù)當前可是區(qū)域動態(tài)計算;需要注意的是 res[0].node 的寬度和高度的計算是當前 canvas 元素上的寬度和高度乘設(shè)備的 pixelRatio ;

    2024年02月09日
    瀏覽(97)
  • uniapp寫微信小程序?qū)崿F(xiàn)電子簽名

    uniapp寫微信小程序?qū)崿F(xiàn)電子簽名

    寫電子簽名一定要注意的是一切全部按照手機上的適配來,為啥這么說呢,因為你在微信開發(fā)者工具中調(diào)試的時候認為是好的,正常的非常nice,當你發(fā)布版本的時候你會發(fā)現(xiàn)問題出來了。我下邊的寫法你可以直接用很簡單。就是要記住canvas的幾個屬性和用法。 直接上干貨 1.簽

    2024年01月18日
    瀏覽(18)
  • 微信小程序?qū)崿F(xiàn)輸入內(nèi)容生成二維碼(canvas)

    微信小程序?qū)崿F(xiàn)輸入內(nèi)容生成二維碼(canvas)

    1.封裝好的QRcode的js文件 2.在我們需要使用的頁面的js文件中引入該文件 ?效果如下: ?3.首先在wxml中書寫 canvas標簽 4.先聲明一個方法來解決中文亂碼的問題 5.當我們文本域的值發(fā)生改變時,也要渲染到視圖層 需求: 當我們文本框輸入內(nèi)容時,點擊以下的生成二維碼按鈕,下

    2024年02月16日
    瀏覽(101)
  • 微信小程序canvas畫圖案列,實現(xiàn)生成頭像并保存,小程序新版canvas變化,小程序中canvas注意事項

    微信小程序canvas畫圖案列,實現(xiàn)生成頭像并保存,小程序新版canvas變化,小程序中canvas注意事項

    你一定見過很多制作頭像的小程序,無論是國慶的紅旗,圣誕的帽子,還是疫情的口罩,都可以用小程序生成應(yīng)景的頭像,那么具體的代碼是怎么實現(xiàn)的呢?前些天幫別人做了一個生成姓氏頭像的功能,里面的實現(xiàn)原理基本一致,都是通過Canvas實現(xiàn),以生成姓氏頭像為例,記錄一下小程序的

    2024年02月09日
    瀏覽(20)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包