Tema: Paint HTML5.
Ver Mensaje Individual
  #4 (permalink)  
Antiguo 09/07/2013, 23:25
Avatar de memoadian
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