超碰中文网,91久久精品日日躁夜夜躁欧美,一区二区三区精品道,一区二区国产在线观看,亚洲精品中文字幕乱码一区二区,天天看片高清影视在线观看,久久99精品久久久久蜜桃,久久成人影视

VPS參考測評推薦
專注分享VPS主機優惠信息
衡天云優惠活動
熱網互聯2026年促銷活動
hostkvm優惠促銷活動
zji優惠促銷活動
QQGNET優惠促銷活動
QQGNET優惠促銷活動

使用applet canvas編寫一個簡單的圖片應用程序(js下載canvas圖片)

主機參考:VPS測評參考推薦/專注分享VPS服務器優惠信息!若您是商家可以在本站進行投稿,查看詳情!此外我們還提供軟文收錄、PayPal代付、廣告贊助等服務,查看詳情!
我們發布的部分優惠活動文章可能存在時效性,購買時建議在本站搜索商家名稱可查看相關文章充分了解該商家!若非中文頁面可使用Edge瀏覽器同步翻譯!PayPal代付/收錄合作

小程序開發教程專欄介紹用畫布寫一張圖片。

使用applet canvas編寫一個簡單的圖片應用程序(js下載canvas圖片)

推薦(免費):小程序開發教程

應用演示

截圖57f2c668df20fbe5ac3c296636fdfeb.png

要求

既然是小應用,希望最終產品有適用場景,有價值。

需求來源

這種應用需求的靈感來源于以往的工作和生活中,經常在不經意間得到同事的漂亮照片。這時候我們就想把這張照片做成表情包。一般會在圖片上添加一個有趣的交流工具(表情包)。

根據上述需求進行需求分解分析,可以整理出需要應用的功能。

用戶需要上傳圖片,添加文本,調整樣式,旋轉和縮放。此外,我們希望插入一些地圖,可以旋轉和縮放。用戶可以將圖片導出到相冊中。

https://github.com/luosijie/f github倉庫...

如果你喜歡我的項目,請給我一顆星鼓勵我。這個應用程序是用一個小程序開發的。

框架:mpx使用技術:小程序的畫布狀態管理

從 # 39;導入{ createStore };@ mpxjs/core # 39;const store = CreateStore({ state:{ cavas:null,//cnavas實例ctx: null,// canvas上下文實例元素:[],// canvas元素activeIndex: null,//當前編輯模式下的元素索引: # 39;背景 # 39;,//當前編輯模式:背景、文字、貼紙字體樣式:{//文字默認樣式為不透明度:1、fill style: # 39;#000000',stroke style: # 39;#000000'} }、突變:{ setCanvas (state,data) { state.canvas = data }、setCtx (state,data) { state.ctx = data }、setElements (state,data) { state.elements = data }、set mode(state . mode = data }、setActiveIndex (state,data) { state.activeIndex = data }、setFontStyle (state,{ key,data }){ state . font style[key]= data }、//添加文本Add text(state){ const size = 50 const請輸入文本 # 39;const text = { type: # 39;正文 # 39;,data: string,scale: 1,size,left: 100,top: 100,rotate: 0,opacity:state . font style . opacity,fill style:state . font style . fill style,stroke style } state . elements . push(text)state . active index = state . elements . length -1 },//添加地圖addsticker (state,data){ state . elements . push(data)state . active index = state . elements . length -1 },//刪除當前選中的Delete

//初始化canvas asyncinitcanvas(){ const query = this . createselectquery()query . select( # 39;#畫布 # 39;) .字段({ node: true,size: true })。exec(async RES = gt;{ const canvas = res[0]。node const CTX = canvas . get context( # 39;2d # 39)store . commit( # 39;setCanvas # 39,canvas)store . commit( # 39;setCtx # 39,ctx)等待此。loadimage( # 39;/images/icon -rotate . png # 39;).然后(res = gt{ this . image . rotate = RES })canvas . width = RES[0]。width * this . dprccanvas . height = RES[0]。height * this . DPR CTX . scale(this . DPR,this.dpr) this.drawgrid ()}}畫圖

/* * *畫圖* @ param { Object } ele canvas element */Draw image(ele){ this . CTX . save()const width = ele . width const height = ele . height const centex = ele . left+ele . width/2 const centerY = ele . top+ele . height/2。this . CTX . translate(centex,centery)this . CTX . rotate(ele . rotate)this . CTX . draw image(ele . data,ele . left -centex,ele.top-centery,width,height) this.ctx.restore()

/* * * Draw text * @ param { Object } ele canvas element */Draw text(ele){ this . CTX . save()const width = ele . size * ele . data . length const height = ele . size const centerX = ele . left+width/2 const centerY = ele . top+height/2 this . CTX . translate(centex,centerY)this . CTX . rotate(ele . rotate)this . CTX . font = ` $ { ele . size } px bold sans -serif ` this . CTX . global alphatop # 39console . log( # 39;draw -text # 39;,ele) this.ctx.filltext (ele.data,ele . left -centex,ele . top -centery)this . CTX . stroke text(ele . data,ele . left -centex,ele . top -centery)this . CTX . restore

init controller(ele){ const cs = this . convert 2 controller size(ele)this . CTX . save()this . CTX . stroke style = # 39;# eee # 39this . CTX . translate(cs . centex,cs . centery)this . CTX . rotate(cs . rotate)//繪制虛線邊框this.ctx.setLineDash([10,5],5)this . CTX . strokerect(cs . left -cs . centex,cs.top-cs.centery,cs.width,cs . height)//繪制控制點-旋轉this . CTX . Draw image(this . image . rotate,cs . left+cs . width -10-cs . centerx,

//畫布渲染函數render canvas(){ this . CTX . clear rect(0,0,this.ctx.canvas.width,this . CTX . canvas . height)this . draw grid()console . log( # 39;draw -背景 # 39;,this . background)if(this . background)this . draw image(this . background)for(設I = 0;我 ltthis . elements . length;++){ constele = this . elements[I]//渲染背景if(ele . type = = = # 39;背景 # 39;){ this . draw image(ele)} if(ele . type = = = # 39;貼紙 # 39;){this.drawImage(ele)} //渲染文本if(ele . type = = # 39;正文 # 39;){this.drawText(ele)} //選擇元素添加控件元素if(this . active index = = I){ this . init controller(ele)} }事件監控。

移動

//移動事件綁定函數handleMove(e){ console . log( # 39;鼠標-移動 # 39;,e)如果(e . touches . length gt;1)返回常數x = e.touches[0]。x常數y = e.touches[0]。y const dx = this.startTouches[0]。x ^ -x const dy = this . start touches[0]。y -y const elements = this . elements . slice()elements[this . active index | | 0]。left = this . start selected . left -dx元素[this.activeIndex || 0]。top = this . start selected . top -dy store . commit( # 39;setElements # 39,elements)} rotate//旋轉綁定函數handle rotate(e){ console . log( # 39;handleRotate # 39)const start = this . start touches[0]const end = e . touches[0]const center = { x:this . start selected . centerx,y:this . start selected . centery } const start length = math . sqrt((center . x -start . x)* * 小編(center . y -start . y)* * 2)const end length = math . sqrt((center . x -end . x)* * 小編(center . y -end . y)* * 2)const radian = this . convern正文 # 39;){ selected . left = this . start selected . centrx -this . start selected . size * this . start selected . data . length * scale/2 selected . top = this . start selected . centery -this . start selected . size * scale/2 selected . size = this . start selected . size * scale } if(selected . type = = = = # 39;貼紙 # 39;){ selected . left = this . start selected . centrx -this . start selected . width * scale/2 selected . top = this . start selected . centery -this . start selected . height * scale/2 selected . width = this . start selected . width * scale selected . height = this . start selected . height * scale } store . # 39;setElements # 39,elements)}縮放//縮放事件綁定函數handle scale(e){ if(e . touches . length!== 2 || this.mode!== '背景 # 39;)返回const start length = math . sqrt((this . start touches[0])。x - this.startTouches[1]。x) ** 2 + (this.startTouches[0])。y ^ -this . start touches[1]。y)* * 2)const endLength = math . sqrt((e . touches[0])。x - e.touches[1]。x) ** 2 + (e.touches[0]。y ^ -e . touches[1]。y)* * 2)const scale = end length/start length const elements = this . elements . slice()const selected = elements[this . active index | | 0]selected . left = this . start selected . width * scale/2 selected . top = this . start selected . centery -this . start selected . height * scale/2 selected . width = this . start selected . height * scale//elements[this . active index | | 0]。scale = this . start selected . scale * scale store . commit( # 39;setElements # 39,elements)}導出圖片export() {if(!store . state . elements . length){ wx . show toast({ title: # 39;添加東西然后導出 # 39;,圖標: # 39;無 # 39;})return } wx . show modal({ title: # 39;提示 # 39;,內容: # 39;將圖片保存到您的手機相冊 # 39;,success(RES){ if(RES . confirm){ console . log( # 39;export -畫布 # 39;,store . state . CTX)const canvas = store . state . canvas wx . canvastotempfilepath({ x:0,y: 0,width: canvas.width,height: canvas.height,canvas,complete(RES){ if(RES . errmsg = = = # 39;canvasToTempFilePath:ok # 39;){ wx . saveimagetophotosalbum({ file path:RES . tempfile path,success(RES){ wx . show toast({ title: # 39;圖像保存成功 # 39;,圖標: # 39;無 # 39;} } } } } } } } } } } } }以上是使用小程序canvas編寫簡單圖片應用的細節。更多請關注主機參考其他相關文章!

這幾篇文章你可能也喜歡:

本文由主機參考刊發,轉載請注明:使用applet canvas編寫一個簡單的圖片應用程序(js下載canvas圖片) http://www.k2270.com/81269.html

【騰訊云】領8888元采購禮包,搶爆款云服務器 每月 9元起,個人開發者加享折上折!
打賞
轉載請注明原文鏈接:主機參考 » 使用applet canvas編寫一個簡單的圖片應用程序(js下載canvas圖片)
主機參考僅做資料收集,不對商家任何信息及交易做信用擔保,購買前請注意風險,有交易糾紛請自行解決!請查閱:特別聲明

評論 搶沙發

評論前必須登錄!