微信小程序尺寸設置可使用rpx來標記尺寸,類同rem可在微信小程序中自適應兼容換算不同的機型尺寸。
但在小程序canvas中,尺寸換算會無效(由于繪畫的滯后),因此需要自適應計算canvas尺寸。
一.解決方案
使用小程序提供的getSystemInfo可以獲取當前設備的系統信息,通過獲取當前的設備尺寸,來換算所需要設置的canvas尺寸,從而使得canvas繪圖可以適用于所有設備。
wx.getSystemInfo(OBJECT)
獲取系統信息。
OBJECT參數說明:

success回調參數說明:

示例代碼:
-
wx.getSystemInfo({
-
success: function(res) {
-
console.log(res.model)
-
console.log(res.pixelRatio)
-
console.log(res.windowWidth)
-
console.log(res.windowHeight)
-
console.log(res.language)
-
console.log(res.version)
-
console.log(res.platform)
-
}
-
})
二.計算示例
獲取當前窗口寬、高以動態換算需要的canvas尺寸
-
wx.getSystemInfo({
-
//獲取系統信息成功,將系統窗口的寬高賦給頁面的寬高
-
success: function (res) {
-
that.width = res.windowWidth
-
that.height = res.windowHeight
-
that.radius = 105 / 602 * that.height
-
}
-
})
|