itowns 2.45.1-next.0 → 2.45.1-next.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (185) hide show
  1. package/dist/455.js +2 -0
  2. package/dist/455.js.map +1 -0
  3. package/dist/debug.js +3 -0
  4. package/dist/debug.js.LICENSE.txt +13 -0
  5. package/dist/debug.js.map +1 -0
  6. package/dist/itowns.js +3 -0
  7. package/dist/itowns.js.LICENSE.txt +5 -0
  8. package/dist/itowns.js.map +1 -0
  9. package/dist/itowns_lasparser.js +2 -0
  10. package/dist/itowns_lasparser.js.map +1 -0
  11. package/dist/itowns_lasworker.js +2 -0
  12. package/dist/itowns_lasworker.js.map +1 -0
  13. package/dist/itowns_potree2worker.js +2 -0
  14. package/dist/itowns_potree2worker.js.map +1 -0
  15. package/dist/itowns_widgets.js +2 -0
  16. package/dist/itowns_widgets.js.map +1 -0
  17. package/lib/Controls/FirstPersonControls.js +308 -0
  18. package/lib/Controls/FlyControls.js +175 -0
  19. package/lib/Controls/GlobeControls.js +1178 -0
  20. package/lib/Controls/PlanarControls.js +1025 -0
  21. package/lib/Controls/StateControl.js +432 -0
  22. package/lib/Controls/StreetControls.js +392 -0
  23. package/lib/Converter/Feature2Mesh.js +612 -0
  24. package/lib/Converter/Feature2Texture.js +174 -0
  25. package/lib/Converter/convertToTile.js +70 -0
  26. package/lib/Converter/textureConverter.js +43 -0
  27. package/lib/Core/3DTiles/C3DTBatchTable.js +131 -0
  28. package/lib/Core/3DTiles/C3DTBatchTableHierarchyExtension.js +96 -0
  29. package/lib/Core/3DTiles/C3DTBoundingVolume.js +156 -0
  30. package/lib/Core/3DTiles/C3DTExtensions.js +97 -0
  31. package/lib/Core/3DTiles/C3DTFeature.js +110 -0
  32. package/lib/Core/3DTiles/C3DTilesEnums.js +20 -0
  33. package/lib/Core/3DTiles/C3DTileset.js +99 -0
  34. package/lib/Core/3DTiles/utils/BinaryPropertyAccessor.js +100 -0
  35. package/lib/Core/AnimationPlayer.js +142 -0
  36. package/lib/Core/CopcNode.js +174 -0
  37. package/lib/Core/Deprecated/Undeprecator.js +74 -0
  38. package/lib/Core/EntwinePointTileNode.js +126 -0
  39. package/lib/Core/Feature.js +488 -0
  40. package/lib/Core/Geographic/GeoidGrid.js +108 -0
  41. package/lib/Core/Label.js +222 -0
  42. package/lib/Core/MainLoop.js +209 -0
  43. package/lib/Core/Picking.js +255 -0
  44. package/lib/Core/PointCloudNode.js +42 -0
  45. package/lib/Core/Potree2Node.js +206 -0
  46. package/lib/Core/Potree2PointAttributes.js +139 -0
  47. package/lib/Core/PotreeNode.js +101 -0
  48. package/lib/Core/Prefab/Globe/Atmosphere.js +293 -0
  49. package/lib/Core/Prefab/Globe/GlobeLayer.js +152 -0
  50. package/lib/Core/Prefab/Globe/GlobeTileBuilder.js +110 -0
  51. package/lib/Core/Prefab/Globe/SkyShader.js +78 -0
  52. package/lib/Core/Prefab/GlobeView.js +155 -0
  53. package/lib/Core/Prefab/Planar/PlanarLayer.js +59 -0
  54. package/lib/Core/Prefab/Planar/PlanarTileBuilder.js +71 -0
  55. package/lib/Core/Prefab/PlanarView.js +62 -0
  56. package/lib/Core/Prefab/TileBuilder.js +82 -0
  57. package/lib/Core/Prefab/computeBufferTileGeometry.js +248 -0
  58. package/lib/Core/Scheduler/Cache.js +17 -0
  59. package/lib/Core/Scheduler/CancelledCommandException.js +15 -0
  60. package/lib/Core/Scheduler/Scheduler.js +294 -0
  61. package/lib/Core/Style.js +660 -0
  62. package/lib/Core/StyleOptions.js +486 -0
  63. package/lib/Core/System/Capabilities.js +63 -0
  64. package/lib/Core/Tile/Tile.js +205 -0
  65. package/lib/Core/Tile/TileGrid.js +49 -0
  66. package/lib/Core/TileGeometry.js +124 -0
  67. package/lib/Core/TileMesh.js +108 -0
  68. package/lib/Core/View.js +1115 -0
  69. package/lib/Layer/C3DTilesLayer.js +459 -0
  70. package/lib/Layer/ColorLayer.js +154 -0
  71. package/lib/Layer/CopcLayer.js +63 -0
  72. package/lib/Layer/ElevationLayer.js +139 -0
  73. package/lib/Layer/EntwinePointTileLayer.js +71 -0
  74. package/lib/Layer/FeatureGeometryLayer.js +77 -0
  75. package/lib/Layer/GeoidLayer.js +80 -0
  76. package/lib/Layer/GeometryLayer.js +233 -0
  77. package/lib/Layer/InfoLayer.js +64 -0
  78. package/lib/Layer/LabelLayer.js +469 -0
  79. package/lib/Layer/Layer.js +335 -0
  80. package/lib/Layer/LayerUpdateState.js +89 -0
  81. package/lib/Layer/LayerUpdateStrategy.js +80 -0
  82. package/lib/Layer/OGC3DTilesLayer.js +543 -0
  83. package/lib/Layer/OrientedImageLayer.js +227 -0
  84. package/lib/Layer/PointCloudLayer.js +405 -0
  85. package/lib/Layer/Potree2Layer.js +171 -0
  86. package/lib/Layer/PotreeLayer.js +72 -0
  87. package/lib/Layer/RasterLayer.js +37 -0
  88. package/lib/Layer/ReferencingLayerProperties.js +62 -0
  89. package/lib/Layer/TiledGeometryLayer.js +459 -0
  90. package/lib/Loader/LASLoader.js +193 -0
  91. package/lib/Loader/Potree2BrotliLoader.js +261 -0
  92. package/lib/Loader/Potree2Loader.js +207 -0
  93. package/lib/Main.js +113 -0
  94. package/lib/MainBundle.js +4 -0
  95. package/lib/Parser/B3dmParser.js +174 -0
  96. package/lib/Parser/CameraCalibrationParser.js +94 -0
  97. package/lib/Parser/GDFParser.js +72 -0
  98. package/lib/Parser/GTXParser.js +75 -0
  99. package/lib/Parser/GeoJsonParser.js +212 -0
  100. package/lib/Parser/GpxParser.js +25 -0
  101. package/lib/Parser/ISGParser.js +71 -0
  102. package/lib/Parser/KMLParser.js +25 -0
  103. package/lib/Parser/LASParser.js +137 -0
  104. package/lib/Parser/MapBoxUrlParser.js +83 -0
  105. package/lib/Parser/PntsParser.js +131 -0
  106. package/lib/Parser/Potree2BinParser.js +92 -0
  107. package/lib/Parser/PotreeBinParser.js +106 -0
  108. package/lib/Parser/PotreeCinParser.js +29 -0
  109. package/lib/Parser/ShapefileParser.js +78 -0
  110. package/lib/Parser/VectorTileParser.js +215 -0
  111. package/lib/Parser/XbilParser.js +120 -0
  112. package/lib/Parser/deprecated/LegacyGLTFLoader.js +1386 -0
  113. package/lib/Parser/iGLTFLoader.js +168 -0
  114. package/lib/Process/3dTilesProcessing.js +304 -0
  115. package/lib/Process/FeatureProcessing.js +76 -0
  116. package/lib/Process/LayeredMaterialNodeProcessing.js +229 -0
  117. package/lib/Process/ObjectRemovalHelper.js +97 -0
  118. package/lib/Process/handlerNodeError.js +23 -0
  119. package/lib/Provider/3dTilesProvider.js +149 -0
  120. package/lib/Provider/DataSourceProvider.js +24 -0
  121. package/lib/Provider/Fetcher.js +233 -0
  122. package/lib/Provider/PointCloudProvider.js +45 -0
  123. package/lib/Provider/TileProvider.js +16 -0
  124. package/lib/Provider/URLBuilder.js +116 -0
  125. package/lib/Renderer/Camera.js +281 -0
  126. package/lib/Renderer/Color.js +56 -0
  127. package/lib/Renderer/ColorLayersOrdering.js +115 -0
  128. package/lib/Renderer/CommonMaterial.js +31 -0
  129. package/lib/Renderer/Label2DRenderer.js +192 -0
  130. package/lib/Renderer/LayeredMaterial.js +243 -0
  131. package/lib/Renderer/OBB.js +150 -0
  132. package/lib/Renderer/OrientedImageCamera.js +118 -0
  133. package/lib/Renderer/OrientedImageMaterial.js +167 -0
  134. package/lib/Renderer/PointsMaterial.js +485 -0
  135. package/lib/Renderer/RasterTile.js +243 -0
  136. package/lib/Renderer/RenderMode.js +31 -0
  137. package/lib/Renderer/Shader/ShaderChunk.js +160 -0
  138. package/lib/Renderer/Shader/ShaderUtils.js +47 -0
  139. package/lib/Renderer/SphereHelper.js +17 -0
  140. package/lib/Renderer/WebXR.js +51 -0
  141. package/lib/Renderer/c3DEngine.js +214 -0
  142. package/lib/Source/C3DTilesGoogleSource.js +74 -0
  143. package/lib/Source/C3DTilesIonSource.js +54 -0
  144. package/lib/Source/C3DTilesSource.js +30 -0
  145. package/lib/Source/CopcSource.js +126 -0
  146. package/lib/Source/EntwinePointTileSource.js +72 -0
  147. package/lib/Source/FileSource.js +188 -0
  148. package/lib/Source/OGC3DTilesGoogleSource.js +29 -0
  149. package/lib/Source/OGC3DTilesIonSource.js +34 -0
  150. package/lib/Source/OGC3DTilesSource.js +21 -0
  151. package/lib/Source/OrientedImageSource.js +59 -0
  152. package/lib/Source/Potree2Source.js +167 -0
  153. package/lib/Source/PotreeSource.js +82 -0
  154. package/lib/Source/Source.js +202 -0
  155. package/lib/Source/TMSSource.js +144 -0
  156. package/lib/Source/VectorTilesSource.js +182 -0
  157. package/lib/Source/WFSSource.js +170 -0
  158. package/lib/Source/WMSSource.js +167 -0
  159. package/lib/Source/WMTSSource.js +92 -0
  160. package/lib/ThreeExtended/capabilities/WebGL.js +69 -0
  161. package/lib/ThreeExtended/libs/ktx-parse.module.js +506 -0
  162. package/lib/ThreeExtended/libs/zstddec.module.js +29 -0
  163. package/lib/ThreeExtended/loaders/DDSLoader.js +200 -0
  164. package/lib/ThreeExtended/loaders/DRACOLoader.js +400 -0
  165. package/lib/ThreeExtended/loaders/GLTFLoader.js +2879 -0
  166. package/lib/ThreeExtended/loaders/KTX2Loader.js +709 -0
  167. package/lib/ThreeExtended/math/ColorSpaces.js +59 -0
  168. package/lib/ThreeExtended/utils/BufferGeometryUtils.js +846 -0
  169. package/lib/ThreeExtended/utils/WorkerPool.js +70 -0
  170. package/lib/Utils/CameraUtils.js +554 -0
  171. package/lib/Utils/DEMUtils.js +350 -0
  172. package/lib/Utils/FeaturesUtils.js +156 -0
  173. package/lib/Utils/Gradients.js +16 -0
  174. package/lib/Utils/ThreeUtils.js +115 -0
  175. package/lib/Utils/gui/C3DTilesStyle.js +218 -0
  176. package/lib/Utils/gui/Main.js +7 -0
  177. package/lib/Utils/gui/Minimap.js +152 -0
  178. package/lib/Utils/gui/Navigation.js +245 -0
  179. package/lib/Utils/gui/Scale.js +104 -0
  180. package/lib/Utils/gui/Searchbar.js +234 -0
  181. package/lib/Utils/gui/Widget.js +80 -0
  182. package/lib/Utils/placeObjectOnGround.js +136 -0
  183. package/lib/Worker/LASLoaderWorker.js +19 -0
  184. package/lib/Worker/Potree2Worker.js +21 -0
  185. package/package.json +2 -2
@@ -0,0 +1,660 @@
1
+ import { Coordinates } from '@itowns/geographic';
2
+ import { LRUCache } from 'lru-cache';
3
+ import Fetcher from "../Provider/Fetcher.js";
4
+ import { Color } from 'three';
5
+ import { deltaE } from "../Renderer/Color.js";
6
+ /* babel-plugin-inline-import './StyleChunk/itowns_stroke_single_before.css' */
7
+ const itowns_stroke_single_before = ".itowns-stroke-single:before {\n display: var(--text_stroke_display);\n content: attr(data-before);\n opacity: 1;\n position: absolute;\n -webkit-text-stroke-width: var(--text_stroke_width);\n -webkit-text-stroke-color: var(--text_stroke_color);\n left: 0;\n right: 0;\n bottom: 0;\n top: 0;\n z-index: -1;\n white-space: inherit;\n overflow-wrap: inherit;\n letter-spacing: inherit;\n text-align: inherit;\n padding: inherit;\n font-family: inherit;\n text-transform: inherit;\n max-width: inherit;\n font-size: inherit;\n}\n";
8
+ const cachedImg = new LRUCache({
9
+ max: 500
10
+ });
11
+ const matrix = document.createElementNS('http://www.w3.org/2000/svg', 'svg').createSVGMatrix();
12
+ const canvas = document.createElement('canvas');
13
+ function baseAltitudeDefault(properties, ctx) {
14
+ return ctx?.coordinates?.z || 0;
15
+ }
16
+ export function readExpression(property, ctx) {
17
+ if (property.expression) {
18
+ return property.expression.evaluate(ctx);
19
+ }
20
+ if (property.stops) {
21
+ const stops = property.stops;
22
+ property = property.stops[0][1];
23
+ for (let i = stops.length - 1; i >= 0; i--) {
24
+ const stop = stops[i];
25
+ if (ctx.zoom >= stop[0]) {
26
+ property = stop[1];
27
+ break;
28
+ }
29
+ }
30
+ }
31
+ if (typeof property === 'string' || property instanceof String) {
32
+ return property.replace(/\{(.+?)\}/g, (a, b) => ctx.properties[b] || '').trim();
33
+ }
34
+ if (property instanceof Function) {
35
+ // TOBREAK: Pass the current `context` as a unique parameter.
36
+ // In this proposal, metadata will be accessed in the callee by the
37
+ // `context.properties` property.
38
+ return property(ctx.properties, ctx);
39
+ }
40
+ return property;
41
+ }
42
+ async function loadImage(url) {
43
+ const imgUrl = url.split('?')[0];
44
+ let promise = cachedImg.get(imgUrl);
45
+ if (!promise) {
46
+ promise = Fetcher.texture(url, {
47
+ crossOrigin: 'anonymous'
48
+ });
49
+ cachedImg.set(imgUrl, promise);
50
+ }
51
+ return (await promise).image;
52
+ }
53
+ function cropImage(img, cropValues) {
54
+ const x = cropValues.x || 0;
55
+ const y = cropValues.y || 0;
56
+ const width = cropValues.width || img.naturalWidth;
57
+ const height = cropValues.height || img.naturalHeight;
58
+ canvas.width = width;
59
+ canvas.height = height;
60
+ const ctx = canvas.getContext('2d', {
61
+ willReadFrequently: true
62
+ });
63
+ ctx.drawImage(img, x, y, width, height, 0, 0, width, height);
64
+ return ctx.getImageData(0, 0, width, height);
65
+ }
66
+ function replaceWhitePxl(imgd, color, id) {
67
+ if (!color) {
68
+ return imgd;
69
+ }
70
+ const imgdColored = cachedImg.get(`${id}_${color}`);
71
+ if (!imgdColored) {
72
+ const pix = imgd.data;
73
+ const newColor = new Color(color);
74
+ const colorToChange = new Color('white');
75
+ for (let i = 0, n = pix.length; i < n; i += 4) {
76
+ const d = deltaE(pix.slice(i, i + 3), colorToChange) / 100;
77
+ pix[i] = pix[i] * d + newColor.r * 255 * (1 - d);
78
+ pix[i + 1] = pix[i + 1] * d + newColor.g * 255 * (1 - d);
79
+ pix[i + 2] = pix[i + 2] * d + newColor.b * 255 * (1 - d);
80
+ }
81
+ cachedImg.set(`${id}_${color}`, imgd);
82
+ return imgd;
83
+ }
84
+ return imgdColored;
85
+ }
86
+ const textAnchorPosition = {
87
+ left: [0, -0.5],
88
+ right: [-1, -0.5],
89
+ top: [-0.5, 0],
90
+ bottom: [-0.5, -1],
91
+ 'top-right': [-1, 0],
92
+ 'bottom-left': [0, -1],
93
+ 'bottom-right': [-1, -1],
94
+ center: [-0.5, -0.5],
95
+ 'top-left': [0, 0]
96
+ };
97
+
98
+ /**
99
+ * Defines a property for the given Style for a specific parameter in a given category (one of fill, stroke, point, text, icon or zoom),
100
+ * by generating its getter and setter.
101
+ * The getter is in charge of returning the right style value from the following ones if they are defined (in that specific order):
102
+ * the value set by the user (`userValue`)
103
+ * the value read from the data source (`dataValue`)
104
+ * the default fallback value (`defaultValue`).
105
+ * The setter can be called to change dynamically the value.
106
+ * @param {Style} style - The Style instance to set.
107
+ * @param {string} category - The category (fill, stroke, point, test, icon or zoom) to set.
108
+ * @param {string} parameter - The parameter of the category to set.
109
+ * @param {All} userValue - The value given by the user (if any). Can be undefined.
110
+ * @param {All} [defaultValue] - The default value to return (if needed).
111
+ */
112
+ function defineStyleProperty(style, category, parameter, userValue, defaultValue) {
113
+ let property;
114
+ Object.defineProperty(style[category], parameter, {
115
+ enumerable: true,
116
+ get: () => {
117
+ // != to check for 'undefined' and 'null' value)
118
+ if (property != undefined) {
119
+ return property;
120
+ }
121
+ if (userValue != undefined) {
122
+ return readExpression(userValue, style.context);
123
+ }
124
+ const dataValue = style.context.featureStyle?.[category]?.[parameter];
125
+ if (dataValue != undefined) {
126
+ return readExpression(dataValue, style.context);
127
+ }
128
+ if (defaultValue instanceof Function) {
129
+ return defaultValue(style.context.properties, style.context) ?? defaultValue;
130
+ }
131
+ return defaultValue;
132
+ },
133
+ set: v => {
134
+ property = v;
135
+ }
136
+ });
137
+ }
138
+
139
+ /**
140
+ * StyleContext stores metadata of one FeatureGeometry that are needed for its style computation:
141
+ * type of feature and what is needed (fill, stroke or draw a point, etc.) as well as where to get its
142
+ * properties and its coordinates (for base_altitude).
143
+ *
144
+ * @property {number} zoom Current zoom to display the FeatureGeometry.
145
+ * @property {Object} collection The FeatureCollection to which the FeatureGeometry is attached.
146
+ * @property {Object} properties Properties of the FeatureGeometry.
147
+ * @property {string} type Geometry type of the feature. Can be `point`, `line`, or `polygon`.
148
+ * @property {StyleOptions|Function}featureStyle StyleOptions object (or a function returning one) to get style
149
+ * information at feature and FeatureGeometry level from the data parsed.
150
+ * @property {Coordinates} coordinates The coordinates (in world space) of the last vertex (x, y, z) set with
151
+ * setLocalCoordinatesFromArray().
152
+ * private properties:
153
+ * @property {Coordinates} worldCoord @private Coordinates object to store coordinates in world space.
154
+ * @property {Coordinates} localCoordinates @private Coordinates object to store coordinates in local space.
155
+ * @property {boolean} worldCoordsComputed @private Have the world coordinates already been computed
156
+ * from the local coordinates?
157
+ * @property {Feature} feature @private The itowns feature of interest.
158
+ * @property {FeatureGeometry} geometry @private The FeatureGeometry to compute the style.
159
+ */
160
+ export class StyleContext {
161
+ #worldCoord = (() => new Coordinates('EPSG:4326', 0, 0, 0))();
162
+ #localCoordinates = (() => new Coordinates('EPSG:4326', 0, 0, 0))();
163
+ #worldCoordsComputed = true;
164
+ #feature = {};
165
+ #geometry = {};
166
+ setZoom(zoom) {
167
+ this.zoom = zoom;
168
+ }
169
+ setFeature(f) {
170
+ this.#feature = f;
171
+ }
172
+ setGeometry(g) {
173
+ this.#geometry = g;
174
+ }
175
+ setCollection(c) {
176
+ this.collection = c;
177
+ this.#localCoordinates.setCrs(c.crs);
178
+ }
179
+ setLocalCoordinatesFromArray(vertices, offset) {
180
+ this.#worldCoordsComputed = false;
181
+ return this.#localCoordinates.setFromArray(vertices, offset);
182
+ }
183
+ get properties() {
184
+ return this.#geometry.properties;
185
+ }
186
+ get type() {
187
+ return this.#feature.type;
188
+ }
189
+ get featureStyle() {
190
+ let featureStyle = this.#feature.style;
191
+ if (featureStyle instanceof Function) {
192
+ featureStyle = featureStyle(this.properties, this);
193
+ }
194
+ return featureStyle;
195
+ }
196
+ get coordinates() {
197
+ if (!this.#worldCoordsComputed) {
198
+ this.#worldCoordsComputed = true;
199
+ this.#worldCoord.copy(this.#localCoordinates).applyMatrix4(this.collection.matrixWorld);
200
+ if (this.#localCoordinates.crs == 'EPSG:4978') {
201
+ return this.#worldCoord.as('EPSG:4326', this.#worldCoord);
202
+ }
203
+ }
204
+ return this.#worldCoord;
205
+ }
206
+ }
207
+ function _addIcon(icon, domElement, opt) {
208
+ const cIcon = icon.cloneNode();
209
+ cIcon.setAttribute('class', 'itowns-icon');
210
+ cIcon.width = icon.width * opt.size;
211
+ cIcon.height = icon.height * opt.size;
212
+ cIcon.style.color = opt.color;
213
+ cIcon.style.opacity = opt.opacity;
214
+ cIcon.style.position = 'absolute';
215
+ cIcon.style.top = '0';
216
+ cIcon.style.left = '0';
217
+ switch (opt.anchor) {
218
+ // center by default
219
+ case 'left':
220
+ cIcon.style.top = `${-0.5 * cIcon.height}px`;
221
+ break;
222
+ case 'right':
223
+ cIcon.style.top = `${-0.5 * cIcon.height}px`;
224
+ cIcon.style.left = `${-cIcon.width}px`;
225
+ break;
226
+ case 'top':
227
+ cIcon.style.left = `${-0.5 * cIcon.width}px`;
228
+ break;
229
+ case 'bottom':
230
+ cIcon.style.top = `${-cIcon.height}px`;
231
+ cIcon.style.left = `${-0.5 * cIcon.width}px`;
232
+ break;
233
+ case 'bottom-left':
234
+ cIcon.style.top = `${-cIcon.height}px`;
235
+ break;
236
+ case 'bottom-right':
237
+ cIcon.style.top = `${-cIcon.height}px`;
238
+ cIcon.style.left = `${-cIcon.width}px`;
239
+ break;
240
+ case 'top-left':
241
+ break;
242
+ case 'top-right':
243
+ cIcon.style.left = `${-cIcon.width}px`;
244
+ break;
245
+ case 'center':
246
+ default:
247
+ cIcon.style.top = `${-0.5 * cIcon.height}px`;
248
+ cIcon.style.left = `${-0.5 * cIcon.width}px`;
249
+ break;
250
+ }
251
+ cIcon.style['z-index'] = -1;
252
+ domElement.appendChild(cIcon);
253
+ return cIcon;
254
+ }
255
+
256
+ /**
257
+ * A Style is a class that defines the visual appearance of {@link
258
+ * FeatureCollection} and {@link Feature}. It is taken into account when drawing
259
+ * them in textures that will be placed onto tiles.
260
+ *
261
+ * As there are five basic elements present in `Features`, there are also five
262
+ * main components in a `Style` object:
263
+ * - `fill` is for all fillings and polygons
264
+ * - `stroke` is for all lines and polygons edges
265
+ * - `point` is for all points
266
+ * - `text` contains all {@link Label} related things
267
+ * - `icon` defines the appearance of icons attached to label.
268
+ *
269
+ * Many style property can be set to functions. When that is the case, the function's
270
+ * return type must necessarily be the same as the types (other than function) of the property.
271
+ * For instance, if the `fill.pattern` property is set to a function, this function must return
272
+ * an `Image`, a `Canvas`, or `String`.
273
+ * The first parameter of functions used to set `Style` properties is always an object containing
274
+ * the properties of the features displayed with the current `Style` instance.
275
+ *
276
+ * @property {Object} fill - Polygons and fillings style.
277
+ * @property {String|Function|THREE.Color} fill.color - Defines the main color of the filling. Can be
278
+ * any [valid color
279
+ * string](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value).
280
+ * Default is no value, indicating that no filling needs to be done.
281
+ * If the `Layer` is a `GeometryLayer` you can use `THREE.Color`.
282
+ * @property {Image|Canvas|String|Object|Function} [fill.pattern] - Defines a pattern to fill the
283
+ * surface with. It can be an `Image` to use directly, an url to fetch the pattern or an object containing
284
+ * the url of the image to fetch and the transformation to apply.
285
+ * from. See [this example] (http://www.itowns-project.org/itowns/examples/#source_file_geojson_raster)
286
+ * for how to use.
287
+ * @property {Image|String} [fill.pattern.source] - The image or the url to fetch the pattern image
288
+ * @property {Object} [fill.pattern.cropValues] - The x, y, width and height (in pixel) of the sub image to use.
289
+ * @property {THREE.Color} [fill.pattern.color] - Can be any [valid color string]
290
+ * @property {Number|Function} fill.opacity - The opacity of the color or of the
291
+ * pattern. Can be between `0.0` and `1.0`. Default is `1.0`.
292
+ * For a `GeometryLayer`, this opacity property isn't used.
293
+ * @property {Number|Function} fill.base_altitude - Only for {@link GeometryLayer}, defines altitude
294
+ * for each coordinate.
295
+ * If `base_altitude` is `undefined`, the original altitude is kept, and if it doesn't exist
296
+ * then the altitude value is set to 0.
297
+ * @property {Number|Function} [fill.extrusion_height] - Only for {@link GeometryLayer} and if user sets it.
298
+ * If defined, polygons will be extruded by the specified amount.
299
+ * @property {Object} stroke - Lines and polygons edges.
300
+ * @property {String|Function|THREE.Color} stroke.color The color of the line. Can be any [valid
301
+ * color string](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value).
302
+ * Default is no value, indicating that no stroke needs to be done.
303
+ * If the `Layer` is a `GeometryLayer` you can use `THREE.Color`.
304
+ * @property {Number|Function} stroke.opacity - The opacity of the line. Can be between
305
+ * `0.0` and `1.0`. Default is `1.0`.
306
+ * For a `GeometryLayer`, this opacity property isn't used.
307
+ * @property {Number|Function} stroke.width - The width of the line. Default is `1.0`.
308
+ * @property {Number|Function} stroke.base_altitude - Only for {@link GeometryLayer}, defines altitude
309
+ * for each coordinate.
310
+ * If `base_altitude` is `undefined`, the original altitude is kept, and if it doesn't exist
311
+ * then the altitude value is set to 0.
312
+ *
313
+ * @property {Object} point - Point style.
314
+ * @property {String|Function} point.color - The color of the point. Can be any [valid
315
+ * color string](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value).
316
+ * Default is no value, indicating that no point will be shown.
317
+ * @property {Number|Function} point.radius - The radius of the point, in pixel. Default
318
+ * is `2.0`.
319
+ * @property {String|Function} point.line - The color of the border of the point. Can be
320
+ * any [valid color
321
+ * string](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value).
322
+ * Not supported for `GeometryLayer`.
323
+ * @property {Number|Function} point.width - The width of the border, in pixel. Default
324
+ * is `0.0` (no border).
325
+ * @property {Number|Function} point.opacity - The opacity of the point. Can be between
326
+ * `0.0` and `1.0`. Default is `1.0`.
327
+ * Not supported for `GeometryLayer`.
328
+ * @property {Number|Function} point.base_altitude - Only for {@link GeometryLayer}, defines altitude
329
+ * for each coordinate.
330
+ * If `base_altitude` is `undefined`, the original altitude is kept, and if it doesn't exist
331
+ * then the altitude value is set to 0.
332
+ * @property {Object} point.model - 3D model to instantiate at each point position
333
+
334
+ *
335
+ * @property {Object} text - All things {@link Label} related.
336
+ * @property {String|Function} text.field - A string representing a property key of
337
+ * a `FeatureGeometry` enclosed in brackets, that will be replaced by the value of the
338
+ * property for each geometry. For example, if each geometry contains a `name` property,
339
+ * `text.field` can be set to `{name}`. Default is no value, indicating that no
340
+ * text will be displayed.
341
+ *
342
+ * It's also possible to create more complex expressions. For example, you can combine
343
+ * text that will always be displayed (e.g. `foo`) and variable properties (e.g. `{bar}`)
344
+ * like the following: `foo {bar}`. You can also use multiple variables in one field.
345
+ * Let's say for instance that you have two properties latin name and local name of a
346
+ * place, you can write something like `{name_latin} - {name_local}` which can result
347
+ * in `Marrakesh - مراكش` for example.
348
+ * @property {String|Function} text.color - The color of the text. Can be any [valid
349
+ * color string](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value).
350
+ * Default is `#000000`.
351
+ * @property {String|Number[]|Function} text.anchor - The anchor of the text relative to its
352
+ * position (see {@link Label} for the position). Can be one of the following values: `top`,
353
+ * `left`, `bottom`, `right`, `center`, `top-left`, `top-right`, `bottom-left`
354
+ * or `bottom-right`. Default is `center`.
355
+ *
356
+ * It can also be defined as an Array of two numbers. Each number defines an offset (in
357
+ * fraction of the label width and height) between the label position and the top-left
358
+ * corner of the text. The first value is the horizontal offset, and the second is the
359
+ * vertical offset. For example, `[-0.5, -0.5]` will be equivalent to `center`.
360
+ * @property {Array|Function} text.offset - The offset of the text, depending on its
361
+ * anchor, in pixels. First value is from `left`, second is from `top`. Default
362
+ * is `[0, 0]`.
363
+ * @property {Number|Function} text.padding - The padding outside the text, in pixels.
364
+ * Default is `2`.
365
+ * @property {Number|Function} text.size - The size of the font, in pixels. Default is
366
+ * `16`.
367
+ * @property {Number|Function} text.wrap - The maximum width, in pixels, before the text
368
+ * is wrapped, because the string is too long. Default is `10`.
369
+ * @property {Number|Function} text.spacing - The spacing between the letters, in `em`.
370
+ * Default is `0`.
371
+ * @property {String|Function} text.transform - A value corresponding to the [CSS
372
+ * property
373
+ * `text-transform`](https://developer.mozilla.org/en-US/docs/Web/CSS/text-transform).
374
+ * Default is `none`.
375
+ * @property {String|Function} text.justify - A value corresponding to the [CSS property
376
+ * `text-align`](https://developer.mozilla.org/en-US/docs/Web/CSS/text-align).
377
+ * Default is `center`.
378
+ * @property {Number|Function} text.opacity - The opacity of the text. Can be between
379
+ * `0.0` and `1.0`. Default is `1.0`.
380
+ * @property {Array|Function} text.font - A list (as an array of string) of font family
381
+ * names, prioritized in the order it is set. Default is `Open Sans Regular,
382
+ * Arial Unicode MS Regular, sans-serif`.
383
+ * @property {String|Function} text.haloColor - The color of the halo. Can be any [valid
384
+ * color string](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value).
385
+ * Default is `#000000`.
386
+ * @property {Number|Function} text.haloWidth - The width of the halo, in pixels.
387
+ * Default is `0`.
388
+ * @property {Number|Function} text.haloBlur - The blur value of the halo, in pixels.
389
+ * Default is `0`.
390
+ *
391
+ * @property {Object} icon - Defines the appearance of icons attached to label.
392
+ * @property {String} icon.source - The url of the icons' image file.
393
+ * @property {String} icon.id - The id of the icons' sub-image in a vector tile data set.
394
+ * @property {String} icon.cropValues - the x, y, width and height (in pixel) of the sub image to use.
395
+ * @property {String} icon.anchor - The anchor of the icon compared to the label position.
396
+ * Can be `left`, `bottom`, `right`, `center`, `top-left`, `top-right`, `bottom-left`
397
+ * or `bottom-right`. Default is `center`.
398
+ * @property {Number} icon.size - If the icon's image is passed with `icon.source` and/or
399
+ * `icon.id`, its size when displayed on screen is multiplied by `icon.size`. Default is `1`.
400
+ * @property {String|Function} icon.color - The color of the icon. Can be any [valid
401
+ * color string](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value).
402
+ * It will change the color of the white pixels of the icon source image.
403
+ * @property {Number|Function} icon.opacity - The opacity of the icon. Can be between
404
+ * `0.0` and `1.0`. Default is `1.0`.
405
+ *
406
+ * @example
407
+ * const style = new itowns.Style({
408
+ * stroke: { color: 'red' },
409
+ * point: { color: 'white', line: 'red' },
410
+ * });
411
+ *
412
+ * const source = new itowns.FileSource(...);
413
+ *
414
+ * const layer = new itowns.ColorLayer('foo', {
415
+ * source: source,
416
+ * style: style,
417
+ * });
418
+ *
419
+ * view.addLayer(layer);
420
+ */
421
+
422
+ class Style {
423
+ /**
424
+ * @param {StyleOptions} [params={}] An object that contain any properties
425
+ * (zoom, fill, stroke, point, text or/and icon)
426
+ * and sub properties of a Style ({@link StyleOptions}).
427
+ */
428
+ constructor() {
429
+ let params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
430
+ this.isStyle = true;
431
+ this.context = new StyleContext();
432
+ params.zoom = params.zoom || {};
433
+ params.fill = params.fill || {};
434
+ params.stroke = params.stroke || {};
435
+ params.point = params.point || {};
436
+ params.text = params.text || {};
437
+ params.icon = params.icon || {};
438
+ this.zoom = {};
439
+ defineStyleProperty(this, 'zoom', 'min', params.zoom.min);
440
+ defineStyleProperty(this, 'zoom', 'max', params.zoom.max);
441
+ this.fill = {};
442
+ defineStyleProperty(this, 'fill', 'color', params.fill.color);
443
+ defineStyleProperty(this, 'fill', 'opacity', params.fill.opacity, 1.0);
444
+ defineStyleProperty(this, 'fill', 'pattern', params.fill.pattern);
445
+ defineStyleProperty(this, 'fill', 'base_altitude', params.fill.base_altitude, baseAltitudeDefault);
446
+ if (params.fill.extrusion_height) {
447
+ defineStyleProperty(this, 'fill', 'extrusion_height', params.fill.extrusion_height);
448
+ }
449
+ this.stroke = {};
450
+ defineStyleProperty(this, 'stroke', 'color', params.stroke.color);
451
+ defineStyleProperty(this, 'stroke', 'opacity', params.stroke.opacity, 1.0);
452
+ defineStyleProperty(this, 'stroke', 'width', params.stroke.width, 1.0);
453
+ defineStyleProperty(this, 'stroke', 'dasharray', params.stroke.dasharray, []);
454
+ defineStyleProperty(this, 'stroke', 'base_altitude', params.stroke.base_altitude, baseAltitudeDefault);
455
+ this.point = {};
456
+ defineStyleProperty(this, 'point', 'color', params.point.color);
457
+ defineStyleProperty(this, 'point', 'line', params.point.line);
458
+ defineStyleProperty(this, 'point', 'opacity', params.point.opacity, 1.0);
459
+ defineStyleProperty(this, 'point', 'radius', params.point.radius, 2.0);
460
+ defineStyleProperty(this, 'point', 'width', params.point.width, 0.0);
461
+ defineStyleProperty(this, 'point', 'base_altitude', params.point.base_altitude, baseAltitudeDefault);
462
+ if (params.point.model) {
463
+ defineStyleProperty(this, 'point', 'model', params.point.model);
464
+ }
465
+ this.text = {};
466
+ defineStyleProperty(this, 'text', 'field', params.text.field);
467
+ defineStyleProperty(this, 'text', 'zOrder', params.text.zOrder, 'auto');
468
+ defineStyleProperty(this, 'text', 'color', params.text.color, '#000000');
469
+ defineStyleProperty(this, 'text', 'anchor', params.text.anchor, 'center');
470
+ defineStyleProperty(this, 'text', 'offset', params.text.offset, [0, 0]);
471
+ defineStyleProperty(this, 'text', 'padding', params.text.padding, 2);
472
+ defineStyleProperty(this, 'text', 'size', params.text.size, 16);
473
+ defineStyleProperty(this, 'text', 'placement', params.text.placement, 'point');
474
+ defineStyleProperty(this, 'text', 'rotation', params.text.rotation, 'auto');
475
+ defineStyleProperty(this, 'text', 'wrap', params.text.wrap, 10);
476
+ defineStyleProperty(this, 'text', 'spacing', params.text.spacing, 0);
477
+ defineStyleProperty(this, 'text', 'transform', params.text.transform, 'none');
478
+ defineStyleProperty(this, 'text', 'justify', params.text.justify, 'center');
479
+ defineStyleProperty(this, 'text', 'opacity', params.text.opacity, 1.0);
480
+ defineStyleProperty(this, 'text', 'font', params.text.font, ['Open Sans Regular', 'Arial Unicode MS Regular', 'sans-serif']);
481
+ defineStyleProperty(this, 'text', 'haloColor', params.text.haloColor, '#000000');
482
+ defineStyleProperty(this, 'text', 'haloWidth', params.text.haloWidth, 0);
483
+ defineStyleProperty(this, 'text', 'haloBlur', params.text.haloBlur, 0);
484
+ this.icon = {};
485
+ defineStyleProperty(this, 'icon', 'source', params.icon.source);
486
+ if (params.icon.key) {
487
+ console.warn("'icon.key' is deprecated: use 'icon.id' instead");
488
+ params.icon.id = params.icon.key;
489
+ }
490
+ defineStyleProperty(this, 'icon', 'id', params.icon.id);
491
+ defineStyleProperty(this, 'icon', 'cropValues', params.icon.cropValues);
492
+ defineStyleProperty(this, 'icon', 'anchor', params.icon.anchor, 'center');
493
+ defineStyleProperty(this, 'icon', 'size', params.icon.size, 1);
494
+ defineStyleProperty(this, 'icon', 'color', params.icon.color);
495
+ defineStyleProperty(this, 'icon', 'opacity', params.icon.opacity, 1.0);
496
+ }
497
+ setContext(ctx) {
498
+ this.context = ctx;
499
+ }
500
+
501
+ /**
502
+ * Applies the style.fill to a polygon of the texture canvas.
503
+ * @param {CanvasRenderingContext2D} txtrCtx The Context 2D of the texture canvas.
504
+ * @param {Path2D} polygon The current texture canvas polygon.
505
+ * @param {Number} invCtxScale The ratio to scale line width and radius circle.
506
+ * @param {Boolean} canBeFilled - true if feature.type == FEATURE_TYPES.POLYGON.
507
+ */
508
+ applyToCanvasPolygon(txtrCtx, polygon, invCtxScale, canBeFilled) {
509
+ // draw line or edge of polygon
510
+ if (this.stroke.width > 0) {
511
+ // TO DO add possibility of using a pattern (https://github.com/iTowns/itowns/issues/2210)
512
+ this._applyStrokeToPolygon(txtrCtx, invCtxScale, polygon);
513
+ }
514
+
515
+ // fill inside of polygon
516
+ if (canBeFilled && (this.fill.pattern || this.fill.color)) {
517
+ // canBeFilled can be move to StyleContext in the later PR
518
+ this._applyFillToPolygon(txtrCtx, invCtxScale, polygon);
519
+ }
520
+ }
521
+ _applyStrokeToPolygon(txtrCtx, invCtxScale, polygon) {
522
+ if (txtrCtx.strokeStyle !== this.stroke.color) {
523
+ txtrCtx.strokeStyle = this.stroke.color;
524
+ }
525
+ const width = this.stroke.width * invCtxScale;
526
+ if (txtrCtx.lineWidth !== width) {
527
+ txtrCtx.lineWidth = width;
528
+ }
529
+ const alpha = this.stroke.opacity;
530
+ if (alpha !== txtrCtx.globalAlpha && typeof alpha == 'number') {
531
+ txtrCtx.globalAlpha = alpha;
532
+ }
533
+ if (txtrCtx.lineCap !== this.stroke.lineCap) {
534
+ txtrCtx.lineCap = this.stroke.lineCap;
535
+ }
536
+ txtrCtx.setLineDash(this.stroke.dasharray.map(a => a * invCtxScale * 2));
537
+ txtrCtx.stroke(polygon);
538
+ }
539
+ async _applyFillToPolygon(txtrCtx, invCtxScale, polygon) {
540
+ // if (this.fill.pattern && txtrCtx.fillStyle.src !== this.fill.pattern.src) {
541
+ // need doc for the txtrCtx.fillStyle.src that seems to always be undefined
542
+ if (this.fill.pattern) {
543
+ let img = this.fill.pattern;
544
+ const cropValues = {
545
+ ...this.fill.pattern.cropValues
546
+ };
547
+ if (this.fill.pattern.source) {
548
+ img = await loadImage(this.fill.pattern.source);
549
+ }
550
+ cropImage(img, cropValues);
551
+ txtrCtx.fillStyle = txtrCtx.createPattern(canvas, 'repeat');
552
+ if (txtrCtx.fillStyle.setTransform) {
553
+ txtrCtx.fillStyle.setTransform(matrix.scale(invCtxScale));
554
+ } else {
555
+ console.warn('Raster pattern isn\'t completely supported on Ie and edge', txtrCtx.fillStyle);
556
+ }
557
+ } else if (txtrCtx.fillStyle !== this.fill.color) {
558
+ txtrCtx.fillStyle = this.fill.color;
559
+ }
560
+ if (this.fill.opacity !== txtrCtx.globalAlpha) {
561
+ txtrCtx.globalAlpha = this.fill.opacity;
562
+ }
563
+ txtrCtx.fill(polygon);
564
+ }
565
+
566
+ /**
567
+ * Applies this style to a DOM element. Limited to the `text` and `icon`
568
+ * properties of this style.
569
+ *
570
+ * @param {Element} domElement - The element to set the style to.
571
+ *
572
+ * @returns {undefined|Promise<HTMLImageElement>}
573
+ * for a text label: undefined.
574
+ * for an icon: a Promise resolving with the HTMLImageElement containing the image.
575
+ */
576
+ async applyToHTML(domElement) {
577
+ if (arguments.length > 1) {
578
+ console.warn('Deprecated argument sprites. Sprites must be configured in style.');
579
+ }
580
+ domElement.style.padding = `${this.text.padding}px`;
581
+ domElement.style.maxWidth = `${this.text.wrap}em`;
582
+ domElement.style.color = this.text.color;
583
+ if (this.text.size > 0) {
584
+ domElement.style.fontSize = `${this.text.size}px`;
585
+ }
586
+ domElement.style.fontFamily = this.text.font.join(',');
587
+ domElement.style.textTransform = this.text.transform;
588
+ domElement.style.letterSpacing = `${this.text.spacing}em`;
589
+ domElement.style.textAlign = this.text.justify;
590
+ domElement.style['white-space'] = 'pre-line';
591
+ if (this.text.haloWidth > 0) {
592
+ domElement.style.setProperty('--text_stroke_display', 'block');
593
+ domElement.style.setProperty('--text_stroke_width', `${this.text.haloWidth}px`);
594
+ domElement.style.setProperty('--text_stroke_color', this.text.haloColor);
595
+ domElement.setAttribute('data-before', domElement.textContent);
596
+ }
597
+ if (!this.icon.source) {
598
+ return;
599
+ }
600
+ const icon = document.createElement('img');
601
+ const iconPromise = new Promise((resolve, reject) => {
602
+ const opt = {
603
+ size: this.icon.size,
604
+ color: this.icon.color,
605
+ opacity: this.icon.opacity,
606
+ anchor: this.icon.anchor
607
+ };
608
+ icon.onload = () => resolve(_addIcon(icon, domElement, opt));
609
+ icon.onerror = err => reject(err);
610
+ });
611
+ if (!this.icon.cropValues && !this.icon.color) {
612
+ icon.src = this.icon.source;
613
+ } else {
614
+ const cropValues = {
615
+ ...this.icon.cropValues
616
+ };
617
+ const color = this.icon.color;
618
+ const id = this.icon.id || this.icon.source;
619
+ const img = await loadImage(this.icon.source);
620
+ const imgd = cropImage(img, cropValues);
621
+ const imgdColored = replaceWhitePxl(imgd, color, id);
622
+ canvas.getContext('2d').putImageData(imgdColored, 0, 0);
623
+ icon.src = canvas.toDataURL('image/png');
624
+ }
625
+ return iconPromise;
626
+ }
627
+
628
+ /**
629
+ * Gets the values corresponding to the anchor of the text. It is
630
+ * proportions, to use with a `translate()` and a `transform` property.
631
+ *
632
+ * @return {Number[]} Two percentage values, for x and y respectively.
633
+ */
634
+ getTextAnchorPosition() {
635
+ if (typeof this.text.anchor === 'string') {
636
+ if (Object.keys(textAnchorPosition).includes(this.text.anchor)) {
637
+ return textAnchorPosition[this.text.anchor];
638
+ } else {
639
+ console.error(`${this.text.anchor} is not a valid input for Style.text.anchor parameter.`);
640
+ return textAnchorPosition.center;
641
+ }
642
+ } else {
643
+ return this.text.anchor;
644
+ }
645
+ }
646
+ }
647
+
648
+ // Add custom style sheet with iTowns specifics
649
+ const CustomStyle = {
650
+ itowns_stroke_single_before
651
+ };
652
+ const customStyleSheet = document.createElement('style');
653
+ customStyleSheet.type = 'text/css';
654
+ Object.keys(CustomStyle).forEach(key => {
655
+ customStyleSheet.innerHTML += `${CustomStyle[key]}\n\n`;
656
+ });
657
+ if (typeof document !== 'undefined') {
658
+ document.getElementsByTagName('head')[0].appendChild(customStyleSheet);
659
+ }
660
+ export default Style;