Je suis conscient que la question de savoir comment appliquer réellement des filtres CSS à une image a été posée plusieurs fois. Certaines réponses sont données qui n'utilisent pas réellement les filtres CSS, mais semblent toujours faire le travail. Malheureusement, je suis très novice avec JavaScript, et je ne comprends pas ces réponses.Comment sauvegarder un canevas avec des filtres appliqués, en utilisant ces réponses stackoverflow?
Alors, ma question est en deux parties:
Certaines de ces réponses sont vieux, a une façon été mise au point d'enregistrer une image en toile avec des filtres CSS appliqué? Si non, comment implémenter les solutions décrites dans l'une de ces réponses stackoverflow? J'ai essentiellement besoin d'un échantillon de code pour appliquer réellement plusieurs filtres à l'image ou aux pixels, ce qui n'était pas contenu dans les réponses originales.
Merci
Réponses:
Dans la première réponse, je suis perdu sur le "..." dans la boucle. Dans la deuxième réponse, il n'y a pas de détails sur l'étape 4. Et si je ne me trompe pas, ces deux filtres, l'utilisation de CSS3 qui est mon choix préféré: Capture/save/export an image with CSS filter effects applied
Je suis perdu à l'étape 3 de celle-ci : is anyone solve about saving filtered canvas as an image?
Encore une fois, je suis perdu sur l'application des filtres non-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;" />
Contrast: <input type="range" name="contrast" min="0" max="200" step="5" onmousemove="apply()" ontouchmove="apply()" style="vertical-align:-7px;"/>
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>
Je devrais probablement avoir mentionné que, après 5 jours, j'ai tout fonctionne. Sauf pour le problème dans ma question. – TonyLuigiC