Jump to content
Search Community

HitTest without draggable

gandoulfe test
Moderator Tag

Warning: Please note

This thread was started before GSAP 3 was released. Some information, especially the syntax, may be out of date for GSAP 3. Please see the GSAP 3 migration guide and release notes for more information about how to update the code to GSAP 3's syntax. 

Recommended Posts

Hi, 

 

First, sorry for my english, I am french. 

 

I made some particles, and I want to erase them with an eraser.

There is no draggable element, the eraser moves by himself automatically (with a bezier path).

 

I want the particles under the eraser to disappear.

 

I am trying to use the HitTest() function but i cannot figure out to use it without a draggable element.

 

Thanks for your help.

 

David.

post-35036-0-80959500-1463750731_thumb.png

See the Pen GZbxYL by DavidN (@DavidN) on CodePen

  • Like 1
Link to comment
Share on other sites

Draggable has a static hitTest() method that you can just feed two elements into, but beware that it simply uses the bounding boxes to measure overlaps, so it's not literally pixel-perfect for rotated or oddly-shaped things (that'd be a lot more processor-intensive). For example:

//simple overlap test
if (Draggable.hitTest(element1, element2)) {
   //do stuff
}

You can also check that a certain amount or percentage of the elements overlap. See the docs: 

http://greensock.com/docs/#/HTML5/GSAP/Utils/Draggable/hitTest/

  • Like 3
Link to comment
Share on other sites

You would need to do SAT collision detection...

See the Pen 40b18190c60161960d8d12ff4fa71d1d?editors=0010 by osublake (@osublake) on CodePen

 

That's very hard to do with HTML elements because you have to calculate the current transformation matrix for every point on your elements. I think this would be easier to do with SVG masks.

See the Pen a9eb0affd4f2c0c9f05a0e33168fe185 by osublake (@osublake) on CodePen

  • Like 3
Link to comment
Share on other sites

I finally made it  ! 

 

 

I have used a ticker event listener to listen when there is a hit between my eraser and my particles 

 

TweenMax.ticker.addEventListener("tick", hitDetection);
and the hitTest function
 
//simple overlap test
if (Draggable.hitTest(element1, element2)) {
   //do stuff
}

If you have an idea to improve the code, please tell me.

 

Thanks.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...