Skip to content

Commit

Permalink
refactor(FeatureContext): use Context on LabelLayer and Feature2Texture
Browse files Browse the repository at this point in the history
  • Loading branch information
ftoromanoff committed Oct 30, 2023
1 parent a411439 commit 568d2ca
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Converter/Feature2Mesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const coord = new Coordinates('EPSG:4326', 0, 0, 0);
* @property {Object} collection
* @property {Coordinates} coordinates
*/
class FeatureContext {
export class FeatureContext {
#worldCoord = new Coordinates('EPSG:4326', 0, 0, 0);
#localCoordinates = new Coordinates('EPSG:4326', 0, 0, 0);
#isProjected = true;
Expand Down
7 changes: 5 additions & 2 deletions src/Converter/Feature2Texture.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { FEATURE_TYPES } from 'Core/Feature';
import Extent from 'Core/Geographic/Extent';
import Coordinates from 'Core/Geographic/Coordinates';
import Style from 'Core/Style';
import { FeatureContext } from 'Converter/Feature2Mesh';

const context = new FeatureContext();

/**
* Draw polygon (contour, line edge and fill) based on feature vertices into canvas
Expand Down Expand Up @@ -74,7 +77,7 @@ const coord = new Coordinates('EPSG:4326', 0, 0, 0);
function drawFeature(ctx, feature, extent, style, invCtxScale) {
const extentDim = extent.planarDimensions();
const scaleRadius = extentDim.x / ctx.canvas.width;
const globals = {
context.globals = {
fill: true,
stroke: true,
point: true,
Expand All @@ -83,7 +86,7 @@ function drawFeature(ctx, feature, extent, style, invCtxScale) {

for (const geometry of feature.geometries) {
if (Extent.intersectsExtent(geometry.extent, extent)) {
const context = { globals, properties: () => geometry.properties };
context.setGeometry(geometry);
const contextStyle = (geometry.properties.style || style).applyContext(context);

if (contextStyle) {
Expand Down
8 changes: 6 additions & 2 deletions src/Layer/LabelLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import Label from 'Core/Label';
import { FEATURE_TYPES } from 'Core/Feature';
import { readExpression } from 'Core/Style';
import { ScreenGrid } from 'Renderer/Label2DRenderer';
import { FeatureContext } from 'Converter/Feature2Mesh';

const context = new FeatureContext();

const coord = new Coordinates('EPSG:4326', 0, 0, 0);

Expand Down Expand Up @@ -240,7 +243,7 @@ class LabelLayer extends GeometryLayer {
// Converting the extent now is faster for further operation
extent.as(data.crs, _extent);
coord.crs = data.crs;
const globals = {
context.globals = {
icon: true,
text: true,
zoom: extent.zoom,
Expand Down Expand Up @@ -271,8 +274,9 @@ class LabelLayer extends GeometryLayer {
if (!_extent.isPointInside(coord)) { return; }

const geometryField = g.properties.style && g.properties.style.text.field;

context.setGeometry(g);
let content;
const context = { globals, properties: () => g.properties };
if (this.labelDomelement) {
content = readExpression(this.labelDomelement, context);
} else if (!geometryField && !featureField && !layerField) {
Expand Down

0 comments on commit 568d2ca

Please sign in to comment.