Skip to content

Commit

Permalink
Replace canvas.js with a minimal shim for substituting headless-gl
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed Sep 2, 2016
1 parent c24473f commit 83d9609
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 87 deletions.
56 changes: 44 additions & 12 deletions js/ui/map.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict';

var Canvas = require('../util/canvas');
var util = require('../util/util');
var browser = require('../util/browser');
var window = require('../util/window');
var Evented = require('../util/evented');
var DOM = require('../util/dom');
var webGLContext = require('../util/webgl_context');

var Style = require('../style/style');
var AnimationLoop = require('../style/animation_loop');
Expand All @@ -21,6 +21,7 @@ var LngLat = require('../geo/lng_lat');
var LngLatBounds = require('../geo/lng_lat_bounds');
var Point = require('point-geometry');
var Attribution = require('./control/attribution');
var isSupported = require('mapbox-gl-supported');

var defaultMinZoom = 0;
var defaultMaxZoom = 20;
Expand Down Expand Up @@ -157,6 +158,8 @@ var Map = module.exports = function(options) {
'_onSourceUpdate',
'_onWindowOnline',
'_onWindowResize',
'_contextLost',
'_contextRestored',
'_update',
'_render'
], this);
Expand Down Expand Up @@ -308,14 +311,11 @@ util.extend(Map.prototype, /** @lends Map.prototype */{
* @returns {Map} `this`
*/
resize: function() {
var width = 0, height = 0;
var dimensions = this._containerDimensions();
var width = dimensions[0];
var height = dimensions[1];

if (this._container) {
width = this._container.offsetWidth || 400;
height = this._container.offsetHeight || 300;
}

this._canvas.resize(width, height);
this._resizeCanvas(width, height);
this.transform.resize(width, height);
this.painter.resize(width, height);

Expand Down Expand Up @@ -899,7 +899,19 @@ util.extend(Map.prototype, /** @lends Map.prototype */{
* @returns {HTMLCanvasElement} The map's `<canvas>` element.
*/
getCanvas: function() {
return this._canvas.getElement();
return this._canvas;
},

_containerDimensions: function() {
var width = 0;
var height = 0;

if (this._container) {
width = this._container.offsetWidth || 400;
height = this._container.offsetHeight || 300;
}

return [width, height];
},

_setupContainer: function() {
Expand All @@ -910,7 +922,15 @@ util.extend(Map.prototype, /** @lends Map.prototype */{
if (this._interactive) {
canvasContainer.classList.add('mapboxgl-interactive');
}
this._canvas = new Canvas(this, canvasContainer);

this._canvas = DOM.create('canvas', 'mapboxgl-canvas', canvasContainer);
this._canvas.style.position = 'absolute';
this._canvas.addEventListener('webglcontextlost', this._contextLost, false);
this._canvas.addEventListener('webglcontextrestored', this._contextRestored, false);
this._canvas.setAttribute('tabindex', 0);

var dimensions = this._containerDimensions();
this._resizeCanvas(dimensions[0], dimensions[1]);

var controlContainer = this._controlContainer = DOM.create('div', 'mapboxgl-control-container', container);
var corners = this._controlCorners = {};
Expand All @@ -919,11 +939,23 @@ util.extend(Map.prototype, /** @lends Map.prototype */{
});
},

_resizeCanvas: function(width, height) {
var pixelRatio = window.devicePixelRatio || 1;

// Request the required canvas size taking the pixelratio into account.
this._canvas.width = pixelRatio * width;
this._canvas.height = pixelRatio * height;

// Maintain the same canvas size, potentially downscaling it for HiDPI displays
this._canvas.style.width = width + 'px';
this._canvas.style.height = height + 'px';
},

_setupPainter: function() {
var gl = this._canvas.getWebGLContext({
var gl = webGLContext(this._canvas, util.extend({
failIfMajorPerformanceCaveat: this._failIfMajorPerformanceCaveat,
preserveDrawingBuffer: this._preserveDrawingBuffer
});
}, isSupported.webGLContextAttributes));

if (!gl) {
this.fire('error', { error: new Error('Failed to initialize WebGL') });
Expand Down
43 changes: 0 additions & 43 deletions js/util/browser/canvas.js

This file was deleted.

6 changes: 6 additions & 0 deletions js/util/browser/webgl_context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

module.exports = function(canvas, attributes) {
return canvas.getContext('webgl', attributes) ||
canvas.getContext('experimental-webgl', attributes);
};
30 changes: 0 additions & 30 deletions js/util/canvas.js

This file was deleted.

10 changes: 10 additions & 0 deletions js/util/webgl_context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

var gl = require('gl');

module.exports = function(canvas, attributes) {
if (canvas._webGLContext)
return canvas._webGLContext;
canvas._webGLContext = gl(canvas.width, canvas.height, attributes);
return canvas._webGLContext;
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
"browser": {
"./js/util/ajax.js": "./js/util/browser/ajax.js",
"./js/util/window.js": "./js/util/browser/window.js",
"./js/util/canvas.js": "./js/util/browser/canvas.js",
"./js/util/webgl_context.js": "./js/util/browser/webgl_context.js",
"./js/util/web_worker.js": "./js/util/browser/web_worker.js"
},
"scripts": {
Expand Down
3 changes: 2 additions & 1 deletion test/suite_implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ module.exports = function(style, options, _callback) {
style: style,
classes: options.classes,
interactive: false,
attributionControl: false
attributionControl: false,
preserveDrawingBuffer: true
});

// Configure the map to never stop the render loop
Expand Down

0 comments on commit 83d9609

Please sign in to comment.