Advertisement
sourav8256

Untitled

Aug 6th, 2023 (edited)
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. add elements
  2. remove elements
  3. update elements
  4. delete elements
  5.  
  6. elementArray = []
  7.  
  8.  
  9. addElement(elem){
  10. elementArray.add(elem);
  11. }
  12.  
  13. getElem(id){
  14. for(int i=0;i<elementArray.length;i++){
  15. if(id == elementArray[i].id){
  16. return elementArray[i];
  17. }
  18. }
  19. }
  20.  
  21. updateElem(){
  22. for(int i=0;i<elementArray.length;i++){
  23. if(elem.id == elementArray[i].id){
  24. elementArray[i] == elem;
  25. }
  26. }
  27. }
  28.  
  29.  
  30. deleteElem(){
  31. for(int i=0;i<elementArray.length;i++){
  32. if(elem.id == elementArray[i].id){
  33. elementArray.removeAt(i);
  34. }
  35. }
  36. }
  37.  
  38.  
  39.  
  40. ========================================= Graphical Only =================================================
  41.  
  42.  
  43. function isElementNearby(targetElement, otherElement, thresholdX, thresholdY) {
  44. // Get the bounding rectangles of the two elements
  45. const targetRect = targetElement.getBoundingClientRect();
  46. const otherRect = otherElement.getBoundingClientRect();
  47.  
  48. // Calculate the distance between the centers of the elements along the X and Y axes
  49. const targetCenterX = targetRect.left + targetRect.width / 2;
  50. const targetCenterY = targetRect.top + targetRect.height / 2;
  51. const otherCenterX = otherRect.left + otherRect.width / 2;
  52. const otherCenterY = otherRect.top + otherRect.height / 2;
  53.  
  54. const distanceX = Math.abs(targetCenterX - otherCenterX);
  55. const distanceY = Math.abs(targetCenterY - otherCenterY);
  56.  
  57. // Compare the distances with the specified thresholds for X and Y axes
  58. return distanceX <= thresholdX && distanceY <= thresholdY;
  59. }
  60.  
  61. // Usage example:
  62. const targetElement = document.getElementById('targetElement');
  63. const otherElement = document.getElementById('otherElement');
  64. const thresholdX = 50; // Threshold for X-axis (in pixels)
  65. const thresholdY = 30; // Threshold for Y-axis (in pixels)
  66.  
  67. const isNearby = isElementNearby(targetElement, otherElement, thresholdX, thresholdY);
  68. console.log('Is nearby:', isNearby);
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76. function isClickOnElementArray(clickX, clickY, elementArray) {
  77. for (const element of elementArray) {
  78. const elementRect = element.getBoundingClientRect();
  79. const elementX = elementRect.left;
  80. const elementY = elementRect.top;
  81. const elementWidth = elementRect.width;
  82. const elementHeight = elementRect.height;
  83.  
  84. // Check if the click coordinates are within the bounding rectangle of the element
  85. if (
  86. clickX >= elementX &&
  87. clickX <= elementX + elementWidth &&
  88. clickY >= elementY &&
  89. clickY <= elementY + elementHeight
  90. ) {
  91. return element; // Click falls on this element
  92. }
  93. }
  94.  
  95. return false; // Click doesn't fall on any element in the array
  96. }
  97.  
  98. // Usage example:
  99. const elementArray = document.getElementsByClassName('clickable-element'); // Replace with your element array
  100. const clickX = 100; // Replace with the actual X-coordinate of the click
  101. const clickY = 200; // Replace with the actual Y-coordinate of the click
  102.  
  103. const isClickOnElement = isClickOnElementArray(clickX, clickY, elementArray);
  104. console.log('Is click on element:', isClickOnElement);
  105.  
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement