/
frame.coffee
85 lines (64 loc) · 1.53 KB
/
frame.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
utils = require "../utils"
{EventEmitter} = require "../eventemitter"
class Frame extends EventEmitter
constructor: (args) ->
@properties = args
@define "properties"
get: ->
p = {}
for key, value of Frame.Properties
p[key] = @[key] or Frame.Properties[key]
return p
set: (args) ->
for key, value of Frame.Properties
@[key] = args[key] if args[key] not in [null, undefined]
for key, value of Frame.CalculatedProperties
@[key] = args[key] if args[key] not in [null, undefined]
@define "minX"
get: -> @x
set: (value) -> @x = value
@define "midX"
get: -> @x + (@width / 2.0)
set: (value) ->
throw Error "Width is zero" if @width is 0
@x = value - (@width / 2.0)
@define "maxX"
get: -> @x + @width
set: (value) ->
throw Error "Width is zero" if @width is 0
@x = value - @width
@define "minY"
get: -> @y
set: (value) -> @y = value
@define "midY"
get: -> @y + (@height / 2.0)
set: (value) ->
throw Error "Width is zero" if @height is 0
@y = value - (@height / 2.0)
@define "maxY"
get: -> @y + @height
set: (value) ->
throw Error "Width is zero" if @height is 0
@y = value - @height
merge: (r2) ->
r1 = @
frame =
x: Math.min(r1.x, r2.x)
y: Math.min(r1.y, r2.y)
width: Math.max(r1.width, r2.width)
height: Math.max(r1.height, r2.height)
return new Frame frame
Frame.Properties =
x: 0
y: 0
z: 0
width: 0
height: 0
Frame.CalculatedProperties =
minX: null
midX: null
maxX: null
minY: null
midY: null
maxY: null
exports.Frame = Frame