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

uni-app(微信小程序)圖片旋轉(zhuǎn)放縮,文字繪制、海報(bào)繪制

這篇具有很好參考價(jià)值的文章主要介紹了uni-app(微信小程序)圖片旋轉(zhuǎn)放縮,文字繪制、海報(bào)繪制。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

總結(jié)一下:

要進(jìn)行海報(bào)繪制離不開(kāi)canvas,我們是先進(jìn)行圖片,文字的拖拽、旋轉(zhuǎn)等操作
最后再對(duì)canvas進(jìn)行繪制,完成海報(bào)繪制。

  1. 背景區(qū)域設(shè)置為 position: relative,方便圖片在當(dāng)前區(qū)域中拖動(dòng)等處理。
  2. 添加圖片,監(jiān)聽(tīng)圖片在背景區(qū)域下的 touchstart touchmove touchend 事件
  3. 拖動(dòng)圖片,在touchmove中,對(duì)圖片進(jìn)行位置(后續(xù)坐標(biāo)-初始坐標(biāo))、角度(勾股定理計(jì)算點(diǎn)到圓心距離,利用角度計(jì)算公式計(jì)算)、放縮比例(勾股定理計(jì)算點(diǎn)到圓心的半徑距離,拖動(dòng)停止的半徑除以初始的半徑,獲得放縮比例scale)的計(jì)算
  4. 最終canvas繪制,利用dom中的空canvas,將圖片依次繪制到canvas中,并獲取鏈接

部分主要代碼如下:

const ctx = uni.createCanvasContext("myCanvas", this);
ctx.drawImage(this.imageSrc, 0, 0, IMG_REAL_W, IMG_REAL_H);

const ctx = uni.createCanvasContext("myCanvas", this);
ctx.drawImage(this.imageSrc, 0, 0, IMG_REAL_W, IMG_REAL_H);
ctx.save();
ctx.beginPath();

// 畫(huà)背景色(白色)
// ctx.setFillStyle('#fff');
// ctx.fillRect(0, 0, this.canvasWidth, this.canvasHeight);
for (let i=0; i<items.length; i++) {
	const cur = items[i]
	ctx.save();
	ctx.translate(0, 0);
	ctx.beginPath();
	if(cur.image) {
		ctx.translate(cur.x, cur.y); // 圓心坐標(biāo)
		ctx.rotate(cur.angle * Math.PI / 180); // 圖片旋轉(zhuǎn)的角度
		ctx.translate(-(cur.width * cur.scale / 2), -(cur.height * cur.scale / 2)) // 圖片的縮放
		ctx.drawImage(cur.image, 0, 0, cur.width * cur.scale, cur.height * cur.scale); // 圖片繪制
	}
	if (cur.text) {
		ctx.font = `${cur.font}px arial`;
		ctx.fillStyle = cur.fillStyle;
		ctx.fillText(cur.text, cur.left, cur.top + Number(cur.font));
		console.log(cur.left, cur.top + Number(cur.font))
	}
	ctx.restore();
}
ctx.draw(true, () => {
	// 獲取畫(huà)布要裁剪的位置和寬度   均為百分比 * 畫(huà)布中圖片的寬度    保證了在微信小程序中裁剪的圖片模糊  位置不對(duì)的問(wèn)題 canvasT = (this.cutT / this.cropperH) * (this.imageH / pixelRatio)
	var canvasW = ((this.cropperW - this.cutL - this.cutR) / this.cropperW) * IMG_REAL_W;
	var canvasH = ((this.cropperH - this.cutT - this.cutB) / this.cropperH) * IMG_REAL_H;
	var canvasL = (this.cutL / this.cropperW) * IMG_REAL_W;
	var canvasT = (this.cutT / this.cropperH) * IMG_REAL_H;
	uni.canvasToTempFilePath({
			x: canvasL,
			y: canvasT,
			width: canvasW,
			height: canvasH,
			// destWidth: canvasW,
			// destHeight: canvasH,
			quality: +this.quality,
			fileType: this.fileType,
			canvasId: "myCanvas",
			success: (res) => {
				uni.hideLoading();
				// 成功獲得地址的地方
				// this.$emit("getImg", res.tempFilePath);
				this.saveImg(res.tempFilePath)
				this.isShow = false;
			},
			fail: (err) => {
				uni.hideLoading();
				uni.showToast({
					title: "圖片截取失敗!",
					icon: "none",
				});
			},
		},
		this
	);
});
<!-- 海報(bào)背景區(qū)域,采用style動(dòng)態(tài)調(diào)整,cropperInitW,cropperInitH一般為滿屏 -->
			<view class="uni-corpper"
				:style="'width:' + cropperInitW + 'px;height:' + cropperInitH + 'px;background:#000'">
				<!-- 海報(bào)繪制區(qū)域,采用style動(dòng)態(tài)調(diào)整,按照?qǐng)D片的長(zhǎng)寬比例動(dòng)態(tài)計(jì)算 cropperW等-->
				<view class="uni-corpper-content" :style="
						'width:' +
							cropperW +
							'px;height:' +
							cropperH +
							'px;left:' +
							cropperL +
							'px;top:' +
							cropperT +
							'px'
					">
					<!-- 背景圖片區(qū)域 cropperW等同上 -->
					<image :src="imageSrc" :style="'width:' + cropperW + 'px;height:' + cropperH + 'px;' + 'border: 3px solid #ff0000;'"></image>
					<!-- 海報(bào)上其他圖片處理,for循環(huán),itemList通過(guò)點(diǎn)擊添加 -->
					<block v-for="item in itemList" :key="item.id">
					<!-- 動(dòng)態(tài)設(shè)置圖片區(qū)域的縮放比例,還有pisition的左右位置,選中時(shí)z-index變大 -->
						<view class='touchWrap' :style="{transform: 'scale(' + item.scale + ')', top: item.top + 'px', left: item.left + 'px', 'z-index':item.active ? 100 : 1}">
							<view class='imgWrap' :class="item.active ? 'touchActive' : ''" :style="{transform: 'rotate(' + item.angle + 'deg)', border: item.active ? 4 * item.oScale : 0 + 'rpx #fff dashed'}">
								<image 
									v-if="item.image"
									:src='item.image' 
									:style="{width: item.width + 'px', height: item.height + 'px'}" 
									<!-- 圖片點(diǎn)擊時(shí),記錄點(diǎn)擊圖片當(dāng)前位置 -->
									@touchstart="(e) => WraptouchStart(e, item)"
									<!-- 圖片拖動(dòng)時(shí),記錄圖片當(dāng)前位置,并實(shí)時(shí)計(jì)算圖片大小、旋轉(zhuǎn)角度等,并存儲(chǔ)至itemList中 -->
									@touchmove="(e) => WraptouchMove(e, item)"
									<!--一般不做處理 -->
									@touchend="(e) => WraptouchEnd(e, item)"
									mode="widthFix"
								>
								</image>
								<!-- 刪除按鈕 -->
								<image 
									class='x' 
									src='/static/close.png' 
									:style="{transform: 'scale(' + item.oScale + ')', 'transform-origin': center}"
									@click="(e) => deleteItem(e, item)"
								>
								</image>
								<!-- 圖片放縮按鈕 -->
								<image 
									v-if="item.image"
									class='o' 
									src='/static/scale.png' 
									:style="{transform: 'scale(' + item.oScale + ')', 'transform-origin': center}"
									<!-- 圖片點(diǎn)擊時(shí),記錄點(diǎn)擊圖片當(dāng)前坐標(biāo),半徑 -->
									@touchstart="(e) => oTouchStart(e, item)"
									<!-- 圖片點(diǎn)擊時(shí),記錄點(diǎn)擊圖片當(dāng)前坐標(biāo),計(jì)算新的半徑(得到scale縮放比例)計(jì)算角度差,獲取當(dāng)前角度 -->
									@touchmove="(e) => oTouchMove(e, item)"
									@touchend="(e) => WraptouchEnd(e, item)"
								>
								</image>
							</view>
						</view>
					</block>
				</view>
							<canvas canvas-id="myCanvas" :style="
				'position:absolute;border: 2px solid red; width:' +
					imageW +
					'px;height:' +
					imageH +
					'px;top:-9999px;left:-9999px;'
			">
			</canvas>
			</view>
// 點(diǎn)擊圖片或文字
			WraptouchStart(e, it) {
				currentChoose = it
				// 循環(huán)圖片數(shù)組獲取點(diǎn)擊的圖片信息
				for (let i = 0; i < items.length; i++) {
					items[i].active = false;
					if (it.id == items[i].id) {
						index = i;
						items[index].active = true;
					}
				}
				// this.setData({
				//   itemList: items
				// })
				this.setList(items, 'itemList')
				// 獲取點(diǎn)擊的坐標(biāo)值 lx ly是圖片點(diǎn)擊時(shí)的位置值
				items[index].lx = e.touches[0].clientX;
				items[index].ly = e.touches[0].clientY;
			},
			// 拖動(dòng)圖片
			WraptouchMove(e) {
				// 獲取點(diǎn)擊的坐標(biāo)值 _lx _ly 是圖片移動(dòng)的位置值
				items[index]._lx = e.touches[0].clientX;
				items[index]._ly = e.touches[0].clientY;
				// left 是_lx 減去 lx,_ly 減去 ly,也就是現(xiàn)在位置,減去原來(lái)的位置。
				items[index].left += items[index]._lx - items[index].lx;
				items[index].top += items[index]._ly - items[index].ly;
				// 同理更新圖片中心坐標(biāo)點(diǎn),用于旋轉(zhuǎn)
				items[index].x += items[index]._lx - items[index].lx;
				items[index].y += items[index]._ly - items[index].ly;
				// 停止了以后,把lx的值賦值為現(xiàn)在的位置
				items[index].lx = e.touches[0].clientX;
				items[index].ly = e.touches[0].clientY;
				
				// this.setData({
				//   itemList: items
				// })
				this.setList(items, 'itemList')
			},
			// 放開(kāi)圖片
			WraptouchEnd(e, it) {
				touchNum ++
				clearTimeout(timer)
				timer = null
				timer = setTimeout(this.timeSta, 250)
			},
			// 計(jì)算坐標(biāo)點(diǎn)到圓心的距離
			getDistancs(cx, cy, pointer_x, pointer_y) {
				var ox = pointer_x - cx;
				var oy = pointer_y - cy;
				return Math.sqrt(
					ox * ox + oy * oy
				);
			},
			/*
			*參數(shù)cx和cy為圖片圓心坐標(biāo)
			*參數(shù)pointer_x和pointer_y為手點(diǎn)擊的坐標(biāo)
			*返回值為手點(diǎn)擊的坐標(biāo)到圓心的角度
			*/
			countDeg(cx, cy, pointer_x, pointer_y) {
				var ox = pointer_x - cx;
				var oy = pointer_y - cy;
				var to = Math.abs(ox / oy); // 勾股定理,計(jì)算當(dāng)前點(diǎn)距離中心點(diǎn)的距離。
				var angle = Math.atan(to) / (2 * Math.PI) * 360; // 計(jì)算當(dāng)前角度
				if (ox < 0 && oy < 0) //相對(duì)在左上角,第四象限,js中坐標(biāo)系是從左上角開(kāi)始的,這里的象限是正常坐標(biāo)系  
				{
					angle = -angle;
				} else if (ox <= 0 && oy >= 0) //左下角,3象限  
				{
					angle = -(180 - angle)
				} else if (ox > 0 && oy < 0) //右上角,1象限  
				{
					angle = angle;
				} else if (ox > 0 && oy > 0) //右下角,2象限  
				{
					angle = 180 - angle;
				}
				return angle; // 返回角度
			},

體驗(yàn):
uni-app(微信小程序)圖片旋轉(zhuǎn)放縮,文字繪制、海報(bào)繪制,uni-app,微信小程序,notepad++文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-707312.html

到了這里,關(guān)于uni-app(微信小程序)圖片旋轉(zhuǎn)放縮,文字繪制、海報(bào)繪制的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

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

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

相關(guān)文章

  • uni-app/微信小程序 實(shí)現(xiàn)圖片或元素淡入淡出效果

    如題: 直接上代碼 html js部分 需要在date中聲明好 ????????????????current: 0, ?? ??? ??? ??? ?hidepic: null, ?? ??? ??? ??? ?showpic: null 因?yàn)槭且堰M(jìn)入就開(kāi)始的,所以 要在生命周期中使用 最后一部別忘了,要給需要淡入淡出的元素或者圖片設(shè)置絕對(duì)定位

    2024年02月12日
    瀏覽(23)
  • uni-app:使用 Painter 在微信小程序?qū)?dāng)前頁(yè)面保存為圖片

    uni-app:使用 Painter 在微信小程序?qū)?dāng)前頁(yè)面保存為圖片

    手機(jī)截屏 Painter 實(shí)現(xiàn) 方式一:Painter Painter 是一個(gè)微信小程序組件,具體介紹和 API 請(qǐng)參考:GitHub文檔。 在 GitHub 下載下來(lái)之后只需要將 components 下的 painter 文件夾放到項(xiàng)目根目錄下的 wxcomponents 文件夾即可。然后就是如何在 uni-app 中使用微信小程序形式的組件,其實(shí)很簡(jiǎn)單,

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

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

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

    2024年02月09日
    瀏覽(25)
  • uni-app uni-file-picker文件上傳實(shí)現(xiàn)拍攝從相冊(cè)選擇獲取圖片上傳文檔服務(wù)器(H5上傳-微信小程序上傳)

    uni-app uni-file-picker文件上傳實(shí)現(xiàn)拍攝從相冊(cè)選擇獲取圖片上傳文檔服務(wù)器(H5上傳-微信小程序上傳)

    前言 最近在使用uni-app寫H5移動(dòng)端,有一個(gè)從手機(jī)拍攝從相冊(cè)選擇獲取圖片上傳到文檔服務(wù)器功能。 查閱uni-app發(fā)現(xiàn)關(guān)于上傳圖片,uni-file-picker文件上傳,uni.chooseImage,uni.uploadFile H5上傳時(shí)它和pc端原理差不多,都是file對(duì)象上傳,PC端是通過(guò)new file對(duì)象,uni-app是直接提供了 微信

    2024年02月15日
    瀏覽(95)
  • 微信小程序uni-app

    微信小程序uni-app

    小程序 是一種不需要下載、安裝即可使用的應(yīng)用,它實(shí)現(xiàn)了應(yīng)用觸手可及的夢(mèng)想,用戶掃一掃或者搜一下就能打開(kāi)應(yīng)用,也實(shí)現(xiàn)了用完即走的理念,用戶不用安裝太多應(yīng)用,應(yīng)用隨處可用,但又無(wú)須安裝卸載。 微信開(kāi)發(fā)文檔 1、工作原理 網(wǎng)頁(yè)開(kāi)發(fā),渲染線程和腳本是互斥的

    2024年02月10日
    瀏覽(106)
  • 微信小程序授權(quán)(uni-app)

    概述 為了避免重復(fù)開(kāi)發(fā),自己封裝了一個(gè)通用用戶授權(quán)回調(diào)方法,只需要傳入需要授權(quán)的scope,權(quán)限中文描述、回調(diào)函數(shù),就可以實(shí)現(xiàn)一整套小程序是否授權(quán)、打開(kāi)授權(quán)設(shè)置,調(diào)用后續(xù)操作函數(shù)的工作 功能 可以根據(jù)自己的實(shí)際應(yīng)用進(jìn)行微調(diào) 目前使用的uni-app版本,可以根據(jù)自

    2024年02月16日
    瀏覽(99)
  • 語(yǔ)法速通 uni-app隨筆【uni-app】【微信小程序】【vue】

    語(yǔ)法速通 uni-app隨筆【uni-app】【微信小程序】【vue】

    其中, pages 目錄/ index 目錄【必有】: index.js 編寫業(yè)務(wù)邏輯 【初始數(shù)據(jù),生命周期函數(shù)】 index.json 編寫配置 index.wxml 編寫模板 【可理解為本頁(yè)html】 index.wxss 【可理解為本頁(yè)css】 直接輸入敲回車,連尖括號(hào)都不需要就可以標(biāo)簽補(bǔ)全 1)初始數(shù)據(jù)寫死 在 index.wxml 引入變

    2024年02月12日
    瀏覽(228)
  • uni-app微信小程序使用echarts

    uni-app微信小程序使用echarts

    前言:本來(lái)是使用的ucharts,但因?yàn)闊o(wú)法監(jiān)聽(tīng)圖例點(diǎn)擊交互,滿足不了需求,所以只能放棄。 首先,下載echart組件??梢韵入S便建個(gè)文件夾,然后 npm init。接著下載依賴 然后找到 node_modulesmpvue-echarts下的文件,如圖 只留下src,其他的刪掉(沒(méi)有用到)。然后復(fù)制 mpvue-echart

    2024年02月10日
    瀏覽(96)
  • 【uni-app微信小程序】實(shí)現(xiàn)支付功能

    實(shí)現(xiàn)微信支付功能需要在小程序后臺(tái)配置支付相關(guān)信息,并且在前端代碼中調(diào)用微信支付API進(jìn)行支付操作。好的, uni-app微信小程序?qū)崿F(xiàn)支付功能整體流程 大致如下: 注冊(cè)微信公眾平臺(tái),并完成開(kāi)發(fā)者資質(zhì)認(rèn)證; 在微信商戶平臺(tái)注冊(cè)商戶賬號(hào),并完成商戶資質(zhì)認(rèn)證; 在商戶

    2024年02月13日
    瀏覽(115)
  • uni-app 微信小程序 激勵(lì)視頻廣告

    封裝激勵(lì)視頻-Ad.js 調(diào)用上面寫的方法:

    2024年02月12日
    瀏覽(99)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

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

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包