/
Fill.js
110 lines (99 loc) · 2.93 KB
/
Fill.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* @module ol/style/Fill
*/
import ImageState from '../ImageState.js';
import {get as getIconImage} from './IconImage.js';
/**
* @typedef {Object} Options
* @property {import("../color.js").Color|import("../colorlike.js").ColorLike|import('../colorlike.js').PatternDescriptor|null} [color=null] A color,
* gradient or pattern.
* See {@link module:ol/color~Color} and {@link module:ol/colorlike~ColorLike} for possible formats. For polygon fills (not for {@link import("./RegularShape.js").default} fills),
* a pattern can also be provided as {@link module:ol/colorlike~PatternDescriptor}.
* Default null; if null, the Canvas/renderer default black will be used.
*/
/**
* @classdesc
* Set fill style for vector features.
* @api
*/
class Fill {
/**
* @param {Options} [options] Options.
*/
constructor(options) {
options = options || {};
/**
* @private
* @type {import("./IconImage.js").default|null}
*/
this.patternImage_ = null;
/**
* @private
* @type {import("../color.js").Color|import("../colorlike.js").ColorLike|import('../colorlike.js').PatternDescriptor|null}
*/
this.color_ = null;
if (options.color !== undefined) {
this.setColor(options.color);
}
}
/**
* Clones the style. The color is not cloned if it is an {@link module:ol/colorlike~ColorLike}.
* @return {Fill} The cloned style.
* @api
*/
clone() {
const color = this.getColor();
return new Fill({
color: Array.isArray(color) ? color.slice() : color || undefined,
});
}
/**
* Get the fill color.
* @return {import("../color.js").Color|import("../colorlike.js").ColorLike|import('../colorlike.js').PatternDescriptor|null} Color.
* @api
*/
getColor() {
return this.color_;
}
/**
* Set the color.
*
* @param {import("../color.js").Color|import("../colorlike.js").ColorLike|import('../colorlike.js').PatternDescriptor|null} color Color.
* @api
*/
setColor(color) {
if (color !== null && typeof color === 'object' && 'src' in color) {
const patternImage = getIconImage(
null,
color.src,
'anonymous',
undefined,
color.offset ? null : color.color ? color.color : null,
!(color.offset && color.size),
);
patternImage.ready().then(() => {
this.patternImage_ = null;
});
if (patternImage.getImageState() === ImageState.IDLE) {
patternImage.load();
}
if (patternImage.getImageState() === ImageState.LOADING) {
this.patternImage_ = patternImage;
}
}
this.color_ = color;
}
/**
* @return {boolean} The fill style is loading an image pattern.
*/
loading() {
return !!this.patternImage_;
}
/**
* @return {Promise<void>} `false` or a promise that resolves when the style is ready to use.
*/
ready() {
return this.patternImage_ ? this.patternImage_.ready() : Promise.resolve();
}
}
export default Fill;