Skip to content

Instantly share code, notes, and snippets.

@awt2542
Last active January 3, 2018 15:57
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save awt2542/c63c285e14ced9773a97dabe946a12b5 to your computer and use it in GitHub Desktop.
Save awt2542/c63c285e14ced9773a97dabe946a12b5 to your computer and use it in GitHub Desktop.
Detect when a layer is inside or overlaps another layer. eg. layerA.isOverlapping(layerB)
Layer::isOverlapping = (targetLayer, offset = 0) ->
if @screenFrame.x + @width + offset >= targetLayer.screenFrame.x &&
@screenFrame.x - offset <= targetLayer.screenFrame.x + targetLayer.width &&
@screenFrame.y + @height + offset >= targetLayer.screenFrame.y &&
@screenFrame.y - offset <= targetLayer.screenFrame.y + targetLayer.height
true
else
false
Layer::isInside = (targetLayer, offset = 0) ->
if @screenFrame.x + offset >= targetLayer.screenFrame.x &&
@screenFrame.x - offset <= targetLayer.screenFrame.x + targetLayer.width - @width &&
@screenFrame.y + offset >= targetLayer.screenFrame.y &&
@screenFrame.y - offset <= targetLayer.screenFrame.y + targetLayer.height - @height
true
else
false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment