zimjs 20.0.7 → 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.
- package/package.json +1 -1
- package/src/zim.js +394 -440
package/package.json
CHANGED
package/src/zim.js
CHANGED
|
@@ -58287,10 +58287,15 @@ Automatically manages user sensor permission via ZIM PermissionAsk if permission
|
|
|
58287
58287
|
NOTE: If permission has already been granted, tilt activates immediately.
|
|
58288
58288
|
If multiple objects call tilt(), turn(), or shake() before permission is granted, they are queued and activated together once the user accepts.
|
|
58289
58289
|
|
|
58290
|
+
NOTE: To change the text of the Dialog set: F.sensorText = "OKAY?"; // BEFORE calling the tilt()
|
|
58291
|
+
To change buttons of the Dialog set: F.sensorAsk.yes.text = "OUI"; F.sensorAsk.no.text = "NON"; // AFTER calling the tilt()
|
|
58292
|
+
|
|
58290
58293
|
ALSO: see noTilt() to remove tilt behaviors.
|
|
58291
58294
|
|
|
58292
58295
|
ALSO: see tiltBoundary() to dynamically change or clear the boundary rectangle.
|
|
58293
58296
|
|
|
58297
|
+
SEE: https://zimjs.com/020/tilt.html
|
|
58298
|
+
|
|
58294
58299
|
EXAMPLE
|
|
58295
58300
|
// Basic rolling ball constrained to stage bounds
|
|
58296
58301
|
const ball = new Circle(30, red)
|
|
@@ -58323,135 +58328,135 @@ ready - (default null) callback function called when permission resolves, receiv
|
|
|
58323
58328
|
|
|
58324
58329
|
RETURNS obj for chaining
|
|
58325
58330
|
--*///+34.9
|
|
58326
|
-
zim.tilt = function(obj, boundary, damp, factor, mode, type, ready) {
|
|
58327
|
-
|
|
58328
|
-
|
|
58329
|
-
|
|
58330
|
-
|
|
58331
|
+
zim.tilt = function(obj, boundary, damp, factor, mode, type, ready) {
|
|
58332
|
+
var sig = "obj, boundary, damp, factor, mode, type, ready";
|
|
58333
|
+
var duo; if (duo = zob(zim.tilt, arguments, sig)) return duo;
|
|
58334
|
+
if ((obj.type=="AC"||(obj.type=="Emoji"&&obj.svg&&obj.svg.type=="AC"))&&WW.zdf) {WW.zdf.ac("tilt", arguments); return obj;}
|
|
58335
|
+
z_d("34.9");
|
|
58331
58336
|
|
|
58332
|
-
|
|
58333
|
-
|
|
58334
|
-
|
|
58337
|
+
// MONITOR
|
|
58338
|
+
var mID = "z~tilt";
|
|
58339
|
+
if (obj && obj.mID && ((obj.mID[2] && obj.mID[2]=="-") || obj.mID[0]=="-")) mID = "z~-";
|
|
58335
58340
|
|
|
58336
|
-
|
|
58341
|
+
if (zot(obj)) return;
|
|
58337
58342
|
|
|
58338
|
-
|
|
58339
|
-
|
|
58340
|
-
|
|
58341
|
-
var f = obj.frame || (typeof zdf != "undefined" ? zdf : WW.zdf);
|
|
58342
|
-
var s = obj.stage || (f ? f.stage : null);
|
|
58343
|
-
|
|
58344
|
-
// Set initial boundary
|
|
58345
|
-
zim.tiltBoundary(obj, boundary);
|
|
58346
|
-
|
|
58347
|
-
if (zot(damp)) damp = 0.05;
|
|
58348
|
-
if (zot(factor)) factor = 0.5;
|
|
58349
|
-
var factorX = typeof factor === "object" ? factor.x : factor;
|
|
58350
|
-
var factorY = typeof factor === "object" ? factor.y : factor;
|
|
58351
|
-
if (zot(mode)) mode = "move";
|
|
58352
|
-
if (zot(type)) type = "deviceorientation";
|
|
58353
|
-
|
|
58354
|
-
var dampX = damp ? new zim.Damp(mode == "position" ? obj.x : 0, damp) : null;
|
|
58355
|
-
var dampY = damp ? new zim.Damp(mode == "position" ? obj.y : 0, damp) : null;
|
|
58356
|
-
var currentRot = {x: 0, y: 0, z: 0};
|
|
58357
|
-
|
|
58358
|
-
function updateTilt() {
|
|
58359
|
-
var orient = 0;
|
|
58360
|
-
if (typeof WW != "undefined") {
|
|
58361
|
-
if (WW.screen && WW.screen.orientation && WW.screen.orientation.angle != null) {
|
|
58362
|
-
orient = WW.screen.orientation.angle;
|
|
58363
|
-
} else if (typeof WW.orientation != "undefined") {
|
|
58364
|
-
orient = WW.orientation;
|
|
58365
|
-
}
|
|
58366
|
-
}
|
|
58367
|
-
|
|
58368
|
-
var beta = currentRot.x;
|
|
58369
|
-
var gamma = currentRot.y;
|
|
58370
|
-
var tiltX, tiltY;
|
|
58371
|
-
|
|
58372
|
-
if (orient == 90) {
|
|
58373
|
-
tiltX = beta;
|
|
58374
|
-
tiltY = -gamma;
|
|
58375
|
-
} else if (orient == -90 || orient == 270) {
|
|
58376
|
-
tiltX = -beta;
|
|
58377
|
-
tiltY = gamma;
|
|
58378
|
-
} else if (orient == 180) {
|
|
58379
|
-
tiltX = -gamma;
|
|
58380
|
-
tiltY = -beta;
|
|
58381
|
-
} else {
|
|
58382
|
-
tiltX = gamma;
|
|
58383
|
-
tiltY = beta;
|
|
58384
|
-
}
|
|
58343
|
+
// Clean up any existing tilt on this object first
|
|
58344
|
+
if (obj._tiltTicker) zim.noTilt(obj);
|
|
58385
58345
|
|
|
58386
|
-
|
|
58387
|
-
|
|
58388
|
-
var rotY = dampY ? dampY.convert(tiltY) : tiltY;
|
|
58389
|
-
obj.x += rotX * factorX;
|
|
58390
|
-
obj.y += rotY * factorY;
|
|
58391
|
-
} else if (mode == "position") {
|
|
58392
|
-
var targetX = (obj._tiltStartX != null ? obj._tiltStartX : obj.x) + tiltX * factorX * 10;
|
|
58393
|
-
var targetY = (obj._tiltStartY != null ? obj._tiltStartY : obj.y) + tiltY * factorY * 10;
|
|
58394
|
-
obj.x = dampX ? dampX.convert(targetX) : targetX;
|
|
58395
|
-
obj.y = dampY ? dampY.convert(targetY) : targetY;
|
|
58396
|
-
}
|
|
58346
|
+
var f = obj.frame || (typeof zdf != "undefined" ? zdf : WW.zdf);
|
|
58347
|
+
var s = obj.stage || (f ? f.stage : null);
|
|
58397
58348
|
|
|
58398
|
-
|
|
58399
|
-
|
|
58400
|
-
obj.y = zim.constrain(obj.y, obj._tiltBoundary.y, obj._tiltBoundary.y + obj._tiltBoundary.height);
|
|
58401
|
-
}
|
|
58349
|
+
// Set initial boundary
|
|
58350
|
+
zim.tiltBoundary(obj, boundary);
|
|
58402
58351
|
|
|
58403
|
-
if (
|
|
58404
|
-
|
|
58352
|
+
if (zot(damp)) damp = 0.05;
|
|
58353
|
+
if (zot(factor)) factor = 0.5;
|
|
58354
|
+
var factorX = typeof factor === "object" ? factor.x : factor;
|
|
58355
|
+
var factorY = typeof factor === "object" ? factor.y : factor;
|
|
58356
|
+
if (zot(mode)) mode = "move";
|
|
58357
|
+
if (zot(type)) type = "deviceorientation";
|
|
58405
58358
|
|
|
58406
|
-
|
|
58407
|
-
|
|
58408
|
-
|
|
58409
|
-
|
|
58410
|
-
|
|
58411
|
-
|
|
58412
|
-
|
|
58413
|
-
|
|
58414
|
-
|
|
58415
|
-
|
|
58416
|
-
|
|
58417
|
-
|
|
58418
|
-
|
|
58419
|
-
|
|
58359
|
+
var dampX = damp ? new zim.Damp(mode == "position" ? obj.x : 0, damp) : null;
|
|
58360
|
+
var dampY = damp ? new zim.Damp(mode == "position" ? obj.y : 0, damp) : null;
|
|
58361
|
+
var currentRot = {x: 0, y: 0, z: 0};
|
|
58362
|
+
|
|
58363
|
+
function updateTilt() {
|
|
58364
|
+
var orient = 0;
|
|
58365
|
+
if (typeof WW != "undefined") {
|
|
58366
|
+
if (WW.screen && WW.screen.orientation && WW.screen.orientation.angle != null) {
|
|
58367
|
+
orient = WW.screen.orientation.angle;
|
|
58368
|
+
} else if (typeof WW.orientation != "undefined") {
|
|
58369
|
+
orient = WW.orientation;
|
|
58370
|
+
}
|
|
58371
|
+
}
|
|
58372
|
+
|
|
58373
|
+
var beta = currentRot.x;
|
|
58374
|
+
var gamma = currentRot.y;
|
|
58375
|
+
var tiltX, tiltY;
|
|
58376
|
+
|
|
58377
|
+
if (orient == 90) {
|
|
58378
|
+
tiltX = beta;
|
|
58379
|
+
tiltY = -gamma;
|
|
58380
|
+
} else if (orient == -90 || orient == 270) {
|
|
58381
|
+
tiltX = -beta;
|
|
58382
|
+
tiltY = gamma;
|
|
58383
|
+
} else if (orient == 180) {
|
|
58384
|
+
tiltX = -gamma;
|
|
58385
|
+
tiltY = -beta;
|
|
58420
58386
|
} else {
|
|
58421
|
-
|
|
58422
|
-
|
|
58423
|
-
currentRot.z = e.alpha || 0;
|
|
58387
|
+
tiltX = gamma;
|
|
58388
|
+
tiltY = beta;
|
|
58424
58389
|
}
|
|
58425
|
-
};
|
|
58426
|
-
f.on(type, obj._tiltListener);
|
|
58427
58390
|
|
|
58428
|
-
|
|
58429
|
-
|
|
58430
|
-
|
|
58391
|
+
if (mode == "move") {
|
|
58392
|
+
var rotX = dampX ? dampX.convert(tiltX) : tiltX;
|
|
58393
|
+
var rotY = dampY ? dampY.convert(tiltY) : tiltY;
|
|
58394
|
+
obj.x += rotX * factorX;
|
|
58395
|
+
obj.y += rotY * factorY;
|
|
58396
|
+
} else if (mode == "position") {
|
|
58397
|
+
var targetX = (obj._tiltStartX != null ? obj._tiltStartX : obj.x) + tiltX * factorX * 10;
|
|
58398
|
+
var targetY = (obj._tiltStartY != null ? obj._tiltStartY : obj.y) + tiltY * factorY * 10;
|
|
58399
|
+
obj.x = dampX ? dampX.convert(targetX) : targetX;
|
|
58400
|
+
obj.y = dampY ? dampY.convert(targetY) : targetY;
|
|
58401
|
+
}
|
|
58431
58402
|
|
|
58432
|
-
|
|
58433
|
-
|
|
58434
|
-
|
|
58435
|
-
|
|
58436
|
-
f._permissionQueue.push(activate);
|
|
58437
|
-
} else {
|
|
58438
|
-
f._permissionPending = true;
|
|
58439
|
-
f._permissionQueue = [activate];
|
|
58403
|
+
if (obj._tiltBoundary) {
|
|
58404
|
+
obj.x = zim.constrain(obj.x, obj._tiltBoundary.x, obj._tiltBoundary.x + obj._tiltBoundary.width);
|
|
58405
|
+
obj.y = zim.constrain(obj.y, obj._tiltBoundary.y, obj._tiltBoundary.y + obj._tiltBoundary.height);
|
|
58406
|
+
}
|
|
58440
58407
|
|
|
58441
|
-
|
|
58442
|
-
|
|
58443
|
-
|
|
58444
|
-
|
|
58445
|
-
|
|
58408
|
+
if (s) s.update();
|
|
58409
|
+
}
|
|
58410
|
+
|
|
58411
|
+
function activate() {
|
|
58412
|
+
obj._tiltStartX = obj.x;
|
|
58413
|
+
obj._tiltStartY = obj.y;
|
|
58414
|
+
if (mode == "position" && dampX && dampY) {
|
|
58415
|
+
dampX.immediate(obj.x);
|
|
58416
|
+
dampY.immediate(obj.y);
|
|
58417
|
+
}
|
|
58418
|
+
|
|
58419
|
+
obj._tiltType = type;
|
|
58420
|
+
obj._tiltListener = function(e) {
|
|
58421
|
+
if (e.rotation) {
|
|
58422
|
+
currentRot.x = e.rotation.x || 0;
|
|
58423
|
+
currentRot.y = e.rotation.y || 0;
|
|
58424
|
+
currentRot.z = e.rotation.z || 0;
|
|
58425
|
+
} else {
|
|
58426
|
+
currentRot.x = e.beta || 0;
|
|
58427
|
+
currentRot.y = e.gamma || 0;
|
|
58428
|
+
currentRot.z = e.alpha || 0;
|
|
58429
|
+
}
|
|
58430
|
+
};
|
|
58431
|
+
f.on(type, obj._tiltListener);
|
|
58432
|
+
|
|
58433
|
+
obj._tiltTicker = zim.Ticker.add(updateTilt);
|
|
58434
|
+
if (ready) ready(true, obj);
|
|
58435
|
+
}
|
|
58436
|
+
|
|
58437
|
+
// Shared Permission check & queueing
|
|
58438
|
+
if (f._permissionGranted) {
|
|
58439
|
+
activate();
|
|
58440
|
+
} else if (f._permissionPending) {
|
|
58441
|
+
f._permissionQueue.push({ activate: activate, reject: function() { if (ready) ready(false, obj); } });
|
|
58442
|
+
} else {
|
|
58443
|
+
f._permissionPending = true;
|
|
58444
|
+
f._permissionQueue = [{ activate: activate, reject: function() { if (ready) ready(false, obj); } }];
|
|
58445
|
+
|
|
58446
|
+
new zim.PermissionAsk(function(yes) {
|
|
58447
|
+
f._permissionPending = false;
|
|
58448
|
+
if (yes) {
|
|
58449
|
+
f._permissionGranted = true;
|
|
58450
|
+
zim.loop(f._permissionQueue, function(item) { item.activate(); });
|
|
58451
|
+
} else {
|
|
58452
|
+
zim.loop(f._permissionQueue, function(item) { item.reject(); });
|
|
58453
|
+
}
|
|
58446
58454
|
f._permissionQueue = [];
|
|
58447
|
-
}
|
|
58448
|
-
|
|
58449
|
-
}
|
|
58450
|
-
}, type);
|
|
58451
|
-
}
|
|
58455
|
+
}, type);
|
|
58456
|
+
}
|
|
58452
58457
|
|
|
58453
|
-
|
|
58454
|
-
};//-34.9
|
|
58458
|
+
return obj;
|
|
58459
|
+
};//-34.9
|
|
58455
58460
|
|
|
58456
58461
|
|
|
58457
58462
|
/*--
|
|
@@ -58472,33 +58477,31 @@ END EXAMPLE
|
|
|
58472
58477
|
|
|
58473
58478
|
RETURNS obj for chaining
|
|
58474
58479
|
--*///+34.91
|
|
58475
|
-
zim.noTilt = function(obj) {
|
|
58476
|
-
|
|
58477
|
-
|
|
58478
|
-
|
|
58479
|
-
|
|
58480
|
-
if (zot(obj) || !obj.on || !obj.zimTouch) return;
|
|
58480
|
+
zim.noTilt = function(obj) {
|
|
58481
|
+
var sig = "obj";
|
|
58482
|
+
var duo; if (duo = zob(zim.noTilt, arguments, sig)) return duo;
|
|
58483
|
+
z_d("34.91");
|
|
58481
58484
|
|
|
58482
|
-
|
|
58483
|
-
var mID = "z~noTilt";
|
|
58484
|
-
if (obj && obj.mID && ((obj.mID[2] && obj.mID[2]=="-") || obj.mID[0]=="-")) mID = "z~-";
|
|
58485
|
-
|
|
58486
|
-
if (zot(obj)) return;
|
|
58487
|
-
var f = obj.frame || (typeof zdf != "undefined" ? zdf : WW.zdf);
|
|
58485
|
+
if (zot(obj) || !obj.on || !obj.zimTouch) return;
|
|
58488
58486
|
|
|
58489
|
-
|
|
58490
|
-
|
|
58491
|
-
obj.
|
|
58492
|
-
|
|
58493
|
-
|
|
58494
|
-
zim.Ticker.remove(obj._tiltTicker);
|
|
58495
|
-
obj._tiltTicker = null;
|
|
58496
|
-
}
|
|
58497
|
-
obj._tiltBoundary = null;
|
|
58487
|
+
// MONITOR
|
|
58488
|
+
var mID = "z~noTilt";
|
|
58489
|
+
if (obj && obj.mID && ((obj.mID[2] && obj.mID[2]=="-") || obj.mID[0]=="-")) mID = "z~-";
|
|
58490
|
+
|
|
58491
|
+
var f = obj.frame || (typeof zdf != "undefined" ? zdf : WW.zdf);
|
|
58498
58492
|
|
|
58499
|
-
|
|
58493
|
+
if (obj._tiltListener && f) {
|
|
58494
|
+
f.off(obj._tiltType || "deviceorientation", obj._tiltListener);
|
|
58495
|
+
obj._tiltListener = null;
|
|
58496
|
+
}
|
|
58497
|
+
if (obj._tiltTicker) {
|
|
58498
|
+
zim.Ticker.remove(obj._tiltTicker);
|
|
58499
|
+
obj._tiltTicker = null;
|
|
58500
|
+
}
|
|
58501
|
+
obj._tiltBoundary = null;
|
|
58500
58502
|
|
|
58501
|
-
|
|
58503
|
+
return obj;
|
|
58504
|
+
};//-34.91
|
|
58502
58505
|
|
|
58503
58506
|
|
|
58504
58507
|
/*--
|
|
@@ -58610,8 +58613,13 @@ NOTE: If permission has already been granted, turn activates immediately.
|
|
|
58610
58613
|
If multiple objects call tilt(), turn(), or shake() before permission is granted, they are queued and activated together once the user accepts.
|
|
58611
58614
|
Rotation is applied relative to the object's initial rotation when turn() is called (for example, an arrow rotated -90 to point up will maintain that -90 offset).
|
|
58612
58615
|
|
|
58616
|
+
NOTE: To change the text of the Dialog set: F.sensorText = "OKAY?"; // BEFORE calling the turn()
|
|
58617
|
+
To change buttons of the Dialog set: F.sensorAsk.yes.text = "OUI"; F.sensorAsk.no.text = "NON"; // AFTER calling the turn()
|
|
58618
|
+
|
|
58613
58619
|
ALSO: see noTurn() to remove turning behaviors.
|
|
58614
58620
|
|
|
58621
|
+
SEE: https://zimjs.com/020/turn.html
|
|
58622
|
+
|
|
58615
58623
|
EXAMPLE
|
|
58616
58624
|
// Point an arrow with device heading (stays pointing up in world space)
|
|
58617
58625
|
const arrow = new Arrow({type:"thick"}).sca(4).rot(-90).centerReg().turn();
|
|
@@ -58669,7 +58677,8 @@ RETURNS obj for chaining
|
|
|
58669
58677
|
var dampRot = damp ? new zim.Damp(obj.rotation, damp) : null;
|
|
58670
58678
|
var startDeviceAngle = null;
|
|
58671
58679
|
var currentDeviceAngle = null;
|
|
58672
|
-
var
|
|
58680
|
+
var lastSteerAng = null;
|
|
58681
|
+
var lastFlatAng = null;
|
|
58673
58682
|
|
|
58674
58683
|
function angleDiff(a, b) {
|
|
58675
58684
|
var diff = (a - b) % 360;
|
|
@@ -58686,28 +58695,18 @@ RETURNS obj for chaining
|
|
|
58686
58695
|
}
|
|
58687
58696
|
|
|
58688
58697
|
function updateTurn() {
|
|
58689
|
-
if (
|
|
58690
|
-
|
|
58691
|
-
var orient = 0;
|
|
58692
|
-
if (typeof WW != "undefined") {
|
|
58693
|
-
if (WW.screen && WW.screen.orientation && WW.screen.orientation.angle != null) {
|
|
58694
|
-
orient = WW.screen.orientation.angle;
|
|
58695
|
-
} else if (typeof WW.orientation != "undefined") {
|
|
58696
|
-
orient = WW.orientation;
|
|
58697
|
-
}
|
|
58698
|
-
}
|
|
58698
|
+
if (currentDeviceAngle === null) return;
|
|
58699
58699
|
|
|
58700
58700
|
var startRot = obj._turnStartRot != null ? obj._turnStartRot : 0;
|
|
58701
58701
|
var targetRot = obj.rotation;
|
|
58702
58702
|
|
|
58703
58703
|
if (mode == "relative") {
|
|
58704
|
-
var diff =
|
|
58704
|
+
var diff = currentDeviceAngle - (startDeviceAngle || 0);
|
|
58705
58705
|
targetRot = startRot - diff * factor;
|
|
58706
58706
|
} else if (mode == "absolute") {
|
|
58707
|
-
|
|
58708
|
-
targetRot = startRot - absHeading * factor;
|
|
58707
|
+
targetRot = startRot - currentDeviceAngle * factor;
|
|
58709
58708
|
} else if (mode == "continuous") {
|
|
58710
|
-
var cDiff = angleDiff(currentDeviceAngle, startDeviceAngle);
|
|
58709
|
+
var cDiff = angleDiff(currentDeviceAngle, startDeviceAngle || 0);
|
|
58711
58710
|
targetRot = obj.rotation - (cDiff * factor * 0.05);
|
|
58712
58711
|
}
|
|
58713
58712
|
|
|
@@ -58729,39 +58728,67 @@ RETURNS obj for chaining
|
|
|
58729
58728
|
obj._turnStartRot = obj.rotation;
|
|
58730
58729
|
startDeviceAngle = null;
|
|
58731
58730
|
currentDeviceAngle = null;
|
|
58732
|
-
|
|
58731
|
+
lastSteerAng = null;
|
|
58732
|
+
lastFlatAng = null;
|
|
58733
58733
|
if (dampRot) dampRot.immediate(obj.rotation);
|
|
58734
58734
|
|
|
58735
58735
|
obj._turnType = type;
|
|
58736
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
|
+
|
|
58737
58746
|
var beta = e.rotation ? e.rotation.x : (e.beta || 0);
|
|
58738
58747
|
var gamma = e.rotation ? e.rotation.y : (e.gamma || 0);
|
|
58739
|
-
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);
|
|
58740
58749
|
|
|
58741
|
-
var
|
|
58742
|
-
|
|
58743
|
-
|
|
58744
|
-
|
|
58745
|
-
if (
|
|
58746
|
-
|
|
58747
|
-
|
|
58748
|
-
|
|
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;
|
|
58749
58760
|
} else {
|
|
58750
|
-
|
|
58761
|
+
tiltX = gamma;
|
|
58762
|
+
tiltY = beta;
|
|
58751
58763
|
}
|
|
58752
58764
|
|
|
58753
|
-
|
|
58754
|
-
|
|
58755
|
-
|
|
58756
|
-
if (dampRot) dampRot.immediate(obj.rotation);
|
|
58757
|
-
} else if (lastMode !== null && curMode !== lastMode) {
|
|
58758
|
-
// Seamless transition between upright and flat
|
|
58759
|
-
var prevDiff = angleDiff(currentDeviceAngle, startDeviceAngle);
|
|
58760
|
-
startDeviceAngle = ang - prevDiff;
|
|
58761
|
-
lastMode = curMode;
|
|
58762
|
-
}
|
|
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;
|
|
58763
58768
|
|
|
58764
|
-
|
|
58769
|
+
if (mode == "absolute") {
|
|
58770
|
+
currentDeviceAngle = flatAng;
|
|
58771
|
+
if (startDeviceAngle === null) startDeviceAngle = flatAng;
|
|
58772
|
+
} else {
|
|
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);
|
|
58782
|
+
|
|
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;
|
|
58786
|
+
|
|
58787
|
+
currentDeviceAngle += delta;
|
|
58788
|
+
lastSteerAng = steerAng;
|
|
58789
|
+
lastFlatAng = flatAng;
|
|
58790
|
+
}
|
|
58791
|
+
}
|
|
58765
58792
|
};
|
|
58766
58793
|
f.on(type, obj._turnListener);
|
|
58767
58794
|
|
|
@@ -58769,24 +58796,24 @@ RETURNS obj for chaining
|
|
|
58769
58796
|
if (ready) ready(true, obj);
|
|
58770
58797
|
}
|
|
58771
58798
|
|
|
58772
|
-
//
|
|
58799
|
+
// Shared Permission check & queueing
|
|
58773
58800
|
if (f._permissionGranted) {
|
|
58774
58801
|
activate();
|
|
58775
58802
|
} else if (f._permissionPending) {
|
|
58776
|
-
f._permissionQueue.push(activate);
|
|
58803
|
+
f._permissionQueue.push({ activate: activate, reject: function() { if (ready) ready(false, obj); } });
|
|
58777
58804
|
} else {
|
|
58778
58805
|
f._permissionPending = true;
|
|
58779
|
-
f._permissionQueue = [activate];
|
|
58806
|
+
f._permissionQueue = [{ activate: activate, reject: function() { if (ready) ready(false, obj); } }];
|
|
58780
58807
|
|
|
58781
58808
|
new zim.PermissionAsk(function(yes) {
|
|
58782
58809
|
f._permissionPending = false;
|
|
58783
58810
|
if (yes) {
|
|
58784
58811
|
f._permissionGranted = true;
|
|
58785
|
-
zim.loop(f._permissionQueue, function(
|
|
58786
|
-
f._permissionQueue = [];
|
|
58812
|
+
zim.loop(f._permissionQueue, function(item) { item.activate(); });
|
|
58787
58813
|
} else {
|
|
58788
|
-
|
|
58814
|
+
zim.loop(f._permissionQueue, function(item) { item.reject(); });
|
|
58789
58815
|
}
|
|
58816
|
+
f._permissionQueue = [];
|
|
58790
58817
|
}, type);
|
|
58791
58818
|
}
|
|
58792
58819
|
|
|
@@ -58851,8 +58878,13 @@ Automatically manages user sensor permission via ZIM PermissionAsk if permission
|
|
|
58851
58878
|
NOTE: If permission has already been granted, shake activates immediately.
|
|
58852
58879
|
If multiple objects call tilt(), turn(), or shake() before permission is granted, they are queued and activated together once the user accepts.
|
|
58853
58880
|
|
|
58881
|
+
NOTE: To change the text of the Dialog set: F.sensorText = "OKAY?"; // BEFORE calling the shake()
|
|
58882
|
+
To change buttons of the Dialog set: F.sensorAsk.yes.text = "OUI"; F.sensorAsk.no.text = "NON"; // AFTER calling the shake()
|
|
58883
|
+
|
|
58854
58884
|
ALSO: see noShake() to remove shake listeners.
|
|
58855
58885
|
|
|
58886
|
+
SEE: https://zimjs.com/020/shake.html
|
|
58887
|
+
|
|
58856
58888
|
EXAMPLE
|
|
58857
58889
|
const color = series(red,green,blue);
|
|
58858
58890
|
const bottle = new Rectangle(80, 160, purple).centerReg().shake({
|
|
@@ -58897,137 +58929,137 @@ Dispatches a "shakestop" event on obj when shaking stops.
|
|
|
58897
58929
|
|
|
58898
58930
|
RETURNS obj for chaining
|
|
58899
58931
|
--*///+34.95
|
|
58900
|
-
zim.shake = function(obj, call, stopCall, threshold, wait, factor, type, ready) {
|
|
58901
|
-
|
|
58902
|
-
|
|
58903
|
-
|
|
58904
|
-
|
|
58932
|
+
zim.shake = function(obj, call, stopCall, threshold, wait, factor, type, ready) {
|
|
58933
|
+
var sig = "obj, call, stopCall, threshold, wait, factor, type, ready";
|
|
58934
|
+
var duo; if (duo = zob(zim.shake, arguments, sig)) return duo;
|
|
58935
|
+
if ((obj.type=="AC"||(obj.type=="Emoji"&&obj.svg&&obj.svg.type=="AC"))&&WW.zdf) {WW.zdf.ac("shake", arguments); return obj;}
|
|
58936
|
+
z_d("34.95");
|
|
58905
58937
|
|
|
58906
|
-
|
|
58907
|
-
|
|
58908
|
-
|
|
58938
|
+
// MONITOR
|
|
58939
|
+
var mID = "z~shake";
|
|
58940
|
+
if (obj && obj.mID && ((obj.mID[2] && obj.mID[2]=="-") || obj.mID[0]=="-")) mID = "z~-";
|
|
58909
58941
|
|
|
58910
|
-
|
|
58942
|
+
if (zot(obj)) return;
|
|
58911
58943
|
|
|
58912
|
-
|
|
58913
|
-
|
|
58944
|
+
// Clean up any existing shake on this object first
|
|
58945
|
+
if (obj._shakeListener) zim.noShake(obj);
|
|
58914
58946
|
|
|
58915
|
-
|
|
58916
|
-
|
|
58947
|
+
var f = obj.frame || (typeof zdf != "undefined" ? zdf : WW.zdf);
|
|
58948
|
+
var s = obj.stage || (f ? f.stage : null);
|
|
58917
58949
|
|
|
58918
|
-
|
|
58919
|
-
|
|
58920
|
-
|
|
58921
|
-
|
|
58950
|
+
if (zot(threshold)) threshold = 10;
|
|
58951
|
+
if (zot(wait)) wait = 0.5;
|
|
58952
|
+
if (zot(factor)) factor = 1;
|
|
58953
|
+
if (zot(type)) type = "devicemotion";
|
|
58922
58954
|
|
|
58923
|
-
|
|
58924
|
-
|
|
58955
|
+
var lastX = 0, lastY = 0, lastZ = 0;
|
|
58956
|
+
var lastTime = 0;
|
|
58925
58957
|
|
|
58926
|
-
|
|
58927
|
-
|
|
58928
|
-
|
|
58958
|
+
function handleMotion(e) {
|
|
58959
|
+
var acc = e.acceleration && (e.acceleration.x != null || e.acceleration.y != null) ?
|
|
58960
|
+
e.acceleration : (e.accelerationIncludingGravity || e);
|
|
58929
58961
|
|
|
58930
|
-
|
|
58931
|
-
|
|
58932
|
-
|
|
58962
|
+
var ax = acc.x || 0;
|
|
58963
|
+
var ay = acc.y || 0;
|
|
58964
|
+
var az = acc.z || 0;
|
|
58933
58965
|
|
|
58934
|
-
|
|
58935
|
-
|
|
58936
|
-
|
|
58966
|
+
var deltaX = ax - lastX;
|
|
58967
|
+
var deltaY = ay - lastY;
|
|
58968
|
+
var deltaZ = az - lastZ;
|
|
58937
58969
|
|
|
58938
|
-
|
|
58939
|
-
|
|
58940
|
-
|
|
58970
|
+
lastX = ax;
|
|
58971
|
+
lastY = ay;
|
|
58972
|
+
lastZ = az;
|
|
58941
58973
|
|
|
58942
|
-
|
|
58974
|
+
var mag = Math.sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ * deltaZ) * factor;
|
|
58943
58975
|
|
|
58944
|
-
|
|
58945
|
-
|
|
58976
|
+
if (mag >= threshold) {
|
|
58977
|
+
var now = Date.now();
|
|
58946
58978
|
|
|
58947
|
-
|
|
58948
|
-
if (obj._shakeStopTimeout) obj._shakeStopTimeout.clear();
|
|
58979
|
+
if (obj._shakeStopTimeout) obj._shakeStopTimeout.clear();
|
|
58949
58980
|
|
|
58950
|
-
|
|
58951
|
-
|
|
58952
|
-
|
|
58953
|
-
|
|
58954
|
-
|
|
58955
|
-
|
|
58981
|
+
obj._shakeStopTimeout = zim.timeout(wait, function() {
|
|
58982
|
+
obj._shakeStopTimeout = null;
|
|
58983
|
+
if (stopCall) stopCall(obj);
|
|
58984
|
+
obj.dispatchEvent({
|
|
58985
|
+
type: "shakestop",
|
|
58986
|
+
event: e
|
|
58987
|
+
});
|
|
58988
|
+
if (s) s.update();
|
|
58956
58989
|
});
|
|
58957
|
-
if (s) s.update();
|
|
58958
|
-
});
|
|
58959
58990
|
|
|
58960
|
-
|
|
58961
|
-
|
|
58991
|
+
if (now - lastTime >= wait * 1000) {
|
|
58992
|
+
lastTime = now;
|
|
58962
58993
|
|
|
58963
|
-
|
|
58964
|
-
|
|
58965
|
-
|
|
58966
|
-
|
|
58967
|
-
|
|
58968
|
-
|
|
58994
|
+
var orient = 0;
|
|
58995
|
+
if (typeof WW != "undefined") {
|
|
58996
|
+
if (WW.screen && WW.screen.orientation && WW.screen.orientation.angle != null) {
|
|
58997
|
+
orient = WW.screen.orientation.angle;
|
|
58998
|
+
} else if (typeof WW.orientation != "undefined") {
|
|
58999
|
+
orient = WW.orientation;
|
|
59000
|
+
}
|
|
58969
59001
|
}
|
|
58970
|
-
}
|
|
58971
59002
|
|
|
58972
|
-
|
|
58973
|
-
|
|
58974
|
-
|
|
58975
|
-
|
|
58976
|
-
|
|
58977
|
-
|
|
58978
|
-
|
|
58979
|
-
|
|
58980
|
-
|
|
58981
|
-
|
|
58982
|
-
|
|
58983
|
-
|
|
58984
|
-
|
|
58985
|
-
|
|
58986
|
-
|
|
58987
|
-
if (call) call(mag, shakeX, shakeY, shakeZ, obj, e);
|
|
58988
|
-
obj.dispatchEvent({
|
|
58989
|
-
type: "shake",
|
|
58990
|
-
total: mag,
|
|
58991
|
-
x: shakeX,
|
|
58992
|
-
y: shakeY,
|
|
58993
|
-
z: shakeZ,
|
|
58994
|
-
event: e
|
|
58995
|
-
});
|
|
59003
|
+
var shakeX, shakeY, shakeZ = deltaZ * factor;
|
|
59004
|
+
if (orient == 90) {
|
|
59005
|
+
shakeX = deltaY * factor;
|
|
59006
|
+
shakeY = -deltaX * factor;
|
|
59007
|
+
} else if (orient == -90 || orient == 270) {
|
|
59008
|
+
shakeX = -deltaY * factor;
|
|
59009
|
+
shakeY = deltaX * factor;
|
|
59010
|
+
} else if (orient == 180) {
|
|
59011
|
+
shakeX = -deltaX * factor;
|
|
59012
|
+
shakeY = -deltaY * factor;
|
|
59013
|
+
} else {
|
|
59014
|
+
shakeX = deltaX * factor;
|
|
59015
|
+
shakeY = deltaY * factor;
|
|
59016
|
+
}
|
|
58996
59017
|
|
|
59018
|
+
if (call) call(mag, shakeX, shakeY, shakeZ, obj, e);
|
|
59019
|
+
obj.dispatchEvent({
|
|
59020
|
+
type: "shake",
|
|
59021
|
+
total: mag,
|
|
59022
|
+
x: shakeX,
|
|
59023
|
+
y: shakeY,
|
|
59024
|
+
z: shakeZ,
|
|
59025
|
+
event: e
|
|
59026
|
+
});
|
|
59027
|
+
|
|
59028
|
+
if (s) s.update();
|
|
59029
|
+
}
|
|
58997
59030
|
}
|
|
58998
59031
|
}
|
|
58999
|
-
}
|
|
59000
59032
|
|
|
59001
|
-
|
|
59002
|
-
|
|
59003
|
-
|
|
59004
|
-
|
|
59005
|
-
|
|
59006
|
-
|
|
59033
|
+
function activate() {
|
|
59034
|
+
obj._shakeType = type;
|
|
59035
|
+
obj._shakeListener = handleMotion;
|
|
59036
|
+
f.on(type, obj._shakeListener);
|
|
59037
|
+
if (ready) ready(true, obj);
|
|
59038
|
+
}
|
|
59007
59039
|
|
|
59008
|
-
|
|
59009
|
-
|
|
59010
|
-
|
|
59011
|
-
|
|
59012
|
-
|
|
59013
|
-
|
|
59014
|
-
|
|
59015
|
-
|
|
59040
|
+
// Shared Permission check & queueing
|
|
59041
|
+
if (f._permissionGranted) {
|
|
59042
|
+
activate();
|
|
59043
|
+
} else if (f._permissionPending) {
|
|
59044
|
+
f._permissionQueue.push({ activate: activate, reject: function() { if (ready) ready(false, obj); } });
|
|
59045
|
+
} else {
|
|
59046
|
+
f._permissionPending = true;
|
|
59047
|
+
f._permissionQueue = [{ activate: activate, reject: function() { if (ready) ready(false, obj); } }];
|
|
59016
59048
|
|
|
59017
|
-
|
|
59018
|
-
|
|
59019
|
-
|
|
59020
|
-
|
|
59021
|
-
|
|
59049
|
+
new zim.PermissionAsk(function(yes) {
|
|
59050
|
+
f._permissionPending = false;
|
|
59051
|
+
if (yes) {
|
|
59052
|
+
f._permissionGranted = true;
|
|
59053
|
+
zim.loop(f._permissionQueue, function(item) { item.activate(); });
|
|
59054
|
+
} else {
|
|
59055
|
+
zim.loop(f._permissionQueue, function(item) { item.reject(); });
|
|
59056
|
+
}
|
|
59022
59057
|
f._permissionQueue = [];
|
|
59023
|
-
}
|
|
59024
|
-
|
|
59025
|
-
}
|
|
59026
|
-
}, type);
|
|
59027
|
-
}
|
|
59058
|
+
}, type);
|
|
59059
|
+
}
|
|
59028
59060
|
|
|
59029
|
-
|
|
59030
|
-
};//-34.95
|
|
59061
|
+
return obj;
|
|
59062
|
+
};//-34.95
|
|
59031
59063
|
|
|
59032
59064
|
|
|
59033
59065
|
/*--
|
|
@@ -59048,30 +59080,30 @@ END EXAMPLE
|
|
|
59048
59080
|
|
|
59049
59081
|
RETURNS obj for chaining
|
|
59050
59082
|
--*///+34.96
|
|
59051
|
-
zim.noShake = function(obj) {
|
|
59052
|
-
|
|
59053
|
-
|
|
59054
|
-
|
|
59083
|
+
zim.noShake = function(obj) {
|
|
59084
|
+
var sig = "obj";
|
|
59085
|
+
var duo; if (duo = zob(zim.noShake, arguments, sig)) return duo;
|
|
59086
|
+
z_d("34.96");
|
|
59055
59087
|
|
|
59056
|
-
|
|
59088
|
+
if (zot(obj) || !obj.on || !obj.zimTouch) return;
|
|
59057
59089
|
|
|
59058
|
-
|
|
59059
|
-
|
|
59060
|
-
|
|
59090
|
+
// MONITOR
|
|
59091
|
+
var mID = "z~noShake";
|
|
59092
|
+
if (obj && obj.mID && ((obj.mID[2] && obj.mID[2]=="-") || obj.mID[0]=="-")) mID = "z~-";
|
|
59061
59093
|
|
|
59062
|
-
|
|
59094
|
+
var f = obj.frame || (typeof zdf != "undefined" ? zdf : WW.zdf);
|
|
59063
59095
|
|
|
59064
|
-
|
|
59065
|
-
|
|
59066
|
-
|
|
59067
|
-
|
|
59068
|
-
|
|
59069
|
-
|
|
59070
|
-
|
|
59071
|
-
|
|
59096
|
+
if (obj._shakeListener && f) {
|
|
59097
|
+
f.off(obj._shakeType || "devicemotion", obj._shakeListener);
|
|
59098
|
+
obj._shakeListener = null;
|
|
59099
|
+
}
|
|
59100
|
+
if (obj._shakeStopTimeout) {
|
|
59101
|
+
obj._shakeStopTimeout.clear();
|
|
59102
|
+
obj._shakeStopTimeout = null;
|
|
59103
|
+
}
|
|
59072
59104
|
|
|
59073
|
-
|
|
59074
|
-
};//-34.96
|
|
59105
|
+
return obj;
|
|
59106
|
+
};//-34.96
|
|
59075
59107
|
|
|
59076
59108
|
// SUBSECTION HIT TESTS
|
|
59077
59109
|
|
|
@@ -98631,118 +98663,6 @@ END EXAMPLE
|
|
|
98631
98663
|
// See Frame for the cursors property
|
|
98632
98664
|
//-83.867
|
|
98633
98665
|
|
|
98634
|
-
/*--
|
|
98635
|
-
Tilt - device orientation event
|
|
98636
|
-
|
|
98637
|
-
Tilt
|
|
98638
|
-
"deviceorientation" Frame event
|
|
98639
|
-
|
|
98640
|
-
DESCRIPTION
|
|
98641
|
-
The Frame has a "deviceorientation" event to capture tilt or device rotation (like a compass)
|
|
98642
|
-
and a "devicemotion" event to capture shaking the device with the accelerometer.
|
|
98643
|
-
|
|
98644
|
-
SEE: the tilt() method (just after gesture()) for a one line version
|
|
98645
|
-
|
|
98646
|
-
SEE: Shake - for the "devicemotion" event for accelerometer motion
|
|
98647
|
-
|
|
98648
|
-
SEE: the PermissionAsk() class which will handle asking for permissions on devices.
|
|
98649
|
-
|
|
98650
|
-
EXAMPLE
|
|
98651
|
-
// SEE ALSO THE TILT METHOD FOR A ONE LINE VERSION
|
|
98652
|
-
|
|
98653
|
-
// TILT OR COMPASS - https://zimjs.com/zapp/Z_Q5UYS
|
|
98654
|
-
// for capturing tilt on device (rotation about an axis)
|
|
98655
|
-
// be on a mobile device or a device with sensors
|
|
98656
|
-
new PermissionAsk(yes=>{
|
|
98657
|
-
if (yes) { // the user answered yes to PermissionAsk
|
|
98658
|
-
// all code goes in here
|
|
98659
|
-
new Label("Tilt the device to move circle").alp(.3).pos(0,30,CENTER);
|
|
98660
|
-
const circle = new Circle().center();
|
|
98661
|
-
const amount = .5;
|
|
98662
|
-
const compass = new Line(60, 4, light, "arrow").rot(90).centerReg(circle); // point up
|
|
98663
|
-
F.on("deviceorientation", e=>{
|
|
98664
|
-
// for tilt
|
|
98665
|
-
circle.mov(e.rotation.y*amount, e.rotation.x*amount);
|
|
98666
|
-
circle.x = constrain(circle.x, 0, W);
|
|
98667
|
-
circle.y = constrain(circle.y, 0, H);
|
|
98668
|
-
// for compass use e.rotation.z
|
|
98669
|
-
// which has 0 angle at device start rotation (not necessarily North)
|
|
98670
|
-
compass.rot(90-e.rotation.z); // rotate opposite device rotation
|
|
98671
|
-
S.update();
|
|
98672
|
-
});
|
|
98673
|
-
} else { // the user answered no to PermissionAsk
|
|
98674
|
-
new Pane("SENSOR not available",yellow).show();
|
|
98675
|
-
}
|
|
98676
|
-
});
|
|
98677
|
-
END EXAMPLE
|
|
98678
|
-
|
|
98679
|
-
EVENTS
|
|
98680
|
-
"deviceorientation" - for tilt
|
|
98681
|
-
fired as device orientation changes:
|
|
98682
|
-
eventObject.rotation.x (beta in HTML specs) holds rotation about the x axis between -180 and 180 (tipped forward or backward)
|
|
98683
|
-
eventObject.rotation.y (gamma in HTML specs) holds rotation about the y axis between -90 and 90 (tipped left or right)
|
|
98684
|
-
eventObject.rotation.z (alpha in HTML specs) holds rotation about the z axis 0-360 clockwise (relative to orientation when app loads)
|
|
98685
|
-
note rotation.z is 360-alpha compared to the HTML 5 specs
|
|
98686
|
-
note also that beta, gamma and alpha from the HTML 5 specs are also provided
|
|
98687
|
-
--*///+83.86
|
|
98688
|
-
// Device motion and orientation events are available as Frame events
|
|
98689
|
-
//-83.86
|
|
98690
|
-
|
|
98691
|
-
/*--
|
|
98692
|
-
Shake - device motion event
|
|
98693
|
-
|
|
98694
|
-
Shake
|
|
98695
|
-
"devicemotion" Frame event
|
|
98696
|
-
|
|
98697
|
-
DESCRIPTION
|
|
98698
|
-
The Frame has a "devicemotion" event to capture shaking the device with the accelerometer.
|
|
98699
|
-
|
|
98700
|
-
SEE: Tilt - for the "deviceorientation" event for tilt or compass
|
|
98701
|
-
|
|
98702
|
-
SEE: the PermissionAsk() class which will handle asking for permissions on devices.
|
|
98703
|
-
|
|
98704
|
-
EXAMPLE
|
|
98705
|
-
// SHAKE - https://zimjs.com/zapp/Z_XF8JM
|
|
98706
|
-
// for capturing shaking motion on device (acceleration about an axis)
|
|
98707
|
-
// be on a mobile device or a device with sensors
|
|
98708
|
-
new PermissionAsk(init, "devicemotion");
|
|
98709
|
-
function init(yes) {
|
|
98710
|
-
if (yes) { // the user answered yes to PermissionAsk
|
|
98711
|
-
// all code goes in here
|
|
98712
|
-
new Label("Shake the device").center();
|
|
98713
|
-
S.update();
|
|
98714
|
-
let id;
|
|
98715
|
-
F.on("devicemotion", e=>{
|
|
98716
|
-
// for shake
|
|
98717
|
-
if (
|
|
98718
|
-
Math.abs(e.acceleration.x) > 5 ||
|
|
98719
|
-
Math.abs(e.acceleration.y) > 5 ||
|
|
98720
|
-
Math.abs(e.acceleration.z) > 5
|
|
98721
|
-
) {
|
|
98722
|
-
if (F.color != green) {
|
|
98723
|
-
F.color = green;
|
|
98724
|
-
S.update();
|
|
98725
|
-
}
|
|
98726
|
-
if (id) id.clear();
|
|
98727
|
-
id = timeout(.2, ()=>{
|
|
98728
|
-
F.color = light;
|
|
98729
|
-
S.update();
|
|
98730
|
-
});
|
|
98731
|
-
}
|
|
98732
|
-
});
|
|
98733
|
-
} else { // the user answered no to PermissionAsk
|
|
98734
|
-
new Pane("SENSOR not available",yellow).show();
|
|
98735
|
-
}
|
|
98736
|
-
}
|
|
98737
|
-
END EXAMPLE
|
|
98738
|
-
|
|
98739
|
-
EVENTS
|
|
98740
|
-
"devicemotion" - for shake
|
|
98741
|
-
fired on moving mobile device - like a shake - eventObject.acceleration holds x, y and z properties of motion
|
|
98742
|
-
--*///+83.87
|
|
98743
|
-
// Device motion and orientation events are available as Frame events
|
|
98744
|
-
//-83.87
|
|
98745
|
-
|
|
98746
98666
|
/*--
|
|
98747
98667
|
zim.PermissionAsk = function(callback, permissionType, color, backgroundColor, style, group, inherit)
|
|
98748
98668
|
|
|
@@ -98758,9 +98678,14 @@ Pre ZIM 018, this was done with a sensors parameter on the Frame.
|
|
|
98758
98678
|
The sensors parameter has now been removed and the events are handled with PermissionAsk.
|
|
98759
98679
|
|
|
98760
98680
|
NOTE: this started as SensorAsk but the class has been adjusted to handle other permissions and the name has been changed in ZIM 016
|
|
98681
|
+
|
|
98761
98682
|
NOTE: as of ZIM 5.5.0 the zim namespace is no longer required (unless zns is set to true before running zim)
|
|
98683
|
+
|
|
98762
98684
|
NOTE: sensor prompt text can be customized prior to calling with F.sensorText = "Custom message";
|
|
98763
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
|
+
|
|
98764
98689
|
EXAMPLE
|
|
98765
98690
|
// DEVICE ORIENTATION - gives angle of device in all 3 dimensions
|
|
98766
98691
|
// See https://zimjs.com/zapp/Z_Q5UYS
|
|
@@ -98930,42 +98855,71 @@ no - reference to the zim Button with NO
|
|
|
98930
98855
|
return;
|
|
98931
98856
|
}
|
|
98932
98857
|
|
|
98933
|
-
// sensors
|
|
98934
|
-
|
|
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") {
|
|
98935
98870
|
setPane();
|
|
98936
98871
|
} else {
|
|
98937
|
-
var
|
|
98938
|
-
|
|
98872
|
+
var tested = false;
|
|
98873
|
+
var timer = setTimeout(function() {
|
|
98874
|
+
if (tested) return;
|
|
98875
|
+
tested = true;
|
|
98876
|
+
WW.removeEventListener(permissionType, testMe);
|
|
98877
|
+
callback(false, 3); // 3 - timeout / no sensor hardware (e.g. desktop)
|
|
98878
|
+
}, 500);
|
|
98879
|
+
|
|
98939
98880
|
function testMe(e) {
|
|
98940
|
-
if (okay) return;
|
|
98941
|
-
if (permissionType=="deviceorientation") {
|
|
98942
|
-
if (e.alpha
|
|
98943
|
-
|
|
98881
|
+
if (tested || okay) return;
|
|
98882
|
+
if (permissionType == "deviceorientation") {
|
|
98883
|
+
if (e.alpha != null || e.beta != null || e.gamma != null) {
|
|
98884
|
+
tested = true;
|
|
98885
|
+
clearTimeout(timer);
|
|
98886
|
+
WW.removeEventListener(permissionType, testMe);
|
|
98887
|
+
setEvents();
|
|
98888
|
+
} else {
|
|
98889
|
+
tested = true;
|
|
98890
|
+
clearTimeout(timer);
|
|
98891
|
+
WW.removeEventListener(permissionType, testMe);
|
|
98892
|
+
callback(false, 1);
|
|
98893
|
+
}
|
|
98944
98894
|
} else {
|
|
98945
|
-
|
|
98946
|
-
|
|
98947
|
-
|
|
98948
|
-
|
|
98949
|
-
|
|
98950
|
-
|
|
98951
|
-
|
|
98952
|
-
|
|
98953
|
-
|
|
98895
|
+
var hasAcc = e.acceleration && (e.acceleration.x != null || e.acceleration.y != null || e.acceleration.z != null);
|
|
98896
|
+
var hasGrav = e.accelerationIncludingGravity && (e.accelerationIncludingGravity.x != null || e.accelerationIncludingGravity.y != null || e.accelerationIncludingGravity.z != null);
|
|
98897
|
+
if (hasAcc || hasGrav) {
|
|
98898
|
+
tested = true;
|
|
98899
|
+
clearTimeout(timer);
|
|
98900
|
+
WW.removeEventListener(permissionType, testMe);
|
|
98901
|
+
setEvents();
|
|
98902
|
+
} else {
|
|
98903
|
+
tested = true;
|
|
98904
|
+
clearTimeout(timer);
|
|
98905
|
+
WW.removeEventListener(permissionType, testMe);
|
|
98906
|
+
callback(false, 2);
|
|
98907
|
+
}
|
|
98908
|
+
}
|
|
98909
|
+
}
|
|
98910
|
+
WW.addEventListener(permissionType, testMe);
|
|
98954
98911
|
}
|
|
98955
98912
|
|
|
98956
|
-
var lastZ = 0;
|
|
98957
|
-
var flip = 0;
|
|
98958
98913
|
function deviceorientationEvent(e) {
|
|
98959
|
-
var z = 360-e.alpha;
|
|
98960
|
-
|
|
98961
|
-
lastZ = z;
|
|
98962
|
-
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};
|
|
98963
98916
|
frame.dispatchEvent(e);
|
|
98964
98917
|
}
|
|
98965
98918
|
function devicemotionEvent(e) {
|
|
98966
98919
|
frame.dispatchEvent(e);
|
|
98967
98920
|
}
|
|
98968
98921
|
function setEvents() {
|
|
98922
|
+
if (frame) frame._permissionGranted = true;
|
|
98969
98923
|
if (permissionType=="deviceorientation") {
|
|
98970
98924
|
frame.zimDeviceorientationEvent = deviceorientationEvent;
|
|
98971
98925
|
WW.addEventListener("deviceorientation", frame.zimDeviceorientationEvent);
|
|
@@ -107998,4 +107952,4 @@ export let Ticker = zim.Ticker;
|
|
|
107998
107952
|
export let Style = zim.Style;
|
|
107999
107953
|
export let assets = zim.assets;
|
|
108000
107954
|
export let assetIDs = zim.assetIDs;
|
|
108001
|
-
export let ZIMON = zim.ZIMON;
|
|
107955
|
+
export let ZIMON = zim.ZIMON;
|