title: How do I save a canvas with filters applied, using these stackoverflow answers?
body: <p>I'm aware that the question of how to actually apply CSS filters to an image has been asked several times.  Some answers are given that do not actually use CSS filters, but would still appear to get the job done.  Unfortunately, I'm very new to JavaScript, and I do not understand these answers.  </p>
  
  <p>So, my question is in two parts: </p>
  
  <ol>
  <li><p>Since some of these answers are old, has a way been developed of saving a canvas image with CSS filters applied? </p></li>
  <li><p>If not, how do I implement the solutions described in one of these stackoverflow answers?  I essentially need a code sample of actually applying multiple filters to the image or pixels, which was not contained in the original answers.  </p></li>
  </ol>
  
  <p>Thank you </p>
  
  <p><strong>Answers:</strong></p>
  
  <p>In the first answer, I'm lost on the "..." in the loop. In the second answer, there are no details on step 4. And if I'm correct, both of these use CSS3  filters, <em>which is my preferred choice</em>: <a href="https://stackoverflow.com/questions/14270897/capture-save-export-an-image-with-css-filter-effects-applied">Capture/save/export an image with CSS filter effects applied</a>   </p>
  
  <p>I'm lost on step 3 of this one: <a href="https://stackoverflow.com/questions/31906939/is-anyone-solve-about-saving-filtered-canvas-as-an-image">is anyone solve about saving filtered canvas as an image?</a>  </p>
  
  <p>Again, I'm lost on the application of the non-CSS filters: <a href="https://stackoverflow.com/questions/30408939/how-to-save-image-from-canvas-with-css-filters">How to save image from canvas with css filters</a></p>
  
  <p><strong>JavaScript:</strong></p>
  
  <pre><code>  // 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.")
    }
  </code></pre>
  
  <p><strong>HTML</strong>     </p>
  
  <pre><code> &lt;!doctype html&gt;
   &lt;html lang="en"&gt;
   &lt;head&gt;
     &lt;title&gt;HTML5 CSS3 Javascript Image Editor&lt;/title&gt;
     &lt;meta charset="utf-8"/&gt;
     &lt;link rel="stylesheet" type="text/css" href="default.css"/&gt;
     &lt;script src="nav.js"&gt;&lt;/script&gt;
     &lt;script type="text/javascript"&gt;
     // JavaScript is in here. 
     &lt;/script&gt;  
  
  &lt;!-- style elements specific to this page are placed in the style tags. --&gt;  
  &lt;style&gt;
  
    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; 
    }
  
  &lt;/style&gt; 
  &lt;/head&gt;
    &lt;body onload="setValues()"&gt;
  &lt;header&gt;
    &lt;a href="index.html"&gt;&lt;img src="logoglow.png" alt="Logo Image" width="215" height="135" /&gt;&lt;/a&gt;
    &lt;a href="index.html"&gt;&lt;img src="ac.png" alt="Header Image" width="800" height="135" /&gt;&lt;/a&gt;
  &lt;/header&gt;
  &lt;main&gt;
    &lt;h3&gt;An Ongoing Experiment in HTML5 / CSS3 / JavaScript Image Editing&lt;/h3&gt;
    &lt;div&gt;
    &lt;!-- Nav is floating left and Main is floating right, clear float styles (IF NEEDED) so they dont interfere with new image. --&gt;     
      &lt;p style="float:left;"&gt;  
      &lt;input type="file" id="fileInput" onclick="fileOpen()"&gt;&lt;/p&gt;
      &lt;br style="clear:both"&gt;
    &lt;/div&gt;
    &lt;div id="iDisplay"&gt;&lt;/div&gt;  
    &lt;br style="clear:both"&gt;&lt;br style="clear:both"&gt;
    &lt;!-- &lt;script&gt;document.write('&lt;img src="' + file + '"/&gt;');&lt;/script&gt;&lt;br&gt;&lt;br&gt; --&gt; 
    &lt;!-- &lt;div style="overflow:scroll;"&gt;  --&gt;
    &lt;canvas style="color:#FFFFFF;" id="C" width="javascript:setCanvasWidth()" height="javascript:setCanvasHeight()"&gt;Your browser is too old to support this feature. &lt;br&gt;Please consider updating to a modern browser.&lt;/canvas&gt;
    &lt;!-- &lt;/div&gt; --&gt; 
    &lt;!-- use javascript:function() to get width and height?  --&gt;
    &lt;div id="afterCanvas"&gt;  
    &lt;br&gt;&lt;br&gt; 
    &lt;form name="form1" id="form1id" action="javascript:save();" style="font-size:90%; text-align:center;"&gt;
      Brightness: &lt;input type="range" name="brightness" min="0" max="200" step="5" onmousemove="apply()" ontouchmove="apply()" style="vertical-align:-7px;" /&gt;&amp;nbsp; &amp;nbsp;
      Contrast: &lt;input type="range" name="contrast" min="0" max="200" step="5" onmousemove="apply()" ontouchmove="apply()" style="vertical-align:-7px;"/&gt;&amp;nbsp; &amp;nbsp;
      Hue: &lt;input type="range" name="hue" min="0" max="360" step="5" onmousemove="apply()" ontouchmove="apply()" style="vertical-align:-7px;"/&gt;
      &lt;br&gt;&lt;br&gt;  
      &lt;input type="submit" style="float:left;" value="Save Changes" /&gt; &lt;!-- chnage to type="submit" if form action is needed --&gt; 
    &lt;/form&gt;
    &lt;/div&gt;
  &lt;/main&gt;
  &lt;/body&gt;
  &lt;/html&gt;  
  </code></pre>