gpt-3.5-turbo-0301· OpenAI
WildChat-4.8M (AI2)
uniapp 绘制黑底白色
- User
uniapp 绘制黑底白色
- Assistant
的矩形
在uniapp中绘制黑底白色的矩形,可以使用uniapp的canvas组件。
首先在页面中添加canvas组件:
<canvas id="myCanvas" :style="{width: '100%', height: '100vh'}"></canvas>然后在页面的onReady或onLoad生命周期中,获取canvas对象并进行绘制操作:
onReady() { const ctx = uni.createCanvasContext('myCanvas', this); ctx.setFillStyle('#000000'); // 设置填充颜色为黑色 ctx.fillRect(0, 0, uni.getSystemInfoSync().windowWidth, uni.getSystemInfoSync().windowHeight); // 绘制一个矩形填充整个canvas ctx.setFillStyle('#ffffff'); // 设置填充颜色为白色 const width = uni.getSystemInfoSync().windowWidth * 0.8; // 获取canvas宽度的80% const height = width * 0.5; // 计算矩形高度为宽度的一半 const x = (uni.getSystemInfoSync().windowWidth - width) / 2; // 计算矩形左上角x坐标 const y = (uni.getSystemInfoSync().windowHeight - height) / 2; // 计算矩形左上角y坐标 ctx.fillRect(x, y, width, height); // 绘制一个矩形,位于canvas中间 ctx.draw(); // 将所有绘制操作进行渲染 }以上代码会在canvas中绘制一个黑底白色的矩形,矩形位于canvas中间,宽度占80%的canvas宽度,高度为宽度的一半。