微信小程序canvas绘制渐变背景颜色方法
作者:xlnxin发布时间:2023-10-06分类:其他教程浏览:1291
导读:<template><view class="mapp-container"><canvas ...
<template>
<view class="mapp-container">
<canvas :style="{width: canvasWidth + 'px',height:canvasHeight + 'px'}" type="2d" canvas-id="myCanvas" id="myCanvas"></canvas>
</view>
</template>
<script>
export default {
data() {
return {
canvasHeight: 0,
canvasWidth: 0
}
},
onLoad() {
let {
windowHeight,
statusBarHeight
} = uni.getSystemInfoSync();
this.canvasHeight = windowHeight;
this.canvasWidth = this.rpxToPx(750);
setTimeout(()=>{
this.init()
},50)
},
methods: {
init() {
let canvasH = this.canvasHeight;
let canvasW = this.canvasWidth;
const query = wx.createSelectorQuery()
query.select('#myCanvas')
.fields({node: true,size: true})
.exec((res) => {
const canvas = res[0].node;
const ctx = canvas.getContext('2d');
const dpr = wx.getSystemInfoSync().pixelRatio
canvas.width = res[0].width * dpr // 获取宽
canvas.height = res[0].height * dpr // 获取高
ctx.scale(dpr, dpr)
// 绘制背景
const grd = ctx.createLinearGradient(0, 0, 0, canvasH)
grd.addColorStop(0, '#27D0FF')
grd.addColorStop(0.5, '#ffffff')
ctx.fillStyle = grd;
ctx.fillRect(0, 0, canvasW, canvasH)
})
},
// rpx转px
rpxToPx(rpx) {
const screenWidth = uni.getSystemInfoSync().screenWidth
return (screenWidth * Number.parseInt(rpx)) / 750
},
}
}
</script>- 上一篇:php 验证只能输入汉字/数字/英文
- 下一篇:js删除数组中键值为1的元素组
- 其他教程排行
-
- 1抖音极速版给大家发37元过年红包啦,快来领取吧!
- 2统信uos系统默认root密码是多少
- 3admin、0000、admin888、123456 md5加密后16位和32位代码
- 4国产安可立思辰打印机手动双面打印设置教程
- 5Windows10 DVDRW识别为CD驱动解决方法,windows无法读取驱动器E:\中的光盘解决方法
- 6navicat premium 15破解版64位32位 v15.0.3激活方法
- 7小程序Cannot read property 'enableUpdateWxAppCode'解决方法
- 8快手极速版给大家发56元新年红包啦,快来领取吧!
- 9查找对话框跑出屏幕外看不见了,怎么拖回来?
- 最近发表

