【问题标题】:How I add a download link on my canvas after i change the image?更改图像后如何在画布上添加下载链接?
【发布时间】:2019-03-30 15:47:18
【问题描述】:

我试图为我的图像创建一个下载链接,在你点击它后图像旋转后,然后你会点击下载以获取新的更改图像, 谢谢 ///////////////////////////////////////// ///////////////////////////////////////// ///////////////////////////////////////// ///////////////////////////////////////// ///////////////////////////

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
  <title>Flipping photo in a canvas tag</title>
  <style>
    #canvas {cursor: pointer;}

       canvas {
    height: 50vh;    

    }  


    </style>
</head>
<body>




<label>Image File:</label><br/>
<input type="file" id="imageLoader" name="imageLoader"/>

    <h1>Example of using <code>&lt;canvas&gt;</code></h1>

    <p>This example shows how a photo is loaded in a <code>&lt;canvas&gt;</code> tag
    and then flipped.</p>

    <p>Click on the photo to flip (provided, of course, that your browser supports <code>&lt;canvas&gt;</code>)</p>

    <canvas id="canvas" style="border:1px red solid;"></canvas>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

    <script type="text/javascript">




        var imageLoader = document.getElementById('imageLoader');
    imageLoader.addEventListener('change', handleImage, false);


        var can = document.getElementById('canvas');
        var ctx = can.getContext('2d');




        var img = new Image();


function handleImage(e){
    var reader = new FileReader();
    reader.onload = function(event){

        img.onload = function(){
            can.width = img.width;
            can.height = img.height;
            ctx.drawImage(img, 0, 0, img.width, img.height);
            ctx.save();
        }
        img.src = event.target.result;
    }
    reader.readAsDataURL(e.target.files[0]);     
}




        can.onclick = function() {
            console.log('here');
            ctx.translate(img.width-1, img.height-1);
            ctx.rotate(Math.PI);
            ctx.drawImage(img, 0, 0, img.width, img.height);

        };    


    </script>


    <p><a href="http://www.phpied.com/photo-canvas-tag-flip/">Blog post is here</a></p>

</body>
</html>

【问题讨论】:

    标签: javascript jquery html css canvas


    【解决方案1】:

    如果将画布转换为数据 url,则可以将其作为 href 属性添加到具有下载属性的链接元素中。

    HTML:

    <canvas id="canvas"></canvas>
    <a download="new-image.png" id="download">Download</a>
    

    Javascript:

    var canvas = document.getElementById('canvas');
    var button = document.getElementById('download');
    button.setAttribute( 'href', canvas.toDataURL('image/png', 1) )
    

    工作示例:https://jsfiddle.net/x867ycek/

    【讨论】:

      【解决方案2】:

      你可以试试这个,但要记住并不是所有的浏览器都支持 canvas.toBlob()。

      从您的代码中传入画布变量can

      function downloadAsFile( canvas, imagename, mime ) {
          mime = mime || 'image/png';
          imagename = imagename || 'canvasImage.png';
          canvas.toBlob( blob => {
            if ( window.navigator.msSaveBlob ) { // IE and Edge
              window.navigator.msSaveBlob( blob, imagename );
            }
            else { // Chrome, Firefox.  Not tested: Safari 
              const url = window.URL.createObjectURL( blob );
              const a = document.createElement( 'a' );
              document.body.appendChild( a );
              a.href = url;
              a.download = imagename;
              a.setAttribute( 'style', 'display:none;' );
              a.click();
              setTimeout( () => {
                window.URL.revokeObjectURL( url );
                document.body.removeChild( a );
              }, 2000);
            }
          }, mime );
        }
      

      【讨论】:

        猜你喜欢
        • 2013-08-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-10
        • 1970-01-01
        • 1970-01-01
        • 2011-10-04
        • 2021-09-25
        相关资源
        最近更新 更多