美文网首页
canvas图片处理小案例

canvas图片处理小案例

作者: 剑指流云 | 来源:发表于2023-06-08 14:25 被阅读0次
    <!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>
    

    相关文章

      网友评论

          本文标题:canvas图片处理小案例

          本文链接:https://www.haomeiwen.com/subject/wvdxohtx.html