mobility-toolbox-js 2.0.0-beta.67 → 2.0.0-beta.69

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.
package/mbt.js CHANGED
@@ -33998,7 +33998,6 @@ uniform ${i3} ${o3} u_${a3};
33998
33998
  close() {
33999
33999
  if (this.websocket && (this.open || this.connecting)) {
34000
34000
  this.websocket.onclose = () => {
34001
- this.websocket = void 0;
34002
34001
  };
34003
34002
  this.websocket.close();
34004
34003
  this.messagesOnOpen = [];
@@ -34787,7 +34786,8 @@ uniform ${i3} ${o3} u_${a3};
34787
34786
  noInterpolate = false,
34788
34787
  hoverVehicleId,
34789
34788
  selectedVehicleId,
34790
- filter
34789
+ filter,
34790
+ getScreenPixel = (pixel, viewStat) => (viewStat.zoom || 0) < 12 ? pixel.map((coord) => Math.floor(coord)) : pixel
34791
34791
  } = options;
34792
34792
  const context = canvas2.getContext("2d");
34793
34793
  context?.clearRect(0, 0, canvas2.width, canvas2.height);
@@ -34802,12 +34802,8 @@ uniform ${i3} ${o3} u_${a3};
34802
34802
  }
34803
34803
  let hoverVehicleImg;
34804
34804
  let hoverVehiclePx;
34805
- let hoverVehicleWidth;
34806
- let hoverVehicleHeight;
34807
34805
  let selectedVehicleImg;
34808
34806
  let selectedVehiclePx;
34809
- let selectedVehicleWidth;
34810
- let selectedVehicleHeight;
34811
34807
  const renderedTrajectories = [];
34812
34808
  for (let i = trajectories.length - 1; i >= 0; i -= 1) {
34813
34809
  const trajectory = trajectories[i];
@@ -34833,30 +34829,25 @@ uniform ${i3} ${o3} u_${a3};
34833
34829
  if (!vehicleImg) {
34834
34830
  continue;
34835
34831
  }
34836
- const imgWidth = vehicleImg.width;
34837
- const imgHeight = vehicleImg.height;
34838
34832
  if (hoverVehicleId !== id && selectedVehicleId !== id) {
34839
- context?.drawImage(vehicleImg, px[0] - imgWidth / 2, px[1] - imgHeight / 2, imgWidth, imgHeight);
34833
+ const [x, y] = getScreenPixel([px[0] - vehicleImg.width / 2, px[1] - vehicleImg.height / 2], viewState);
34834
+ context?.drawImage(vehicleImg, x, y);
34840
34835
  }
34841
34836
  if (hoverVehicleId && hoverVehicleId === id) {
34842
34837
  hoverVehicleImg = vehicleImg;
34843
34838
  hoverVehiclePx = px;
34844
- hoverVehicleWidth = imgWidth;
34845
- hoverVehicleHeight = imgHeight;
34846
34839
  }
34847
34840
  if (selectedVehicleId && selectedVehicleId === id) {
34848
34841
  selectedVehicleImg = vehicleImg;
34849
34842
  selectedVehiclePx = px;
34850
- selectedVehicleWidth = imgWidth;
34851
- selectedVehicleHeight = imgHeight;
34852
34843
  }
34853
34844
  renderedTrajectories.push(trajectory);
34854
34845
  }
34855
- if (selectedVehicleImg && selectedVehiclePx && selectedVehicleWidth && selectedVehicleHeight) {
34856
- context?.drawImage(selectedVehicleImg, selectedVehiclePx[0] - selectedVehicleWidth / 2, selectedVehiclePx[1] - selectedVehicleHeight / 2, selectedVehicleWidth, selectedVehicleHeight);
34846
+ if (selectedVehicleImg && selectedVehiclePx) {
34847
+ context?.drawImage(selectedVehicleImg, Math.floor(selectedVehiclePx[0] - selectedVehicleImg.width / 2), Math.floor(selectedVehiclePx[1] - selectedVehicleImg.height / 2));
34857
34848
  }
34858
- if (hoverVehicleImg && hoverVehiclePx && hoverVehicleWidth && hoverVehicleHeight) {
34859
- context?.drawImage(hoverVehicleImg, hoverVehiclePx[0] - hoverVehicleWidth / 2, hoverVehiclePx[1] - hoverVehicleHeight / 2, hoverVehicleWidth, hoverVehicleHeight);
34849
+ if (hoverVehicleImg && hoverVehiclePx) {
34850
+ context?.drawImage(hoverVehicleImg, Math.floor(hoverVehiclePx[0] - hoverVehicleImg.width / 2), Math.floor(hoverVehiclePx[1] - hoverVehicleImg.height / 2));
34860
34851
  }
34861
34852
  return {
34862
34853
  renderedTrajectories
@@ -36095,24 +36086,24 @@ uniform ${i3} ${o3} u_${a3};
36095
36086
  return cacheDelayBg[key];
36096
36087
  };
36097
36088
  var cacheDelayText = {};
36098
- var getDelayTextCanvas = (width, text, fontSize, font, delayColor, delayOutlineColor = "#000", pixelRatio = 1) => {
36099
- const key = `${width}, ${text}, ${font}, ${delayColor}, ${delayOutlineColor}, ${pixelRatio}`;
36089
+ var getDelayTextCanvas = (text, fontSize, font, delayColor, delayOutlineColor = "#000", pixelRatio = 1) => {
36090
+ const key = `${text}, ${font}, ${delayColor}, ${delayOutlineColor}, ${pixelRatio}`;
36100
36091
  if (!cacheDelayText[key]) {
36101
- const canvas2 = createCanvas_default(width, fontSize + 8 * pixelRatio);
36092
+ const canvas2 = createCanvas_default(Math.ceil(text.length * fontSize), Math.ceil(fontSize + 8 * pixelRatio));
36102
36093
  if (canvas2) {
36103
36094
  const ctx = canvas2.getContext("2d");
36104
36095
  if (!ctx) {
36105
36096
  return null;
36106
36097
  }
36098
+ ctx.font = font;
36107
36099
  ctx.textAlign = "left";
36108
36100
  ctx.textBaseline = "middle";
36109
36101
  ctx.font = font;
36110
36102
  ctx.fillStyle = delayColor;
36111
36103
  ctx.strokeStyle = delayOutlineColor;
36112
36104
  ctx.lineWidth = 1.5 * pixelRatio;
36113
- const delayText = text;
36114
- ctx.strokeText(delayText, 0, fontSize);
36115
- ctx.fillText(delayText, 0, fontSize);
36105
+ ctx.strokeText(text, 0, fontSize);
36106
+ ctx.fillText(text, 0, fontSize);
36116
36107
  cacheDelayText[key] = canvas2;
36117
36108
  }
36118
36109
  }
@@ -36246,52 +36237,58 @@ uniform ${i3} ${o3} u_${a3};
36246
36237
  const margin = 1 * pixelRatio;
36247
36238
  const radiusDelay = radius + 2;
36248
36239
  const markerSize = radius * 2;
36249
- const size = radiusDelay * 2 + margin * 2 + 100 * pixelRatio;
36240
+ const size = radiusDelay * 2 + margin * 2;
36250
36241
  const origin = size / 2;
36251
- const canvas2 = createCanvas_default(size, size);
36242
+ let delayBg = null;
36243
+ if (isDisplayStrokeAndDelay && delay !== null) {
36244
+ delayBg = getDelayBgCanvas(origin, radiusDelay, getDelayColor2(delay, cancelled));
36245
+ }
36246
+ let delayText = null;
36247
+ let fontSize = 0;
36248
+ if (isDisplayStrokeAndDelay && (hover || (delay || 0) >= delayDisplay || cancelled)) {
36249
+ fontSize = Math.max(cancelled ? 19 : 14, Math.min(cancelled ? 19 : 17, radius * 1.2)) * pixelRatio;
36250
+ const text = getDelayText2(delay, cancelled);
36251
+ if (text) {
36252
+ delayText = getDelayTextCanvas(text, fontSize, `bold ${fontSize}px arial, sans-serif`, getDelayColor2(delay, cancelled, true), delayOutlineColor, pixelRatio);
36253
+ }
36254
+ }
36255
+ let circleFillColor;
36256
+ if (useDelayStyle) {
36257
+ circleFillColor = getDelayColor2(delay, cancelled);
36258
+ } else {
36259
+ circleFillColor = color || getBgColor2(type);
36260
+ }
36261
+ const hasStroke = isDisplayStrokeAndDelay || hover || selected;
36262
+ const hasDash = !!isDisplayStrokeAndDelay && !!useDelayStyle && delay === null && operatorProvidesRealtime === "yes";
36263
+ const circle = getCircleCanvas(origin, radius, circleFillColor, hasStroke, hasDash, pixelRatio);
36264
+ const width = size + (delayText?.width || 0) * 2;
36265
+ const height = size;
36266
+ const canvas2 = createCanvas_default(width, height);
36252
36267
  if (canvas2) {
36253
36268
  const ctx = canvas2.getContext("2d");
36254
36269
  if (!ctx) {
36255
36270
  return null;
36256
36271
  }
36257
- if (isDisplayStrokeAndDelay && delay !== null) {
36258
- const delayBg = getDelayBgCanvas(origin, radiusDelay, getDelayColor2(delay, cancelled));
36259
- if (delayBg) {
36260
- ctx.drawImage(delayBg, 0, 0);
36261
- }
36272
+ const originX = delayText?.width || 0;
36273
+ if (delayBg) {
36274
+ ctx.drawImage(delayBg, originX, 0);
36262
36275
  }
36263
- if (isDisplayStrokeAndDelay && (hover || (delay || 0) >= delayDisplay || cancelled)) {
36264
- const fontSize = Math.max(cancelled ? 19 : 14, Math.min(cancelled ? 19 : 17, radius * 1.2)) * pixelRatio;
36265
- const text = getDelayText2(delay, cancelled);
36266
- if (text) {
36267
- const textWidth = text.length * fontSize;
36268
- const delayText = getDelayTextCanvas(textWidth, text, fontSize, `bold ${fontSize}px arial, sans-serif`, getDelayColor2(delay, cancelled, true), delayOutlineColor, pixelRatio);
36269
- if (delayText) {
36270
- ctx.drawImage(delayText, origin + radiusDelay + margin, origin - fontSize);
36271
- }
36272
- }
36273
- }
36274
- let circleFillColor;
36275
- if (useDelayStyle) {
36276
- circleFillColor = getDelayColor2(delay, cancelled);
36277
- } else {
36278
- circleFillColor = color || getBgColor2(type);
36279
- }
36280
- const hasStroke = isDisplayStrokeAndDelay || hover || selected;
36281
- const hasDash = !!isDisplayStrokeAndDelay && !!useDelayStyle && delay === null && operatorProvidesRealtime === "yes";
36282
- const circle = getCircleCanvas(origin, radius, circleFillColor, hasStroke, hasDash, pixelRatio);
36283
36276
  if (circle) {
36284
- ctx.drawImage(circle, 0, 0);
36277
+ ctx.drawImage(circle, originX, 0);
36285
36278
  }
36286
- if (isDisplayText && ctx) {
36287
- const fontSize = Math.max(radius, 10);
36288
- const textSize = getTextSize2(ctx, markerSize, name, fontSize);
36279
+ let circleText = null;
36280
+ if (isDisplayText) {
36281
+ const fontSize2 = Math.max(radius, 10);
36282
+ const textSize = getTextSize2(ctx, markerSize, name, fontSize2);
36289
36283
  const textColor2 = !useDelayStyle ? textColor || getTextColor2(type) : "#000000";
36290
36284
  const hasStroke2 = !!useDelayStyle && delay === null && operatorProvidesRealtime === "yes";
36291
- const text = getTextCanvas(name, origin, textSize, textColor2, circleFillColor, hasStroke2, pixelRatio);
36292
- if (text) {
36293
- ctx.drawImage(text, 0, 0);
36294
- }
36285
+ circleText = getTextCanvas(name, origin, textSize, textColor2, circleFillColor, hasStroke2, pixelRatio);
36286
+ }
36287
+ if (circleText) {
36288
+ ctx.drawImage(circleText, originX, 0);
36289
+ }
36290
+ if (delayText) {
36291
+ ctx.drawImage(delayText, originX + Math.ceil(origin + radiusDelay) + margin, Math.ceil(origin - fontSize));
36295
36292
  }
36296
36293
  cache2[key] = canvas2;
36297
36294
  }
@@ -49161,7 +49158,25 @@ uniform ${i3} ${o3} u_${a3};
49161
49158
  "funicular",
49162
49159
  "coach"
49163
49160
  ];
49164
- this.motsByZoom = options.motsByZoom || [allMots];
49161
+ const onlyRail = ["rail"];
49162
+ const withoutCable = ["tram", "subway", "rail", "bus"];
49163
+ this.motsByZoom = options.motsByZoom || [
49164
+ onlyRail,
49165
+ onlyRail,
49166
+ onlyRail,
49167
+ onlyRail,
49168
+ onlyRail,
49169
+ onlyRail,
49170
+ onlyRail,
49171
+ onlyRail,
49172
+ onlyRail,
49173
+ withoutCable,
49174
+ withoutCable,
49175
+ allMots,
49176
+ allMots,
49177
+ allMots,
49178
+ allMots
49179
+ ];
49165
49180
  this.getMotsByZoom = (zoom) => {
49166
49181
  return options.getMotsByZoom && options.getMotsByZoom(zoom, this.motsByZoom) || this.motsByZoom[zoom] || this.motsByZoom[this.motsByZoom.length - 1];
49167
49182
  };
@@ -49232,11 +49247,13 @@ uniform ${i3} ${o3} u_${a3};
49232
49247
  time,
49233
49248
  live,
49234
49249
  canvas: canvas2,
49235
- styleOptions
49250
+ styleOptions,
49251
+ mode
49236
49252
  } = options;
49237
49253
  let currCanvas = canvas2;
49238
49254
  let currSpeed = speed || 1;
49239
49255
  let currTime = time || new Date();
49256
+ let currMode = mode || RealtimeModes.TOPOGRAPHIC;
49240
49257
  let currStyle = style || realtimeDefaultStyle_default;
49241
49258
  super.defineProperties(options);
49242
49259
  Object.defineProperties(this, {
@@ -49252,6 +49269,19 @@ uniform ${i3} ${o3} u_${a3};
49252
49269
  currCanvas = cnvas;
49253
49270
  }
49254
49271
  },
49272
+ mode: {
49273
+ get: () => currMode,
49274
+ set: (newMode) => {
49275
+ if (newMode === currMode) {
49276
+ return;
49277
+ }
49278
+ currMode = newMode;
49279
+ if (this.api?.wsApi?.open) {
49280
+ this.stop();
49281
+ this.start();
49282
+ }
49283
+ }
49284
+ },
49255
49285
  style: {
49256
49286
  get: () => currStyle,
49257
49287
  set: (newStyle) => {
@@ -49448,14 +49478,6 @@ uniform ${i3} ${o3} u_${a3};
49448
49478
  }
49449
49479
  this.api.bbox = bbox;
49450
49480
  }
49451
- setMode(mode) {
49452
- if (this.mode === mode) {
49453
- return;
49454
- }
49455
- this.mode = mode;
49456
- this.api.subscribeTrajectory(this.mode, this.onTrajectoryMessage, void 0, this.isUpdateBboxOnMoveEnd);
49457
- this.api.subscribeDeletedVehicles(this.mode, this.onDeleteTrajectoryMessage, void 0, this.isUpdateBboxOnMoveEnd);
49458
- }
49459
49481
  getRefreshTimeInMs(zoom = 0) {
49460
49482
  const roundedZoom = zoom !== void 0 ? Math.round(zoom) : -1;
49461
49483
  const timeStep = this.getRenderTimeIntervalByZoom(roundedZoom) || 25;
@@ -49523,7 +49545,7 @@ uniform ${i3} ${o3} u_${a3};
49523
49545
  }
49524
49546
  purgeTrajectory(trajectory, extent, zoom) {
49525
49547
  const { type, bounds } = trajectory.properties;
49526
- if (!intersects(extent, bounds) || this.mots && !this.mots.includes(type) || type !== "rail" && zoom < 9) {
49548
+ if (!intersects(extent, bounds) || this.mots && !this.mots.includes(type)) {
49527
49549
  this.removeTrajectory(trajectory);
49528
49550
  return true;
49529
49551
  }
@@ -49791,7 +49813,9 @@ uniform ${i3} ${o3} u_${a3};
49791
49813
  }
49792
49814
  hasFeatureInfoAtCoordinate(coordinate) {
49793
49815
  if (this.map && this.canvas) {
49794
- const context = this.canvas.getContext("2d");
49816
+ const context = this.canvas.getContext("2d", {
49817
+ willReadFrequently: true
49818
+ });
49795
49819
  const pixel = this.map.getPixelFromCoordinate(coordinate);
49796
49820
  return !!context?.getImageData(pixel[0] * (this.pixelRatio || 1), pixel[1] * (this.pixelRatio || 1), 1, 1).data[3];
49797
49821
  }
@@ -49831,6 +49855,13 @@ uniform ${i3} ${o3} u_${a3};
49831
49855
  return super.getRefreshTimeInMs(this.map.getView().getZoom());
49832
49856
  }
49833
49857
  getFeatureInfoAtCoordinate(coordinate, options = {}) {
49858
+ if (!this.map || !this.map.getView()) {
49859
+ return Promise.resolve({
49860
+ layer: this,
49861
+ features: [],
49862
+ coordinate
49863
+ });
49864
+ }
49834
49865
  const resolution = this.map.getView().getResolution();
49835
49866
  return super.getFeatureInfoAtCoordinate(coordinate, {
49836
49867
  resolution,
@@ -49847,6 +49878,12 @@ uniform ${i3} ${o3} u_${a3};
49847
49878
  }
49848
49879
  onZoomEnd() {
49849
49880
  super.onZoomEnd();
49881
+ if (this.visible && this.isUpdateBboxOnMoveEnd) {
49882
+ this.setBbox();
49883
+ }
49884
+ if (this.visible && this.isUpdateBboxOnMoveEnd && this.userClickInteractions && this.selectedVehicleId) {
49885
+ this.highlightTrajectory(this.selectedVehicleId);
49886
+ }
49850
49887
  }
49851
49888
  onFeatureHover(features, layer, coordinate) {
49852
49889
  super.onFeatureHover(features, layer, coordinate);
@@ -50713,7 +50750,7 @@ uniform ${i3} ${o3} u_${a3};
50713
50750
  center: fromLonLat([center.lng, center.lat]),
50714
50751
  extent: bounds,
50715
50752
  resolution: res,
50716
- zoom: this.map.getZoom(),
50753
+ zoom: this.getOlZoom(),
50717
50754
  rotation: -(this.map.getBearing() * Math.PI) / 180,
50718
50755
  pixelRatio: this.pixelRatio
50719
50756
  };
@@ -50737,7 +50774,7 @@ uniform ${i3} ${o3} u_${a3};
50737
50774
  }
50738
50775
  }
50739
50776
  purgeTrajectory(trajectory, extent, zoom) {
50740
- return super.purgeTrajectory(trajectory, extent || this.getMercatorExtent(), zoom || Math.floor(this.map.getZoom() + 1));
50777
+ return super.purgeTrajectory(trajectory, extent || this.getMercatorExtent(), zoom || Math.floor(this.getOlZoom()));
50741
50778
  }
50742
50779
  setBbox(extent, zoom) {
50743
50780
  let newExtent = extent;