Skip to content

Commit 078d5c1

Browse files
committed
Fixes for scaling
* Removed scale prop and set scale automatically. * Fix canvas being scaled twice.
1 parent 3241c7f commit 078d5c1

File tree

3 files changed

+16
-17
lines changed

3 files changed

+16
-17
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ npm install react-countdown-clock
2121
color="#000"
2222
alpha={0.9}
2323
size={300}
24-
scale={window.devicePixelRatio}
2524
onComplete={myCallback} />
2625
```
2726

@@ -41,7 +40,6 @@ npm install react-countdown-clock
4140
| onComplete | func | | Callback when time completes |
4241
| paused | boolean | false | Pause countdown of the timer |
4342
| pausedText | string | | Text to display when paused, defaults to the current time |
44-
| scale | float | 1 | Scale of canvas, set this to `window.devicePixelRatio` |
4543

4644
## Bugs & Contributions
4745

coffee/react-countdown-clock.coffee

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ ReactCountdownClock = CreateReactClass
99
_content: null
1010
_canvas: null
1111
_timeoutIds: []
12+
_scale: window.devicePixelRatio || 1
1213

1314
displayName: 'ReactCountdownClock'
1415

1516
componentDidUpdate: (props) ->
17+
console.log @props
1618
if props.seconds != @props.seconds
1719
@_seconds = @props.seconds
1820
@_setupTimer()
@@ -62,19 +64,22 @@ ReactCountdownClock = CreateReactClass
6264
if tick > 1000 then 1000 else tick
6365

6466
_setupCanvases: ->
67+
return if @_background && @_timer
68+
6569
@_background = @refs.background.getContext '2d'
70+
@_background.scale @_scale, @_scale
71+
6672
@_timer = @refs.timer.getContext '2d'
6773
@_timer.textAlign = 'center'
6874
@_timer.textBaseline = 'middle'
75+
@_timer.scale @_scale, @_scale
76+
6977
if @props.onClick?
7078
@refs.component.addEventListener 'click', @props.onClick
71-
if @props.scale?
72-
@_background.scale(@props.scale, @props.scale)
73-
@_timer.scale(@props.scale, @props.scale)
7479

7580
_startTimer: ->
7681
# Give it a moment to collect it's thoughts for smoother render
77-
@_timeoutIds.push(setTimeout ( => @_tick() ), 200)
82+
@_timeoutIds.push(setTimeout( => @_tick() ), 200)
7883

7984
_pauseTimer: ->
8085
@_stopTimer()
@@ -177,13 +182,12 @@ ReactCountdownClock = CreateReactClass
177182
@_timer.fill()
178183

179184
render: ->
180-
width = @props.size * @props.scale
181-
height = @props.size * @props.scale
182-
style = { position: 'absolute', width: width / 2, height: height / 2 }
185+
canvasStyle = { position: 'absolute', width: @props.size, height: @props.size }
186+
canvasProps = { style: canvasStyle, height: @props.size * @_scale, width: @props.size * @_scale }
183187

184-
<div ref='component' className="react-countdown-clock" style={width: width, height: height}>
185-
<canvas ref='background' style={style} width={width} height={height}></canvas>
186-
<canvas ref='timer' style={style} width={width} height={height}></canvas>
188+
<div ref='component' className="react-countdown-clock" style={width: @props.size, height: @props.size}>
189+
<canvas ref='background' {...canvasProps}></canvas>
190+
<canvas ref='timer' {...canvasProps}></canvas>
187191
</div>
188192

189193
ReactCountdownClock.propTypes =
@@ -200,7 +204,6 @@ ReactCountdownClock.propTypes =
200204
showMilliseconds: PropTypes.bool
201205
paused: PropTypes.bool
202206
pausedText: PropTypes.string
203-
scale: PropTypes.number
204207

205208
ReactCountdownClock.defaultProps =
206209
seconds: 60
@@ -212,6 +215,5 @@ ReactCountdownClock.defaultProps =
212215
font: 'Arial'
213216
showMilliseconds: true
214217
paused: false
215-
scale: 1
216218

217219
module.exports = ReactCountdownClock

index.html

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<script src="./build/react-countdown-clock.js"></script>
2626
<script>
2727
var MAX = 120;
28-
var MIN = 30;
28+
var MIN = 10;
2929

3030
var randomAmountOfSeconds = function(){
3131
return Math.floor( Math.random() * ( MAX - MIN + 1) + MIN )
@@ -69,8 +69,7 @@
6969
alpha: 0.9,
7070
onComplete: this.handleOnComplete,
7171
onClick: this.handleOnClick,
72-
fontSize: this.state.fontSize,
73-
scale: window.devicePixelRatio
72+
fontSize: this.state.fontSize
7473
})
7574
)
7675
}

0 commit comments

Comments
 (0)