canvas生成圆图和微信小程序canvas圆图
2024-08-27 19:50:48
先在HTML中创建 img和canvas并设置id属性
<canvas id="canvas" width="500" height="500" style="border: 1px solid red;"></canvas> <img src="./dialog.png" id="img" />
js中
微信小程序也可以使用。
当然获取canvas会不一样。微信小程序获取canvas,参考(https://www.cnblogs.com/1748sb/p/12955714.html)。
画圆采用'yiven'博主的:https://www.cnblogs.com/yiven/p/7551535.html 。
let c=document.getElementById('canvas');
let img=document.getElementById('img')
let context=c.getContext('2d');
// 图片加载
img.onload = function(e){
console.log('图片加载成功',e)
drawRoundedImg(50,100,100,100,50,img);
}
// 你会需要一张背景
function backGround(){
context.save();
context.fillStyle="#FFFFFF"
context.fillRect(x,y,w,h);
context.restore();
}
// 你可能还需要生成一些文字
function text(){
context.save();
context.fillStyle="black";
context.font="25px 微软雅黑";
context.fillText('踏步走',x,y); // 名字
context.restore();
}
// 生成一张圆在插入图片
function drawRoundedImg(x,y,w,h,r,bgimg){
context.save();
context.beginPath();
context.moveTo(x+r,y);
context.arcTo(x+w,y,x+w,y+h,r);
context.arcTo(x+w,y+h,x,y+h,r);
context.arcTo(x,y+h,x,y,r);
context.arcTo(x,y,x+w,y,r);
context.lineWidth = 1;//线条的宽度
context.strokeStyle = "red";//线条的颜色
context.stroke();
context.clip();
context.drawImage(bgimg, x, y, w, h);
context.restore();
context.closePath();
}
生成后
最新文章
- Scrapy框架实现爬虫
- MongoDB的配置、启动、关闭
- nuget github host
- 如何对SQL Server 2005进行设置以允许远程连接(转载)
- 【课程分享】基于Lucene4.6+Solr4.6+Heritrix1.14+S2SH实战开发从无到有垂直搜索引擎
- NIO学习:buffer读入与写出(文件复制示例)
- HDU 2502 月之数(简单递推)
- mac 终端常见指令
- UVA - 10098 - Generating Fast (枚举排列)
- C 文件直接包含
- JAVA设计模式:代理模式
- sphinx随笔记了一下
- PHP7的新功能
- 补习系列(15)-springboot 分布式会话原理
- Python strip()与lstrip()、rstrip()
- JMeter 连接 sql server
- saltstack SLS
- Python3网络爬虫(四):使用User Agent和代理IP隐藏身份《转》
- nslookup debug
- Dart async proc