word/memory: adjust grid count to fit size
authorMischa POSLAWSKY <perl@shiar.org>
Fri, 25 Mar 2022 15:28:57 +0000 (16:28 +0100)
committerMischa POSLAWSKY <perl@shiar.org>
Sat, 28 May 2022 17:35:56 +0000 (19:35 +0200)
word/memory.js
word/memory.plp

index 89fd853c3cb89210c2a1d3e02e1b9fdaaaec2b55..ac1e829996258598c1b710bded8c182b99c8c84a 100644 (file)
@@ -60,11 +60,13 @@ class WordMemory extends WordQuiz {
 
                let cards;
                if (this.words) {
-                       const formstyle = window.getComputedStyle(this.form)
-                       const gridsize = [
-                               formstyle['grid-template-rows'], formstyle['grid-template-columns']
-                       ].map(val => val.match(/ /g).length + 1).reduce((x, y) => x * y) / 2;
-                       cards = this.words.splice(0, gridsize || 6).map(row => row[2]);
+                       const aspect = this.form.clientWidth / window.innerHeight;
+                       //TODO image ratio
+                       let count = 35;
+                       let cols = Math.round(Math.sqrt(count) * aspect**.5);
+                       count = cols * Math.ceil(count / cols);
+                       this.form.style['grid-template-columns'] = `repeat(${cols}, 1fr)`;
+                       cards = this.words.splice(0, count>>1).map(row => row[2]);
                        cards.push(...cards.map(val => -val));
                }
                else {
index d15d619dce6e934c23e656373a94ec205ba198dc..65184d83131dc09e00a178c17b22649e6b576672 100644 (file)
@@ -11,25 +11,17 @@ Html({
 /* cards */
 #quiz {
        display: grid;
-       grid: repeat(3, 1fr) / repeat(4, 1fr);
+       grid: auto / repeat(6, 1fr);
        grid-gap: 1ex;
-       min-height: calc(100vh - 11.5ex);
-}
-@media (orientation: portrait) {
-       #quiz {
-               grid: repeat(6, 1fr) / repeat(2, 1fr);
-       }
 }
 html {
        overflow: hidden; /* rotation overflow on celebration */
 }
 
 figure {
-       display: inline-block;
        background: #224;
        border: 1px solid #888;
        perspective: 100em;
-       position: relative;
 }
 figure:not(.turn):hover {
        cursor: pointer;
@@ -44,7 +36,9 @@ figure img {
        -webkit-backface-visibility: hidden;
        transform: rotateY(180deg); /* back */
        transform-style: preserve-3d;
-       -float: left; /* ff workaround to prevent click selection */
+       float: left; /* ff workaround to prevent click selection */
+       height: 100%;
+       object-fit: contain; /* center */
 }
 figure.mirror img {
        transform: rotateY(180deg) scaleX(-1);
@@ -79,10 +73,5 @@ figure.good {
 </style>
 EOT
 });
-if (my $gridsize = $get{grid}) {
-       printf "<style>#quiz {grid: repeat(%d, 1fr) / repeat(%d, 1fr)}</style>",
-               reverse split /\D+/, $gridsize;
-       say '';
-}
 say '<h1>memory</h1><p id="quiz"></p>';
 say "<script>new WordMemory('/$wordlistbase.json')</script>";