Files
openlayers/src/ol/ImageCanvas.js
Tim Schaub 054af09032 Make code prettier
This updates ESLint and our shared eslint-config-openlayers to use Prettier.  Most formatting changes were automatically applied with this:

    npm run lint -- --fix

A few manual changes were required:

 * In `examples/offscreen-canvas.js`, the `//eslint-disable-line` comment needed to be moved to the appropriate line to disable the error about the `'worker-loader!./offscreen-canvas.worker.js'` import.
 * In `examples/webpack/exapmle-builder.js`, spaces could not be added after a couple `function`s for some reason.  While editing this, I reworked `ExampleBuilder` to be a class.
 * In `src/ol/format/WMSGetFeatureInfo.js`, the `// @ts-ignore` comment needed to be moved down one line so it applied to the `parsersNS` argument.
2020-04-06 12:54:09 -06:00

94 lines
2.1 KiB
JavaScript

/**
* @module ol/ImageCanvas
*/
import ImageBase from './ImageBase.js';
import ImageState from './ImageState.js';
/**
* 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=): void): void} Loader
*/
class ImageCanvas extends ImageBase {
/**
* @param {import("./extent.js").Extent} extent Extent.
* @param {number} resolution Resolution.
* @param {number} pixelRatio Pixel ratio.
* @param {HTMLCanvasElement} canvas Canvas.
* @param {Loader=} opt_loader Optional loader function to
* support asynchronous canvas drawing.
*/
constructor(extent, resolution, pixelRatio, canvas, opt_loader) {
const state =
opt_loader !== undefined ? ImageState.IDLE : ImageState.LOADED;
super(extent, resolution, pixelRatio, state);
/**
* Optional canvas loader function.
* @type {?Loader}
* @private
*/
this.loader_ = opt_loader !== undefined ? opt_loader : null;
/**
* @private
* @type {HTMLCanvasElement}
*/
this.canvas_ = canvas;
/**
* @private
* @type {?Error}
*/
this.error_ = null;
}
/**
* Get any error associated with asynchronous rendering.
* @return {?Error} Any error that occurred during rendering.
*/
getError() {
return this.error_;
}
/**
* Handle async drawing complete.
* @param {Error=} err Any error during drawing.
* @private
*/
handleLoad_(err) {
if (err) {
this.error_ = err;
this.state = ImageState.ERROR;
} else {
this.state = ImageState.LOADED;
}
this.changed();
}
/**
* Load not yet loaded URI.
*/
load() {
if (this.state == ImageState.IDLE) {
this.state = ImageState.LOADING;
this.changed();
this.loader_(this.handleLoad_.bind(this));
}
}
/**
* @return {HTMLCanvasElement} Canvas element.
*/
getImage() {
return this.canvas_;
}
}
export default ImageCanvas;