javascript - canvas base64保存的jpeg图片是黑色
怪我咯
怪我咯 2017-04-10 17:04:25
[JavaScript讨论组]

我在右框输出canvas生成的base64后是黑色背景这是怎么回事

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <style type="text/css">
            body{margin: 0;padding: 0;}
            .box{width:100%;overflow:hidden;margin: 0 auto;}
            .piclistbox{width: 50%;float: left;overflow: hidden;border: 1px solid red;box-sizing: border-box;height:300px;}
            .canvasbox{width: 50%;float:right;overflow: hidden;border: 1px solid red;box-sizing: border-box;height:300px;}
        </style>
    </head>
    <body>
        <p class="box">
        <p class="piclistbox">
            <canvas id="pic_canvas" width="400" height="400"></canvas>
        </p>
        <p class="canvasbox">
            <img src="#" id="imgshow"/>
        </p>
        </p>
        <input type="button" value="生成" class="btnpoptailor"></input>
    </body>
    <script type="text/javascript">
                var canvas = document.getElementById('pic_canvas');
                var img = new Image();
                var _this = this;
                img.src = "http://img1.3lian.com/img013/v1/83/d/1.jpg";
                img.onload = function(){
                    
                    var ctx = canvas.getContext("2d");
                    ctx.clearRect(0,0,canvas.width,canvas.height);
                    if(img.height > 400){
                       img.width *= 400 / img.height;
                       img.height = 400;
                    }                
                    if(img.width > 400){
                       img.height *= 400 / img.width;
                       img.width = 400;
                    }
                    canvas.width = img.width;
                    canvas.height = img.height;
                    
                    ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
                }
                var imgUrl = canvas.toDataURL("image/jpeg");
                document.getElementById("imgshow").setAttribute('src',imgUrl);
    </script>
</html>

链接地址代码

怪我咯
怪我咯

走同样的路,发现不同的人生

全部回复(2)
PHPz

有两个问题:
1.图片跨域问题
这里有答案,需要翻墙

2.这段代码应该要放到图片onload后执行。

   var imgUrl = canvas.toDataURL("image/jpeg");
   document.getElementById("imgshow").setAttribute('src',imgUrl);

下面是转自 stackoverflow 的答案,自行翻译下,大概意思就是图片跨域问题造成的toDataUrl方法不能用。

33
down vote
accepted
Unless google serves this image with the correct Access-Control-Allow-Origin header, then you wont be able to use their image in canvas. This is due to not having CORS approval. You can read more about this here, but it essentially means:

Although you can use images without CORS approval in your canvas, doing so taints the canvas. Once a canvas has been tainted, you can no longer pull data back out of the canvas. For example, you can no longer use the canvas toBlob(), toDataURL(), or getImageData() methods; doing so will throw a security error.

This protects users from having private data exposed by using images to pull information from remote web sites without permission.
I suggest just passing the URL to your server-side language and using curl to download the image. Be careful to sanitise this though!

EDIT:

As this answer is still the accepted answer, you should check out @shadyshrif's answer, which is to use:

var img = new Image();
img.setAttribute('crossOrigin', 'anonymous');
img.src = url;
This will only work if you have the correct permissions, but will at least allow you to do what you want
高洛峰

var imgUrl = canvas.toDataURL("image/jpeg");
这句话必须放在img.onload的方法中,而不是方法之外

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号