canvas绘制白色描边圆形头像(CV必备)

// headerPhtotTop 头像距离顶部距离
// headerPhtotLeft 头像距离左边距离
// headerPhtot.path 图片地址
// 54为图片的宽高,这里偷懒了直接给了事数,可以自己写一个声明
let headerPhtotTop = 778;
let headerPhtotLeft = 95;
const circle = {
  x: 54 / 2 + headerPhtotLeft,
  y: 54 / 2 + headerPhtotTop,
  r: 54 / 2,
};

//开始路径画圆,剪切处理 设置头像白色边框
ctx.save();
ctx.beginPath();
ctx.arc(circle.x, circle.y, circle.r + 2, 0, Math.PI * 2, false);
ctx.clip(); //剪切路径
ctx.fillStyle = "#ffffff";
ctx.fillRect(headerPhtotLeft - 2, headerPhtotTop - 2, 54 + 4, 54 + 4);
ctx.restore();
// 设置圆形头像
ctx.save();
ctx.beginPath();
ctx.arc(circle.x, circle.y, circle.r, 0, Math.PI * 2, false);
ctx.clip(); //剪切路径
ctx.drawImage(headerPhtot.path, headerPhtotLeft, headerPhtotTop, 54, 54);
//恢复状态
ctx.restore();

canvas的坑还挺多的,所以多半都是需要自己微调,

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值