微信小程序canvas尺寸的設置方法教程
微信小程序尺寸設置可使用rpx來標記尺寸,類同rem可在微信小程序中自適應兼容換算不同的機型尺寸。
但在小程序canvas中,尺寸換算會無效(由于繪畫的滯后),因此需要自適應計算canvas尺寸。
一.解決方案
使用小程序提供的getSystemInfo可以獲取當前設備的系統(tǒng)信息,通過獲取當前的設備尺寸,來換算所需要設置的canvas尺寸,從而使得canvas繪圖可以適用于所有設備。
wx.getSystemInfo(OBJECT)
示例代碼:
-
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) -
} -
})
二.計算示例
獲取當前窗口寬、高以動態(tài)換算需要的canvas尺寸
-
wx.getSystemInfo({ -
//獲取系統(tǒng)信息成功,將系統(tǒng)窗口的寬高賦給頁面的寬高 -
success: function (res) { -
that.width = res.windowWidth -
that.height = res.windowHeight -
that.radius = 105 / 602 * that.height -
} -
})
更多小程序資訊,盡在:hydrodefense.cn/xiaocx/


立即掃碼關注

多門店/直營/加盟連鎖管理系統(tǒng)