Skip to content

Commit

Permalink
feat(canvas): add support for HiDPI screens
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphaël Benitte committed Aug 29, 2017
1 parent 41886c1 commit 26ebb9b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
17 changes: 12 additions & 5 deletions src/components/charts/bar/BarCanvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class BarCanvas extends Component {
height,
outerWidth,
outerHeight,
pixelRatio,
margin,

// layout
Expand All @@ -65,8 +66,10 @@ class BarCanvas extends Component {
getColor,
} = props

this.surface.width = outerWidth
this.surface.height = outerHeight
this.surface.width = outerWidth * pixelRatio
this.surface.height = outerHeight * pixelRatio

this.ctx.scale(pixelRatio, pixelRatio)

let result
if (groupMode === 'grouped') {
Expand Down Expand Up @@ -132,7 +135,7 @@ class BarCanvas extends Component {
}

render() {
const { outerWidth, outerHeight, isInteractive, theme } = this.props
const { outerWidth, outerHeight, pixelRatio, isInteractive, theme } = this.props

return (
<Container isInteractive={isInteractive} theme={theme}>
Expand All @@ -141,8 +144,12 @@ class BarCanvas extends Component {
ref={surface => {
this.surface = surface
}}
width={outerWidth}
height={outerHeight}
width={outerWidth * pixelRatio}
height={outerHeight * pixelRatio}
style={{
width: outerWidth,
height: outerHeight,
}}
onMouseEnter={partial(this.handleMouseHover, showTooltip, hideTooltip)}
onMouseMove={partial(this.handleMouseHover, showTooltip, hideTooltip)}
onMouseLeave={partial(this.handleMouseLeave, hideTooltip)}
Expand Down
6 changes: 6 additions & 0 deletions src/components/charts/bar/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ export const BarPropTypes = {

// interactivity
isInteractive: PropTypes.bool,

// canvas specific
pixelRatio: PropTypes.number.isRequired,
}

export const BarDefaultProps = {
Expand All @@ -67,4 +70,7 @@ export const BarDefaultProps = {

// interactivity
isInteractive: true,

// canvas specific
pixelRatio: window && window.devicePixelRatio ? window.devicePixelRatio : 1,
}
17 changes: 12 additions & 5 deletions src/components/charts/heatmap/HeatMapCanvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class HeatMapCanvas extends Component {
height,
outerWidth,
outerHeight,
pixelRatio,
margin,
offsetX,
offsetY,
Expand All @@ -53,8 +54,10 @@ class HeatMapCanvas extends Component {
cellShape,
} = props

this.surface.width = outerWidth
this.surface.height = outerHeight
this.surface.width = outerWidth * pixelRatio
this.surface.height = outerHeight * pixelRatio

this.ctx.scale(pixelRatio, pixelRatio)

let renderNode
if (cellShape === 'rect') {
Expand Down Expand Up @@ -125,7 +128,7 @@ class HeatMapCanvas extends Component {
}

render() {
const { outerWidth, outerHeight, isInteractive, theme } = this.props
const { outerWidth, outerHeight, pixelRatio, isInteractive, theme } = this.props

return (
<Container isInteractive={isInteractive} theme={theme}>
Expand All @@ -134,8 +137,12 @@ class HeatMapCanvas extends Component {
ref={surface => {
this.surface = surface
}}
width={outerWidth}
height={outerHeight}
width={outerWidth * pixelRatio}
height={outerHeight * pixelRatio}
style={{
width: outerWidth,
height: outerHeight,
}}
onMouseEnter={partial(this.handleMouseHover, showTooltip, hideTooltip)}
onMouseMove={partial(this.handleMouseHover, showTooltip, hideTooltip)}
onMouseLeave={partial(this.handleMouseLeave, hideTooltip)}
Expand Down
8 changes: 7 additions & 1 deletion src/components/charts/heatmap/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ export const HeatMapPropTypes = {
colors: quantizeColorScalePropType.isRequired,
colorScale: PropTypes.func.isRequired, // computed

// interactivityP
// interactivity
isInteractive: PropTypes.bool,

// canvas specific
pixelRatio: PropTypes.number.isRequired,
}

export const HeatMapDefaultProps = {
Expand Down Expand Up @@ -85,4 +88,7 @@ export const HeatMapDefaultProps = {

// interactivity
isInteractive: true,

// canvas specific
pixelRatio: window && window.devicePixelRatio ? window.devicePixelRatio : 1,
}

0 comments on commit 26ebb9b

Please sign in to comment.