Run operations in a worker
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
goog.provide('ol.ImageCanvas');
|
||||
|
||||
goog.require('goog.asserts');
|
||||
goog.require('ol.ImageBase');
|
||||
goog.require('ol.ImageState');
|
||||
|
||||
@@ -13,12 +14,23 @@ goog.require('ol.ImageState');
|
||||
* @param {number} pixelRatio Pixel ratio.
|
||||
* @param {Array.<ol.Attribution>} attributions Attributions.
|
||||
* @param {HTMLCanvasElement} canvas Canvas.
|
||||
* @param {ol.ImageCanvasLoader=} opt_loader Optional loader function to
|
||||
* support asynchronous canvas drawing.
|
||||
*/
|
||||
ol.ImageCanvas = function(extent, resolution, pixelRatio, attributions,
|
||||
canvas) {
|
||||
canvas, opt_loader) {
|
||||
|
||||
goog.base(this, extent, resolution, pixelRatio, ol.ImageState.LOADED,
|
||||
attributions);
|
||||
/**
|
||||
* Optional canvas loader function.
|
||||
* @type {?ol.ImageCanvasLoader}
|
||||
* @private
|
||||
*/
|
||||
this.loader_ = goog.isDef(opt_loader) ? opt_loader : null;
|
||||
|
||||
var state = goog.isDef(opt_loader) ?
|
||||
ol.ImageState.IDLE : ol.ImageState.LOADED;
|
||||
|
||||
goog.base(this, extent, resolution, pixelRatio, state, attributions);
|
||||
|
||||
/**
|
||||
* @private
|
||||
@@ -26,13 +38,68 @@ ol.ImageCanvas = function(extent, resolution, pixelRatio, attributions,
|
||||
*/
|
||||
this.canvas_ = canvas;
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {Error}
|
||||
*/
|
||||
this.error_ = null;
|
||||
|
||||
};
|
||||
goog.inherits(ol.ImageCanvas, ol.ImageBase);
|
||||
|
||||
|
||||
/**
|
||||
* Get any error associated with asynchronous rendering.
|
||||
* @return {Error} Any error that occurred during rendering.
|
||||
*/
|
||||
ol.ImageCanvas.prototype.getError = function() {
|
||||
return this.error_;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Handle async drawing complete.
|
||||
* @param {Error} err Any error during drawing.
|
||||
* @private
|
||||
*/
|
||||
ol.ImageCanvas.prototype.handleLoad_ = function(err) {
|
||||
if (err) {
|
||||
this.error_ = err;
|
||||
this.state = ol.ImageState.ERROR;
|
||||
} else {
|
||||
this.state = ol.ImageState.LOADED;
|
||||
}
|
||||
this.changed();
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Trigger drawing on canvas.
|
||||
*/
|
||||
ol.ImageCanvas.prototype.load = function() {
|
||||
if (this.state == ol.ImageState.IDLE) {
|
||||
goog.asserts.assert(!goog.isNull(this.loader_));
|
||||
this.state = ol.ImageState.LOADING;
|
||||
this.changed();
|
||||
this.loader_(goog.bind(this.handleLoad_, this));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
ol.ImageCanvas.prototype.getImage = function(opt_context) {
|
||||
return this.canvas_;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* A function that is called to trigger asynchronous canvas drawing. It is
|
||||
* called with a "done" callback that should be called when drawing is done.
|
||||
* If any error occurs during drawing, the "done" callback should be called with
|
||||
* that error.
|
||||
*
|
||||
* @typedef {function(function(Error))}
|
||||
*/
|
||||
ol.ImageCanvasLoader;
|
||||
|
||||
Reference in New Issue
Block a user