Draw.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. var Draw = function(canvas) {
  2. this.canvas = canvas;
  3. this.width = canvas.width;
  4. this.height = canvas.height;
  5. this.context = canvas.getContext('2d');
  6. this.mousedownPoint = null;
  7. this.beforMousemovePoint = null;
  8. this.drawFlag = false;
  9. this.isDraw = false; //记录该canvas是否有写
  10. this.timeoutObject = null; //记录cavas的setTimeOut
  11. var $this = this;
  12. this.context.lineWidth = 3;
  13. $(canvas).on('mousedown', function(event) {
  14. $this.mousedownPoint = $this.toCanvasCoord(event.clientX, event.clientY);
  15. $this.drawFlag = true;
  16. });
  17. $(canvas).on('mousemove', function(event) {
  18. if ($this.drawFlag) {
  19. if (!$this.beforMousemovePoint) {
  20. $this.beforMousemovePoint = $this.toCanvasCoord(event.clientX, event.clientY);
  21. $this.context.beginPath();
  22. $this.context.moveTo($this.mousedownPoint.x, $this.mousedownPoint.y);
  23. $this.context.lineTo($this.beforMousemovePoint.x, $this.beforMousemovePoint.y);
  24. $this.context.stroke();
  25. } else {
  26. $this.context.beginPath();
  27. $this.context.moveTo($this.beforMousemovePoint.x, $this.beforMousemovePoint.y);
  28. var point = $this.toCanvasCoord(event.clientX, event.clientY);
  29. $this.context.lineTo(point.x, point.y);
  30. $this.context.stroke();
  31. $this.beforMousemovePoint = point;
  32. }
  33. $this.isDraw = true;
  34. }
  35. });
  36. $(canvas).on('mouseup', function(event) {
  37. $this.drawFlag = false;
  38. $this.mousedownPoint = null;
  39. $this.beforMousemovePoint = null;
  40. });
  41. $(canvas).on('mouseleave', function(event) {
  42. if ($this.drawFlag) {
  43. $this.drawFlag = false;
  44. $this.mousedownPoint = null;
  45. $this.beforMousemovePoint = null;
  46. }
  47. });
  48. //新增加事件
  49. $(canvas).bind('touchstart', function(event) {
  50. event.preventDefault();
  51. var touch = event.originalEvent.targetTouches[0];
  52. mouseX =touch.clientX;
  53. mouseY =touch.clientY;
  54. $this.mousedownPoint = $this.toCanvasCoord(mouseX, mouseY);
  55. $this.drawFlag = true;
  56. });
  57. $(canvas).bind('touchmove', function(event) {
  58. event.preventDefault();
  59. var touch = event.originalEvent.targetTouches[0];
  60. mouseX =touch.clientX;
  61. mouseY =touch.clientY;
  62. console.log(mouseX+'top:'+mouseY);
  63. if ($this.drawFlag) {
  64. if (!$this.beforMousemovePoint) {
  65. $this.beforMousemovePoint = $this.toCanvasCoord(mouseX, mouseY);
  66. $this.context.beginPath();
  67. $this.context.moveTo($this.mousedownPoint.x, $this.mousedownPoint.y);
  68. $this.context.lineTo($this.beforMousemovePoint.x, $this.beforMousemovePoint.y);
  69. $this.context.stroke();
  70. } else {
  71. $this.context.beginPath();
  72. $this.context.moveTo($this.beforMousemovePoint.x, $this.beforMousemovePoint.y);
  73. var point = $this.toCanvasCoord(mouseX, mouseY);
  74. $this.context.lineTo(point.x, point.y);
  75. $this.context.stroke();
  76. $this.beforMousemovePoint = point;
  77. }
  78. $this.isDraw = true;
  79. }
  80. });
  81. $(canvas).bind('touchend', function(event) {
  82. event.preventDefault();
  83. $this.drawFlag = false;
  84. $this.mousedownPoint = null;
  85. $this.beforMousemovePoint = null;
  86. if ($this.drawFlag) {
  87. $this.drawFlag = false;
  88. $this.mousedownPoint = null;
  89. $this.beforMousemovePoint = null;
  90. }
  91. });
  92. }
  93. Draw.prototype = {
  94. toCanvasCoord: function(x, y) {
  95. var point = {};
  96. var coord = this.canvas.getBoundingClientRect();
  97. point.x = x - coord.left;
  98. point.y = y - coord.top;
  99. return point;
  100. },
  101. erase: function() {
  102. this.context.clearRect(0, 0, this.width, this.height);
  103. }
  104. };