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

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

這篇具有很好參考價值的文章主要介紹了uni-app 微信小程序 圖文生成圖片 wxml-to-canvas。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

在做的小程序要增加一個將文字與圖片生成圖片不可修改的功能,第一次做,在網(wǎng)上找了不少資料。參考了wxml-to-canvas | 微信開放文檔? ,又看了一些相關(guān)事例,嘗試寫了一下。
?

需要準備的文件及配置項:

1、先把代碼片段下載到本地

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

2、創(chuàng)建wxcomponents目錄,把代碼片段中的文件拷到此目錄下,并將下圖的目錄改成真實目錄。

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

3、修改配置文件pages.json,找到要寫此功能的路徑,加上

"style": {
?? ??? ??? ??? ?"app-plus": {
?? ??? ??? ??? ??? ?"titleNView": false //禁用原生導(dǎo)航欄
?? ??? ??? ??? ?},
?? ??? ??? ??? ?"navigationBarTitleText": "",
?? ??? ??? ??? ?"enablePullDownRefresh": false,
?? ??? ??? ??? ?"navigationStyle": "custom",
?? ??? ??? ??? ?"usingComponents":{
?? ??? ??? ??? ??? ?"wxml-to-canvas": "/wxcomponents/wxml-to-canvas/index"
?? ??? ??? ??? ?}
?? ??? ??? ?}

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

?4、開始寫組件。注意this.widget = this.selectComponent('.widget');一定要放在onLoad頁面生命周期中,不然不生效

?<view :style="{width: canvasWidth + 'px', height: canvasHeight + 'px' }">
?? ??? ?<wxml-to-canvas class="widget" :width="canvasWidth" :height="canvasHeight"></wxml-to-canvas>
?? ?</view>

const {
?? ??? ?wxml,
?? ??? ?style
?? ?} = require('./notification.js')
?? ?export default {
?? ??? ?data() {
?? ??? ??? ?return {
?? ??? ??? ??? ?imgSrc: '/static/img/3.png',
?? ??? ??? ??? ?//最后生成的圖片信息
?? ??? ??? ??? ?imageData: null,
?? ??? ??? ??? ?canvasWidth: 320, // 默認canvas寬高
?? ??? ??? ??? ?canvasHeight: 480,
?? ??? ??? ??? ?screenWidth: null, // 設(shè)備寬度
?? ??? ??? ??? ?screenHeight: null, // 設(shè)備寬度
?? ??? ??? ??? ?userInfo: {},
?? ??? ??? ??? ?isRegister: '',
?? ??? ??? ??? ?controlContent: undefined,
?? ??? ??? ??? ?statusCode: undefined,
?? ??? ??? ??? ?//上個頁面用到的圖片地址
?? ??? ??? ??? ?tempFile:undefined
?? ??? ??? ?}
?? ??? ?},
?? ??? ?onLoad(option) {
?? ??? ??? ?this.userInfo = uni.getStorageSync('weixin-userInfo') ? JSON.parse(uni.getStorageSync('weixin-userInfo')) :{};
?? ??? ??? ?// 獲取設(shè)備信息
?? ??? ??? ?wx.getSystemInfo({
?? ??? ??? ??? ?success: (res) => {
?? ??? ??? ??? ??? ?this.screenWidth = res.screenWidth
?? ??? ??? ??? ??? ?this.screenHeight = 800 //高度建議計算得出或?qū)懰?。如使用res.screenHeight,文字過長時無法生成(安卓手機,最新鴻蒙系統(tǒng)高度不能超過1000)
?? ??? ??? ??? ??? ?this.canvasWidth = this.screenWidth
?? ??? ??? ??? ??? ?this.canvasHeight = this.screenHeight
?? ??? ??? ??? ??? ?setTimeout(() => {
?? ??? ??? ??? ??? ??? ?this.widget = this.selectComponent('.widget');
?? ??? ??? ??? ??? ??? ?this.controlContent = option.controlContent;
?? ??? ??? ??? ??? ??? ?this.tempFile = option.tempFile
?? ??? ??? ??? ??? ??? ?this.download();
?? ??? ??? ??? ??? ?}, 1000)
?? ??? ??? ??? ?}
?? ??? ??? ?});
?? ??? ?},
?? ??? ?methods: {
?? ??? ??? ?//生成圖片
?? ??? ??? ?download() {
?? ??? ??? ??? ?// 數(shù)字容器寬度 動態(tài)設(shè)置?
?? ??? ??? ??? ?setTimeout(() => {
?? ??? ??? ??? ??? ?uni.showLoading({
?? ??? ??? ??? ??? ??? ?title: '圖片生成中...'
?? ??? ??? ??? ??? ?})
?? ??? ??? ??? ??? ?this.renderToCanvas()
?? ??? ??? ??? ?}, 1000)
?? ??? ??? ?},
?? ??? ??? ?renderToCanvas() {
?? ??? ??? ??? ?const _wxml = wxml('test', this.tempFile, this.controlContent) //調(diào)用wxml
?? ??? ??? ??? ?const _style = style(this.screenWidth, this.canvasWidth, this.canvasHeight)
?? ??? ??? ??? ?setTimeout(() => {
?? ??? ??? ??? ??? ?const p1 = this.widget.renderToCanvas({
?? ??? ??? ??? ??? ??? ?wxml: _wxml,
?? ??? ??? ??? ??? ??? ?style: _style
?? ??? ??? ??? ??? ?})
?? ??? ??? ??? ??? ?p1.then((res) => {
?? ??? ??? ??? ??? ??? ?uni.hideLoading()
?? ??? ??? ??? ??? ??? ?this.saveImageToPhotosAlbum();
?? ??? ??? ??? ??? ?}).catch((err) => {
?? ??? ??? ??? ??? ??? ?console.log('生成失敗')
?? ??? ??? ??? ??? ?})
?? ??? ??? ??? ?}, 100)

?? ??? ??? ?},
?? ??? ??? //保存圖片到本地
			saveImageToPhotosAlbum() {
				uni.showLoading({
					title: '正在保存中...'
				})
				const p2 = this.widget.canvasToTempFilePath()
				let that = this
				p2.then(result => {
					let path = result.tempFilePath
					uni.uploadFile({
						url: '上傳服務(wù)地址',
						filePath: path,
						name: 'file',
						formData: {
							'user': 'test'
						},
						success: (res) => {
							let data = JSON.parse(res.data)
							if (data.code == 200) {
								uni.saveImageToPhotosAlbum({
									filePath: path,
									success: () => {
										uni.hideLoading()
										uni.showToast({
											title: '保存成功,可去手機相冊查看',
											duration: 2000,
											icon: 'none'
										});
										/* uni.redirectTo({
										    url: '../communityControl/notification?tempFile='+ this.tempFile    
										});     */
										uni.navigateBack();
									}
								});
							}

						}
					});
				})
			}
?? ??? ?}
?? ?}

?5、寫notification.js文件,必須要按照wxml-to-canvas寫生成模板,不然不生效

const wxml = (name, pic, content) => `
<view class="container">
    <text class="content">` + content + `</text>
     <image src="` + pic + `"  class="pic"/>
</view>
`

/**
 * @param {*} screenWidth 屏幕寬度
 * @param {*} canvasWidth  畫布寬度
 * @param {*} canvasHeight  畫布高度
 * @param {*} numberWidth  數(shù)字寬度,動態(tài)設(shè)置
 * @return {*} 
 */
const style = (screenWidth, canvasWidth, canvasHeight) => {
    return {
        "container": {
            width: canvasWidth,
            height: canvasHeight,
            position: 'relative',
            overflow: 'hidden',
            backgroundColor: '#ffffff',
			padding: '30rpx 20rpx',
        },
        "name": {
            fontSize: 20,
            color: '#333',
            marginLeft: canvasWidth * 0.08,
            width: canvasWidth * 0.84,
            height: screenWidth * 0.18,
            textAlign: 'center',
        },
        "content": {
            fontSize: 14,
            color: '#333',
            width: canvasWidth * 0.84,
            height: screenWidth * 0.84,
            marginLeft: canvasWidth * 0.08,
			marginTop: canvasWidth * 0.08,
        },
        "pic": {
            width: canvasWidth * 0.4,
            height: screenWidth * 0.2,
            marginTop: canvasWidth * 0.1,
            marginLeft: canvasWidth * 0.35,
            marginBottom: canvasWidth * 0.05,
            borderRadius: screenWidth * 0.14,
            overflow: 'hidden',
        },
    }
}

module.exports = {
    wxml,
    style
}

本文檔適用于vue2,并正式運用到項目中,但本人未在vue3的環(huán)境下使用,有友友提醒說不能用在vue3中,特在此說明,也歡迎使用的友友們提出寶貴意見。文章來源地址http://www.zghlxwxcb.cn/news/detail-489535.html

到了這里,關(guān)于uni-app 微信小程序 圖文生成圖片 wxml-to-canvas的文章就介紹完了。如果您還想了解更多內(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)文章

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

    如題: 直接上代碼 html js部分 需要在date中聲明好 ????????????????current: 0, ?? ??? ??? ??? ?hidepic: null, ?? ??? ??? ??? ?showpic: null 因為是已進入就開始的,所以 要在生命周期中使用 最后一部別忘了,要給需要淡入淡出的元素或者圖片設(shè)置絕對定位

    2024年02月12日
    瀏覽(23)
  • uqrcode+uni-app 微信小程序生成二維碼

    uqrcode+uni-app 微信小程序生成二維碼

    使用微信小程序需要彈出 動態(tài) 二維碼的需求,從插件市場選了一個下載次數(shù)較多的組件引入到項目中 uqrcode ,使用步驟如下: 1、從插件市場下載 插件地址:https://ext.dcloud.net.cn/plugin?id=1287,若你是跟我一樣是用uni-app開發(fā)微信小程序的,則該插件的介紹只需要看下面的即可,

    2024年02月06日
    瀏覽(25)
  • uni-app(微信小程序)圖片旋轉(zhuǎn)放縮,文字繪制、海報繪制

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

    總結(jié)一下: 要進行海報繪制離不開canvas,我們是先進行圖片,文字的拖拽、旋轉(zhuǎn)等操作 最后再對canvas進行繪制,完成海報繪制。 背景區(qū)域設(shè)置為 position: relative,方便圖片在當前區(qū)域中拖動等處理。 添加圖片,監(jiān)聽圖片在背景區(qū)域下的 touchstart touchmove touchend 事件 拖動圖片,

    2024年02月09日
    瀏覽(96)
  • uni-app:使用 Painter 在微信小程序?qū)斍绊撁姹4鏋閳D片

    uni-app:使用 Painter 在微信小程序?qū)斍绊撁姹4鏋閳D片

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

    2024年02月12日
    瀏覽(28)
  • uni-app(微信小程序) 根據(jù)小程序頁面路徑(可帶參數(shù)) 生成二維碼、分享碼

    uni-app(微信小程序) 根據(jù)小程序頁面路徑(可帶參數(shù)) 生成二維碼、分享碼

    微信官方文檔 小程序 看文檔點這里 第一個獲取小程序碼,就是根據(jù)你要通過二維碼打開的頁面路徑生成一個小程序碼,且這個小程序碼是永久的 其實文檔內(nèi)也說明了,很少用到。即使需要生成這樣的小程序碼,可以去微信公眾平臺的小程序管理后臺生成,還方便。 調(diào)用方

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

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

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

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

    微信小程序uni-app

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

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

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

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

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

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

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

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

    2024年02月13日
    瀏覽(114)

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

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

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

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

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包