@geoblocks/cesium-iso 0.1.0 → 0.2.0

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 (3) hide show
  1. package/README.md +2 -0
  2. package/cesium-iso.js +45 -13
  3. package/package.json +2 -2
package/README.md CHANGED
@@ -36,6 +36,8 @@ iso.turnTo(200); // to a heading, in degrees, the shorter way
36
36
  iso.zoom(0.8); // in, toward the middle of the screen
37
37
  iso.glideTo(position);
38
38
  iso.target = position; // at once
39
+ iso.heading = 90; // at once, in degrees, no easing
40
+ iso.size = 300; // at once, in meters across the screen's shorter side, within minSize and maxSize
39
41
  ```
40
42
 
41
43
  ## Options
package/cesium-iso.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import {Cartesian2, Cartesian3, Cartographic, FixedFrameTransforms, IntersectionTests, Math as CesiumMath, Matrix4, Plane} from '@cesium/core';
2
+ import {EasingFunction} from '@cesium/engine';
2
3
 
3
4
  // in meters, how far the camera stays from its target by default, out of the scene's way: an orthographic view's
4
5
  // size does not depend on it, its depth's precision does (32-bit floats: half a millimeter at 5 km)
@@ -17,7 +18,7 @@ const TWIST = CesiumMath.toRadians(15);
17
18
  const DAMPING = 0.95;
18
19
  const LET_GO = 60;
19
20
 
20
- const easeInOut = (/** @type {number} */ t) => t < 0.5 ? 2 * t * t : 1 - (-2 * t + 2) ** 2 / 2;
21
+ const easeInOut = EasingFunction.QUADRATIC_IN_OUT;
21
22
  const positionScratch = new Cartesian2();
22
23
  const planeScratch = new Plane(Cartesian3.UNIT_Z, 0);
23
24
  const normalScratch = new Cartesian3();
@@ -55,7 +56,7 @@ export default class CesiumIso {
55
56
  /** @type {Cartesian3} */
56
57
  this.target_ = new Cartesian3();
57
58
  this.heading_ = 0;
58
- /** @type {Map<number, {x: number, y: number}>} */
59
+ /** @type {Map<number, Cartesian2>} */
59
60
  this.pointers_ = new Map();
60
61
  /** @type {{grab?: Cartesian3 | undefined, turn?: boolean, last?: {x: number, y: number}, distance?: number, angle?: number, twist?: number} | undefined} */
61
62
  this.drag_ = undefined;
@@ -184,6 +185,30 @@ export default class CesiumIso {
184
185
  return CesiumMath.toDegrees(this.heading_);
185
186
  }
186
187
 
188
+ /**
189
+ * The heading set, in degrees: the view turned there at once, with no easing (turnTo eases); an animation under way,
190
+ * a drift or a turn, is dropped. While active.
191
+ * @param {number} heading
192
+ */
193
+ set heading(heading) {
194
+ this.turning_ = this.drift_ = this.drifted_ = undefined;
195
+ this.heading_ = CesiumMath.toRadians(heading);
196
+ this.place_();
197
+ }
198
+
199
+ /**
200
+ * The view's size, in meters across the screen's shorter side; set, within its bounds, the view zooms at once about
201
+ * its target (zoom zooms about a point of the screen).
202
+ */
203
+ get size() {
204
+ return this.size_;
205
+ }
206
+
207
+ set size(size) {
208
+ this.size_ = CesiumMath.clamp(size, this.minSize_, this.maxSize_);
209
+ this.place_();
210
+ }
211
+
187
212
  /**
188
213
  * A turn, eased, to a heading, in degrees, the shorter way round: the view toward a side a place is seen from.
189
214
  * @param {number} heading
@@ -192,7 +217,7 @@ export default class CesiumIso {
192
217
  this.drift_ = this.drifted_ = undefined;
193
218
  this.anchor_();
194
219
  const to = CesiumMath.toRadians(heading);
195
- const delta = Math.atan2(Math.sin(to - this.heading_), Math.cos(to - this.heading_));
220
+ const delta = CesiumMath.negativePiToPi(to - this.heading_);
196
221
  this.turning_ = {from: this.heading_, to: this.heading_ + delta, start: performance.now()};
197
222
  this.animate_();
198
223
  }
@@ -260,7 +285,10 @@ export default class CesiumIso {
260
285
  Cartesian3.clone(up, camera.up);
261
286
  Cartesian3.cross(direction, up, camera.right);
262
287
  // the orthographic frustum's width
263
- Object.assign(camera.frustum, {width: this.width_()});
288
+ // the orthographic frustum's width; a subclass placing the camera on a perspective frustum has none to set
289
+ if ('width' in camera.frustum) {
290
+ camera.frustum.width = this.width_();
291
+ }
264
292
  this.viewer.scene.requestRender();
265
293
  }
266
294
 
@@ -278,7 +306,7 @@ export default class CesiumIso {
278
306
  * @param {number} sign
279
307
  */
280
308
  cornerAfter_(heading, sign) {
281
- const quarter = Math.PI / 2;
309
+ const quarter = CesiumMath.PI_OVER_TWO;
282
310
  return this.corner_ + (Math.round((heading - this.corner_) / quarter) + sign) * quarter;
283
311
  }
284
312
 
@@ -365,8 +393,8 @@ export default class CesiumIso {
365
393
  const points = [...this.pointers_.values()];
366
394
  if (points.length === 2) {
367
395
  const [a, b] = points;
368
- const middle = new Cartesian2((a.x + b.x) / 2, (a.y + b.y) / 2);
369
- return {grab: this.ground_(middle, this.target_), distance: Math.hypot(b.x - a.x, b.y - a.y), angle: Math.atan2(b.y - a.y, b.x - a.x), twist: 0};
396
+ const middle = Cartesian2.lerp(a, b, 0.5, new Cartesian2());
397
+ return {grab: this.ground_(middle, this.target_), distance: Cartesian2.distance(a, b), angle: Math.atan2(b.y - a.y, b.x - a.x), twist: 0};
370
398
  }
371
399
  const slide = e.pointerType === 'touch' || e.button === 2 || e.ctrlKey || e.shiftKey;
372
400
  const point = /** @type {{x: number, y: number}} */ (points[0]);
@@ -384,15 +412,15 @@ export default class CesiumIso {
384
412
  const drag = this.drag_;
385
413
  const points = [...this.pointers_.values()];
386
414
  if (points.length === 2 && drag.distance !== undefined) {
387
- const [a, b] = /** @type {[{x: number, y: number}, {x: number, y: number}]} */ (points);
388
- const middle = new Cartesian2((a.x + b.x) / 2, (a.y + b.y) / 2);
389
- const [distance, angle] = [Math.hypot(b.x - a.x, b.y - a.y), Math.atan2(b.y - a.y, b.x - a.x)];
415
+ const [a, b] = /** @type {[Cartesian2, Cartesian2]} */ (points);
416
+ const middle = Cartesian2.lerp(a, b, 0.5, new Cartesian2());
417
+ const [distance, angle] = [Cartesian2.distance(a, b), Math.atan2(b.y - a.y, b.x - a.x)];
390
418
  this.size_ = CesiumMath.clamp(this.size_ * drag.distance / distance, this.minSize_, this.maxSize_);
391
419
  this.place_();
392
420
  if (drag.grab) {
393
421
  this.keep_(drag.grab, middle);
394
422
  }
395
- const turned = Math.atan2(Math.sin(angle - /** @type {number} */ (drag.angle)), Math.cos(angle - /** @type {number} */ (drag.angle)));
423
+ const turned = CesiumMath.negativePiToPi(angle - /** @type {number} */ (drag.angle));
396
424
  drag.twist = /** @type {number} */ (drag.twist) + turned;
397
425
  if (Math.abs(drag.twist) >= TWIST) {
398
426
  // the screen's y down: the fingers turning clockwise on it turn the ground clockwise under them
@@ -405,7 +433,7 @@ export default class CesiumIso {
405
433
  return;
406
434
  } else if (drag.turn && drag.last) {
407
435
  const point = /** @type {{x: number, y: number}} */ (points[0]);
408
- this.heading_ -= 2 * Math.PI * (point.x - drag.last.x) / this.viewer.scene.canvas.clientHeight;
436
+ this.heading_ -= CesiumMath.TWO_PI * (point.x - drag.last.x) / this.viewer.scene.canvas.clientHeight;
409
437
  drag.last = point;
410
438
  this.place_();
411
439
  } else if (drag.grab) {
@@ -443,6 +471,10 @@ export default class CesiumIso {
443
471
  * @param {PointerEvent} e
444
472
  */
445
473
  up_(e) {
474
+ // a pointer pressed elsewhere (on a control over the canvas) and let go here was never the view's
475
+ if (!this.pointers_.has(e.pointerId)) {
476
+ return;
477
+ }
446
478
  this.pointers_.delete(e.pointerId);
447
479
  // a finger lifted: the other slides on, from here
448
480
  this.drag_ = this.pointers_.size ? this.start_(e) : undefined;
@@ -539,7 +571,7 @@ export default class CesiumIso {
539
571
  }
540
572
  if (this.turning_) {
541
573
  const t = Math.min(1, (now - this.turning_.start) / TURN_TIME);
542
- this.heading_ = this.turning_.from + (this.turning_.to - this.turning_.from) * easeInOut(t);
574
+ this.heading_ = CesiumMath.lerp(this.turning_.from, this.turning_.to, easeInOut(t));
543
575
  this.place_();
544
576
  if (t === 1) {
545
577
  this.turning_ = undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geoblocks/cesium-iso",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "license": "BSD-3-Clause",
5
5
  "type": "module",
6
6
  "repository": {
@@ -21,5 +21,5 @@
21
21
  "@cesium/core": ">=0.1.0",
22
22
  "@cesium/engine": ">=10"
23
23
  },
24
- "gitHead": "430bbbb63bf3d60221d670f65d5e51fdb6d5b944"
24
+ "gitHead": "5c6ee12b4e35f46ff733cf628da105f55121fbf3"
25
25
  }