-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
hitdetect.js
209 lines (203 loc) · 7.05 KB
/
hitdetect.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/**
* @module ol/render/canvas/hitdetect
*/
import CanvasImmediateRenderer from './Immediate.js';
import {Icon} from '../../style.js';
import {ascending} from '../../array.js';
import {clamp} from '../../math.js';
import {createCanvasContext2D} from '../../dom.js';
import {
getTransformFromProjections,
getUserProjection,
toUserExtent,
} from '../../proj.js';
import {intersects} from '../../extent.js';
export const HIT_DETECT_RESOLUTION = 0.5;
/**
* @param {import("../../size.js").Size} size Canvas size in css pixels.
* @param {Array<import("../../transform.js").Transform>} transforms Transforms
* for rendering features to all worlds of the viewport, from coordinates to css
* pixels.
* @param {Array<import("../../Feature.js").FeatureLike>} features
* Features to consider for hit detection.
* @param {import("../../style/Style.js").StyleFunction|undefined} styleFunction
* Layer style function.
* @param {import("../../extent.js").Extent} extent Extent in render projection.
* @param {number} resolution Resolution.
* @param {number} rotation Rotation.
* @param {number} [squaredTolerance] Squared tolerance.
* @param {import("../../proj/Projection.js").default} [projection] Render projection.
* @return {ImageData} Hit detection image data.
*/
export function createHitDetectionImageData(
size,
transforms,
features,
styleFunction,
extent,
resolution,
rotation,
squaredTolerance,
projection,
) {
const userExtent = projection ? toUserExtent(extent, projection) : extent;
const width = size[0] * HIT_DETECT_RESOLUTION;
const height = size[1] * HIT_DETECT_RESOLUTION;
const context = createCanvasContext2D(width, height);
context.imageSmoothingEnabled = false;
const canvas = context.canvas;
const renderer = new CanvasImmediateRenderer(
context,
HIT_DETECT_RESOLUTION,
extent,
null,
rotation,
squaredTolerance,
projection
? getTransformFromProjections(getUserProjection(), projection)
: null,
);
const featureCount = features.length;
// Stretch hit detection index to use the whole available color range
const indexFactor = Math.floor((256 * 256 * 256 - 1) / featureCount);
const featuresByZIndex = {};
for (let i = 1; i <= featureCount; ++i) {
const feature = features[i - 1];
const featureStyleFunction = feature.getStyleFunction() || styleFunction;
if (!featureStyleFunction) {
continue;
}
let styles = featureStyleFunction(feature, resolution);
if (!styles) {
continue;
}
if (!Array.isArray(styles)) {
styles = [styles];
}
const index = i * indexFactor;
const color = index.toString(16).padStart(7, '#00000');
for (let j = 0, jj = styles.length; j < jj; ++j) {
const originalStyle = styles[j];
const geometry = originalStyle.getGeometryFunction()(feature);
if (!geometry || !intersects(userExtent, geometry.getExtent())) {
continue;
}
const style = originalStyle.clone();
const fill = style.getFill();
if (fill) {
fill.setColor(color);
}
const stroke = style.getStroke();
if (stroke) {
stroke.setColor(color);
stroke.setLineDash(null);
}
style.setText(undefined);
const image = originalStyle.getImage();
if (image) {
const imgSize = image.getImageSize();
if (!imgSize) {
continue;
}
const imgContext = createCanvasContext2D(
imgSize[0],
imgSize[1],
undefined,
{alpha: false},
);
const img = imgContext.canvas;
imgContext.fillStyle = color;
imgContext.fillRect(0, 0, img.width, img.height);
style.setImage(
new Icon({
img: img,
anchor: image.getAnchor(),
anchorXUnits: 'pixels',
anchorYUnits: 'pixels',
offset: image.getOrigin(),
opacity: 1,
size: image.getSize(),
scale: image.getScale(),
rotation: image.getRotation(),
rotateWithView: image.getRotateWithView(),
}),
);
}
const zIndex = style.getZIndex() || 0;
let byGeometryType = featuresByZIndex[zIndex];
if (!byGeometryType) {
byGeometryType = {};
featuresByZIndex[zIndex] = byGeometryType;
byGeometryType['Polygon'] = [];
byGeometryType['Circle'] = [];
byGeometryType['LineString'] = [];
byGeometryType['Point'] = [];
}
const type = geometry.getType();
if (type === 'GeometryCollection') {
const geometries =
/** @type {import("../../geom/GeometryCollection.js").default} */ (
geometry
).getGeometriesArrayRecursive();
for (let i = 0, ii = geometries.length; i < ii; ++i) {
const geometry = geometries[i];
byGeometryType[geometry.getType().replace('Multi', '')].push(
geometry,
style,
);
}
} else {
byGeometryType[type.replace('Multi', '')].push(geometry, style);
}
}
}
const zIndexKeys = Object.keys(featuresByZIndex).map(Number).sort(ascending);
for (let i = 0, ii = zIndexKeys.length; i < ii; ++i) {
const byGeometryType = featuresByZIndex[zIndexKeys[i]];
for (const type in byGeometryType) {
const geomAndStyle = byGeometryType[type];
for (let j = 0, jj = geomAndStyle.length; j < jj; j += 2) {
renderer.setStyle(geomAndStyle[j + 1]);
for (let k = 0, kk = transforms.length; k < kk; ++k) {
renderer.setTransform(transforms[k]);
renderer.drawGeometry(geomAndStyle[j]);
}
}
}
}
return context.getImageData(0, 0, canvas.width, canvas.height);
}
/**
* @param {import("../../pixel").Pixel} pixel Pixel coordinate on the hit
* detection canvas in css pixels.
* @param {Array<F>} features Features. Has to
* match the `features` array that was passed to `createHitDetectionImageData()`.
* @param {ImageData} imageData Hit detection image data generated by
* `createHitDetectionImageData()`.
* @return {Array<F>} Features.
* @template {import("../../Feature.js").FeatureLike} F
*/
export function hitDetect(pixel, features, imageData) {
/** @type {Array<F>} */
const resultFeatures = [];
if (imageData) {
const x = Math.floor(Math.round(pixel[0]) * HIT_DETECT_RESOLUTION);
const y = Math.floor(Math.round(pixel[1]) * HIT_DETECT_RESOLUTION);
// The pixel coordinate is clamped down to the hit-detect canvas' size to account
// for browsers returning coordinates slightly larger than the actual canvas size
// due to a non-integer pixel ratio.
const index =
(clamp(x, 0, imageData.width - 1) +
clamp(y, 0, imageData.height - 1) * imageData.width) *
4;
const r = imageData.data[index];
const g = imageData.data[index + 1];
const b = imageData.data[index + 2];
const i = b + 256 * (g + 256 * r);
const indexFactor = Math.floor((256 * 256 * 256 - 1) / features.length);
if (i && i % indexFactor === 0) {
resultFeatures.push(features[i / indexFactor - 1]);
}
}
return resultFeatures;
}