canvas.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. class Canvas {
  2. #paint;
  3. #canvas;
  4. #context;
  5. #clickX = new Array();
  6. #clickY = new Array();
  7. #clickDrag = new Array();
  8. constructor(frame_id, canvas_id) {
  9. let canvas_div = document.getElementById(frame_id);
  10. this.#canvas = document.createElement('canvas');
  11. // this.#canvas.setAttribute('style', 'width:100%;height:100%;');
  12. // this.#canvas.setAttribute('width', 1516);
  13. // this.#canvas.setAttribute('height', 165);
  14. this.#canvas.setAttribute('id', canvas_id);
  15. this.#canvas.setAttribute('class', 'canvas');
  16. canvas_div.appendChild(this.#canvas);
  17. if (typeof G_vmlCanvasManager != 'undefined') {
  18. this.#canvas = G_vmlCanvasManager.initElement(this.#canvas);
  19. }
  20. this.#context = this.#canvas.getContext("2d");
  21. this.attachEvent(canvas_id)
  22. }
  23. getCanvasSize() {
  24. return this.#canvas.getAttribute('height')
  25. }
  26. setCanvasSize(width, height) {
  27. this.#canvas.setAttribute('width', width);
  28. this.#canvas.setAttribute('height', height);
  29. }
  30. getClickX() {
  31. return [...this.#clickX]
  32. }
  33. setClickX(value) {
  34. this.#clickX = value;
  35. }
  36. getClickY() {
  37. return [...this.#clickY]
  38. }
  39. setClickY(value) {
  40. this.#clickY = value;
  41. }
  42. getClickDrag() {
  43. return [...this.#clickDrag]
  44. }
  45. setClickDrag(value) {
  46. this.#clickDrag = value;
  47. }
  48. setCanvasWidth(value) {
  49. this.#canvas.width = value;
  50. }
  51. setCanvasHeight(value) {
  52. this.#canvas.height = value;
  53. }
  54. addClick(x, y, dragging) {
  55. this.#clickX.push(x);
  56. this.#clickY.push(y);
  57. this.#clickDrag.push(dragging);
  58. // console.log(this.#clickX)
  59. // console.log(this.#clickY)
  60. // console.log(this.#clickDrag)
  61. }
  62. redraw() {
  63. this.#context.clearRect(0, 0, this.#context.canvas.width, this.#context.canvas.height); // Clears the canvas
  64. this.#context.strokeStyle = "#df4b26";
  65. this.#context.lineJoin = "round";
  66. this.#context.lineWidth = 2;
  67. for (var i=0; i < this.#clickX.length; i++) {
  68. this.#context.beginPath();
  69. if (this.#clickDrag[i] && i) {
  70. this.#context.moveTo(this.#clickX[i-1], this.#clickY[i-1]);
  71. } else {
  72. this.#context.moveTo(this.#clickX[i]-1, this.#clickY[i]);
  73. }
  74. this.#context.lineTo(this.#clickX[i], this.#clickY[i]);
  75. this.#context.closePath();
  76. this.#context.stroke();
  77. }
  78. }
  79. clearCanvas() {
  80. this.#clickX = [];
  81. this.#clickY = [];
  82. this.#clickDrag = [];
  83. this.#context.clearRect(0, 0, this.#canvas.width, this.#canvas.height); //clear canvas
  84. }
  85. attachEvent() {
  86. // PC
  87. this.#canvas.addEventListener('mousedown', (e)=>{
  88. let offset = $(e.target).offset()
  89. let mouseX = e.pageX - this.offsetLeft;
  90. let mouseY = e.pageY - this.offsetTop;
  91. this.#paint = true;
  92. this.addClick(e.pageX - offset.left, e.pageY - offset.top);
  93. this.redraw();
  94. });
  95. this.#canvas.addEventListener('mousemove', (e)=>{
  96. if (this.#paint) {
  97. let offset = $(e.target).offset()
  98. this.addClick(e.pageX - offset.left, e.pageY - offset.top, true);
  99. this.redraw();
  100. }
  101. });
  102. // 모바일
  103. this.#canvas.addEventListener('touchstart', (e)=>{
  104. let offset = $(e.target).offset()
  105. let mouseX = e.pageX - this.offsetLeft;
  106. let mouseY = e.pageY - this.offsetTop;
  107. this.#paint = true;
  108. this.addClick(e.pageX - offset.left, e.pageY - offset.top);
  109. this.redraw();
  110. });
  111. this.#canvas.addEventListener('touchmove', (e)=>{
  112. if (this.#paint) {
  113. const touch = e.touches[0];
  114. const mouseEvent = new MouseEvent("mousemove", {
  115. clientX: touch.clientX,
  116. clientY: touch.clientY
  117. });
  118. const offset = $(e.target).offset()
  119. this.addClick(mouseEvent.pageX - offset.left, mouseEvent.pageY - offset.top, true);
  120. this.redraw();
  121. }
  122. });
  123. this.#canvas.addEventListener('mouseup', (e)=>{
  124. this.#paint = false;
  125. });
  126. this.#canvas.addEventListener('mouseleave', (e)=>{
  127. this.#paint = false;
  128. });
  129. }
  130. }