Painter.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. function Painter(context, dom) {
  2. const dpr = window.devicePixelRatio;
  3. const c = document.createElement('canvas');
  4. c.width = 16 * dpr;
  5. c.height = 16 * dpr;
  6. c.style.position = 'absolute';
  7. c.style.width = '16px';
  8. c.style.height = '16px';
  9. c.style.pointerEvents = 'none';
  10. c.style.display = 'none';
  11. const ctx = c.getContext("2d");
  12. ctx.lineWidth = 0.5;
  13. ctx.beginPath();
  14. ctx.arc(8 * dpr, 8 * dpr, 7, 0, Math.PI * 2);
  15. ctx.stroke();
  16. if (dom) dom.appendChild(c);
  17. let cx = null;
  18. let cy = null;
  19. let ccolor = null;
  20. let isNewLine = false;
  21. function draw(x1, y1, x2, y2) {
  22. if (isNewLine === true) {
  23. isNewLine = false;
  24. return;
  25. }
  26. const dx = x2 - x1;
  27. const dy = y2 - y1;
  28. const d = Math.sqrt(dx * dx + dy * dy) * 0.015;
  29. if (ccolor !== null) {
  30. context.beginPath();
  31. context.moveTo(x1, y1);
  32. context.lineTo(x2, y2);
  33. switch (ccolor) {
  34. case 0:
  35. context.strokeStyle = 'rgba(0, 0, 0, ' + (0.9 - d) + ')';
  36. break;
  37. case 1:
  38. context.strokeStyle = 'rgba(0, 0, 238, ' + (0.9 - d) + ')';
  39. break;
  40. case 2:
  41. context.strokeStyle = 'rgba(0, 208, 0, ' + (0.9 - d) + ')';
  42. break;
  43. case 3:
  44. context.strokeStyle = 'rgba(238, 0, 238, ' + (0.9 - d) + ')';
  45. break;
  46. case 4:
  47. context.strokeStyle = 'rgba(238, 0, 0, ' + (0.9 - d) + ')';
  48. break;
  49. case 5:
  50. context.strokeStyle = 'rgba(119, 60, 0, ' + (0.9 - d) + ')';
  51. break;
  52. case 6:
  53. context.strokeStyle = 'rgba(238, 119, 0, ' + (0.9 - d) + ')';
  54. break;
  55. case 7:
  56. context.strokeStyle = 'rgba(238, 208, 0, ' + (0.9 - d) + ')';
  57. break;
  58. case 8:
  59. context.strokeStyle = 'rgba(238, 238, 238, ' + (1 - d) + ')';
  60. break;
  61. }
  62. context.stroke();
  63. }
  64. }
  65. return {
  66. execute: function (data) {
  67. switch (data.getUint8(1)) {
  68. case 0: // POINTER_DOWN
  69. this.down(
  70. data.getUint16(2),
  71. data.getUint16(4),
  72. data.getUint8(6)
  73. );
  74. break;
  75. case 1: // POINTER_UP
  76. this.up();
  77. break;
  78. case 2: // POINTER_MOVE_ABS
  79. this.move(
  80. data.getUint16(2),
  81. data.getUint16(4)
  82. );
  83. break;
  84. case 3: // POINTER_DOWN_DELTA_8_8
  85. if (cx !== null) {
  86. this.move(
  87. cx + data.getInt8(2),
  88. cy + data.getInt8(3)
  89. );
  90. }
  91. break;
  92. case 4: // POINTER_DOWN_DELTA_X
  93. if (cx !== null) {
  94. this.move(
  95. cx + data.getInt8(2),
  96. cy
  97. );
  98. }
  99. break;
  100. case 5: // POINTER_DOWN_DELTA_Y
  101. if (cx !== null) {
  102. this.move(
  103. cx,
  104. cy + data.getInt8(2)
  105. );
  106. }
  107. break;
  108. case 6: // POINTER_DOWN_DELTA_4_4
  109. if (cx !== null) {
  110. this.move(
  111. cx + (data.getUint8(2) >> 4) - 8,
  112. cy + (data.getUint8(2) & 15) - 8
  113. );
  114. }
  115. break;
  116. case 8: // USER_DISCONNECT
  117. this.disconnect();
  118. break;
  119. }
  120. },
  121. move: function (x, y) {
  122. draw(cx, cy, x, y);
  123. cx = x;
  124. cy = y;
  125. //
  126. c.style.left = ((x / dpr) - 8) + 'px';
  127. c.style.top = ((y / dpr) - 8) + 'px';
  128. },
  129. down: function (x, y, color) {
  130. cx = x;
  131. cy = y;
  132. ccolor = color;
  133. //
  134. c.style.left = ((x / dpr) - 8) + 'px';
  135. c.style.top = ((y / dpr) - 8) + 'px';
  136. c.style.display = '';
  137. },
  138. up: function () {
  139. isNewLine = true;
  140. c.style.display = 'none';
  141. },
  142. disconnect: function () {
  143. cx = null;
  144. cy = null;
  145. c.style.display = 'none';
  146. }
  147. };
  148. }
  149. export { Painter }