首页 > canvas 跨域

canvas 跨域

我这个下面的canvas.toDataURL报错,是因为上面的picUrl图片地址跨域,有什么办法解决?

            img.src = picUrl;//图片跨域了
            
            img.onload = function(){
                
                var ctx = canvas.getContext("2d");
                ctx.clearRect(0,0,canvas.width,canvas.height);
                if(img.height > 800){
                   img.width *= 800 / img.height;
                   img.height = 800;
                }                
                if(img.width > 800){
                   img.height *= 800 / img.width;
                   img.width = 800;
                }
                canvas.width = img.width;
                canvas.height = img.height;
                ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
                var imgUrl = canvas.toDataURL("image/jpeg");
            }

同上,你还可以在自己的不跨域的服务器里加个接口,让他去抓图片,前端用的图片访问这个接口


想办法解决跨域问题,两种思路:

【热门文章】
【热门文章】