@@ -20,6 +20,7 @@ module.exports = React.createClass
2020 alpha : React .PropTypes .number
2121 timeFormat : React .PropTypes .string
2222 onComplete : React .PropTypes .func
23+ onClick : React .PropTypes .func
2324 showMilliseconds : React .PropTypes .bool
2425
2526 getDefaultProps : ->
@@ -32,9 +33,15 @@ module.exports = React.createClass
3233 font : ' Arial'
3334 showMilliseconds : true
3435
35- componentWillReceiveProps : (props ) ->
36- @_seconds = props .seconds
37- @ _setupTimer ()
36+ componentDidUpdate : (props ) ->
37+ if props .seconds != @props .seconds
38+ @_seconds = props .seconds
39+ @ _setupTimer ()
40+
41+ if props .color != @props .color
42+ @ _clearBackground ()
43+ @ _drawBackground ()
44+ @ _updateCanvas ()
3845
3946 componentDidMount : ->
4047 @_seconds = @props .seconds
@@ -45,7 +52,8 @@ module.exports = React.createClass
4552
4653 _setupTimer : ->
4754 @ _setScale ()
48- @ _setupCanvas ()
55+ @ _setupCanvases ()
56+ @ _drawBackground ()
4957 @ _drawTimer ()
5058 @ _startTimer ()
5159
@@ -70,11 +78,13 @@ module.exports = React.createClass
7078 tick = @_seconds * tickScale
7179 if tick > 1000 then 1000 else tick
7280
73- _setupCanvas : ->
74- @_canvas = @refs .canvas
75- @_context = @_canvas .getContext ' 2d'
76- @_context .textAlign = ' center'
77- @_context .textBaseline = ' middle'
81+ _setupCanvases : ->
82+ @_background = @refs .background .getContext ' 2d'
83+ @_timer = @refs .timer .getContext ' 2d'
84+ @_timer .textAlign = ' center'
85+ @_timer .textBaseline = ' middle'
86+ if @props .onClick ?
87+ @refs .component .addEventListener ' click' , @props .onClick
7888
7989 _startTimer : ->
8090 # Give it a moment to collect it's thoughts for smoother render
@@ -84,6 +94,9 @@ module.exports = React.createClass
8494 for timeout in @_timeoutIds
8595 clearTimeout timeout
8696
97+ if @props .onClick ?
98+ @refs .component .removeEventListener ' click' , @props .onClick
99+
87100 _tick : ->
88101 start = Date .now ()
89102 @_timeoutIds .push (setTimeout ( =>
@@ -103,16 +116,20 @@ module.exports = React.createClass
103116 if @props .onComplete
104117 @props .onComplete ()
105118
119+ _clearBackground : ->
120+ @_background .clearRect 0 , 0 , @refs .timer .width , @refs .timer .height
121+
106122 _clearTimer : ->
107- @_context .clearRect 0 , 0 , @_canvas .width , @_canvas .height
108- @ _drawBackground ()
123+ @_timer .clearRect 0 , 0 , @refs .timer .width , @refs .timer .height
109124
110125 _drawBackground : ->
111- @_context .beginPath ()
112- @_context .globalAlpha = @props .alpha / 3
113- @_context .arc @_radius , @_radius , @_radius , 0 , Math .PI * 2 , false
114- @_context .arc @_radius , @_radius , @_innerRadius , Math .PI * 2 , 0 , true
115- @_context .fill ()
126+ @_background .beginPath ()
127+ @_background .globalAlpha = @props .alpha / 3
128+ @_background .fillStyle = @props .color
129+ @_background .arc @_radius , @_radius , @_radius , 0 , Math .PI * 2 , false
130+ @_background .arc @_radius , @_radius , @_innerRadius , Math .PI * 2 , 0 , true
131+ @_background .closePath ()
132+ @_background .fill ()
116133
117134 _formattedTime : ->
118135 decimals = (@_seconds <= 9.9 && @props .showMilliseconds ) ? 1 : 0
@@ -149,14 +166,20 @@ module.exports = React.createClass
149166 _drawTimer : ->
150167 percent = @_fraction * @_seconds + 1.5
151168 formattedTime = @ _formattedTime ()
152- @_context .globalAlpha = @props .alpha
153- @_context .fillStyle = @props .color
154- @_context .font = " bold #{ @ _fontSize (formattedTime)} #{ @props .font } "
155- @_context .fillText formattedTime, @_radius , @_radius
156- @_context .beginPath ()
157- @_context .arc @_radius , @_radius , @_radius , Math .PI * 1.5 , Math .PI * percent, false
158- @_context .arc @_radius , @_radius , @_innerRadius , Math .PI * percent, Math .PI * 1.5 , true
159- @_context .fill ()
169+
170+ # Timer
171+ @_timer .globalAlpha = @props .alpha
172+ @_timer .fillStyle = @props .color
173+ @_timer .font = " bold #{ @ _fontSize (formattedTime)} #{ @props .font } "
174+ @_timer .fillText formattedTime, @_radius , @_radius
175+ @_timer .beginPath ()
176+ @_timer .arc @_radius , @_radius , @_radius , Math .PI * 1.5 , Math .PI * percent, false
177+ @_timer .arc @_radius , @_radius , @_innerRadius , Math .PI * percent, Math .PI * 1.5 , true
178+ @_timer .closePath ()
179+ @_timer .fill ()
160180
161181 render : ->
162- <canvas ref = ' canvas' className = " react-countdown-clock" width = {@props .size } height = {@props .size }></canvas >
182+ <div ref = ' component' className = " react-countdown-clock" >
183+ <canvas ref = ' background' style = { position : ' absolute' } width = {@props .size } height = {@props .size }></canvas >
184+ <canvas ref = ' timer' style = { position : ' absolute' } width = {@props .size } height = {@props .size }></canvas >
185+ </div >
0 commit comments