-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCollectionComponent.coffee
60 lines (46 loc) · 1.51 KB
/
CollectionComponent.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
###
Module: CollectionComponent
Author: Ray
Website: http://rayps.com
README: https://github.com/RayPS/Framer-CollectionComponent
###
class exports.CollectionComponent extends Layer
constructor: (@options = {}) ->
@options.amount ?= 16
@options.columns ?= 4
@options.gutter ?= 2
@options.cellWidth ?= 50
@options.cellHeight ?= 50
@options.cell ?= (cell) ->
@options.backgroundColor ?= "transparent"
super @options
@rows = Math.floor @options.amount / @options.columns
@width = @options.cellWidth * @options.columns + @options.gutter * (@options.columns - 1)
@height = @options.cellHeight * @options.rows + @options.gutter * (@rows - 1)
@defineCoords()
@render()
throw Error "cell is not a function" if typeof @cell is not "function"
render: ->
palette = @randomPalette()
for i in [0...@options.amount]
offsetX = i % @options.columns
offsetY = Math.floor i / @options.columns
@options.cell new Layer
parent: @
name: "Cell " + i
width: @options.cellWidth
height: @options.cellHeight
x: offsetX * @options.cellWidth + offsetX * @options.gutter
y: offsetY * @options.cellHeight + offsetY * @options.gutter
backgroundColor: palette[i]
coords: x: offsetX + 1, y: offsetY + 1
randomPalette: ->
c1 = Color.random()
c2 = Color.random()
p = []
p.push Color.mix c1, c2, i * 0.03 for i in [0...@options.amount]
return p
defineCoords: ->
try Layer.define "coords",
get: -> @_properties["coords"]
set: (coords) -> @_properties["coords"] = coords