Skip to content

Commit

Permalink
Support retina displays
Browse files Browse the repository at this point in the history
  • Loading branch information
danvk committed Sep 29, 2015
1 parent 96cf5c6 commit d5fc720
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/main/CoverageTrack.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class CoverageTrack extends React.Component {

// Hold off until height & width are known.
if (width === 0) return;
d3utils.setAttributes(canvas, {width, height});
d3utils.sizeCanvas(canvas, width, height);

var yScale = d3.scale.linear()
.domain([this.state.maxCoverage, 0])
Expand Down
2 changes: 1 addition & 1 deletion src/main/GeneTrack.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ var GeneTrack = React.createClass({
.range([0, width])
.clamp(true);

d3utils.setAttributes(canvas, {width, height});
d3utils.sizeCanvas(canvas, width, height);

var ctx = dataCanvas.getDataContext(canvasUtils.getContext(canvas));
ctx.reset();
Expand Down
2 changes: 1 addition & 1 deletion src/main/GenomeTrack.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ var GenomeTrack = React.createClass({

// Hold off until height & width are known.
if (width === 0) return;
d3utils.setAttributes(canvas, {width, height});
d3utils.sizeCanvas(canvas, width, height);

var scale = this.getScale();
var pxPerLetter = scale(1) - scale(0);
Expand Down
2 changes: 1 addition & 1 deletion src/main/LocationTrack.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class LocationTrack extends React.Component {
{range, width, height} = this.props,
scale = this.getScale();

d3utils.setAttributes(canvas, {width, height});
d3utils.sizeCanvas(canvas, width, height);

var ctx = dataCanvas.getDataContext(canvasUtils.getContext(this.getDOMNode()));
ctx.save();
Expand Down
2 changes: 1 addition & 1 deletion src/main/PileupTrack.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ class PileupTrack extends React.Component {
// Height can only be computed after the pileup has been updated.
var height = yForRow(this.cache.pileupHeightForRef(this.props.range.contig));

d3utils.setAttributes(canvas, {width, height});
d3utils.sizeCanvas(canvas, width, height);

var ctx = canvasUtils.getContext(canvas);
var dtx = dataCanvas.getDataContext(ctx);
Expand Down
2 changes: 1 addition & 1 deletion src/main/ScaleTrack.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class ScaleTrack extends React.Component {
var canvas = this.getDOMNode(),
{range, width, height} = this.props;

d3utils.setAttributes(canvas, {width, height});
d3utils.sizeCanvas(canvas, width, height);

var ctx = dataCanvas.getDataContext(canvasUtils.getContext(canvas));
ctx.save();
Expand Down
2 changes: 1 addition & 1 deletion src/main/VariantTrack.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ var VariantTrack = React.createClass({
// Hold off until height & width are known.
if (width === 0) return;

d3utils.setAttributes(canvas, {width, height});
d3utils.sizeCanvas(canvas, width, height);
var ctx = canvasUtils.getContext(canvas);
var dtx = dataCanvas.getDataContext(ctx);
this.renderScene(dtx);
Expand Down
17 changes: 11 additions & 6 deletions src/main/d3utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,22 @@ function formatRange(viewSize: number): any {
}

/**
* Set attributes on an element. This can be used in place of, e.g.
* d3.select(div).attr({width, height});
* Sizes a canvas appropriately for this device.
*/
function setAttributes(el: Element, attrs: {[key:string]: any}) {
for (var k in attrs) {
el.setAttribute(k, attrs[k]);
function sizeCanvas(el: HTMLCanvasElement, width: number, height: number) {
var ratio = window.devicePixelRatio;
el.width = width * ratio;
el.height = height * ratio;
el.style.width = width + 'px';
el.style.height = height + 'px';
var ctx = el.getContext('2d');
if (ctx != null && ctx instanceof CanvasRenderingContext2D) {
ctx.scale(ratio, ratio);
}
}

module.exports = {
formatRange,
getTrackScale,
setAttributes
sizeCanvas
};

0 comments on commit d5fc720

Please sign in to comment.