Advertisement
KoctrX

Untitled

Nov 22nd, 2021
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.11 KB | None | 0 0
  1. export default new class {
  2. grid(canvas = false, removeOld = true) {
  3. if (removeOld) {
  4. canvas.getObjects().forEach(g => g._isGrid ? canvas.remove(g) : '');
  5. }
  6.  
  7. const width = canvas.width / canvas.getZoom();
  8. const height = canvas.height / canvas.getZoom();
  9. const step = 30;
  10.  
  11. const lines = [];
  12. const addLine = (i, to) => {
  13. let coords = [i * step, 0, i * step, height];
  14. if (to == 'y') {
  15. coords = [0, i * step, width, i * step];
  16. }
  17.  
  18. lines.push(new fabric.Line(coords, {
  19. fill: '#000000',
  20. stroke: '#000000',
  21. strokeWidth: 1,
  22. selectable: false,
  23. evented: false,
  24. }));
  25. };
  26.  
  27. new Array(Math.ceil(width / step)).fill(null).forEach((v, i) => addLine(i));
  28. new Array(Math.ceil(height / step)).fill(null).forEach((v, i) => addLine(i, 'y'));
  29.  
  30. const group = new fabric.Group(lines, {
  31. selectable: false,
  32. evented: false,
  33. _isGrid: true,
  34. originX: 'left',
  35. originY: 'top'
  36. });
  37.  
  38. if (this.gridObject) { canvas.remove(this.gridObject); }
  39.  
  40. this.gridObject = group;
  41. this.gridObject._isGrid = true;
  42.  
  43. return this.gridObject;
  44. }
  45.  
  46. ruler(canvas = false, zoomOrigin = 1, removeOld = true, color = '#55606c') {
  47. if (removeOld) {
  48. canvas.getObjects().forEach(g => g.__tt == 'ruler' ? canvas.remove(g) : '');
  49. }
  50.  
  51. let measure = 'px';
  52. const width = canvas.getWidth() / canvas.getZoom();
  53. const height = canvas.getHeight() / canvas.getZoom();
  54.  
  55. let minDistance = (width * .1);
  56. if (minDistance < 20) minDistance = 20;
  57.  
  58. const ruler = [];
  59. let correctDistance = correctDistance = width / 10;
  60.  
  61. let zoom = canvas.getZoom();
  62. let font_size = 12 / zoom;
  63. let cwidth = canvas.getWidth() / canvas.getZoom();
  64. let cheight = canvas.getHeight() / canvas.getZoom();
  65.  
  66. let bg_color = "rgba(231, 231, 231, 1)",
  67. measure_bg_size = 20/zoom
  68.  
  69.  
  70. //подложка линейки горизональной
  71. var measure_bg_w = new fabric.Rect({
  72. left: measure_bg_size, //делаем отступ что бы линейка не налаживалась
  73. top: cheight-measure_bg_size,
  74. fill: bg_color,
  75. width: cwidth,
  76. height: measure_bg_size
  77. });
  78.  
  79. //подложка линейки вертикальной
  80. var measure_bg_h = new fabric.Rect({
  81. left: 0,
  82. top: 0,
  83. width: measure_bg_size,
  84. fill: bg_color,
  85. height: cheight
  86. });
  87.  
  88. ruler.push(measure_bg_w);
  89. ruler.push(measure_bg_h);
  90.  
  91. if(false){
  92. let measure_text = new fabric.Text(measure, {
  93. fill: color,
  94. left: 30 / zoom,
  95. top: 30 / zoom,
  96. fontSize: font_size,
  97. textBackgroundColor: bg_color
  98. });
  99.  
  100. ruler.push(measure_text);
  101. }
  102.  
  103. let prev_text_step = 0,
  104. prev_line = 0;
  105.  
  106. for (let i = 0; i < height / correctDistance; i++) {
  107. const lineOpts = {
  108. fill: color,
  109. stroke: color,
  110. strokeWidth: 1 / zoom
  111. };
  112.  
  113. let leftText = false;
  114. let txt = (measure == 'px') ? (`${Math.round((i * correctDistance) / zoomOrigin)}`) : `${i}`;
  115.  
  116. if (txt != "0"){
  117. let text_step = (i * correctDistance);
  118. if (text_step - prev_line > (10 / zoom)) {
  119. if (text_step <= height - 1) {
  120. ruler.push(new fabric.Line([
  121. 0, text_step,
  122. 13 / zoom, text_step
  123. ], lineOpts));
  124. prev_line = text_step;
  125. }
  126. }
  127. if (text_step - prev_text_step < (100 / zoom)) {
  128. } else {
  129. leftText = new fabric.Text(txt, {
  130. fill: color,
  131. left: 20 / zoom,
  132. top: (text_step) - (6 / zoom),
  133. fontSize: font_size,
  134. textBackgroundColor: bg_color
  135. });
  136. prev_text_step = text_step;
  137. }
  138. }
  139.  
  140. const miniStep = (((i + 1) * correctDistance) - (i * correctDistance)) / 10;
  141. if (miniStep >= 2) {
  142. if (i * correctDistance <= height - 1) {
  143. const coords = new Array(9).fill(0).reduce((result, item, index) => {
  144. const left = (i * correctDistance) + ((index + 1) * miniStep);
  145. if (left + 1 <= height) {
  146. result.push(new fabric.Line([
  147. 0, left,
  148. (index == 4 ? 8 : 5) / zoom, left,
  149. ], lineOpts));
  150. }
  151. return result;
  152. }, []);
  153. ruler.push(...coords);
  154. }
  155. }
  156. if (leftText) {
  157. ruler.push(leftText);
  158. }
  159. }
  160. prev_text_step = 0;
  161. prev_line = 0;
  162.  
  163. console.log("correctDistance", correctDistance, "full width", width);
  164. let center = width/2;
  165. for(let i=0; i<(width/correctDistance)/2; i++){
  166. let step_right = center+(i * correctDistance),
  167. step_left = center-(i * correctDistance);
  168.  
  169.  
  170. // ruler.push(new fabric.Line([
  171. // text_step,
  172. // cheight-(measure_bg_size-2),
  173. // text_step,
  174. // (cheight)
  175. // ], lineOpts));
  176. console.log("draw segments", step_left, step_right );
  177. }
  178.  
  179.  
  180. //слева на право, внизу.
  181. for(let i = 0; i < width / correctDistance; i++){
  182. const lineOpts = {
  183. fill: color, stroke: color,
  184. strokeWidth: 1 / zoom
  185. };
  186.  
  187. let txt = (measure == 'px') ? (`${Math.round((i * correctDistance) / zoomOrigin)}`) : `${i}`;
  188. let topText = false;
  189. if (txt != "0") {
  190. let text_step = (i * correctDistance);
  191. //рисуем большие полоски
  192. if (text_step - prev_line > (10 / zoom)) {
  193. if (text_step <= width - 1) {
  194. ruler.push(new fabric.Line([
  195. text_step,
  196. cheight-(measure_bg_size-2),
  197. text_step,
  198. (cheight)
  199. ], lineOpts));
  200. }
  201. prev_line = text_step;
  202. }
  203.  
  204. //height
  205. //рисуем текста
  206. if (text_step - prev_text_step > (100 / zoom)) {
  207. // topText = new fabric.Text(txt, {
  208. // fill: color,
  209. // left: (i * correctDistance),
  210. // top: cheight-(16 / zoom),
  211. // fontSize: font_size,
  212. // textBackgroundColor: bg_color
  213. // });
  214. // topText.set({
  215. // left: topText.left - (i == 0 ? 0 : (topText.width / 2))
  216. // });
  217. prev_text_step = text_step;
  218. }
  219. }
  220.  
  221. //100
  222.  
  223. //рисум маленькие полоски
  224. const miniStep = (((i + 1) * correctDistance) - (i * correctDistance)) / (10);
  225. if (miniStep >= 2) {
  226. if (i * correctDistance <= width - 1) {
  227. let prev_min = (i * correctDistance) + ((0 + 1) * miniStep);
  228. const coords = new Array(9).fill(0).reduce((result, item, index) => {
  229. const left = (i * correctDistance) + ((index + 1) * miniStep);
  230. let top = 100;
  231. let line_v_offset = measure_bg_size-2;
  232. if (left + 1 <= width) {
  233. let line_color = "red";
  234. if(index == 4){
  235. line_color = "blue";
  236. }
  237. //line
  238. lineOpts.fill = line_color;
  239. lineOpts.stroke = line_color;
  240. lineOpts.color = line_color
  241.  
  242. result.push(new fabric.Line([
  243. left,
  244. cheight-line_v_offset, //top
  245. left,
  246. (index == 4 ? (cheight-(measure_bg_size/2)) : (cheight-(measure_bg_size/1.5))) / zoom //height
  247. ], lineOpts));
  248. }
  249. prev_min = left;
  250. return result;
  251. }, []);
  252. ruler.push(...coords);
  253. }
  254. }
  255. if (topText) {
  256. ruler.push(topText);
  257. }
  258. }
  259.  
  260. return new fabric.Group(ruler, {
  261. measure: measure,
  262. // objectCaching: true,
  263. hoverCursor: 'default',
  264. __tt: 'ruler',
  265. selectable: false,
  266. evented: false,
  267. })
  268. }
  269. }
  270.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement