Skip to content

Commit

Permalink
Remove fake "canvas" module
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Wojciechowski committed Sep 2, 2016
1 parent c24473f commit fdd3aeb
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 63 deletions.
43 changes: 0 additions & 43 deletions js/util/browser/canvas.js

This file was deleted.

51 changes: 32 additions & 19 deletions js/util/canvas.js
@@ -1,30 +1,43 @@
'use strict';

var gl = require('gl');
var util = require('./util');
var isSupported = require('mapbox-gl-supported');
var window = require('./window');

module.exports = Canvas;

function Canvas(map) {
var requiredContextAttributes = {
antialias: false,
alpha: true,
stencil: true,
depth: true,
preserveDrawingBuffer: true
};

this.context = gl(
map.getContainer().offsetWidth * window.devicePixelRatio,
map.getContainer().offsetHeight * window.devicePixelRatio,
requiredContextAttributes
);
function Canvas(map, container) {
this.canvas = window.document.createElement('canvas');

if (map && container) {
this.canvas.style.position = 'absolute';
this.canvas.classList.add('mapboxgl-canvas');
this.canvas.addEventListener('webglcontextlost', map._contextLost.bind(map), false);
this.canvas.addEventListener('webglcontextrestored', map._contextRestored.bind(map), false);
this.canvas.setAttribute('tabindex', 0);
container.appendChild(this.canvas);
}
}

Canvas.prototype.resize = function() {};
Canvas.prototype.resize = 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';
};

Canvas.prototype.getWebGLContext = function(attributes) {
attributes = util.extend({}, attributes, isSupported.webGLContextAttributes);

Canvas.prototype.getWebGLContext = function() {
return this.context;
return this.canvas.getContext('webgl', attributes) ||
this.canvas.getContext('experimental-webgl', attributes);
};

Canvas.prototype.getElement = function() {};
Canvas.prototype.getElement = function() {
return this.canvas;
};
18 changes: 18 additions & 0 deletions js/util/window.js
@@ -1,6 +1,8 @@
'use strict';

var jsdom = require('jsdom');
var util = require('./util');
var gl = require('gl');

var window = jsdom.jsdom().defaultView;

Expand All @@ -9,4 +11,20 @@ window.cancelAnimationFrame = clearImmediate;

window.devicePixelRatio = 1;

window.HTMLCanvasElement.prototype.getContext = function(type, attributes) {
if (!this.context) {
var node = this;
while (node.offsetWidth == null) {
node = node.parentNode;
}

this.context = gl(
node.offsetWidth * window.devicePixelRatio,
node.offsetHeight * window.devicePixelRatio,
util.extend({}, attributes, {preserveDrawingBuffer: true})
);
}
return this.context;
};

module.exports = window;
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -82,7 +82,6 @@
"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/web_worker.js": "./js/util/browser/web_worker.js"
},
"scripts": {
Expand Down

0 comments on commit fdd3aeb

Please sign in to comment.