Skip to content

Instantly share code, notes, and snippets.

View steveruizok's full-sized avatar
🏠

Steve Ruiz steveruizok

🏠
View GitHub Profile
class Button extends Layer
constructor: (options = {}) ->
super options
@onTap @buttonAction
buttonAction: => print "Hello"
@steveruizok
steveruizok / buttonWrapper.coffee
Created October 23, 2017 05:42
A simple button wrapper.
buttonWrapper = (layer) ->
layer.states =
default:
brightness: 100
opacity: 1
hover:
brightness: 130
opacity: 1
down:
brightness: 82
# Set a layer's contraints to its parent
# @example Utils.constrain(layer, {left: true, top: true, asepectRatio: true})
Utils.constrain = (layer, opts...) ->
if not layer.parent? then throw 'Utils.constrain requires a layer with a parent.'
options =
left: false,
top: false,
right: false,
bottom: false,
# Capture keyboard events
keyHandlers = {}
enabled = false
enable = ->
window.addEventListener 'keydown', (event) ->
event.preventDefault() if exports.preventDefault
try keyHandlers[event.key]()
# Transitions
# Compiled by @myvo, packaged up by @steveruizok
#
# Usage
#
# - Drop this file in your project's modules folder.
#
# - At the top of your project, include:
# Transitions = require "Transitions"
#
arrayOfLayers = []
[0..4].forEach (num, i) ->
arrayOfLayers[i] = new Layer
x: i * 220
y: Align.center(-200)
arrayOfLayers[i].isOn = false
arrayOfLayers[i].turnOn = -> @isOn = true
specialButton = undefined
handleTap = (event, layer) ->
if specialButton is @
@backgroundColor = "grey"
specialButton = undefined
return
specialButton = @
_.range(6).forEach (i) ->
b = new Layer
size: 80
y: Align.center()
x: 128 + 96 * i
backgroundColor: "grey"
b.onTap ->
@_special = !@_special
@backgroundColor = if @_special then "orange" else "grey"
class MyClass extends Layer
constructor: (options = {}) ->
super options
@_isRightEnough = @x > 200
@onIsRightEnough = -> null
@on "change:x", ->
isRightEnough = @x > 200
###
Define a property on a Layer that will emit a change event when that property changes.
Also, optionally give the property an initial value and a callback to run when the property changes.
@param {Layer} layer The layer on which to define the property.
@param {String} property The name of the property.
@param {Object} [value] The initial value of the property.
@param {Function} [callback] The callback to run when this property changes. Executed with two arguments: the property's new value and the Layer itself.
@param {Function} [validation] A function to validate the property's new value.
@param {String} [error] An error to throw if the validation function returned false.