Skip to content

Commit 17ae499

Browse files
committed
Merge branch 'master' into gh-pages
2 parents aa38aa3 + 3ed3914 commit 17ae499

File tree

6 files changed

+67
-30
lines changed

6 files changed

+67
-30
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Changelog
22

3+
## 1.0.10 - Dev 23, 2016
4+
* Reduce the ammount of canvas drawing
5+
* Allow colour to be updated without resetting the timer
6+
7+
## 1.0.9 - Aug 31, 2016
8+
* Fix invalid react proptype for showMilliseconds. Thanks @sabazusi.
9+
310
## 1.0.8 - Aug 8, 2016
411

512
* Updated React version in peerDependencies

build/react-countdown-clock.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/react-countdown-clock.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coffee/react-countdown-clock.coffee

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ module.exports = React.createClass
2020
alpha: React.PropTypes.number
2121
timeFormat: React.PropTypes.string
2222
onComplete: React.PropTypes.func
23-
showMilliseconds: React.PropTypes.boolean
23+
onClick: React.PropTypes.func
24+
showMilliseconds: React.PropTypes.bool
2425

2526
getDefaultProps: ->
2627
seconds: 60
@@ -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>

index.html

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,20 @@
4848
handleOnComplete: function(){
4949
this.setState(this.getState());
5050
},
51+
handleOnClick: function(){
52+
console.log("CLICK")
53+
this.setState({
54+
color: randomColor()
55+
});
56+
},
5157
render: function(){
5258
return (
5359
React.createElement(ReactCountdownClock, {
5460
seconds: this.state.seconds,
5561
color: this.state.color,
5662
alpha: 0.9,
57-
onComplete: this.handleOnComplete
63+
onComplete: this.handleOnComplete,
64+
onClick: this.handleOnClick
5865
})
5966
)
6067
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-countdown-clock",
3-
"version": "1.0.8",
3+
"version": "1.0.10",
44
"description": "HTML5 canvas countdown clock React component",
55
"main": "build/react-countdown-clock.js",
66
"repository": {

0 commit comments

Comments
 (0)