Foros del Web » Creando para Internet » HTML »

Paint HTML5.

Estas en el tema de Paint HTML5. en el foro de HTML en Foros del Web. Buenas, hace un tiempo conseguí un código en internet de una especie de paint en HTML5 usando canvas... aquí está: Código HTML: <!DOCTYPE html> <html ...
  #1 (permalink)  
Antiguo 09/07/2013, 08:26
 
Fecha de Ingreso: marzo-2013
Ubicación: Venezuela
Mensajes: 8
Antigüedad: 11 años, 1 mes
Puntos: 0
Paint HTML5.

Buenas, hace un tiempo conseguí un código en internet de una especie de paint en HTML5 usando canvas...

aquí está:

Código HTML:
<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="utf-8" />
   <title>Desktops and Tablets</title>
 
   <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
 
   <script type="text/javascript">
      $(document).ready(function () {
         initialize();
      });
 

      // works out the X, Y position of the click inside the canvas from the X, Y position on the page
      function getPosition(mouseEvent, sigCanvas) {
         var x, y;
         if (mouseEvent.pageX != undefined && mouseEvent.pageY != undefined) {
            x = mouseEvent.pageX;
            y = mouseEvent.pageY;
         } else {
            x = mouseEvent.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
            y = mouseEvent.clientY + document.body.scrollTop + document.documentElement.scrollTop;
         }
 
         return { X: x - sigCanvas.offsetLeft, Y: y - sigCanvas.offsetTop };
      }
 
      function initialize() {
         // get references to the canvas element as well as the 2D drawing context
         var sigCanvas = document.getElementById("canvasSignature");
         var context = sigCanvas.getContext("2d");
         context.strokeStyle = 'Black';
 
         // This will be defined on a TOUCH device such as iPad or Android, etc.
         var is_touch_device = 'ontouchstart' in document.documentElement;
 
         if (is_touch_device) {
            // create a drawer which tracks touch movements
            var drawer = {
               isDrawing: false,
               touchstart: function (coors) {
                  context.beginPath();
                  context.moveTo(coors.x, coors.y);
                  this.isDrawing = true;
               },
               touchmove: function (coors) {
                  if (this.isDrawing) {
                     context.lineTo(coors.x, coors.y);
                     context.stroke();
                  }
               },
               touchend: function (coors) {
                  if (this.isDrawing) {
                     this.touchmove(coors);
                     this.isDrawing = false;
                  }
               }
            };
 
            // create a function to pass touch events and coordinates to drawer
            function draw(event) {
 
               // get the touch coordinates.  Using the first touch in case of multi-touch
               var coors = {
                  x: event.targetTouches[0].pageX,
                  y: event.targetTouches[0].pageY
               };
 
               // Now we need to get the offset of the canvas location
               var obj = sigCanvas;
 
               if (obj.offsetParent) {
                  // Every time we find a new object, we add its offsetLeft and offsetTop to curleft and curtop.
                  do {
                     coors.x -= obj.offsetLeft;
                     coors.y -= obj.offsetTop;
                  }
				  // The while loop can be "while (obj = obj.offsetParent)" only, which does return null
				  // when null is passed back, but that creates a warning in some editors (i.e. VS2010).
                  while ((obj = obj.offsetParent) != null);
               }
 
               // pass the coordinates to the appropriate handler
               drawer[event.type](coors);
            }
 

            // attach the touchstart, touchmove, touchend event listeners.
            sigCanvas.addEventListener('touchstart', draw, false);
            sigCanvas.addEventListener('touchmove', draw, false);
            sigCanvas.addEventListener('touchend', draw, false);
 
            // prevent elastic scrolling
            sigCanvas.addEventListener('touchmove', function (event) {
               event.preventDefault();
            }, false); 
         }
         else {
 
            // start drawing when the mousedown event fires, and attach handlers to
            // draw a line to wherever the mouse moves to
            $("#canvasSignature").mousedown(function (mouseEvent) {
               var position = getPosition(mouseEvent, sigCanvas);
 
               context.moveTo(position.X, position.Y);
               context.beginPath();
 
               // attach event handlers
               $(this).mousemove(function (mouseEvent) {
                  drawLine(mouseEvent, sigCanvas, context);
               }).mouseup(function (mouseEvent) {
                  finishDrawing(mouseEvent, sigCanvas, context);
               }).mouseout(function (mouseEvent) {
                  finishDrawing(mouseEvent, sigCanvas, context);
               });
            });
 
         }
      }
 
      // draws a line to the x and y coordinates of the mouse event inside
      // the specified element using the specified context
      function drawLine(mouseEvent, sigCanvas, context) {
 
         var position = getPosition(mouseEvent, sigCanvas);
 
         context.lineTo(position.X, position.Y);
         context.stroke();
      }
 
      // draws a line from the last coordiantes in the path to the finishing
      // coordinates and unbind any event handlers which need to be preceded
      // by the mouse down event
      function finishDrawing(mouseEvent, sigCanvas, context) {
         // draw the line to the finishing coordinates
         drawLine(mouseEvent, sigCanvas, context);
 
         context.closePath();
 
         // unbind any events which could draw
         $(sigCanvas).unbind("mousemove")
                     .unbind("mouseup")
                     .unbind("mouseout");
      }
   </script>
   
</head>
 
<body>
   
   <div id="canvasDiv">
      <div align="center">
        <!-- It's bad practice (to me) to put your CSS here.  I'd recommend the use of a CSS file! -->
        <canvas id="canvasSignature"width="409" height="300" style="border:2px solid #000000;"></canvas>
      </div>
   </div>
</body>
</html> 

Mi duda es, cómo puedo hacer una especie de caputura de pantalla para tomar lo que el usuario dibujó y mandarlo a otra página (por ejemplo, en un foro, está esta opción y el usuario dibuja, acepta y en la publicación sale lo que dibujó)

Gracias
  #2 (permalink)  
Antiguo 09/07/2013, 08:58
Avatar de CesarHC  
Fecha de Ingreso: junio-2011
Ubicación: localhost
Mensajes: 566
Antigüedad: 12 años, 10 meses
Puntos: 56
Respuesta: Paint HTML5.

Pues tal vez te sirva esto hace tiempo lo probe.
__________________
Solo la práctica no te traicionara ¡¡¡¡¡¡

Seguir el camino tu debes PHP The Right Way.
  #3 (permalink)  
Antiguo 09/07/2013, 18:27
Avatar de hackjose  
Fecha de Ingreso: abril-2010
Ubicación: Edo Mexico
Mensajes: 1.178
Antigüedad: 14 años
Puntos: 131
Respuesta: Paint HTML5.

El metodo todataurl te devuelve la imagen en "formato" base 64(una cadena de caracteres).
Esa cadena base 64 la puedes enviar al server por medio de ajax.
Esa cadena base 64 se usa como una imagen, la puedes usar con el tag <img /> o con background-url

Saludos
  #4 (permalink)  
Antiguo 09/07/2013, 23:25
Avatar de memoadian
Colaborador
 
Fecha de Ingreso: junio-2009
Ubicación: <?php echo 'México'?>
Mensajes: 3.696
Antigüedad: 14 años, 10 meses
Puntos: 641
Respuesta: Paint HTML5.

Jajaja me encantó :3

Código HTML:
Ver original
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.    <meta charset="utf-8" />
  4.    <title>Desktops and Tablets</title>
  5.  
  6.    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>
  7.  
  8.    <script type="text/javascript">
  9.       $(document).ready(function () {
  10.          initialize();
  11.          $('.verImagen').click(function(){
  12.             var sigCanvas = document.getElementById("canvasSignature");
  13.             var dataUrl = sigCanvas.toDataURL(); // obtenemos la imagen como png
  14.  
  15.             dataUrl = dataUrl.replace("image/png"); // sustituimos el tipo por octet
  16.             document.location.href = dataUrl; // para forzar al navegador a descargarlo
  17.          });
  18.       });
  19.  
  20.  
  21.       // works out the X, Y position of the click inside the canvas from the X, Y position on the page
  22.       function getPosition(mouseEvent, sigCanvas) {
  23.          var x, y;
  24.          if (mouseEvent.pageX != undefined && mouseEvent.pageY != undefined) {
  25.            x = mouseEvent.pageX;
  26.             y = mouseEvent.pageY;
  27.          } else {
  28.             x = mouseEvent.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
  29.             y = mouseEvent.clientY + document.body.scrollTop + document.documentElement.scrollTop;
  30.          }
  31.  
  32.          return { X: x - sigCanvas.offsetLeft, Y: y - sigCanvas.offsetTop };
  33.       }
  34.  
  35.       function initialize() {
  36.          // get references to the canvas element as well as the 2D drawing context
  37.          var sigCanvas = document.getElementById("canvasSignature");
  38.          var context = sigCanvas.getContext("2d");
  39.          context.strokeStyle = 'Black';
  40.  
  41.          // This will be defined on a TOUCH device such as iPad or Android, etc.
  42.          var is_touch_device = 'ontouchstart' in document.documentElement;
  43.  
  44.          if (is_touch_device) {
  45.             // create a drawer which tracks touch movements
  46.             var drawer = {
  47.                isDrawing: false,
  48.                touchstart: function (coors) {
  49.                   context.beginPath();
  50.                   context.moveTo(coors.x, coors.y);
  51.                   this.isDrawing = true;
  52.                },
  53.                touchmove: function (coors) {
  54.                   if (this.isDrawing) {
  55.                      context.lineTo(coors.x, coors.y);
  56.                      context.stroke();
  57.                   }
  58.                },
  59.                touchend: function (coors) {
  60.                   if (this.isDrawing) {
  61.                      this.touchmove(coors);
  62.                      this.isDrawing = false;
  63.                   }
  64.                }
  65.             };
  66.  
  67.             // create a function to pass touch events and coordinates to drawer
  68.             function draw(event) {
  69.  
  70.                // get the touch coordinates.  Using the first touch in case of multi-touch
  71.                var coors = {
  72.                   x: event.targetTouches[0].pageX,
  73.                   y: event.targetTouches[0].pageY
  74.                };
  75.  
  76.                // Now we need to get the offset of the canvas location
  77.                var obj = sigCanvas;
  78.  
  79.                if (obj.offsetParent) {
  80.                   // Every time we find a new object, we add its offsetLeft and offsetTop to curleft and curtop.
  81.                   do {
  82.                      coors.x -= obj.offsetLeft;
  83.                      coors.y -= obj.offsetTop;
  84.                   }
  85.                   // The while loop can be "while (obj = obj.offsetParent)" only, which does return null
  86.                   // when null is passed back, but that creates a warning in some editors (i.e. VS2010).
  87.                   while ((obj = obj.offsetParent) != null);
  88.                }
  89.  
  90.                // pass the coordinates to the appropriate handler
  91.                drawer[event.type](coors);
  92.             }
  93.  
  94.  
  95.             // attach the touchstart, touchmove, touchend event listeners.
  96.             sigCanvas.addEventListener('touchstart', draw, false);
  97.             sigCanvas.addEventListener('touchmove', draw, false);
  98.             sigCanvas.addEventListener('touchend', draw, false);
  99.  
  100.             // prevent elastic scrolling
  101.             sigCanvas.addEventListener('touchmove', function (event) {
  102.                event.preventDefault();
  103.             }, false);
  104.          }
  105.          else {
  106.  
  107.             // start drawing when the mousedown event fires, and attach handlers to
  108.             // draw a line to wherever the mouse moves to
  109.             $("#canvasSignature").mousedown(function (mouseEvent) {
  110.                var position = getPosition(mouseEvent, sigCanvas);
  111.  
  112.                context.moveTo(position.X, position.Y);
  113.                context.beginPath();
  114.  
  115.                // attach event handlers
  116.                $(this).mousemove(function (mouseEvent) {
  117.                   drawLine(mouseEvent, sigCanvas, context);
  118.                }).mouseup(function (mouseEvent) {
  119.                   finishDrawing(mouseEvent, sigCanvas, context);
  120.                }).mouseout(function (mouseEvent) {
  121.                   finishDrawing(mouseEvent, sigCanvas, context);
  122.                });
  123.             });
  124.  
  125.          }
  126.       }
  127.  
  128.       // draws a line to the x and y coordinates of the mouse event inside
  129.       // the specified element using the specified context
  130.       function drawLine(mouseEvent, sigCanvas, context) {
  131.  
  132.          var position = getPosition(mouseEvent, sigCanvas);
  133.  
  134.          context.lineTo(position.X, position.Y);
  135.          context.stroke();
  136.       }
  137.  
  138.       // draws a line from the last coordiantes in the path to the finishing
  139.       // coordinates and unbind any event handlers which need to be preceded
  140.       // by the mouse down event
  141.       function finishDrawing(mouseEvent, sigCanvas, context) {
  142.          // draw the line to the finishing coordinates
  143.          drawLine(mouseEvent, sigCanvas, context);
  144.  
  145.          context.closePath();
  146.  
  147.          // unbind any events which could draw
  148.          $(sigCanvas).unbind("mousemove")
  149.                      .unbind("mouseup")
  150.                      .unbind("mouseout");
  151.       }
  152.    </script>
  153.    
  154. </head>
  155.  
  156.    
  157.    <div id="canvasDiv">
  158.       <div align="center">
  159.         <!-- It's bad practice (to me) to put your CSS here.  I'd recommend the use of a CSS file! -->
  160.         <canvas id="canvasSignature"width="409" height="300" style="border:2px solid #000000;"></canvas>
  161.       </div>
  162.    </div>
  163.    <button class="verImagen">Ver imagen</button>
  164. </body>
  165. </html>

http://comprar-web.com/portfolio/paint

Etiquetas: css, html5, js, paint, type
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 02:57.