如何使用这些stackoverflow答案保存应用了过滤器的画布?

时间:2017-05-11 08:08:03

标签: javascript html5 css3 canvas css-filters

我知道有多次询问如何将CSS过滤器实际应用于图像的问题。给出了一些实际上不使用CSS过滤器的答案,但仍然可以完成工作。不幸的是,我对JavaScript很新,我不明白这些答案。

所以,我的问题分为两部分:

  1. 由于其中一些答案是旧的,有没有一种方法可以保存使用CSS过滤器的画布图像?

  2. 如果没有,我如何实现其中一个stackoverflow答案中描述的解决方案?我基本上需要一个实际应用多个滤镜到图像或像素的代码示例,这些代码示例未包含在原始答案中。

  3. 谢谢

    数目:

    在第一个答案中,我迷失了循环中的“......”。在第二个答案中,没有关于第4步的详细信息。如果我是正确的,这两个都使用CSS3过滤器,这是我的首选Capture/save/export an image with CSS filter effects applied

    我迷失在这一步的第3步:is anyone solve about saving filtered canvas as an image?

    同样,我迷失了非CSS过滤器的应用:How to save image from canvas with css filters

    JavaScript的:

      // I know I eventually need to move these globals so they're passed as parameters.        
      var image; 
      var c; 
      var context; 
      var file; 
    
      // Begin File Open Code. 
      function fileOpen()
      {
        var fileInput = document.getElementById('fileInput');
        var fileDisplayArea = document.getElementById('iDisplay');
    
        fileInput.addEventListener('change', function(e)
        {
          file = fileInput.files[0];
          var imageType = /image.*/;
          if (file.type.match(imageType)) 
          {
            var reader = new FileReader();
            reader.onload = function(e)
            {                 
              iDisplay.innerHTML = "";
              image = new Image();
    
              image.src = reader.result;
              image.id = "I";               // Add an id attribute so the image editor code can access the image. // Image has since been moved to canvas.   
              iDisplay.appendChild(image);
              image.onload = function()
              {
                c = document.getElementById("C"); 
                context = c.getContext("2d");
                c.width = image.width; 
                c.height = image.height; 
                context.drawImage(image,0,0);  
              }                
            }
            reader.readAsDataURL(file); 
                }
          else
          {
            iDisplay.innerHTML = "File type not supported."
                }
            }); 
        setValues(); // Call setValues() as function exits to set Image Editing Code Hue Slider to 0.  
      }
      // End File Open Code 
    
      // Begin Image Editing Code. 
      var degrees = 0;
      var percentB = 100; 
      var percentC = 100;
    
      // Set initial values of sliders 
      function setValues()
      {      
        form1.brightness.value = 100;
        form1.contrast.value = 100;
        form1.hue.value = 0;      
      }
    
      // Get slider settings and apply changes to image
      function apply()
      { 
        degrees = form1.hue.value;
        percentC = form1.contrast.value;
        percentB = form1.brightness.value;
        // This is the crux of my question: How do I apply this properly, using the stackoverflow answers, so that the edited image may be saved?   
        if (document.getElementById("C") != null) document.getElementById("C").style.filter = "brightness(" + parseInt(percentB) + "%)" + " contrast(" + parseInt(percentC) + "%)" + " hue-rotate(" + parseInt(degrees) + "deg)";   
        if (document.getElementById("C") != null) document.getElementById("C").style.WebkitFilter = "brightness(" + parseInt(percentB) + "%)" + " contrast(" + parseInt(percentC) + "%)" + " hue-rotate(" + parseInt(degrees) + "deg)";   
      }
      // End Image Editing Code 
    
      // Canvas was not cooperating, it needed c.width not c.style.width, can probably remove these and replace with small default values.    
      function setCanvasWidth()
      {
        c.width = image.width;  
      }
    
      function setCanvasHeight()
      {
        c.height = image.height;  
      }
    
      // Begin File Save Code (or user can right click and save if filters get applied.  
      function save()
      {
        alert("Place file save code here.")
      }
    

    HTML

     <!doctype html>
     <html lang="en">
     <head>
       <title>HTML5 CSS3 Javascript Image Editor</title>
       <meta charset="utf-8"/>
       <link rel="stylesheet" type="text/css" href="default.css"/>
       <script src="nav.js"></script>
       <script type="text/javascript">
       // JavaScript is in here. 
       </script>  
    
    <!-- style elements specific to this page are placed in the style tags. -->  
    <style>
    
      image  
      {
        max-width: 100%;
        display: block;
        margin: auto; 
        margin-left: auto; 
        margin-right: auto;  
      }
    
      #C 
      {
        max-width: 100%;
        display: block;
        margin: auto;     
        left: 0;
        right: 0;
        margin-left: auto; 
        margin-right: auto;  
      }
    
      #iDisplay 
      {
        margin-top: 2em;
        max-width: 100%;
        overflow-x: auto;
        margin-left: auto; 
        margin-right: auto;  
        display: none; 
      }
    
    </style> 
    </head>
      <body onload="setValues()">
    <header>
      <a href="index.html"><img src="logoglow.png" alt="Logo Image" width="215" height="135" /></a>
      <a href="index.html"><img src="ac.png" alt="Header Image" width="800" height="135" /></a>
    </header>
    <main>
      <h3>An Ongoing Experiment in HTML5 / CSS3 / JavaScript Image Editing</h3>
      <div>
      <!-- Nav is floating left and Main is floating right, clear float styles (IF NEEDED) so they dont interfere with new image. -->     
        <p style="float:left;">  
        <input type="file" id="fileInput" onclick="fileOpen()"></p>
        <br style="clear:both">
      </div>
      <div id="iDisplay"></div>  
      <br style="clear:both"><br style="clear:both">
      <!-- <script>document.write('<img src="' + file + '"/>');</script><br><br> --> 
      <!-- <div style="overflow:scroll;">  -->
      <canvas style="color:#FFFFFF;" id="C" width="javascript:setCanvasWidth()" height="javascript:setCanvasHeight()">Your browser is too old to support this feature. <br>Please consider updating to a modern browser.</canvas>
      <!-- </div> --> 
      <!-- use javascript:function() to get width and height?  -->
      <div id="afterCanvas">  
      <br><br> 
      <form name="form1" id="form1id" action="javascript:save();" style="font-size:90%; text-align:center;">
        Brightness: <input type="range" name="brightness" min="0" max="200" step="5" onmousemove="apply()" ontouchmove="apply()" style="vertical-align:-7px;" />&nbsp; &nbsp;
        Contrast: <input type="range" name="contrast" min="0" max="200" step="5" onmousemove="apply()" ontouchmove="apply()" style="vertical-align:-7px;"/>&nbsp; &nbsp;
        Hue: <input type="range" name="hue" min="0" max="360" step="5" onmousemove="apply()" ontouchmove="apply()" style="vertical-align:-7px;"/>
        <br><br>  
        <input type="submit" style="float:left;" value="Save Changes" /> <!-- chnage to type="submit" if form action is needed --> 
      </form>
      </div>
    </main>
    </body>
    </html>  
    

0 个答案:

没有答案