Skip to content

Instantly share code, notes, and snippets.

@hernamesbarbara
Last active October 20, 2018 19:56
Show Gist options
  • Save hernamesbarbara/76dab47c526c03391c3484ff5bac4721 to your computer and use it in GitHub Desktop.
Save hernamesbarbara/76dab47c526c03391c3484ff5bac4721 to your computer and use it in GitHub Desktop.
readData = () ->
rawJson = Utils.domLoadDataSync "data.json"
return JSON.parse rawJson
# recursively clone an object
clone = (obj) ->
return obj if obj is null or typeof (obj) isnt "object"
temp = new obj.constructor()
for key of obj
temp[key] = clone(obj[key])
return temp
createColor = (hexStr, opacity) ->
# returns an rgba string with opacity parameter
# e.g. createColor("#333333", 0.5) => "rgba(221, 221, 221, 1)"
try
opacity = parseFloat(opacity, 10)
catch error
opacity = 1.0
finally
if opacity is null or isNaN(opacity) or opacity > 1 or opacity < 0
opacity = 1.0
color = new Color(hexStr)
rgbStr = color.toRgbString()
[r, g, b] = rgbStr.replace(/^(rgb|rgba)\(/,'').replace(/\)$/,'').replace(/\s/g,'').split(',')
return "rgba(#{r}, #{g}, #{b}, #{opacity})"
class CardHolder extends Layer
constructor: (parent, options={}) ->
options.width = parent.width
options.height = parent.height
options.x = parent.x
options.y = parent.y
@cards = []
@maxCards = options.maxCards
super options
appendCard: (card) ->
card.height = @height / @maxCards
card.y = @cards.length * card.height
@cards.push(card)
return card
class Card extends Layer
constructor: (cardHolder, options={}) ->
options.width = cardHolder.width
options.parent = cardHolder
super options
cardHolderOpts = {
backgroundColor: "transparent"
maxCards: 4
}
cardOpts = {
backgroundColor: "red"
borderWidth:
bottom: 0.75
textOverflow: "ellipsis"
}
cardHolder = new CardHolder(mainContent, cardHolderOpts)
colours = ["#b8f1de", "#f0cef9", "#fa9494", "#3467d6"]
for tup in _.zip([0..cardHolder.maxCards-1], colours)
idx = tup[0]
colour = tup[1]
opts = clone(cardOpts)
opts.backgroundColor = colour
card = new Card(cardHolder, opts)
cardHolder.appendCard(card)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment