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

uni-app實現(xiàn)canvas繪制圖片、海報等

這篇具有很好參考價值的文章主要介紹了uni-app實現(xiàn)canvas繪制圖片、海報等。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

前段時間有個項目,需要繪制海報,我一聽這不是可以用canvas來實現(xiàn)嗎,所以我在五一假期就記錄下實現(xiàn)方式

我把canvas繪制圖片封裝成一個組件,直接引入使用就好了。

這里雖然是uni-app寫的,但實現(xiàn)方式其實都是一樣的,之后其他原生小程序用到也是大差不大的,真的很簡單??

遇到的坑:uni-app在轉app的時候 - ios 的canvas畫布過大可能導致繪制空白?

創(chuàng)建canvas繪制圖片的組件 - 代碼如下

<template>
	<view>
		<canvas canvas-id="canvas" :style="{'width': width + 'px', 'height': height + 'px'}" style="position: fixed; left: -9999px; top: -9999px;"></canvas>
	</view>
</template>

<script>
	export default {
		name: "drawImage",
		props: {
			// 繪制圖片的尺寸
			imageSize: {
				type: Object,
				default: () => {},
			},
			// canvas繪制的數(shù)據(jù)
			canvasData: {
				type: Array,
				default: () => [],
			},
			// 是否開始繪制
			isDraw: {
				type: Boolean,
				default: false,
			},
		},
		data() {
			return {
				// 屏幕寬度
				screenWidth: 0,
				// canvas畫布的寬度
				width: 0,
				// canvas畫布的高度
				height: 0,
				// 當前圖片放大倍數(shù) - 清晰度
				count: 2,
			};
		},
		mounted() {
            // 這段代碼主要是為了防止uni-app在app端IOS的問題,因為在IOS 畫布過大可能會導致繪制空白 - 可以自行調整
			// #ifdef APP-PLUS
			if(uni.getSystemInfoSync().platform === 'ios') {
				this.count = 1.8;
			}
			// #endif
		},
		watch: {
			// 監(jiān)聽是否開始繪制
			isDraw: async function(newVal) {
				if(newVal) {
					this.getSystemInfo();
					this.getImageByCanvasData(this.canvasData);
				}
			}
		},
		methods: {
			/** 獲取系統(tǒng)信息 */
			getSystemInfo() {
				const { screenWidth } = uni.getSystemInfoSync();

				this.width = this.imageSize.width * this.count;
				this.height = this.imageSize.height * this.count;

				this.screenWidth = screenWidth;
			},
			
			/**
			 * 通過數(shù)據(jù)繪制圖片
			 * @param {array} data		canvas繪制的數(shù)組
			 * 格式:每一項的數(shù)據(jù)
			 * { type: 'rect',  attr: { color: '', x, y, width, height, radian_1, radian_2, radian_3, radian_4 } }
			 * { type: 'image', attr: { image: '', x, y, width, height, radian_1, radian_2, radian_3, radian_4 } }
			 * { type: 'text',  attr: { text: '', x, y, color, size, weight, writingMode } }
			 * */
			async getImageByCanvasData(data) {
				// 獲取canvas上下文對象
				const context = uni.createCanvasContext("canvas", this);
				// 清空畫布
				context.clearRect(0, 0, this.width * this.count, this.height * this.count);

				for(const item of data) {
					// 判斷類型
					if(item.type === 'rect') {
						// 繪制圓邊矩形
						this.drawRoundRectangular(
							context, 
							item.attr.color, 
							item.attr.x * this.count, 
							item.attr.y * this.count, 
							item.attr.width * this.count, 
							item.attr.height * this.count, 
							item.attr.radian_1 ? item.attr.radian_1 * this.count : 0, 
							item.attr.radian_2 ? item.attr.radian_2 * this.count : -1, 
							item.attr.radian_3 ? item.attr.radian_3 * this.count : -1, 
							item.attr.radian_4 ? item.attr.radian_4 * this.count : -1
						);
					}
					else if(item.type === 'image' && item.attr.image) {
						// 繪制圓邊圖片
						await this.drawRoundImageToCanvas(
							context, 
							item.attr.image, 
							item.attr.x * this.count, 
							item.attr.y * this.count, 
							item.attr.width * this.count, 
							item.attr.height * this.count, 
							item.attr.radian_1 ? item.attr.radian_1 * this.count : 0, 
							item.attr.radian_2 ? item.attr.radian_2 * this.count : -1, 
							item.attr.radian_3 ? item.attr.radian_3 * this.count : -1, 
							item.attr.radian_4 ? item.attr.radian_4 * this.count : -1
						);
					}
					else if(item.type === 'text' && item.attr.text) {
						// 繪制文本
						this.drawTextToCanvas(
							context, 
							item.attr.text, 
							item.attr.x * this.count, 
							item.attr.y * this.count, 
							item.attr.color, 
							parseInt(item.attr.size ? item.attr.size * this.count : 16 * this.count), 
							item.attr.weight,
							item.attr.writingMode ? item.attr.writingMode : 'initial'
						);
					}
				}
				
				// 繪制圖片 
				context.draw(false, () => {
					uni.canvasToTempFilePath({
						canvasId: 'canvas',
						x: 0,
						y: 0,
						width: this.width,
						height: this.height,
						destWidth: this.width,
						height: this.height,
						success: res => {
							this.$emit("generateImageSuccessful", res.tempFilePath);
						},
					}, this);
				});
			},
			
			/**
			 * 繪制文本
			 * @param {string} context		Canvase的實例
			 * @param {string} text			文本內容
			 * @param {number} x			矩形的x坐標
			 * @param {number} y			矩形的y坐標
			 * @param {number} color		文本顏色
			 * @param {number} size			字體的大小
			 * @param {string} weight		字體的粗細
			 * @param {string} writingMode	字體的排列方式 - initial 水平  tb 垂直
			 * */
			drawTextToCanvas(context, text, x, y, color = '#000', size = 16, weight = '400', writingMode = 'initial') {
				context.fillStyle = color;
				context.font = `normal ${weight} ${size}px sans-serif`;
				if(writingMode === 'tb') {
					const temp = text.split("");
					for(let i = 0; i < temp.length; i++) {
						context.fillText(temp[i], x, i * size + y);
					}
				}
				else {
					// 判斷是否有換行符
					const temp = text.split("\n");
					for(let i = 0; i < temp.length; i++) {
						context.fillText(temp[i], x, i * size + y + i * size * 0.2);  // i * size * 0.2 增加換行的間距
					}
				}
			},
			
			/**
			 * 繪制圓邊矩形
			 * @param {string} context	Canvase的實例
			 * @param {string} color	填充的顏色
			 * @param {number} x		矩形的x坐標
			 * @param {number} y		矩形的y坐標
			 * @param {number} width	矩形的寬度
			 * @param {number} height	矩形的高度
			 * @param {number} height	圖片的高度
			 * @param {number} radian_1	弧度大小 - radian_1 右上 的弧度, 1個參數(shù)代表全部
			 * @param {number} radian_2	弧度大小 - radian_2 右下 的弧度
			 * @param {number} radian_3	弧度大小 - radian_3 左下 的弧度
			 * @param {number} radian_4	弧度大小 - radian_4 左上 的弧度
			 * */
			drawRoundRectangular(context, color, x, y, width, height, radian_1 = 0, radian_2 = -1, radian_3 = -1, radian_4 = -1) {
				context.save();
				this.drawRoundPath(context, x, y, width, height, radian_1, radian_2, radian_3, radian_4);
				context.setFillStyle(color);
				context.fill();
				context.restore();
			},
			
			/**
			 * 繪制圓角圖片
			 * @param {string} context	Canvase的實例
			 * @param {string} image	圖片地址
			 * @param {number} x		圖片的x坐標
			 * @param {number} y		圖片的y坐標
			 * @param {number} width	圖片的寬度
			 * @param {number} height	圖片的高度
			 * @param {number} radian_1	弧度大小 - radian_1 右上 的弧度, 1個參數(shù)代表全部
			 * @param {number} radian_2	弧度大小 - radian_2 右下 的弧度
			 * @param {number} radian_3	弧度大小 - radian_3 左下 的弧度
			 * @param {number} radian_4	弧度大小 - radian_4 左上 的弧度
			 * */
			async drawRoundImageToCanvas(context, image, x, y, width, height, radian_1 = 0, radian_2 = -1, radian_3 = -1, radian_4 = -1) {
				context.save();
				this.drawRoundPath(context, x, y, width, height, radian_1, radian_2, radian_3, radian_4);
				context.drawImage(await this.handleNetworkImgaeTransferTempImage(image), x, y, width, height);
				context.restore();
			},
			
			/**
			 * 繪制圓邊路徑
			 * @param {string} context	Canvase的實例
			 * @param {number} x		圖片的x坐標
			 * @param {number} y		圖片的y坐標
			 * @param {number} width	圖片的寬度
			 * @param {number} height	圖片的高度
			 * @param {number} radian_1	弧度大小 - radian_1 右上 的弧度, 1個參數(shù)代表全部
			 * @param {number} radian_2	弧度大小 - radian_2 右下 的弧度
			 * @param {number} radian_3	弧度大小 - radian_3 左下 的弧度
			 * @param {number} radian_4	弧度大小 - radian_4 左上 的弧度
			 * */
			drawRoundPath(context, x, y, width, height, radian_1 = 0, radian_2 = -1, radian_3 = -1, radian_4 = -1) {
				// 設置弧度
				radian_2 = radian_2 === -1 ? radian_1 : radian_2;
				radian_3 = radian_3 === -1 ? radian_1 : radian_3;
				radian_4 = radian_4 === -1 ? radian_1 : radian_4;
				
				// 創(chuàng)建路徑 - 繪制帶圓邊的矩形
				context.beginPath();											
				context.moveTo(x + width / 2, y);
				context.arcTo(x + width, y, x + width, y + height, radian_1);
				context.arcTo(x + width, y + height, x, y + height, radian_2);
				context.arcTo(x, y + height, x, y, radian_3);
				context.arcTo(x, y, x + width, y, radian_4);
				// 關閉路徑 - 結束繪制
				context.closePath();
				context.strokeStyle = "transparent";
				context.stroke();
				context.clip();
			},
			
			/** 將網(wǎng)絡圖片變成臨時圖片 */
			handleNetworkImgaeTransferTempImage(url) {
				return new Promise(resolve => {
					if(url.indexOf('http') === 0) {
						uni.downloadFile({
							url,
							success: res => {
								resolve(res.tempFilePath);
							}
						});
					}
					else {
						resolve(url);
					}
				});
			},
		}
	}
</script>

<style scoped lang="scss">

</style>

在頁面中的使用?

<template>
	<view>
		<!-- 顯示一下繪制完成后的路徑 -->
		<image :src="tempImage" style="width: 375px; height: 667px;"></image>
		
		<!-- 引入繪制圖片的組件 -->
		<drawImage :isDraw="isDraw" :canvasData="canvasData" :imageSize="{width: 375, height: 667}" @generateImageSuccessful="generateImageSuccessful" />
	</view>
</template>

<script>
export default {
	data() {
		return {
			// 是否開始繪制
			isDraw: false,
			/**
			 * 需要繪制的圖片數(shù)據(jù) - 具體參數(shù)需要看組件內的
			 * { type: 'rect',  attr: { color: '', x, y, width, height, radian_1, radian_2, radian_3, radian_4 } }
			 * { type: 'image', attr: { image: '', x, y, width, height, radian_1, radian_2, radian_3, radian_4 } }
			 * { type: 'text',  attr: { text: '', x, y, color, size, weight, writingMode } }
			 * */
			canvasData: [],
			// 臨時路徑
			tempImage: "",
		}
	},
	onLoad() {
		this.canvasData = [
			{ type: 'rect', attr: { color: '#00a127', x: 0, y: 0, width: 375, height: 667 } },
			{ type: 'text',  attr: { text: '略略大魔王', x: 100, y: 100, color: '#fff', size: 20, weight: '500', writingMode: 'tb' } }
		];
		
		this.isDraw = true;
	},
	methods: {
		/** 繪制成功后的回調 - 返回一個臨時路徑 */
		generateImageSuccessful(image) {
			this.tempImage = image;
		}
	}
};
</script>

<style scoped lang="scss">
	
</style>

這里只是簡單的演示下(效果圖):

createcanvascontext,前端,javascript,前端,html,小程序,vue文章來源地址http://www.zghlxwxcb.cn/news/detail-699102.html

到了這里,關于uni-app實現(xiàn)canvas繪制圖片、海報等的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!

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

領支付寶紅包贊助服務器費用

相關文章

  • 小程序-uni-app:將頁面(html+css)生成圖片/海報/名片,進行下載 保存到手機

    小程序-uni-app:將頁面(html+css)生成圖片/海報/名片,進行下載 保存到手機

    一、需要描述 本文實現(xiàn),uniapp微信小程序,把頁面內容保存為圖片,并且下載到手機上。 說實話網(wǎng)上找了很多資料,但是效果不理想,直到看了一個開源項目,我知道可以實現(xiàn)了。 本文以開源項目uniapp-wxml-to-canvas 為藍本 記錄集成的步驟,以供參考。 詳細內容可以下載并啟

    2024年02月07日
    瀏覽(97)
  • 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日
    瀏覽(26)
  • uni-app微信小程序-利用canvas給圖片添加水印

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

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

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

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

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

    2024年02月09日
    瀏覽(25)
  • 微信小程序canvas實現(xiàn)簡易手寫簽名版(uni-app)

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

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

    2024年02月12日
    瀏覽(100)
  • uni-app 微信小程序中如何通過 canvas 畫布實現(xiàn)電子簽名?

    uni-app 微信小程序中如何通過 canvas 畫布實現(xiàn)電子簽名?

    一、實際應用場景 電子簽名軟件應用場景:電子簽名在金融、銀行、貸款行業(yè)中可以用于對內日常辦公流轉的文檔的蓋章簽字,對外涉及業(yè)務合作協(xié)議,采購合同,貸款申請、信用評估、貸款合同、貸款文件表、說明函等等。 可以說,只要是涉及紙質文檔簽字蓋章的場景,

    2024年02月10日
    瀏覽(26)
  • 前端vue uni-app自定義精美海報生成組件

    前端vue uni-app自定義精美海報生成組件

    在當前技術飛速發(fā)展的時代,軟件開發(fā)的復雜度也在不斷提高。傳統(tǒng)的開發(fā)方式往往將一個系統(tǒng)做成整塊應用,一個小的改動或者一個小功能的增加都可能引起整體邏輯的修改,從而造成牽一發(fā)而動全身的情況。為了解決這個問題,組件化開發(fā)逐漸成為了一種趨勢。通過組件

    2024年02月14日
    瀏覽(32)
  • uni-app實現(xiàn)圖片上傳功能

    uni-app實現(xiàn)圖片上傳功能

    效果 代碼 ?

    2024年02月13日
    瀏覽(108)
  • uni-app - 電子簽字板組件(簽名專用寫字畫板,支持調整寫字板 “橫縱“ 方向,可調整線條粗細顏色等,Canvas 繪制非常絲滑流暢)完美兼容 H5 APP 小程序,最好用的畫板簽字教程插件源碼

    uni-app - 電子簽字板組件(簽名專用寫字畫板,支持調整寫字板 “橫縱“ 方向,可調整線條粗細顏色等,Canvas 繪制非常絲滑流暢)完美兼容 H5 APP 小程序,最好用的畫板簽字教程插件源碼

    網(wǎng)上的教程代碼非常亂且都有 BUG 存在,非常難移植到自己的項目中,本文代碼干凈整潔注釋詳細。 本文實現(xiàn)了 全端兼容,簽名專用的寫字板組件,真機流暢絲滑且無 BUG, 您直接復制組件源碼,按照詳細示例+超詳細的注釋輕松幾分鐘完成, 如下圖 真機測試 ,您還可以通過

    2024年02月10日
    瀏覽(50)
  • uni-app 實現(xiàn)圖片上傳添加水印操作

    改進原因: 1、Canvas 2D(新接口)需要顯式設置畫布寬高,默認:300 150,最大:1365 1365 ios 無法上傳較大圖片的尺寸,固對超過此尺寸的圖片進行了等比縮放的處理; 2、在頁面中設置canvas寬高,導致頁面有滾動條;現(xiàn)在采用離屏的canvas,但是離屏的canvas,canvasToTempFilePath方法

    2024年02月07日
    瀏覽(228)

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

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

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

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

二維碼1

領取紅包

二維碼2

領紅包