11import { $ } from '@semantic-ui/query' ;
2-
3- function checkIntersection ( ) {
4- const result = $ ( '.source' ) . intersects ( '.target' , {
5- returnDetails : true ,
6- } ) ;
7- console . log ( result ) ;
8- const info = `INTERSECTION DETAILS:
9-
10- Intersects: ${ result . intersects ? '✓ YES' : '✗ NO' }
11- Overlap Ratio: ${ ( result . ratio * 100 ) . toFixed ( 1 ) } %
12-
13- Side Detection:
14- Top: ${ result . top ? '✓' : '✗' }
15- Bottom: ${ result . bottom ? '✓' : '✗' }
16- Left: ${ result . left ? '✓' : '✗' }
17- Right: ${ result . right ? '✓' : '✗' }
18-
19- ${
20- result . rect
21- ? `Intersection Rectangle:
22- X: ${ result . rect . left . toFixed ( 1 ) } px
23- Y: ${ result . rect . top . toFixed ( 1 ) } px
24- Width: ${ result . rect . width . toFixed ( 1 ) } px
25- Height: ${ result . rect . height . toFixed ( 1 ) } px`
26- : 'Intersection Rectangle: None'
27- } `;
28-
29- $ ( '.output pre' ) . text ( info ) ;
30-
31- // Update visual state
32- const $boxes = $ ( '.box' ) ;
33- if ( result . intersects ) {
34- $boxes . addClass ( 'intersecting' ) ;
35- }
36- else {
37- $boxes . removeClass ( 'intersecting' ) ;
38- }
39- }
2+ import { mapObject , roundNumber } from '@semantic-ui/utils' ;
403
414// Drag functionality
425let isDragging = false ;
436let $dragged = null ;
447let offset = { top : 0 , left : 0 } ;
458
9+ // handle drag and drop start
4610$ ( '.box' ) . on ( 'mousedown' , function ( event ) {
4711 event . preventDefault ( ) ;
4812 isDragging = true ;
@@ -55,6 +19,7 @@ $('.box').on('mousedown', function(event) {
5519 moveElementToMouse ( event ) ;
5620} ) ;
5721
22+ // handle drag & drop
5823$ ( document )
5924 . on ( 'pointermove' , function ( event ) {
6025 if ( isDragging && $dragged ) {
@@ -70,11 +35,66 @@ function moveElementToMouse(event) {
7035 const container = $ ( '.container' ) . dimensions ( ) ;
7136 $dragged . position ( {
7237 relativeTo : '.container' ,
73- top : event . clientY - container . top - offset . top ,
74- left : event . clientX - container . left - offset . left ,
38+ top : event . clientY - container . top + container . scrollTop - offset . top ,
39+ left : event . clientX - container . left + container . scrollLeft - offset . left ,
7540 } ) ;
7641 checkIntersection ( ) ;
7742}
7843
7944// Initial check
8045checkIntersection ( ) ;
46+
47+ function checkIntersection ( ) {
48+ const result = $ ( '.source' ) . intersects ( '.target' , {
49+ returnDetails : true ,
50+ } ) ;
51+ const rect = ( result . rect )
52+ ? mapObject ( result . rect , val => `${ roundNumber ( val ) } px` )
53+ : null ;
54+ const info = `INTERSECTION DETAILS:
55+
56+ Intersects: ${ result . intersects ? '✓ YES' : '✗ NO' }
57+ Overlap Ratio: ${ ( result . ratio * 100 ) . toFixed ( 1 ) } %
58+
59+ Side Detection:
60+ Top: ${ result . top ? '✓' : '✗' }
61+ Bottom: ${ result . bottom ? '✓' : '✗' }
62+ Left: ${ result . left ? '✓' : '✗' }
63+ Right: ${ result . right ? '✓' : '✗' }
64+
65+ ${
66+ result . rect
67+ ? `Intersection Rectangle:
68+ X: ${ roundNumber ( rect . left ) }
69+ Y: ${ roundNumber ( rect . top ) }
70+ Width: ${ roundNumber ( rect . width ) }
71+ Height: ${ roundNumber ( rect . height ) } `
72+ : 'Intersection Rectangle: None'
73+ } `;
74+
75+ $ ( '.output pre' ) . text ( info ) ;
76+
77+ if ( rect ) {
78+ $ ( '.overlap' ) . css ( {
79+ opacity : 1 ,
80+ top : rect . top ,
81+ left : rect . left ,
82+ width : rect . width ,
83+ height : rect . height ,
84+ } ) ;
85+ }
86+ else {
87+ $ ( '.overlap' ) . css ( {
88+ opacity : 0 ,
89+ } ) ;
90+ }
91+
92+ // Update visual state
93+ const $boxes = $ ( '.box' ) . not ( '.overlap' ) ;
94+ if ( result . intersects ) {
95+ $boxes . addClass ( 'intersecting' ) ;
96+ }
97+ else {
98+ $boxes . removeClass ( 'intersecting' ) ;
99+ }
100+ }
0 commit comments