@jamesrock/rockjs 1.36.0 → 1.38.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.
- package/index.js +84 -1
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -412,6 +412,18 @@ export class DisplayObject {
|
|
|
412
412
|
return this;
|
|
413
413
|
|
|
414
414
|
};
|
|
415
|
+
on(event, handler, passive = true) {
|
|
416
|
+
|
|
417
|
+
this.node.addEventListener(event, handler, {passive});
|
|
418
|
+
return this;
|
|
419
|
+
|
|
420
|
+
};
|
|
421
|
+
once(event, handler, passive = true) {
|
|
422
|
+
|
|
423
|
+
this.node.addEventListener(event, handler, {passive, once: true});
|
|
424
|
+
return this;
|
|
425
|
+
|
|
426
|
+
};
|
|
415
427
|
dispatchEvent(event) {
|
|
416
428
|
|
|
417
429
|
this.node.dispatchEvent(new Event(event));
|
|
@@ -892,7 +904,7 @@ export class SoundManager {
|
|
|
892
904
|
|
|
893
905
|
if(!this.buffers[sound]) {
|
|
894
906
|
console.log(`SoundManager: '${sound}' not loaded!`);
|
|
895
|
-
return;
|
|
907
|
+
return this;
|
|
896
908
|
};
|
|
897
909
|
|
|
898
910
|
const source = this.context.createBufferSource();
|
|
@@ -907,6 +919,8 @@ export class SoundManager {
|
|
|
907
919
|
|
|
908
920
|
source.start();
|
|
909
921
|
|
|
922
|
+
return this;
|
|
923
|
+
|
|
910
924
|
};
|
|
911
925
|
volume(sound, value) {
|
|
912
926
|
|
|
@@ -933,6 +947,75 @@ export class SoundManager {
|
|
|
933
947
|
};
|
|
934
948
|
};
|
|
935
949
|
|
|
950
|
+
export class Tempo {
|
|
951
|
+
constructor() {
|
|
952
|
+
|
|
953
|
+
this.taps = [];
|
|
954
|
+
|
|
955
|
+
};
|
|
956
|
+
tap(base = 120) {
|
|
957
|
+
|
|
958
|
+
const now = performance.now();
|
|
959
|
+
this.taps.push(now);
|
|
960
|
+
|
|
961
|
+
if(this.taps.length > 1 && now - this.taps[this.taps.length - 2] > 2000) {
|
|
962
|
+
taps = [now];
|
|
963
|
+
return base;
|
|
964
|
+
};
|
|
965
|
+
|
|
966
|
+
if(this.taps.length < 2) return base;
|
|
967
|
+
if(this.taps.length > 10) this.taps.shift();
|
|
968
|
+
|
|
969
|
+
const intervals = [];
|
|
970
|
+
for(let i = 1; i < this.taps.length; i++) {
|
|
971
|
+
intervals.push(this.taps[i] - this.taps[i - 1]);
|
|
972
|
+
};
|
|
973
|
+
|
|
974
|
+
const averageInterval = intervals.reduce((a, b) => a + b) / intervals.length;
|
|
975
|
+
|
|
976
|
+
return floorTo(60000 / averageInterval);
|
|
977
|
+
|
|
978
|
+
};
|
|
979
|
+
};
|
|
980
|
+
|
|
981
|
+
export class Toggle extends DisplayObject {
|
|
982
|
+
constructor(items, name = '{name}', initialValue, className) {
|
|
983
|
+
|
|
984
|
+
super();
|
|
985
|
+
|
|
986
|
+
this.node = makeNode('form', className);
|
|
987
|
+
this.name = name;
|
|
988
|
+
this.initialValue = initialValue;
|
|
989
|
+
this.toggle = makeToggle(items, name, initialValue);
|
|
990
|
+
|
|
991
|
+
append(this.node)(this.toggle);
|
|
992
|
+
|
|
993
|
+
};
|
|
994
|
+
getValue() {
|
|
995
|
+
|
|
996
|
+
const data = new FormData(this.node);
|
|
997
|
+
return data.get(this.name);
|
|
998
|
+
|
|
999
|
+
};
|
|
1000
|
+
getValueAsNumber() {
|
|
1001
|
+
|
|
1002
|
+
return Number(this.getValue());
|
|
1003
|
+
|
|
1004
|
+
};
|
|
1005
|
+
updateItemLabel(value, label) {
|
|
1006
|
+
|
|
1007
|
+
this.toggle.querySelector(`label[for="${this.name}-${value}"]`).innerText = label;
|
|
1008
|
+
return this;
|
|
1009
|
+
|
|
1010
|
+
};
|
|
1011
|
+
scrollIntoView() {
|
|
1012
|
+
|
|
1013
|
+
this.toggle.querySelector(`input[value="${this.initialValue}"]`).scrollIntoView({block: 'center'});
|
|
1014
|
+
return this;
|
|
1015
|
+
|
|
1016
|
+
};
|
|
1017
|
+
};
|
|
1018
|
+
|
|
936
1019
|
// temporary alias
|
|
937
1020
|
export const createNode = makeNode;
|
|
938
1021
|
export const createSVGNode = makeSVGNode;
|