I am developing an application in html, php and some javascript, surely someone has already read my doubts here. I have a canvas where I make a signature for the users and I have in my form, a button in which I convert the canvas into a png image and the windows box automatically appears to select the folder to download, but I don't want that. I want the signature to be automatically downloaded to a folder. Attached html code and JavaScript
Canvas definition:
<canvas id="canvas">
Su navegador no soporta canvas
</canvas>
This is the code snippet for the transform button:
<script type="text/javascript">
function guardar() {
var link = document.createElement('a')
link.download = "firma";
link.href = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream");
link.click();
}
</script>
Canvas events:
<script type="text/javascript">
var limpiar = document.getElementById("limpiar");
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var cw = canvas.width = 150, cx = cw / 2;
var ch = canvas.height = 150, cy = ch / 2;
var dibujar = false;
var factorDeAlisamiento = 5;
var trazados = [];
var puntos = [];
ctx.lineJoin = "round";
limpiar.addEventListener('click', function(evt){
dibujar = false;
ctx.clearRect(0, 0, cw, ch);
trazados.length = 0;
puntos.length = 0;
}, false);
canvas.addEventListener('mousedown', function(evt) {
dibujar = true;
//ctx.clearRect(0, 0, cw, ch);
ctx.beginPath();
}, false);
canvas.addEventListener('mouseup', function(evt) {
dibujar = false;
}, false);
canvas.addEventListener("mouseout", function(evt) {
dibujar = false;
}, false);
canvas.addEventListener("mousemove", function(evt) {
if (dibujar) {
var m = oMousePos(canvas, evt);
puntos.push(m);
ctx.lineTo(m.x, m.y);
ctx.stroke();
}
}, false);
function reducirArray(n,elArray) {
var nuevoArray = [];
nuevoArray[0] = elArray[0];
for (var i = 0; i < elArray.length; i++) {
if (i % n == 0) {
nuevoArray[nuevoArray.length] = elArray[i];
}
}
nuevoArray[nuevoArray.length - 1] = elArray[elArray.length - 1];
Trazados.push(nuevoArray);
}
function calcularPuntoDeControl(ry, a, b) {
var pc = {}
pc.x = (ry[a].x + ry[b].x) / 2;
pc.y = (ry[a].y + ry[b].y) / 2;
return pc;
}
function alisarTrazado(ry) {
if (ry.length > 1) {
var ultimoPunto = ry.length - 1;
ctx.beginPath();
ctx.moveTo(ry[0].x, ry[0].y);
for (i = 1; i < ry.length - 2; i++) {
var pc = calcularPuntoDeControl(ry, i, i + 1);
ctx.quadraticCurveTo(ry[i].x, ry[i].y, pc.x, pc.y);
}
ctx.quadraticCurveTo(ry[ultimoPunto - 1].x, ry[ultimoPunto - 1].y, ry[ultimoPunto].x, ry[ultimoPunto].y);
ctx.stroke();
}
}
function redibujarTrazados(){
dibujar = false;
ctx.clearRect(0, 0, cw, ch);
reducirArray(factorDeAlisamiento,puntos);
for(var i = 0; i < Trazados.length; i++){
alisarTrazado(Trazados[i]);
}
}
function oMousePos(canvas, evt){
var ClientRect = canvas.getBoundingClientRect();
return { //objeto
x: Math.round(evt.clientX - ClientRect.left),
y: Math.round(evt.clientY - ClientRect.top)
}
}
</script>
I would be missing the code or part of the code, to know how to save the image in a specific path, a specific folder.
A greeting and thanks in advance.
The short answer is that it is not possible to force that.
The reason is simple, it is the browser (or the user) that decides whether to show the "Save As" window or not, from Javascript you cannot control that since it is the user's preference and the browser does not provide an API to alter that settings, some browsers come with this option enabled by default, others do not.
In Chrome you can see it in chrome://settings/ >> Advanced >> Downloads >> "Ask where to save each file before downloading"
To send the image to the server, you prepare a form, include it as a "file" field and send it, but having it on a canvas makes it easier, since you can send the image to php as a collection of points through json, then you can save it in a database field and convert to an image when it suits you best with the sigToSvg class ( https://github.com/kaltura/agent-contrib/blob/master/sigToSvg.php ), if instead of svg the you prefer in png or jpeg you use the Imagick or gd class, from php it would look like this:
If you still want to send the image as an image:
Then you send the normal form or by ajax Normally:
by ajax:
Hope this can help you