Advertisement
KoctrX

Untitled

Dec 30th, 2020
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. lib.canvas.__eventListeners = {};
  2. line = false;
  3.  
  4. lib.canvas.on('mouse:down', e=>{
  5. if (!e.e.ctrlKey)
  6. return;
  7.  
  8. line = new fabric.Line([e.pointer.x, e.pointer.y, e.pointer.x, e.pointer.y],{
  9. left: e.pointer.x,
  10. top: e.pointer.y,
  11. stroke: '#f00',
  12. selectable: false,
  13. evented: false,
  14. name: 'newLine'
  15. });
  16.  
  17. lib.canvas.add(line);
  18. lib.canvas.renderAll();
  19. }
  20. );
  21.  
  22. lib.canvas.on('mouse:move', e=>{
  23. if (line) {
  24. line.set({
  25. x2: e.pointer.x,
  26. y2: e.pointer.y
  27. });
  28. lib.canvas.renderAll();
  29. }
  30. }
  31. );
  32.  
  33. $(window).unbind('keyup');
  34. $(window).on('keyup', e=>{
  35. console.log(e.keyCode);
  36. if (e.keyCode == 13) {
  37. const firstLine = lib.canvas.getObjects().find(l=>l.name == 'newLine');
  38. const lines = lib.canvas.getObjects().filter(obj=>obj.name == 'newLine');
  39. if (!firstLine || lines.length < 3) {
  40. return;
  41. }
  42.  
  43. line.set({
  44. x2: firstLine.x1,
  45. y2: firstLine.y1
  46. });
  47. lib.canvas.renderAll();
  48. line = null;
  49. const polygon = new fabric.Polygon(lines.map(line=>({
  50. x: line.x1,
  51. y: line.y1
  52. })), {
  53. originX: 'left',
  54. originY: 'top',
  55. fill: 'rgba(0, 0, 0, .4)',
  56. stroke: '#f00', strokeWidth: 1,
  57. //absolutePositioned: true
  58. });
  59.  
  60. lib.canvas.add(polygon);
  61. //polygon.set({ left: polygon.left - 100, top: polygon.top - 100 });
  62. //lib.canvas.getObjects()[0].clipPath = polygon;
  63.  
  64. lib.canvas.getObjects().forEach(obj=>{
  65. if (obj.name == 'newLine') {
  66. lib.canvas.remove(obj);
  67. }
  68. }
  69. );
  70. lib.canvas.renderAll();
  71. }
  72. }
  73. );
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement