/**
* 把图片处理成圆形,如果不是正方形就按最小边一半为半径处理
* @param {object} imgObj 图片(img)对象
* @param {number} imgType 设置生成图片的大小:0设置生成的图片大小是以图片设置的css大小为准,1设置生成的图片大小是以图片分辨率为准,默认值为0
* @return {string} return base64 png图片字符串
*/
function circle_image_v2(img, imgType) {
var type = imgType || 0;
var radius, diameter, canvas, ctx, natural;
if (type) {
if (img.naturalWidth > img.naturalHeight) {
radius = img.naturalHeight / 2;
} else {
radius = img.naturalWidth / 2;
}
} else {
if (img.width > img.height) {
radius = img.height / 2;
} else {
radius = img.width / 2;
}
if (img.naturalWidth > img.naturalHeight) {
natural = img.naturalHeight;
} else {
natural = img.naturalWidth;
}
}
diameter = radius * 2;
canvas = document.createElement('canvas');
if (!canvas.getContext) { // 判断浏览器是否支持canvas,如果不支持在此处做相应的提示
console.log('您的浏览器版本过低,暂不支持。');
return false;
}
canvas.width = diameter;
canvas.height = diameter;
contex = canvas.getContext("2d");
contex.clearRect(0, 0, diameter, diameter);
contex.save();
contex.beginPath();
contex.arc(radius, radius, radius, 0, Math.PI * 2, false);
contex.clip();
if (type) {
contex.drawImage(img, 0, 0, diameter, diameter, 0, 0, diameter, diameter);
} else {
contex.drawImage(img, 0, 0, natural, natural, 0, 0, diameter, diameter);
}
contex.restore();
return canvas.toDataURL('image/png');
}

传入要处理的图片对象,返回base64链接

//window.requestAnimationFrame 的兼容

if(!window.requestAnimationFrame){
window.requestAnimationFrame = (
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.msRquestAniamtionFrame ||
window.oRequestAnimationFrame ||
function (callback){
return setTimeout(callback,Math.floor(1000/60))
}
)
} 作者:犯迷糊的小羊
链接:https://www.jianshu.com/p/e70c9cfbdb38
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

参考链接:https://blog.csdn.net/fxss5201/article/details/79691923

最新文章

  1. 一致性算法RAFT详解
  2. Windows安装Scrapy遇坑解决办
  3. iOS架构师之路:控制器(View Controller)瘦身设计
  4. Asp.net NVelocity 模版引擎
  5. 《Linux私房菜》笔记和问题记录
  6. powershell,系统学习的第一种脚本语言
  7. OC学习笔记之属性详解和易错点
  8. 学习和理解C#的委托
  9. Container With Most Water 解答
  10. 几乎没用到过的css 样式
  11. Oracle\PLSQL Developer报“动态执行表不可访问,本会话的自动统计被禁止”的解决方案
  12. CSS 文件的4种引入方式
  13. 【Electron】Electron开发入门(五):项目打包
  14. 机器学习数学|微积分梯度jensen不等式
  15. ASP.NET CORE中使用Cookie身份认证
  16. android studio升级2.3后出现的问题
  17. dataguard丢失归档日志处理
  18. Set集合判断对象重复的方法
  19. Ubuntu16.04中如何启用floodlight的一种方式
  20. android.telephony.SmsManager.sendMultipartTextMessage

热门文章

  1. Python全栈开发:进程代码实例
  2. Python全栈开发:冒泡排序
  3. C++: Type conversions
  4. 网站PC端和移动端,用户通过设备识别
  5. pytorch基础2
  6. centos 6.5 yum安装rabbitMQ
  7. ios与android设备即时语音互通的录音格式预研说明
  8. 设置和修改Linux的swap分区大小
  9. spring retry 重试机制完整例子
  10. python流程控制-条件语句If,while循环