uniapp、小程序canvas相关

  • 1、圆形or圆形头像
javascript 复制代码
//示例
const ctx = uni.createCanvasContext('myCanvas'); //canvas
const round = uni.upx2px(72) / 2; // 半径
const x = uni.upx2px(92); //目标x轴位置
const y = uni.upx2px(236); //目标y轴位置

//if 图片是不是静态资源
async =>
const imgSrc = 'https://xxxxxxxxxx';
const imgRes = await uni.getImageInfo({
	src: imgSrc
});
const url = await imgRes?.path;

//else
const imgSrc = '项目静态文件/xxx.png';

drawRound(ctx, round, x, y, url); //调用

const drawRound = (ctx, round, x, y, img) => {
	ctx.save();
	ctx.beginPath();
	let r = round;
	let d = 2 * r;
	let cx = x + r;
	let cy = y + r;
	ctx.arc(cx, cy, r, 0, 2 * Math.PI);
	ctx.clip();
	ctx.drawImage(img, x, y, d, d);
	ctx.restore();
};
  • 2、填充背景
javascript 复制代码
const ctx = uni.createCanvasContext('myCanvas'); //canvas
width, height => canvas的width,height
const createCanvasbj = (ctx, width, height) => {
	ctx.beginPath();
	ctx.rect(0, 0, width, height);
	ctx.setFillStyle('#FAFAFA');
	ctx.fill();
};
  • 3、文字
javascript 复制代码
ctx.setFillStyle('#646978');
ctx.setFontSize(20);
ctx.fillText('123123', x, y);

*4、阴影

javascript 复制代码
const createShadow = () => {
	ctx.beginPath();
	ctx.shadowOffsetX = 0;
	ctx.shadowOffsetY = 5;
	ctx.shadowColor = '#D4D4D4';
	ctx.shadowBlur = 10;
	ctx.rect(x, y, Width, Height);
	ctx.setFillStyle('#FFFFFF');
	ctx.fill();
};

*5、圆角矩形

javascript 复制代码
const createRoundedrectangle = ( ctx, tagLeft, tagWidth, tagTop, tagHeight, radius) => {
	//tagLeft >= x
	//tagTop >= y
	//tagHeight => width
	//radius => 圆角
	
	ctx.beginPath();
	ctx.arc(tagLeft + tagWidth - radius, tagTop + tagHeight - radius, radius, 0, Math.PI * 0.5);
	ctx.lineTo(tagLeft + radius, tagTop + tagHeight);
	ctx.arc(tagLeft + radius, tagTop + tagHeight - radius, radius, Math.PI * 0.5, Math.PI);
	ctx.lineTo(tagLeft, tagTop + radius);
	ctx.arc(tagLeft + radius, tagTop + radius, radius, Math.PI, Math.PI * 1.5);
	ctx.lineTo(tagLeft + tagWidth - radius, tagTop);
	ctx.arc(tagLeft + tagWidth - radius, tagTop + radius, radius, Math.PI * 1.5, Math.PI * 2);
	ctx.lineTo(tagLeft + tagWidth, tagTop + tagHeight - radius);
	ctx.closePath();
	ctx.setStrokeStyle('#E60012');
	ctx.setFillStyle('#E60012');
	ctx.fill();
};
相关推荐
lqj_本人41 分钟前
鸿蒙OS&UniApp 开发的下拉刷新与上拉加载列表#三方框架 #Uniapp
华为·uni-app·harmonyos
lqj_本人2 小时前
鸿蒙OS&UniApp 制作个人信息编辑界面与头像上传功能#三方框架 #Uniapp
uni-app·harmonyos
lqj_本人2 小时前
鸿蒙OS&UniApp 实现的二维码扫描与生成组件#三方框架 #Uniapp
uni-app
熊猫钓鱼>_>2 小时前
中国版Cursor,基于腾讯云CodeBuddy的节日推荐小程序的智能化开发实践
小程序·腾讯云·节日
AC-PEACE4 小时前
小程序初始化加载时间优化 步骤思考与总结
react.js·小程序·前端框架
老李不敲代码4 小时前
榕壹云打车系统:基于Spring Boot+MySQL+UniApp的开源网约车解决方案
spring boot·mysql·微信小程序·uni-app·软件需求
说私域4 小时前
桑德拉精神与开源链动2+1模式AI智能名片S2B2C商城小程序的协同价值研究
人工智能·小程序·开源·零售
说私域6 小时前
基于开源AI大模型与S2B2C生态的个人品牌优势挖掘与标签重构研究
人工智能·小程序·开源·零售
wx_cxc28486989187 小时前
商城小程序源码介绍
微信小程序·小程序
lqj_本人8 小时前
鸿蒙OS&UniApp 开发实时聊天页面的最佳实践与实现#三方框架 #Uniapp
uni-app