canvas图片处理小案例

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .container {
            width: 0;
            height: 0;
            overflow: hidden;
        }

        canvas {
            border: 1px solid #000;
        }
    </style>
</head>

<body>
    <form>
        <input type="file" name="img" id="btn">
    </form>
    <div class="container">
        <canvas width="0" height="0"></canvas>
    </div>
    <script>
        dataURLtoBlob = function (dataurl) {
            var arr = dataurl.split(','),
                mime = arr[0].match(/:(.*?);/)[1],
                bstr = atob(arr[1]),
                n = bstr.length,
                u8arr = new Uint8Array(n);
            while (n--) {
                u8arr[n] = bstr.charCodeAt(n);
            }
            return new Blob([u8arr], { type: mime });
        }
        //将blob转换为file
        blobToFile = function (theBlob, fileName) {
            theBlob.lastModifiedDate = new Date();
            theBlob.name = fileName;
            return theBlob;
        }

        var from = document.querySelector('#btn')
        from.onchange = function (e) {
            var imgurl = window.URL.createObjectURL(from.files[0])
            console.log(imgurl)
            var image = new Image()
            image.onload = function () {
                var myCanvas = document.querySelector('canvas')
                var ctx = myCanvas.getContext('2d')
                var imageWidth = image.width
                var imageHeight = image.height
                myCanvas.height = 750 * imageHeight / imageWidth
                myCanvas.width = 750
                ctx.drawImage(image, 0, 0, imageWidth, imageHeight, 0, 0, 750, 750 * imageHeight / imageWidth)
                var imageData = new Image();
                // canvas.toDataURL 返回的是一串Base64编码的URL
                // 指定格式 PNG  
                imageData.src = myCanvas.toDataURL("image/png");
                var bloburl = myCanvas.toDataURL();
                // 转file对象
                //调用
                var blob = dataURLtoBlob(bloburl);
                var file = blobToFile(blob, 'new');
                console.log(file)
                console.log(typeof(file))
                // 保存到本地
                // console.log('bloburl', bloburl);
                // var anchor = document.createElement('a');
                // if ('download' in anchor) {
                //     anchor.style.visibility = 'hidden';
                //     anchor.href = bloburl;
                //     anchor.download = name;

                //     document.body.appendChild(anchor);
                //     var evt = document.createEvent('MouseEvents');
                //     evt.initEvent('click', true, true);
                //     anchor.dispatchEvent(evt);

                //     document.body.removeChild(anchor);
                // } else {
                //     location.href = bloburl;
                // }
            }
            image.src = imgurl
        }

    </script>
</body>

</html>
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容