Advertisement
Guest User

sending end code

a guest
Jun 25th, 2011
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 3.01 KB | None | 0 0
  1. <html>
  2.  
  3. <head>
  4. <title>Testing HTML5's Canvas</title>
  5. <script type="text/javascript">
  6. var mouseDown = 0;
  7. window.onmousedown = function() {
  8.     mouseDown = 1;
  9. }
  10. function depressButton(btn) {
  11.     btn.style.borderStyle = "inset";
  12. }
  13. function unpressButton(btn) {
  14.     btn.style.borderStyle = "outset";
  15. }
  16. window.onmouseup = function() {
  17.     mouseDown = 0;
  18. }
  19. var xmlhttp;
  20. if (window.XMLHttpRequest)
  21. { // code for IE7+, Firefox, Chrome, Opera, Safari
  22.     xmlhttp = new XMLHttpRequest();
  23. }
  24. else
  25. { // code for IE6, IE5
  26.     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  27. }
  28. </script>
  29. <style type="text/css">
  30. .toolbutton {
  31.     background-color: silver;
  32.     display: inline-block;
  33.     border-style: outset;
  34.     height: 15;
  35.     font-size: 9pt;
  36.     text-align: center;
  37.     margin: 2px;
  38.     width: 50;
  39.     cursor: pointer;
  40. }
  41. </style>
  42. </head>
  43.  
  44. <body>
  45.  
  46. <span class="toolbutton" onmousedown="depressButton(this);" onmouseup="unpressButton(this)" onmouseout="unpressButton(this)" onclick="selectTool('pencil')">PENCIL</span>
  47. <span class="toolbutton" onmousedown="depressButton(this);" onmouseup="unpressButton(this)" onmouseout="unpressButton(this)" onclick="selectTool('eraser')">ERASER</span>
  48. <span class="toolbutton" onmousedown="depressButton(this);" onmouseup="unpressButton(this)" onmouseout="unpressButton(this)" onclick="selectTool('line')">LINE</span><br />
  49. <canvas id="myCanvas" width="800" height="400" style="border: 2px black solid; cursor: crosshair;">
  50. Yoru browser does not support Canvas. :C
  51. </canvas><br />
  52.  
  53. <label>X: </label><label id="x">0</label><br />
  54. <label>Y: </label><label id="y">0</label><br />
  55. <label>mouseDown: </label><label id="md"></label>
  56. <div id="dump"></div>
  57.  
  58. <script type="text/javascript">
  59.  
  60. var context = document.getElementById("myCanvas").getContext("2d");
  61. context.fillStyle = "red";
  62. context.strokeStyle = "red";
  63.  
  64. context.beginPath();
  65. context.moveTo(0, 0);
  66.  
  67. function serialize(c) {
  68.     return c.toDataURL();
  69. }
  70.  
  71. function deserialize(data, c) {
  72.     var img = new Image();
  73.     img.onload = function() {
  74.         c.width = img.width;
  75.         c.height = img.height;
  76.         c.getContext("2d").drawImage(img, 0, 0);
  77.     };
  78.  
  79.     img.src = data;
  80. }
  81. function postToServer(data) {
  82.     data = "data=" + data;
  83.     xmlhttp.open("POST", "canvas.php", true);
  84.     xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  85.     xmlhttp.setRequestHeader("Content-length", data.length);
  86.     xmlhttp.setRequestHeader("Connection", "close");
  87.     xmlhttp.send(data);
  88. }
  89. function mouseMoveHandler(e) {
  90.     var x = e.pageX - 8;
  91.     var y = e.pageY - 35;
  92.     document.getElementById("x").innerHTML = x;
  93.     document.getElementById("y").innerHTML = y;
  94.     document.getElementById("md").innerHTML = mouseDown;
  95.     //
  96.     if (mouseDown) {
  97.         context.lineTo(x, y);
  98.         context.stroke();
  99.         context.beginPath();
  100.     }
  101.     context.moveTo(x, y);
  102.     var imagedata = serialize(document.getElementById("myCanvas"));
  103.     postToServer(imagedata);
  104.     document.getElementById("dump").innerHTML = imagedata;
  105. }
  106.  
  107. document.onmousemove = mouseMoveHandler;
  108. </script>
  109. </body>
  110.  
  111. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement