Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Honor devicePixelRatio take 3 #3288

Merged
merged 4 commits into from
Jan 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ Change Log
* Added `getAbsoluteUri` helper function.
* Added `VRButton` which is a simple, single-button widget that toggles VR mode. It is off by default. To enable the button, set the `vrButton` option to `Viewer` to `true`. Only Cardboard for mobile is supported. More VR devices will be supported when the WebVR API is more stable.
* Added `Scene.useWebVR` to switch the scene to use stereoscopic rendering.
* Cesium now honors `window.devicePixelRatio` on browsers that support the CSS `imageRendering` attribute. This greatly improves performance on mobile devices and high DPI displays by rendering at the browser-recommended resolution. This also reduces bandwidth usage and increases battery life in these cases. To enable the previous behavior, use the following code:
```javascript
if(Cesium.FeatureDetection.supportsImageRenderingPixelated()){
viewer.resolutionScale = window.devicePixelRatio;
}
```

### 1.17 - 2016-01-04

Expand Down
28 changes: 26 additions & 2 deletions Source/Core/FeatureDetection.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,28 @@ define([
return hasPointerEvents;
}

var imageRenderingValueResult;
var supportsImageRenderingPixelatedResult;
function supportsImageRenderingPixelated() {
if (!defined(supportsImageRenderingPixelatedResult)) {
var canvas = document.createElement('canvas');
canvas.setAttribute('style',
'image-rendering: -moz-crisp-edges;' +
'image-rendering: pixelated;');
//canvas.style.imageRendering will be undefined, null or an empty string on unsupported browsers.
var tmp = canvas.style.imageRendering;
supportsImageRenderingPixelatedResult = defined(tmp) && tmp !== '';
if (supportsImageRenderingPixelatedResult) {
imageRenderingValueResult = tmp;
}
}
return supportsImageRenderingPixelatedResult;
}

function imageRenderingValue() {
return supportsImageRenderingPixelated() ? imageRenderingValueResult : undefined;
}

/**
* A set of functions to detect whether the current browser supports
* various features.
Expand All @@ -175,7 +197,9 @@ define([
firefoxVersion : firefoxVersion,
isWindows : isWindows,
hardwareConcurrency : defaultValue(theNavigator.hardwareConcurrency, 3),
supportsPointerEvents : supportsPointerEvents
supportsPointerEvents : supportsPointerEvents,
supportsImageRenderingPixelated: supportsImageRenderingPixelated,
imageRenderingValue: imageRenderingValue
};

/**
Expand Down Expand Up @@ -213,4 +237,4 @@ define([
};

return FeatureDetection;
});
});
17 changes: 14 additions & 3 deletions Source/Widgets/CesiumWidget/CesiumWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ define([
'../../Core/destroyObject',
'../../Core/DeveloperError',
'../../Core/Ellipsoid',
'../../Core/FeatureDetection',
'../../Core/formatError',
'../../Core/requestAnimationFrame',
'../../Core/ScreenSpaceEventHandler',
Expand All @@ -33,6 +34,7 @@ define([
destroyObject,
DeveloperError,
Ellipsoid,
FeatureDetection,
formatError,
requestAnimationFrame,
ScreenSpaceEventHandler,
Expand Down Expand Up @@ -98,13 +100,16 @@ define([
var canvas = widget._canvas;
var width = canvas.clientWidth;
var height = canvas.clientHeight;
var zoomFactor = defaultValue(window.devicePixelRatio, 1.0) * widget._resolutionScale;
var resolutionScale = widget._resolutionScale;
if (!widget._supportsImageRenderingPixelated) {
resolutionScale *= defaultValue(window.devicePixelRatio, 1.0);
}

widget._canvasWidth = width;
widget._canvasHeight = height;

width *= zoomFactor;
height *= zoomFactor;
width *= resolutionScale;
height *= resolutionScale;

canvas.width = width;
canvas.height = height;
Expand Down Expand Up @@ -205,6 +210,12 @@ define([
container.appendChild(element);

var canvas = document.createElement('canvas');
var supportsImageRenderingPixelated = FeatureDetection.supportsImageRenderingPixelated();
this._supportsImageRenderingPixelated = supportsImageRenderingPixelated;
if (supportsImageRenderingPixelated) {
canvas.style.imageRendering = FeatureDetection.imageRenderingValue();
}

canvas.oncontextmenu = function() {
return false;
};
Expand Down
10 changes: 10 additions & 0 deletions Specs/Core/FeatureDetectionSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,14 @@ defineSuite([
console.log('detected Firefox ' + firefoxVersion.join('.'));
}
});

it('detects imageRendering support', function() {
var supportsImageRenderingPixelated = FeatureDetection.supportsImageRenderingPixelated();
expect(typeof supportsImageRenderingPixelated).toEqual('boolean');
if (supportsImageRenderingPixelated) {
expect(FeatureDetection.imageRenderingValue()).toBeDefined();
} else {
expect(FeatureDetection.imageRenderingValue()).not.toBeDefined();
}
});
});