注冊

微信小程序畫布canvas,微信小程序畫布屬性實(shí)例

2017-12-22
導(dǎo)讀:canvas 畫布。 屬性名 類型 默認(rèn)值 說明 canvas-id String canvas 組件的唯一標(biāo)識符 disable-scroll Boolean false 當(dāng)在 canvas 中移動時(shí)且有綁定手勢事件時(shí),禁止屏幕滾動以及下拉刷新 bindtouchstart Eve...

canvas


畫布。

屬性名 類型 默認(rèn)值 說明
canvas-id String   canvas 組件的唯一標(biāo)識符
disable-scroll Boolean false 當(dāng)在 canvas 中移動時(shí)且有綁定手勢事件時(shí),禁止屏幕滾動以及下拉刷新
bindtouchstart EventHandle   手指觸摸動作開始
bindtouchmove EventHandle   手指觸摸后移動
bindtouchend EventHandle   手指觸摸動作結(jié)束
bindtouchcancel EventHandle   手指觸摸動作被打斷,如來電提醒,彈窗
bindlongtap EventHandle   手指長按 500ms 之后觸發(fā),觸發(fā)了長按事件后進(jìn)行移動不會觸發(fā)屏幕的滾動
binderror EventHandle   當(dāng)發(fā)生錯(cuò)誤時(shí)觸發(fā) error 事件,detail = {errMsg: 'something wrong'}

注:

  1. canvas標(biāo)簽?zāi)J(rèn)寬度300px、高度225px

  2. 同一頁面中的canvas-id不可重復(fù),如果使用一個(gè)已經(jīng)出現(xiàn)過的canvas-id,該canvas標(biāo)簽對應(yīng)的畫布將被隱藏并不再正常工作

示例代碼:

<!-- canvas.wxml -->
<canvas style="width: 300px; height: 200px;" canvas-id="firstCanvas"></canvas>
<!-- 當(dāng)使用絕對定位時(shí),文檔流后邊的canvas的顯示層級高于前邊的canvas-->
<canvas style="width: 400px; height: 500px;" canvas-id="secondCanvas"></canvas>
<!-- 因?yàn)閏anvas-id與前一個(gè)canvas重復(fù),該canvas不會顯示,并會發(fā)送一個(gè)錯(cuò)誤事件到AppService -->
<canvas style="width: 400px; height: 500px;" canvas-id="secondCanvas" binderror="canvasIdErrorCallback"></canvas>
// canvas.js
Page({
  canvasIdErrorCallback: function (e) {
    console.error(e.detail.errMsg)
  },
  onReady: function (e) {
    //使用wx.createContext獲取繪圖上下文context
    var context = wx.createCanvasContext('firstCanvas')

    context.setStrokeStyle("#00ff00")
    context.setLineWidth(5)
    context.rect(0,0,200,200)
    context.stroke()
    context.setStrokeStyle ("#ff0000")
    context.setLineWidth(2)
    context.moveTo(160,100)
    context.arc(100,100,60,0,2*Math.PI,true)
    context.moveTo(140,100)
    context.arc(100,100,40,0,Math.PI,false)
    context.moveTo(85,80)
    context.arc(80,80,5,0,2*Math.PI,true)
    context.moveTo(125,80)
    context.arc(120,80,5,0,2*Math.PI,true)
    context.stroke()
    context.draw()
  }
})

相關(guān)api:wx.createCanvasContext

 

Bug & Tip

  1. tip:canvas組件是由客戶端創(chuàng)建的原生組件,它的層級是最高的。
  2. tip: 請勿在scroll-view中使用canvas組件。
  3. tip:css動畫對canvas組件無效。
更多微信小程序開發(fā)教程,可以關(guān)注hi小程序。
重磅推薦:小程序開店目錄

第一部分:小商店是什么

第二部分:如何開通一個(gè)小商店

第三部分:如何登錄小商店

第四部分:開店任務(wù)常見問題

第五部分:小商店可以賣什么

第六部分:HiShop小程序特色功能

第七部分:小程序直播

第八部分:小程序收貨/物流

第九部分:小程序怎么結(jié)算

第十部分:小程序客服

第十一部分:電商創(chuàng)業(yè)

第十二部分:小程序游戲開發(fā)