Merge pull request #5173 from fredj/use_ol.dom.createCanvasContext2D

Use ol.dom.createCanvasContext2D
This commit is contained in:
Frédéric Junod
2016-04-05 14:42:49 +02:00
4 changed files with 22 additions and 44 deletions

View File

@@ -5,6 +5,7 @@ goog.require('ol.events.EventType');
goog.require('ol.Image'); goog.require('ol.Image');
goog.require('ol.ImageLoadFunctionType'); goog.require('ol.ImageLoadFunctionType');
goog.require('ol.ImageState'); goog.require('ol.ImageState');
goog.require('ol.dom');
goog.require('ol.extent'); goog.require('ol.extent');
goog.require('ol.proj'); goog.require('ol.proj');
goog.require('ol.source.Image'); goog.require('ol.source.Image');
@@ -78,17 +79,16 @@ ol.source.ImageStatic.prototype.handleImageChange = function(evt) {
imageWidth = this.imageSize_[0]; imageWidth = this.imageSize_[0];
imageHeight = this.imageSize_[1]; imageHeight = this.imageSize_[1];
} else { } else {
imageWidth = image.width; // TODO: remove the type cast when a closure-compiler > 20160315 is used.
imageHeight = image.height; // see: https://github.com/google/closure-compiler/pull/1664
imageWidth = /** @type {number} */ (image.width);
imageHeight = /** @type {number} */ (image.height);
} }
var resolution = ol.extent.getHeight(imageExtent) / imageHeight; var resolution = ol.extent.getHeight(imageExtent) / imageHeight;
var targetWidth = Math.ceil(ol.extent.getWidth(imageExtent) / resolution); var targetWidth = Math.ceil(ol.extent.getWidth(imageExtent) / resolution);
if (targetWidth != imageWidth) { if (targetWidth != imageWidth) {
var canvas = /** @type {HTMLCanvasElement} */ var context = ol.dom.createCanvasContext2D(targetWidth, imageHeight);
(document.createElement('canvas')); var canvas = context.canvas;
canvas.width = targetWidth;
canvas.height = /** @type {number} */ (imageHeight);
var context = canvas.getContext('2d');
context.drawImage(image, 0, 0, imageWidth, imageHeight, context.drawImage(image, 0, 0, imageWidth, imageHeight,
0, 0, canvas.width, canvas.height); 0, 0, canvas.width, canvas.height);
this.image_.setImage(canvas); this.image_.setImage(canvas);

View File

@@ -3,6 +3,7 @@ goog.provide('ol.style.AtlasManager');
goog.require('goog.asserts'); goog.require('goog.asserts');
goog.require('ol'); goog.require('ol');
goog.require('ol.dom');
/** /**
* Provides information for an image inside an atlas manager. * Provides information for an image inside an atlas manager.
@@ -286,19 +287,15 @@ ol.style.Atlas = function(size, space) {
/** /**
* @private * @private
* @type {HTMLCanvasElement} * @type {CanvasRenderingContext2D}
*/ */
this.canvas_ = /** @type {HTMLCanvasElement} */ this.context_ = ol.dom.createCanvasContext2D(size, size);
(document.createElement('CANVAS'));
this.canvas_.width = size;
this.canvas_.height = size;
/** /**
* @private * @private
* @type {CanvasRenderingContext2D} * @type {HTMLCanvasElement}
*/ */
this.context_ = /** @type {CanvasRenderingContext2D} */ this.canvas_ = this.context_.canvas;
(this.canvas_.getContext('2d'));
}; };

View File

@@ -4,6 +4,7 @@ goog.require('goog.asserts');
goog.require('ol'); goog.require('ol');
goog.require('ol.color'); goog.require('ol.color');
goog.require('ol.colorlike'); goog.require('ol.colorlike');
goog.require('ol.dom');
goog.require('ol.has'); goog.require('ol.has');
goog.require('ol.render.canvas'); goog.require('ol.render.canvas');
goog.require('ol.style.Fill'); goog.require('ol.style.Fill');
@@ -268,18 +269,14 @@ ol.style.Circle.prototype.render_ = function(atlasManager) {
if (atlasManager === undefined) { if (atlasManager === undefined) {
// no atlas manager is used, create a new canvas // no atlas manager is used, create a new canvas
this.canvas_ = /** @type {HTMLCanvasElement} */ var context = ol.dom.createCanvasContext2D(size, size);
(document.createElement('CANVAS')); this.canvas_ = context.canvas;
this.canvas_.height = size;
this.canvas_.width = size;
// canvas.width and height are rounded to the closest integer // canvas.width and height are rounded to the closest integer
size = this.canvas_.width; size = this.canvas_.width;
imageSize = size; imageSize = size;
// draw the circle on the canvas // draw the circle on the canvas
var context = /** @type {CanvasRenderingContext2D} */
(this.canvas_.getContext('2d'));
this.draw_(renderOptions, context, 0, 0); this.draw_(renderOptions, context, 0, 0);
this.createHitDetectionCanvas_(renderOptions); this.createHitDetectionCanvas_(renderOptions);
@@ -369,15 +366,9 @@ ol.style.Circle.prototype.createHitDetectionCanvas_ = function(renderOptions) {
// if no fill style is set, create an extra hit-detection image with a // if no fill style is set, create an extra hit-detection image with a
// default fill style // default fill style
this.hitDetectionCanvas_ = /** @type {HTMLCanvasElement} */ var context = ol.dom.createCanvasContext2D(renderOptions.size, renderOptions.size);
(document.createElement('CANVAS')); this.hitDetectionCanvas_ = context.canvas;
var canvas = this.hitDetectionCanvas_;
canvas.height = renderOptions.size;
canvas.width = renderOptions.size;
var context = /** @type {CanvasRenderingContext2D} */
(canvas.getContext('2d'));
this.drawHitDetectionCanvas_(renderOptions, context, 0, 0); this.drawHitDetectionCanvas_(renderOptions, context, 0, 0);
}; };

View File

@@ -4,6 +4,7 @@ goog.require('goog.asserts');
goog.require('ol'); goog.require('ol');
goog.require('ol.color'); goog.require('ol.color');
goog.require('ol.colorlike'); goog.require('ol.colorlike');
goog.require('ol.dom');
goog.require('ol.has'); goog.require('ol.has');
goog.require('ol.render.canvas'); goog.require('ol.render.canvas');
goog.require('ol.style.AtlasManager'); goog.require('ol.style.AtlasManager');
@@ -354,18 +355,13 @@ ol.style.RegularShape.prototype.render_ = function(atlasManager) {
if (atlasManager === undefined) { if (atlasManager === undefined) {
// no atlas manager is used, create a new canvas // no atlas manager is used, create a new canvas
this.canvas_ = /** @type {HTMLCanvasElement} */ var context = ol.dom.createCanvasContext2D(size, size);
(document.createElement('CANVAS')); this.canvas_ = context.canvas;
this.canvas_.height = size;
this.canvas_.width = size;
// canvas.width and height are rounded to the closest integer // canvas.width and height are rounded to the closest integer
size = this.canvas_.width; size = this.canvas_.width;
imageSize = size; imageSize = size;
var context = /** @type {CanvasRenderingContext2D} */
(this.canvas_.getContext('2d'));
this.draw_(renderOptions, context, 0, 0); this.draw_(renderOptions, context, 0, 0);
this.createHitDetectionCanvas_(renderOptions); this.createHitDetectionCanvas_(renderOptions);
@@ -465,15 +461,9 @@ ol.style.RegularShape.prototype.createHitDetectionCanvas_ = function(renderOptio
// if no fill style is set, create an extra hit-detection image with a // if no fill style is set, create an extra hit-detection image with a
// default fill style // default fill style
this.hitDetectionCanvas_ = /** @type {HTMLCanvasElement} */ var context = ol.dom.createCanvasContext2D(renderOptions.size, renderOptions.size);
(document.createElement('CANVAS')); this.hitDetectionCanvas_ = context.canvas;
var canvas = this.hitDetectionCanvas_;
canvas.height = renderOptions.size;
canvas.width = renderOptions.size;
var context = /** @type {CanvasRenderingContext2D} */
(canvas.getContext('2d'));
this.drawHitDetectionCanvas_(renderOptions, context, 0, 0); this.drawHitDetectionCanvas_(renderOptions, context, 0, 0);
}; };