canvas and MySQL DB

0

I am trying to develop a small application for the management of users of a clinic, and I am faced with a problem and it is as follows. This clinic has to make its patients sign a sheet of paper, which is the data protection law in Spain and to save paper, ink etc. The clinic has offered to develop the application and in passing, the data protection law, among other things, can be signed digitally. Well, up here pretty well. For the firm I have developed a small box with "canvas" of html5 and to this I have assigned a small javascript script, attached code below. But now, it is interesting to keep this signature of that person in a database or as a separate file in the hosting ... And my question is, how can I do this part? I've been reading about what can be done with this function

canvas.toDataURL();

but I do not get it and I do not find anything really useful for me. Can you lend me a hand? Thanks in advance.

I've attached code so far.

1st HTML

<canvas id="canvas"> 
        Su navegador no soporta canvas
</canvas>

2nd JavaScript code for the drawing, and mouse events for the canvas

<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>

3rd code to try to save the canvas in an image, as I read there

<script>
        function convertToImg(canvas){
            var image = canvas.toDataURL(); 
            var canvas = new Image();
            var convertir = document.getElementById("convertir").src = image;       
        //return canvas;    
        }

        convertir.addEventListener('click', function(evt){
            dibujar = false;
            ctx.clearRect(0, 0, cw, ch);
            trazados.length = 0;
            puntos.length = 0;
        }, false);
</script>

finally a button that makes the call to the script to convert the canvas into an image

<button id="convertir">Convertir</button>   

up to here or at least I have achieved, but I can not pass the canvas to image and then save it in a MySQL DB or save it directly in MySQL. What kind of data should be used in the BD to keep this kind of stuff?

Greetings and thanks in advance

    
asked by scorpions 17.08.2017 в 12:35
source

1 answer

0

In the end I ended up developing this application in Java where if I could create a canvas and draw what I wanted, I attached code:

public class Dibujo extends Canvas implements MouseListener, MouseMotionListener{

private ArrayList rectangles = new ArrayList ();    private ArrayList circles = new ArrayList ();    private ArrayList figures = new ArrayList ();    private Path2D figure;    private int x, y;    private BufferedImage imageCanvas;    public Color color;    public int thicknessLine;    public boolean handHigher = true, circle = false, rectangle = false;

Drawing () {        // this setColor is while painting in which color is shown        this.setBackground (Color.WHITE);        this.figura = new Path2D.Float ();        line thickness = 3;    }

@Override
public void paint(Graphics graficos){
    imagenCanvas = new BufferedImage(this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_BGR);
    super.paint(graficos);

    Graphics2D dibujar = (Graphics2D) graficos /*imagenCanvas.createGraphics()*/;
    //Este setColor es para la zona de dibujo
    dibujar.setColor(Color.WHITE);
    dibujar.fillRect(0, 0, this.getWidth(), this.getHeight());
    dibujar.setStroke(new BasicStroke(this.grosorLinea));
    // este setColor es para la línea
    dibujar.setColor(Color.BLACK);
    dibujar.draw(this.figura);

    super.paint(imagenCanvas.createGraphics());

    Graphics2D dibujo = imagenCanvas.createGraphics();
    //Este setColor es para la zona de dibujo
    dibujo.setColor(Color.WHITE);
    dibujo.fillRect(0, 0, this.getWidth(), this.getHeight());
    dibujo.setStroke(new BasicStroke(this.grosorLinea));
    // este setColor es para la línea
    dibujo.setColor(Color.BLACK);
    dibujo.draw(this.figura);

   try {
       ImageIO.write(imagenCanvas, "png", new File("firma.png"));
   } catch (IOException ex) {
       JOptionPane.showMessageDialog(null, "Imposible guardar la imagen de la firma");
   }
}

@Override
public void mouseClicked(MouseEvent e) {}

@Override
public void mousePressed(MouseEvent e) {
    figura.moveTo(e.getX(), e.getY());
}

@Override
public void mouseReleased(MouseEvent e) {

}

@Override
public void mouseEntered(MouseEvent e) {}

@Override
public void mouseExited(MouseEvent e) {}

@Override
public void mouseDragged(MouseEvent e) {
    //obtiene automáticamente las coordenadas del ratón.
    this.x = e.getX();
    this.y = e.getY();
    this.figura.lineTo(this.x, this.y);
    repaint();  
}


@Override
public void mouseMoved(MouseEvent e) {

}
    
answered by 18.07.2018 / 23:31
source