virtual-keypad 5.13.0 → 5.13.2

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/src/maxKeySize.js CHANGED
@@ -1,105 +1,142 @@
1
- const getKeysDimensions = (elem, n, aspect=0.5) => {
2
- /**** translation of maxKeySize.m, original provided courtesy of Prof Denis Pelli ****/
3
- let keyHeightPx;
4
-
5
- // key = aspect*keyHeightPx × keyHeightPx
6
- const widthPx = elem.clientWidth;
7
- const heightPx = elem.clientHeight;
8
- let screenArea=widthPx*heightPx
9
-
10
- keyHeightPx = (-1+Math.sqrt(1+4*n*heightPx/widthPx))/(2*n/widthPx);
11
- keyHeightPx = (-1+Math.sqrt(1+4*n*heightPx*aspect/widthPx))/(2*aspect*n/widthPx);
12
-
13
- while (n > (Math.floor(heightPx/keyHeightPx - 1)*Math.floor(widthPx/(aspect*keyHeightPx)))){
14
- // Round size down so that screen is a multiple.
15
- const ss=[heightPx/Math.ceil(heightPx/keyHeightPx), widthPx/Math.ceil(widthPx/(aspect*keyHeightPx))];
16
- for (let i=0; i<=ss.length; i++) {
17
- // We’ve already rejected the current size, so only consider smaller sizes.
18
- // This guarantees that sizePx will decrease on every loop iteration.
19
- if (ss[i]>=keyHeightPx) ss[i]=0;
20
- }
21
- // We want the largest possible size so try the largest of the current options.
22
- keyHeightPx=Math.max(...ss);
23
- }
24
-
25
- // Compute efficiency as area covered by keys divided by screen area.
26
- // First term for n keys. Second term is for Space and Return, which fully
27
- // occupy their row.
28
- const areaOfKeys=aspect*keyHeightPx*keyHeightPx*n + widthPx*keyHeightPx;
29
- screenArea=heightPx*widthPx;
30
- const numKeysHorizontally = Math.floor(widthPx/(aspect*keyHeightPx));
31
- const numKeysVertically = Math.floor(heightPx/keyHeightPx-1);
32
- const efficiency = 100*areaOfKeys/screenArea;
33
-
34
- return {keyHeightPx: keyHeightPx, cols: numKeysHorizontally, rows:numKeysVertically, widthPx: widthPx, heightPx: heightPx}
35
- };
36
-
37
- export const applyMaxKeySize = (numberOfKeys) => {
38
- const aspect = 1;
39
- let margin = 5;
40
- const keysElem = document.getElementById("keypad");
41
- const {keyHeightPx, cols, rows, widthPx, heightPx} = getKeysDimensions(keysElem, numberOfKeys, aspect);
42
- const keyElems = [...keysElem.getElementsByClassName("response-button")];
43
- const controlKeyElemsMask = keyElems.map(e => e.parentNode.id === "keypad-control-keys");
44
- const gridCoords = keyElems.filter((k,i) => !controlKeyElemsMask[i]).map((k,i) => [Math.floor(i/cols), i%cols]);
45
- const widthUsed = cols*(keyHeightPx*aspect);
46
- const heightUsed = rows*keyHeightPx + keyHeightPx;
47
- const freeHeight = heightPx - heightUsed;
48
- const freeWidth = widthPx - widthUsed;
49
- const verticalMarginOffset = Math.floor(freeHeight/2);
50
- const horizontalMarginOffset = Math.floor(freeWidth/2);
51
-
52
- let keyFontSize;
53
- let j=0;
54
- keyElems.forEach((k,i) => {
55
- k.style.position = "fixed";
56
- const controlKey = controlKeyElemsMask[i];
57
- let top, left, width;
58
- if (controlKey) {
59
- top = heightPx-keyHeightPx;
60
- width = widthPx/2 - horizontalMarginOffset - margin;
61
- const m = horizontalMarginOffset + margin*0.5;
62
- left = (k.id.toLowerCase().includes("space") ? m : m + width + margin);
63
-
64
- const f = getLargeFontSize(k, width, keyHeightPx-margin);
65
- k.style.fontSize = f + "px";
66
- k.style.borderRadius = "25px";
67
- k.style.height = (keyHeightPx - margin)+ "px";
68
- } else {
69
- const height = keyHeightPx - margin;
70
- width = height*aspect;
71
- const [y,x] = gridCoords[j];
72
- j += 1;
73
- top = y*height + ((y+1)*margin) + verticalMarginOffset - margin/2;
74
- left = x*width + ((x+1)*margin) + horizontalMarginOffset - margin/2;
75
- if (!keyFontSize) {
76
- keyFontSize = getLargeFontSize(k, width, height);
77
- }
78
- k.style.height = height + "px";
79
- k.style.fontSize = height/2 + "px";
80
- }
81
- k.style.width = width + "px";
82
- k.style.top = top + "px";
83
- k.style.left = left + "px";
84
- k.style.visibility = "visible";
85
- });
86
- };
87
-
88
- const getLargeFontSize = (k, width, height) => {
89
- k.style.height = "auto";
90
- k.style.width = "auto";
91
- k.style.whiteSpace = "nowrap";
92
-
93
- // Set to a nominal font size, s
94
- const s = 20;
95
- k.style.fontSize = s + "px";
96
- // Measure width of elem, w
97
- const w = k.getBoundingClientRect().width;
98
- const h = k.getBoundingClientRect().height
99
- const rW = 1/(w/width)
100
- const rH = 1/(h/height)
101
- const r = Math.min(rW, rH)
102
- // Set font size to s*r
103
- const f = Math.floor(s*r);
104
- return f;
105
- };
1
+ const getKeysDimensions = (elem, n, aspect = 0.5) => {
2
+ /**** translation of maxKeySize.m, original provided courtesy of Prof Denis Pelli ****/
3
+ let keyHeightPx;
4
+
5
+ // key = aspect*keyHeightPx × keyHeightPx
6
+ const widthPx = elem.clientWidth;
7
+ const heightPx = elem.clientHeight;
8
+ let screenArea = widthPx * heightPx;
9
+
10
+ keyHeightPx =
11
+ (-1 + Math.sqrt(1 + (4 * n * heightPx) / widthPx)) / ((2 * n) / widthPx);
12
+ keyHeightPx =
13
+ (-1 + Math.sqrt(1 + (4 * n * heightPx * aspect) / widthPx)) /
14
+ ((2 * aspect * n) / widthPx);
15
+
16
+ while (
17
+ n >
18
+ Math.floor(heightPx / keyHeightPx - 1) *
19
+ Math.floor(widthPx / (aspect * keyHeightPx))
20
+ ) {
21
+ // Round size down so that screen is a multiple.
22
+ const ss = [
23
+ heightPx / Math.ceil(heightPx / keyHeightPx),
24
+ widthPx / Math.ceil(widthPx / (aspect * keyHeightPx)),
25
+ ];
26
+ for (let i = 0; i <= ss.length; i++) {
27
+ // We’ve already rejected the current size, so only consider smaller sizes.
28
+ // This guarantees that sizePx will decrease on every loop iteration.
29
+ if (ss[i] >= keyHeightPx) ss[i] = 0;
30
+ }
31
+ // We want the largest possible size so try the largest of the current options.
32
+ keyHeightPx = Math.max(...ss);
33
+ }
34
+
35
+ // Compute efficiency as area covered by keys divided by screen area.
36
+ // First term for n keys. Second term is for Space and Return, which fully
37
+ // occupy their row.
38
+ const areaOfKeys =
39
+ aspect * keyHeightPx * keyHeightPx * n + widthPx * keyHeightPx;
40
+ screenArea = heightPx * widthPx;
41
+ const numKeysHorizontally = Math.floor(widthPx / (aspect * keyHeightPx));
42
+ const numKeysVertically = Math.floor(heightPx / keyHeightPx - 1);
43
+ const efficiency = (100 * areaOfKeys) / screenArea;
44
+
45
+ return {
46
+ keyHeightPx: keyHeightPx,
47
+ cols: numKeysHorizontally,
48
+ rows: numKeysVertically,
49
+ widthPx: widthPx,
50
+ heightPx: heightPx,
51
+ };
52
+ };
53
+
54
+ export const applyMaxKeySize = (numberOfKeys) => {
55
+ const aspect = 1;
56
+ let margin = 5;
57
+ const keysElem = document.getElementById("keypad");
58
+ const { keyHeightPx, cols, rows, widthPx, heightPx } = getKeysDimensions(
59
+ keysElem,
60
+ numberOfKeys,
61
+ aspect
62
+ );
63
+ console.log(
64
+ `keyHeightPx: ${keyHeightPx}, cols: ${cols}, rows: ${rows}, widthPx: ${widthPx}, heightPx: ${heightPx}`
65
+ );
66
+ const keyElems = [...keysElem.getElementsByClassName("response-button")];
67
+ const controlKeyElemsMask = keyElems.map(
68
+ (e) => e.parentNode.id === "keypad-control-keys"
69
+ );
70
+ const gridCoords = keyElems
71
+ .filter((k, i) => !controlKeyElemsMask[i])
72
+ .map((k, i) => [Math.floor(i / cols), i % cols]);
73
+ const widthUsed = cols * (keyHeightPx * aspect);
74
+ const heightUsed = rows * keyHeightPx + keyHeightPx;
75
+ const freeHeight = heightPx - heightUsed;
76
+ const freeWidth = widthPx - widthUsed;
77
+ const verticalMarginOffset = Math.floor(freeHeight / 2);
78
+ const horizontalMarginOffset = Math.floor(freeWidth / 2);
79
+
80
+ console.log(
81
+ `widthUsed: ${widthUsed}, heightUsed: ${heightUsed}, freeHeight: ${freeHeight}, freeWidth: ${freeWidth}, verticalMarginOffset: ${verticalMarginOffset}, horizontalMarginOffset: ${horizontalMarginOffset}`
82
+ );
83
+
84
+ const controlKeys = [];
85
+ let controlKeyFontSize = Infinity;
86
+ let keyFontSize;
87
+ let j = 0;
88
+ keyElems.forEach((k, i) => {
89
+ k.style.position = "fixed";
90
+ const controlKey = controlKeyElemsMask[i];
91
+ let top, left, width;
92
+ if (controlKey) {
93
+ top = heightPx - keyHeightPx;
94
+ width = widthPx / 2 - margin;
95
+ const m = margin * 0.5;
96
+ left = k.id.toLowerCase().includes("space") ? m : m + width + margin;
97
+
98
+ const f = getLargeFontSize(k, width, keyHeightPx - margin);
99
+ controlKeyFontSize = Math.min(f, controlKeyFontSize);
100
+ controlKeys.push(k);
101
+ k.style.borderRadius = "25px";
102
+ k.style.height = keyHeightPx - margin + "px";
103
+ } else {
104
+ const height = keyHeightPx - margin;
105
+ width = height * aspect;
106
+ const [y, x] = gridCoords[j];
107
+ j += 1;
108
+ top = y * height + (y + 1) * margin + verticalMarginOffset - margin / 2;
109
+ left = x * width + (x + 1) * margin + horizontalMarginOffset - margin / 2;
110
+ if (!keyFontSize) {
111
+ keyFontSize = getLargeFontSize(k, width, height);
112
+ }
113
+ k.style.height = height + "px";
114
+ k.style.fontSize = height / 2 + "px";
115
+ }
116
+ k.style.width = width + "px";
117
+ k.style.top = top + "px";
118
+ k.style.left = left + "px";
119
+ k.style.visibility = "visible";
120
+ });
121
+ // Set the control keys to the same size, the smaller of the two
122
+ controlKeys.forEach((k) => (k.style.fontSize = controlKeyFontSize + "px"));
123
+ };
124
+
125
+ const getLargeFontSize = (k, width, height) => {
126
+ k.style.height = "auto";
127
+ k.style.width = "auto";
128
+ k.style.whiteSpace = "nowrap";
129
+
130
+ // Set to a nominal font size, s
131
+ const s = 20;
132
+ k.style.fontSize = s + "px";
133
+ // Measure width of elem, w
134
+ const w = k.getBoundingClientRect().width;
135
+ const h = k.getBoundingClientRect().height;
136
+ const rW = 1 / (w / width);
137
+ const rH = 1 / (h / height);
138
+ const r = Math.min(rW, rH);
139
+ // Set font size to s*r
140
+ const f = Math.floor(s * r);
141
+ return f;
142
+ };
package/src/receiver.css CHANGED
@@ -1,12 +1,12 @@
1
- #display {
2
- max-height: 70vh;
3
- overflow-y: scroll;
4
- min-height: 350px;
5
- min-width: 350px;
6
- padding-left: 3em;
7
- }
8
- #info {
9
- max-width: 800px;
10
- padding-left: 1em;
11
- padding-top: 0.25em;
1
+ #display {
2
+ max-height: 70vh;
3
+ overflow-y: scroll;
4
+ min-height: 350px;
5
+ min-width: 350px;
6
+ padding-left: 3em;
7
+ }
8
+ #info {
9
+ max-width: 800px;
10
+ padding-left: 1em;
11
+ padding-top: 0.25em;
12
12
  }