uniapp canvas文字和元素居中

文字居中:ctx.textAlign = "center";

元素居中:ctx.arc(screenWidth / 2, 122, 40, 0, 2 * Math.PI);

ctx.arc()的x轴为当前屏幕的宽度/2;

javascript 复制代码
let screenWidth = 540;
let screenHeight = 960;
// 头像
if (photoimg) {
    ctx.setFillStyle('#000000')
	ctx.save();
	ctx.beginPath() //开始创建一个路径
	ctx.arc(screenWidth / 2, 122, 40, 0, 2 * Math.PI) //画一个圆形裁剪区域
	ctx.strokeStyle = 'transparent';
	ctx.stroke();
	ctx.clip() //裁剪
	ctx.drawImage(photoimg, 230, 82, 80, 80);
	ctx.restore()	
}
				
// 昵称
if (that.userInfo) {
	let names = that.userInfo.nickname
	ctx.setFontSize(28);
	ctx.setFillStyle('#333333')
	ctx.font = 'normal blod 28px PingFangSC, PingFang SC';
	ctx.textAlign = "center"
	let titleW = ctx.measureText(names)
	ctx.fillText(names, screenWidth / 2, 196);
}
相关推荐
mayaairi2 分钟前
Vue2 生命周期完全指南:从创建到销毁的8个钩子函数
前端·javascript·vue.js
研☆香41 分钟前
一个简单的气泡弹窗制作
javascript
xy34531 小时前
axure9.0 如何打造一个计时器(简单版)
前端·ui·html·axure·原型·产品设计
大模型丫丫1 小时前
用 TypeScript、NestJS 和 LangGraph 搭建一个可控的 AI Agent
javascript·人工智能·typescript
IMPYLH1 小时前
HTML 的 <pre> 元素
前端·javascript·html
shmily麻瓜小菜鸡2 小时前
JavaScript / TypeScript 易踩坑知识点 —— 异步编程类
开发语言·javascript·typescript
tsumikistep2 小时前
【前端】Vue + Axios 跨域问题实战:7070 代理转发 8080 + 401 报错分析(黑马外卖)
前端·javascript·vue.js
DK185838322522 小时前
【源码开源部署】电竞代练护航陪玩小程序:全游戏服务平台完整解决方案(附宝塔部署教程)
游戏·微信小程序·uni-app·开源·php
lichenyang4532 小时前
鸿蒙首页从等高商品 Grid 到双列瀑布流:同一张图也能做出小红书式浏览节奏
前端
里欧跑得慢2 小时前
Flutter主题与样式详解
前端·css·flutter·web