canvas绘制圆图输出图片格式

  
         function drawCircleImage(url, callback) {
            const canvas = document.createElement('canvas');
            const img = new Image();
            img.setAttribute("crossOrigin", 'Anonymous');
            img.src = url;
            img.onload = function() {
                canvas.width = img.width;
                canvas.height = img.height;
                let ctx = canvas.getContext("2d");
                //获取图片宽高的最小值
                let min = Math.min(img.width, img.height);
                let r = min / 2;
                ctx.fillStyle = ctx.createPattern(img, 'no-repeat');
                ctx.clearRect(0, 0, img.width, img.height);
                ctx.arc(img.width / 2, img.height / 2, r, 0, Math.PI * 2);
                ctx.fill();
       let base64 = canvas.toDataURL("image/jpeg");
       callback(base64); 
    }; 
   }    

  

posted @ 2019-04-15 14:39  Peter_Yang0942  阅读(478)  评论(0编辑  收藏  举报