You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
4.4 KiB

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <style type="text/css">
  6. #c{
  7. border: 1px solid red;
  8. }
  9. </style>
  10. </head>
  11. <body>
  12. <input type="checkbox" id="er" >橡皮
  13. <br>
  14. 颜色:黑<input type="radio" name="color" value="black" checked><input type="radio" name="color" value="red"><input type="radio" name="color" value="blue">
  15. <br>
  16. <canvas id="c" width="400" height="400"></canvas>
  17. <br>
  18. <button id="clear">清空画布</button>
  19. <script src="http://cdn.bootcss.com/jquery/3.0.0-alpha1/jquery.min.js"></script>
  20. <script type="text/javascript" src="/socket.io/socket.io.js"></script>
  21. <script type="text/javascript">
  22. var socket = io.connect();
  23. //从服务器获得消息
  24. socket.on("message", function(msg){
  25. dealMsg(msg);
  26. });
  27. socket.on("load", function(data){
  28. for(var i = 0; i < data.msgs.length; i++){
  29. var m = data.msgs[i];
  30. dealMsg(m);
  31. }
  32. });
  33. function dealMsg(msg){
  34. if(msg.name == "line"){
  35. line(msg.x1, msg.y1, msg.x2, msg.y2, msg.color);
  36. }else if(msg.name == "text"){
  37. text(msg.x, msg.y, msg.text, msg.color);
  38. }else if(msg.name == "eraser"){
  39. eraser(msg.x1, msg.y1, msg.x2, msg.y2);
  40. }else if(msg.name == "clear"){
  41. clear();
  42. }
  43. }
  44. var er = $("#er");
  45. var c = $("#c");
  46. var cxt = c[0].getContext("2d");
  47. var drawing = false;
  48. var drawStartX = 0;
  49. var drawStartY = 0;
  50. var drawEndX = 0;
  51. var drawEndY = 0;
  52. var ering = false;
  53. var erStartX = 0;
  54. var erStartY = 0;
  55. var erEndX = 0;
  56. var erEndY = 0;
  57. c.mousedown(function(e){
  58. if(!er.is(":checked")){
  59. drawStartX = e.pageX - this.offsetLeft;
  60. drawStartY = e.pageY - this.offsetTop;
  61. drawing = true;
  62. }else{
  63. erStartX = e.pageX - this.offsetLeft;
  64. erStartY = e.pageY - this.offsetTop;
  65. ering = true;
  66. }
  67. });
  68. c.mouseup(function(e){
  69. if(!er.is(":checked")){
  70. drawEndX = e.pageX - this.offsetLeft;
  71. drawEndY = e.pageY - this.offsetTop;
  72. drawing = false;
  73. socket.emit("line", {
  74. x1 : drawStartX,
  75. y1 : drawStartY,
  76. x2 : drawEndX,
  77. y2 : drawEndY,
  78. color : color()
  79. });
  80. }else{
  81. erEndX = e.pageX - this.offsetLeft;
  82. erEndY = e.pageY - this.offsetTop;
  83. ering = false;
  84. socket.emit("eraser", {
  85. x1 : erStartX,
  86. y1 : erStartY,
  87. x2 : erEndX,
  88. y2 : erEndY
  89. });
  90. }
  91. });
  92. c.dblclick(function(e){
  93. var tx = e.pageX - this.offsetLeft;
  94. var ty = e.pageY - this.offsetTop;
  95. var txt = prompt("输入文字");
  96. socket.emit("text", {
  97. x : tx,
  98. y : ty,
  99. text : txt,
  100. color : color()
  101. });
  102. });
  103. $("#clear").click(function(){
  104. if(confirm("确定要清空画布?")){
  105. socket.emit("clear", {
  106. });
  107. }
  108. });
  109. //绘制....
  110. //绘制直线
  111. function line(x1, y1, x2, y2, c){
  112. cxt.strokeStyle = c;
  113. cxt.beginPath();
  114. cxt.moveTo(x1, y1);
  115. cxt.lineTo(x2, y2);
  116. cxt.closePath();
  117. cxt.stroke();
  118. }
  119. //绘制文字
  120. function text(x, y, t, c){
  121. cxt.fillStyle = c;
  122. cxt.fillText(t, x, y);
  123. }
  124. //橡皮擦(实际是绘制白色的矩形)
  125. function eraser(x1, y1, x2, y2){
  126. cxt.fillStyle = "#FFFFFF";
  127. cxt.fillRect(x1, y1, x2 - x1, y2 - y1);
  128. }
  129. //清空画布
  130. function clear(){
  131. cxt.clearRect(0, 0, 400, 400);
  132. }
  133. function color(){
  134. return $("[name=color]:checked").val();
  135. }
  136. //网页加载后读取已画图进度
  137. $(function(){
  138. socket.emit("load",{
  139. });
  140. });
  141. </script>
  142. </body>
  143. </html>