Remove deprecated method PluggableMap#forEachLayerAtPixel

This commit is contained in:
Maximilian Krög
2022-07-24 21:39:34 +02:00
parent 0f8de89318
commit 2897f03ea5
12 changed files with 39 additions and 634 deletions

View File

@@ -13,8 +13,10 @@ import View from '../../../../../../src/ol/View.js';
import {get as getProj} from '../../../../../../src/ol/proj.js';
describe('ol/renderer/canvas/ImageLayer', function () {
describe('#forEachLayerAtCoordinate', function () {
describe('#getData', function () {
let map, target, source;
/** @type {ImageLayer} */
let layer;
beforeEach(function (done) {
const projection = new Projection({
code: 'custom-image',
@@ -30,14 +32,13 @@ describe('ol/renderer/canvas/ImageLayer', function () {
projection: projection,
imageExtent: [0, 0, 20, 20],
});
layer = new ImageLayer({
source: source,
});
map = new Map({
pixelRatio: 1,
target: target,
layers: [
new ImageLayer({
source: source,
}),
],
layers: [layer],
view: new View({
projection: projection,
center: [10, 10],
@@ -57,19 +58,13 @@ describe('ol/renderer/canvas/ImageLayer', function () {
it('properly detects pixels', function () {
map.renderSync();
let has = false;
function hasLayer() {
has = true;
}
map.forEachLayerAtPixel([20, 80], hasLayer);
expect(has).to.be(true);
has = false;
map.forEachLayerAtPixel([10, 90], hasLayer);
expect(has).to.be(false);
expect(layer.getData([20, 80])[3]).to.not.be(0);
expect(layer.getData([10, 90])[3]).to.be(0);
});
});
describe('#forEachLayerAtPixel Image CORS', function () {
describe('#getData Image CORS', function () {
let map,
target,
imageExtent,
@@ -133,34 +128,22 @@ describe('ol/renderer/canvas/ImageLayer', function () {
document.body.removeChild(target);
});
it('should detect pixels even if there is no color because neither crossOrigin or extent is set', function () {
it('should not detect pixels when crossOrigin is not set', function () {
imageLayerCross.setVisible(false);
imageLayer.setVisible(true);
map.renderSync();
let has = false;
function hasLayer() {
has = true;
}
map.forEachLayerAtPixel([50, 50], hasLayer);
expect(has).to.be(true);
has = false;
map.forEachLayerAtPixel([10, 10], hasLayer);
expect(has).to.be(true);
expect(imageLayer.getData([50, 50])).to.be(null);
expect(imageLayer.getData([10, 10])).to.be(null);
});
it('should not detect pixels outside of the layer extent with crossOrigin set', function () {
imageLayerCross.setVisible(true);
imageLayer.setVisible(false);
map.renderSync();
let has = false;
function hasLayer() {
has = true;
}
map.forEachLayerAtPixel([50, 50], hasLayer);
expect(has).to.be(true);
has = false;
map.forEachLayerAtPixel([10, 10], hasLayer);
expect(has).to.be(false);
expect(imageLayerCross.getData([50, 50])).to.not.be(null);
expect(imageLayerCross.getData([10, 10])).to.be(null);
});
it('should not detect pixels outside of the layer extent with extent set', function () {
@@ -168,87 +151,9 @@ describe('ol/renderer/canvas/ImageLayer', function () {
imageLayerCross.setExtent(imageExtent);
imageLayer.setVisible(false);
map.renderSync();
let has = false;
function hasLayer() {
has = true;
}
map.forEachLayerAtPixel([50, 50], hasLayer);
expect(has).to.be(true);
has = false;
map.forEachLayerAtPixel([10, 10], hasLayer);
expect(has).to.be(false);
});
});
describe('#getDataAtPixel', function () {
let map, target, source, imageLayer;
beforeEach(function (done) {
const projection = new Projection({
code: 'custom-image',
units: 'pixels',
extent: [0, 0, 200, 200],
});
target = document.createElement('div');
target.style.width = '100px';
target.style.height = '100px';
document.body.appendChild(target);
const imageExtent = [0, 0, 20, 20];
source = new Static({
url: 'spec/ol/data/dot.png',
projection: projection,
imageExtent: imageExtent,
});
imageLayer = new ImageLayer({
source: source,
extent: imageExtent,
});
map = new Map({
pixelRatio: 1,
target: target,
layers: [imageLayer],
view: new View({
projection: projection,
center: [10, 10],
zoom: 1,
maxZoom: 8,
}),
});
source.on('imageloadend', function () {
done();
});
});
afterEach(function () {
map.setTarget(null);
document.body.removeChild(target);
});
it('should not detect pixels outside of the layer extent', function () {
map.renderSync();
const pixel = [10, 10];
const frameState = map.frameState_;
const hitTolerance = 0;
const layerRenderer = imageLayer.getRenderer();
const data = layerRenderer.getDataAtPixel(
pixel,
frameState,
hitTolerance
);
expect(data).to.be(null);
});
it('should detect pixels in the layer extent', function () {
map.renderSync();
const pixel = [50, 50];
const frameState = map.frameState_;
const hitTolerance = 0;
const layerRenderer = imageLayer.getRenderer();
const data = layerRenderer.getDataAtPixel(
pixel,
frameState,
hitTolerance
);
expect(data.length > 0).to.be(true);
expect(imageLayerCross.getData([50, 50])).to.not.be(null);
expect(imageLayerCross.getData([10, 10])).to.be(null);
});
});

View File

@@ -1,7 +1,6 @@
import DataTileSource from '../../../../../../src/ol/source/DataTile.js';
import Layer from '../../../../../../src/ol/layer/Layer.js';
import Map from '../../../../../../src/ol/Map.js';
import Projection from '../../../../../../src/ol/proj/Projection.js';
import TileLayer from '../../../../../../src/ol/layer/WebGLTile.js';
import VectorLayer from '../../../../../../src/ol/layer/Vector.js';
import VectorSource from '../../../../../../src/ol/source/Vector.js';
@@ -231,176 +230,4 @@ describe('ol/renderer/webgl/Layer', function () {
dispose(map);
});
});
describe('#getDataAtPixel (preserveDrawingBuffer false)', function () {
let map, target, source, layer, getContextOriginal;
beforeEach(function (done) {
getContextOriginal = HTMLCanvasElement.prototype.getContext;
HTMLCanvasElement.prototype.getContext = function (type, attributes) {
if (attributes && attributes.preserveDrawingBuffer) {
attributes.preserveDrawingBuffer = false;
}
return getContextOriginal.call(this, type, attributes);
};
const projection = new Projection({
code: 'custom-image',
units: 'pixels',
extent: [0, 0, 200, 200],
});
target = document.createElement('div');
target.style.width = '100px';
target.style.height = '100px';
document.body.appendChild(target);
source = new DataTileSource({
loader: function (z, x, y) {
return new Uint8Array(x == 0 ? [255, 0, 0, 255] : [0, 0, 0, 0]);
},
projection: projection,
maxZoom: 0,
tileSize: 1,
maxResolution: 100,
});
layer = new TileLayer({
source: source,
extent: [50, 0, 150, 100],
});
map = new Map({
pixelRatio: 1,
target: target,
layers: [layer],
view: new View({
projection: projection,
center: [100, 100],
zoom: 0,
}),
});
map.once('rendercomplete', function () {
done();
});
});
afterEach(function () {
HTMLCanvasElement.prototype.getContext = getContextOriginal;
map.setLayers([]);
map.setTarget(null);
document.body.removeChild(target);
});
it('should not detect pixels outside of the layer extent', function () {
const pixel = [10, 10];
const frameState = map.frameState_;
const hitTolerance = 0;
const layerRenderer = layer.getRenderer();
const data = layerRenderer.getDataAtPixel(
pixel,
frameState,
hitTolerance
);
expect(data).to.be(null);
});
it('should handle unreadable pixels in the layer extent', function () {
const pixel = [10, 60];
const frameState = map.frameState_;
const hitTolerance = 0;
const layerRenderer = layer.getRenderer();
const data = layerRenderer.getDataAtPixel(
pixel,
frameState,
hitTolerance
);
expect(data.length).to.be(0);
});
});
describe('#getDataAtPixel (preserveDrawingBuffer true)', function () {
let map, target, source, layer;
beforeEach(function (done) {
const projection = new Projection({
code: 'custom-image',
units: 'pixels',
extent: [0, 0, 200, 200],
});
target = document.createElement('div');
target.style.width = '100px';
target.style.height = '100px';
document.body.appendChild(target);
source = new DataTileSource({
loader: function (z, x, y) {
return new Uint8Array(x == 0 ? [255, 0, 0, 255] : [0, 0, 0, 0]);
},
projection: projection,
maxZoom: 0,
tileSize: 1,
maxResolution: 100,
});
layer = new TileLayer({
source: source,
extent: [50, 0, 150, 100],
});
map = new Map({
pixelRatio: 1,
target: target,
layers: [layer],
view: new View({
projection: projection,
center: [100, 100],
zoom: 0,
}),
});
map.once('rendercomplete', function () {
done();
});
});
afterEach(function () {
map.setLayers([]);
map.setTarget(null);
document.body.removeChild(target);
});
it('should not detect pixels outside of the layer extent', function () {
const pixel = [10, 10];
const frameState = map.frameState_;
const hitTolerance = 0;
const layerRenderer = layer.getRenderer();
const data = layerRenderer.getDataAtPixel(
pixel,
frameState,
hitTolerance
);
expect(data).to.be(null);
});
it('should detect pixels in the layer extent', function () {
const pixel = [10, 60];
const frameState = map.frameState_;
const hitTolerance = 0;
const layerRenderer = layer.getRenderer();
const data = layerRenderer.getDataAtPixel(
pixel,
frameState,
hitTolerance
);
expect(data.length > 0).to.be(true);
expect(data[0]).to.be(255);
expect(data[1]).to.be(0);
expect(data[2]).to.be(0);
expect(data[3]).to.be(255);
});
it('should handle no data in the layer extent', function () {
const pixel = [60, 60];
const frameState = map.frameState_;
const hitTolerance = 0;
const layerRenderer = layer.getRenderer();
const data = layerRenderer.getDataAtPixel(
pixel,
frameState,
hitTolerance
);
expect(data).to.be(null);
});
});
});