Skip to content
This repository has been archived by the owner on Jul 29, 2019. It is now read-only.

Consolidate code for determining the pixel ratio #3088

Merged
merged 3 commits into from
Jun 13, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions lib/network/modules/Canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,17 +325,30 @@ class Canvas {


/**
* @private
* Determine the pixel ratio for various browsers.
*
* Note that there is no dependency on local context; this might as well be
* a static method.
*
* Also called in CanvasRenderer.
*/
_setPixelRatio(ctx) {
this.pixelRatio = (window.devicePixelRatio || 1) / (ctx.webkitBackingStorePixelRatio ||
_getPixelRatio(ctx) {
return (window.devicePixelRatio || 1) / (ctx.webkitBackingStorePixelRatio ||
ctx.mozBackingStorePixelRatio ||
ctx.msBackingStorePixelRatio ||
ctx.oBackingStorePixelRatio ||
ctx.backingStorePixelRatio || 1);
}


/**
* @private
*/
_setPixelRatio(ctx) {
this.pixelRatio = this._getPixelRatio(ctx);
}


/**
* Convert the X coordinate in DOM-space (coordinate point in browser relative to the container div) to
* the X coordinate in canvas-space (the simulation sandbox, which the camera looks upon)
Expand Down
13 changes: 2 additions & 11 deletions lib/network/modules/CanvasRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,7 @@ class CanvasRenderer {
this.canvas.setSize();
}

this.pixelRatio = (window.devicePixelRatio || 1) / (ctx.webkitBackingStorePixelRatio ||
ctx.mozBackingStorePixelRatio ||
ctx.msBackingStorePixelRatio ||
ctx.oBackingStorePixelRatio ||
ctx.backingStorePixelRatio || 1);

this.pixelRatio = this.canvas._getPixelRatio(ctx);
ctx.setTransform(this.pixelRatio, 0, 0, this.pixelRatio, 0, 0);

// clear the canvas
Expand Down Expand Up @@ -206,11 +201,7 @@ class CanvasRenderer {
_resizeNodes() {
let ctx = this.canvas.frame.canvas.getContext('2d');
if (this.pixelRatio === undefined) {
this.pixelRatio = (window.devicePixelRatio || 1) / (ctx.webkitBackingStorePixelRatio ||
ctx.mozBackingStorePixelRatio ||
ctx.msBackingStorePixelRatio ||
ctx.oBackingStorePixelRatio ||
ctx.backingStorePixelRatio || 1);
this.pixelRatio = this.canvas._getPixelRatio(ctx);
}
ctx.setTransform(this.pixelRatio, 0, 0, this.pixelRatio, 0, 0);
ctx.save();
Expand Down