Skip to content

Instantly share code, notes, and snippets.

@andrewliebchen
Created December 8, 2017 17:02
Show Gist options
  • Save andrewliebchen/912be509827829ae25c47871aece8f97 to your computer and use it in GitHub Desktop.
Save andrewliebchen/912be509827829ae25c47871aece8f97 to your computer and use it in GitHub Desktop.
BoundsLayer component: a layer that knows if a click is inside or outside itself
class BoundsLayer extends Layer
constructor: (props) ->
super props
@_origin = @convertPointToScreen({x: 0, y: 0})
@background = new Layer
parent: @
size: Screen.size
x: -@_origin.x
y: -@_origin.y
backgroundColor: null
@background.sendToBack()
@background.on Events.Click, (event, layer) =>
bounds =
x:
start: @_origin.x
end: @x + @width
y:
start: @_origin.y
end: @y + @height
inXBounds = _.inRange event.x, bounds.x.start, bounds.x.end
inYBounds = _.inRange event.y, bounds.y.start, bounds.y.end
@in = inXBounds && inYBounds
# Example
target = new BoundsLayer
x: Align.center
y: Align.center
backgroundColor: 'blue'
target.onClick ->
unless @in then @visible = false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment