zimjs 20.0.8 → 20.0.9

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 (2) hide show
  1. package/package.json +1 -1
  2. package/src/zim.js +81 -158
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zimjs",
3
- "version": "20.0.8",
3
+ "version": "20.0.9",
4
4
  "type": "module",
5
5
  "main": "./src/zim.js",
6
6
  "types": "./ts-src/typings/zim",
package/src/zim.js CHANGED
@@ -58294,6 +58294,8 @@ ALSO: see noTilt() to remove tilt behaviors.
58294
58294
 
58295
58295
  ALSO: see tiltBoundary() to dynamically change or clear the boundary rectangle.
58296
58296
 
58297
+ SEE: https://zimjs.com/020/tilt.html
58298
+
58297
58299
  EXAMPLE
58298
58300
  // Basic rolling ball constrained to stage bounds
58299
58301
  const ball = new Circle(30, red)
@@ -58616,6 +58618,8 @@ To change buttons of the Dialog set: F.sensorAsk.yes.text = "OUI"; F.sensorAsk.n
58616
58618
 
58617
58619
  ALSO: see noTurn() to remove turning behaviors.
58618
58620
 
58621
+ SEE: https://zimjs.com/020/turn.html
58622
+
58619
58623
  EXAMPLE
58620
58624
  // Point an arrow with device heading (stays pointing up in world space)
58621
58625
  const arrow = new Arrow({type:"thick"}).sca(4).rot(-90).centerReg().turn();
@@ -58673,7 +58677,8 @@ RETURNS obj for chaining
58673
58677
  var dampRot = damp ? new zim.Damp(obj.rotation, damp) : null;
58674
58678
  var startDeviceAngle = null;
58675
58679
  var currentDeviceAngle = null;
58676
- var lastMode = null;
58680
+ var lastSteerAng = null;
58681
+ var lastFlatAng = null;
58677
58682
 
58678
58683
  function angleDiff(a, b) {
58679
58684
  var diff = (a - b) % 360;
@@ -58690,28 +58695,18 @@ RETURNS obj for chaining
58690
58695
  }
58691
58696
 
58692
58697
  function updateTurn() {
58693
- if (startDeviceAngle === null || currentDeviceAngle === null) return;
58694
-
58695
- var orient = 0;
58696
- if (typeof WW != "undefined") {
58697
- if (WW.screen && WW.screen.orientation && WW.screen.orientation.angle != null) {
58698
- orient = WW.screen.orientation.angle;
58699
- } else if (typeof WW.orientation != "undefined") {
58700
- orient = WW.orientation;
58701
- }
58702
- }
58698
+ if (currentDeviceAngle === null) return;
58703
58699
 
58704
58700
  var startRot = obj._turnStartRot != null ? obj._turnStartRot : 0;
58705
58701
  var targetRot = obj.rotation;
58706
58702
 
58707
58703
  if (mode == "relative") {
58708
- var diff = angleDiff(currentDeviceAngle, startDeviceAngle);
58704
+ var diff = currentDeviceAngle - (startDeviceAngle || 0);
58709
58705
  targetRot = startRot - diff * factor;
58710
58706
  } else if (mode == "absolute") {
58711
- var absHeading = (currentDeviceAngle - orient + 360) % 360;
58712
- targetRot = startRot - absHeading * factor;
58707
+ targetRot = startRot - currentDeviceAngle * factor;
58713
58708
  } else if (mode == "continuous") {
58714
- var cDiff = angleDiff(currentDeviceAngle, startDeviceAngle);
58709
+ var cDiff = angleDiff(currentDeviceAngle, startDeviceAngle || 0);
58715
58710
  targetRot = obj.rotation - (cDiff * factor * 0.05);
58716
58711
  }
58717
58712
 
@@ -58733,38 +58728,67 @@ RETURNS obj for chaining
58733
58728
  obj._turnStartRot = obj.rotation;
58734
58729
  startDeviceAngle = null;
58735
58730
  currentDeviceAngle = null;
58736
- lastMode = null;
58731
+ lastSteerAng = null;
58732
+ lastFlatAng = null;
58737
58733
  if (dampRot) dampRot.immediate(obj.rotation);
58738
58734
 
58739
58735
  obj._turnType = type;
58740
58736
  obj._turnListener = function(e) {
58737
+ var orient = 0;
58738
+ if (typeof WW != "undefined") {
58739
+ if (WW.screen && WW.screen.orientation && WW.screen.orientation.angle != null) {
58740
+ orient = WW.screen.orientation.angle;
58741
+ } else if (typeof WW.orientation != "undefined") {
58742
+ orient = WW.orientation;
58743
+ }
58744
+ }
58745
+
58741
58746
  var beta = e.rotation ? e.rotation.x : (e.beta || 0);
58742
58747
  var gamma = e.rotation ? e.rotation.y : (e.gamma || 0);
58743
- var alpha = e.rotation && e.rotation.z != null ? e.rotation.z : (e.alpha != null ? (360 - e.alpha) : 0);
58748
+ var alpha = e.rotation && e.rotation.z != null ? e.rotation.z : (e.alpha != null ? (360 - e.alpha) % 360 : 0);
58749
+
58750
+ var tiltX, tiltY;
58751
+ if (orient == 90) {
58752
+ tiltX = beta;
58753
+ tiltY = -gamma;
58754
+ } else if (orient == -90 || orient == 270) {
58755
+ tiltX = -beta;
58756
+ tiltY = gamma;
58757
+ } else if (orient == 180) {
58758
+ tiltX = -gamma;
58759
+ tiltY = -beta;
58760
+ } else {
58761
+ tiltX = gamma;
58762
+ tiltY = beta;
58763
+ }
58744
58764
 
58745
- var isUpright = Math.abs(beta) > 20 || Math.abs(gamma) > 20;
58746
- var curMode = isUpright ? "upright" : "flat";
58747
- var ang;
58765
+ var mag = Math.sqrt(tiltX * tiltX + tiltY * tiltY);
58766
+ var steerAng = Math.atan2(tiltX, tiltY) * (180 / Math.PI);
58767
+ var flatAng = (alpha - orient + 360) % 360;
58748
58768
 
58749
- if (isUpright) {
58750
- var radB = beta * (Math.PI / 180);
58751
- var radG = gamma * (Math.PI / 180);
58752
- ang = Math.atan2(Math.sin(radG), Math.cos(radG) * Math.sin(radB)) * (180 / Math.PI);
58769
+ if (mode == "absolute") {
58770
+ currentDeviceAngle = flatAng;
58771
+ if (startDeviceAngle === null) startDeviceAngle = flatAng;
58753
58772
  } else {
58754
- ang = alpha;
58755
- }
58773
+ if (currentDeviceAngle === null) {
58774
+ startDeviceAngle = 0;
58775
+ currentDeviceAngle = 0;
58776
+ lastSteerAng = steerAng;
58777
+ lastFlatAng = flatAng;
58778
+ if (dampRot) dampRot.immediate(obj.rotation);
58779
+ } else {
58780
+ var dSteer = angleDiff(steerAng, lastSteerAng);
58781
+ var dFlat = angleDiff(flatAng, lastFlatAng);
58756
58782
 
58757
- if (startDeviceAngle === null) {
58758
- startDeviceAngle = ang;
58759
- lastMode = curMode;
58760
- if (dampRot) dampRot.immediate(obj.rotation);
58761
- } else if (lastMode !== null && curMode !== lastMode) {
58762
- var prevDiff = angleDiff(currentDeviceAngle, startDeviceAngle);
58763
- startDeviceAngle = ang - prevDiff;
58764
- lastMode = curMode;
58765
- }
58783
+ // Smoothly blend between flat table (mag <= 20) and upright wheel (mag >= 50)
58784
+ var w = zim.constrain((mag - 20) / 30, 0, 1);
58785
+ var delta = (1 - w) * dFlat + w * dSteer;
58766
58786
 
58767
- currentDeviceAngle = ang;
58787
+ currentDeviceAngle += delta;
58788
+ lastSteerAng = steerAng;
58789
+ lastFlatAng = flatAng;
58790
+ }
58791
+ }
58768
58792
  };
58769
58793
  f.on(type, obj._turnListener);
58770
58794
 
@@ -58859,6 +58883,8 @@ To change buttons of the Dialog set: F.sensorAsk.yes.text = "OUI"; F.sensorAsk.n
58859
58883
 
58860
58884
  ALSO: see noShake() to remove shake listeners.
58861
58885
 
58886
+ SEE: https://zimjs.com/020/shake.html
58887
+
58862
58888
  EXAMPLE
58863
58889
  const color = series(red,green,blue);
58864
58890
  const bottle = new Rectangle(80, 160, purple).centerReg().shake({
@@ -98637,118 +98663,6 @@ END EXAMPLE
98637
98663
  // See Frame for the cursors property
98638
98664
  //-83.867
98639
98665
 
98640
- /*--
98641
- Tilt - device orientation event
98642
-
98643
- Tilt
98644
- "deviceorientation" Frame event
98645
-
98646
- DESCRIPTION
98647
- The Frame has a "deviceorientation" event to capture tilt or device rotation (like a compass)
98648
- and a "devicemotion" event to capture shaking the device with the accelerometer.
98649
-
98650
- SEE: the tilt() method (just after gesture()) for a one line version
98651
-
98652
- SEE: Shake - for the "devicemotion" event for accelerometer motion
98653
-
98654
- SEE: the PermissionAsk() class which will handle asking for permissions on devices.
98655
-
98656
- EXAMPLE
98657
- // SEE ALSO THE TILT METHOD FOR A ONE LINE VERSION
98658
-
98659
- // TILT OR COMPASS - https://zimjs.com/zapp/Z_Q5UYS
98660
- // for capturing tilt on device (rotation about an axis)
98661
- // be on a mobile device or a device with sensors
98662
- new PermissionAsk(yes=>{
98663
- if (yes) { // the user answered yes to PermissionAsk
98664
- // all code goes in here
98665
- new Label("Tilt the device to move circle").alp(.3).pos(0,30,CENTER);
98666
- const circle = new Circle().center();
98667
- const amount = .5;
98668
- const compass = new Line(60, 4, light, "arrow").rot(90).centerReg(circle); // point up
98669
- F.on("deviceorientation", e=>{
98670
- // for tilt
98671
- circle.mov(e.rotation.y*amount, e.rotation.x*amount);
98672
- circle.x = constrain(circle.x, 0, W);
98673
- circle.y = constrain(circle.y, 0, H);
98674
- // for compass use e.rotation.z
98675
- // which has 0 angle at device start rotation (not necessarily North)
98676
- compass.rot(90-e.rotation.z); // rotate opposite device rotation
98677
- S.update();
98678
- });
98679
- } else { // the user answered no to PermissionAsk
98680
- new Pane("SENSOR not available",yellow).show();
98681
- }
98682
- });
98683
- END EXAMPLE
98684
-
98685
- EVENTS
98686
- "deviceorientation" - for tilt
98687
- fired as device orientation changes:
98688
- eventObject.rotation.x (beta in HTML specs) holds rotation about the x axis between -180 and 180 (tipped forward or backward)
98689
- eventObject.rotation.y (gamma in HTML specs) holds rotation about the y axis between -90 and 90 (tipped left or right)
98690
- eventObject.rotation.z (alpha in HTML specs) holds rotation about the z axis 0-360 clockwise (relative to orientation when app loads)
98691
- note rotation.z is 360-alpha compared to the HTML 5 specs
98692
- note also that beta, gamma and alpha from the HTML 5 specs are also provided
98693
- --*///+83.86
98694
- // Device motion and orientation events are available as Frame events
98695
- //-83.86
98696
-
98697
- /*--
98698
- Shake - device motion event
98699
-
98700
- Shake
98701
- "devicemotion" Frame event
98702
-
98703
- DESCRIPTION
98704
- The Frame has a "devicemotion" event to capture shaking the device with the accelerometer.
98705
-
98706
- SEE: Tilt - for the "deviceorientation" event for tilt or compass
98707
-
98708
- SEE: the PermissionAsk() class which will handle asking for permissions on devices.
98709
-
98710
- EXAMPLE
98711
- // SHAKE - https://zimjs.com/zapp/Z_XF8JM
98712
- // for capturing shaking motion on device (acceleration about an axis)
98713
- // be on a mobile device or a device with sensors
98714
- new PermissionAsk(init, "devicemotion");
98715
- function init(yes) {
98716
- if (yes) { // the user answered yes to PermissionAsk
98717
- // all code goes in here
98718
- new Label("Shake the device").center();
98719
- S.update();
98720
- let id;
98721
- F.on("devicemotion", e=>{
98722
- // for shake
98723
- if (
98724
- Math.abs(e.acceleration.x) > 5 ||
98725
- Math.abs(e.acceleration.y) > 5 ||
98726
- Math.abs(e.acceleration.z) > 5
98727
- ) {
98728
- if (F.color != green) {
98729
- F.color = green;
98730
- S.update();
98731
- }
98732
- if (id) id.clear();
98733
- id = timeout(.2, ()=>{
98734
- F.color = light;
98735
- S.update();
98736
- });
98737
- }
98738
- });
98739
- } else { // the user answered no to PermissionAsk
98740
- new Pane("SENSOR not available",yellow).show();
98741
- }
98742
- }
98743
- END EXAMPLE
98744
-
98745
- EVENTS
98746
- "devicemotion" - for shake
98747
- fired on moving mobile device - like a shake - eventObject.acceleration holds x, y and z properties of motion
98748
- --*///+83.87
98749
- // Device motion and orientation events are available as Frame events
98750
- //-83.87
98751
-
98752
98666
  /*--
98753
98667
  zim.PermissionAsk = function(callback, permissionType, color, backgroundColor, style, group, inherit)
98754
98668
 
@@ -98769,6 +98683,9 @@ NOTE: as of ZIM 5.5.0 the zim namespace is no longer required (unless zns is set
98769
98683
 
98770
98684
  NOTE: sensor prompt text can be customized prior to calling with F.sensorText = "Custom message";
98771
98685
 
98686
+ NOTE: as of ZIM 020 tilt(), turn(), and shake() can be applied as methods to DisplayObjects
98687
+ and will call PermissionAsk automatically so would suggest using those methods
98688
+
98772
98689
  EXAMPLE
98773
98690
  // DEVICE ORIENTATION - gives angle of device in all 3 dimensions
98774
98691
  // See https://zimjs.com/zapp/Z_Q5UYS
@@ -98938,8 +98855,18 @@ no - reference to the zim Button with NO
98938
98855
  return;
98939
98856
  }
98940
98857
 
98941
- // Sensors probe: iOS uses PermissionAsk popup; Android/Desktop probes hardware directly
98942
- if (typeof DeviceOrientationEvent != "undefined" && DeviceOrientationEvent && typeof DeviceOrientationEvent.requestPermission == "function") {
98858
+ // Fast check: if sensors not supported in environment (e.g. non-secure HTTP or missing API)
98859
+ var hasOrientAPI = typeof DeviceOrientationEvent != "undefined" && DeviceOrientationEvent;
98860
+ var hasMotionAPI = typeof DeviceMotionEvent != "undefined" && DeviceMotionEvent;
98861
+ if (WW.isSecureContext === false || (pt == "deviceorientation" && !hasOrientAPI) || (pt == "devicemotion" && !hasMotionAPI)) {
98862
+ callback(false, pt == "deviceorientation" ? 1 : 2);
98863
+ return;
98864
+ }
98865
+
98866
+ var isIOS = (M == "ios" || (typeof navigator != "undefined" && (/iPad|iPhone|iPod/.test(navigator.userAgent) || (navigator.platform === "MacIntel" && navigator.maxTouchPoints > 1))));
98867
+
98868
+ // Sensors probe: Only iOS devices with requestPermission require the PermissionAsk popup
98869
+ if (isIOS && typeof DeviceOrientationEvent != "undefined" && DeviceOrientationEvent && typeof DeviceOrientationEvent.requestPermission == "function") {
98943
98870
  setPane();
98944
98871
  } else {
98945
98872
  var tested = false;
@@ -98948,7 +98875,7 @@ no - reference to the zim Button with NO
98948
98875
  tested = true;
98949
98876
  WW.removeEventListener(permissionType, testMe);
98950
98877
  callback(false, 3); // 3 - timeout / no sensor hardware (e.g. desktop)
98951
- }, 1000);
98878
+ }, 500);
98952
98879
 
98953
98880
  function testMe(e) {
98954
98881
  if (tested || okay) return;
@@ -98983,13 +98910,9 @@ no - reference to the zim Button with NO
98983
98910
  WW.addEventListener(permissionType, testMe);
98984
98911
  }
98985
98912
 
98986
- var lastZ = 0;
98987
- var flip = 0;
98988
98913
  function deviceorientationEvent(e) {
98989
- var z = 360-e.alpha;
98990
- if (Math.abs(z-lastZ) > 180 - 45 && Math.abs(z-lastZ) < 180 + 45) flip = flip == 0 ? 180 : 0;
98991
- lastZ = z;
98992
- e.rotation = {x:e.beta, y:e.gamma, z:(z + flip) % 360};
98914
+ var z = e.alpha != null ? (360 - e.alpha) % 360 : 0;
98915
+ e.rotation = {x:e.beta || 0, y:e.gamma || 0, z:z};
98993
98916
  frame.dispatchEvent(e);
98994
98917
  }
98995
98918
  function devicemotionEvent(e) {
@@ -108029,4 +107952,4 @@ export let Ticker = zim.Ticker;
108029
107952
  export let Style = zim.Style;
108030
107953
  export let assets = zim.assets;
108031
107954
  export let assetIDs = zim.assetIDs;
108032
- export let ZIMON = zim.ZIMON;
107955
+ export let ZIMON = zim.ZIMON;