Vertical words... happy
authorBenjamin Smedberg <benjamin@smedbergs.us>
Fri, 12 Dec 2008 10:53:53 -0500
changeset 3 c6cb8be67953
parent 2 0830e9aac98b
child 4 8be31ac9ef27
push id1
push userbsmedberg@mozilla.com
push dateFri, 12 Dec 2008 21:25:26 +0000
Vertical words... happy
jswordle.js
--- a/jswordle.js
+++ b/jswordle.js
@@ -1,10 +1,11 @@
 const kWidth = 800;
 const kHeight = 400;
+const kParanoid = false;
 
 const commonWords = [
   'the',
   'of',
   'to',
   'and',
   'a',
   'in',
@@ -262,44 +263,40 @@ function compareArrays(a1, a2)
       throw new Error("Arrays differ at index: " + i);
     }
 }
 
 function drawWord(cx, word, x, y, vertical, size, width, data)
 {
   setStatus("drawWord: " + [word, x, y, vertical, size, width]);
 
-  cx.strokeRect(x, y, width, size);
-
   let update = {
     x: x - size,
     y: y - size
   };
 
   if (vertical) {
     cx.textAlign = 'right';
     cx.save();
     cx.translate(x, y);
     cx.rotate(270 * Math.PI / 180);
-    cx.fillText(word, x, y);
+    cx.fillText(word, 0, 0);
     cx.restore();
 
     update.w = size * 3;
     update.h = width + size * 2;
   }
   else {
     cx.textAlign = 'left';
     cx.fillText(word, x, y);
 
     update.w = width + size * 2;
     update.h = size * 3;
   }
 
-  cx.strokeRect(update.x + 2, update.y + 2, update.w - 4, update.h - 4);
-
   let mdata = safeGetImageData(cx, update.x, update.y, update.w, update.h);
   mergeImageData(data, mdata, Math.max(update.x, 0), Math.max(update.y, 0));
 }
 
 function draw()
 {
   statusel.textContent = '';
 
@@ -342,29 +339,43 @@ function draw()
 
   /* Place the first five words randomly */
   for (; i < 5; ++i) {
     let [word, size] = words[i];
 
     cx.font = size + 'px Baskerville';
     let width = cx.measureText(word).width;
 
-    let x, y;
+    let x, y, vertical;
 
     while (true) {
-      x = randomInt(0, kWidth - width);
-      y = randomInt(0, kHeight - size);
+      vertical = Math.random() < 0.4;
 
-      if (!hitTest(x, y, width, size, imgdata))
+      let hitw, hith;
+      if (vertical) {
+	hitw = size;
+	hith = width;
+      }
+      else {
+	hitw = width;
+	hith = size;
+      }	
+ 
+      x = randomInt(0, kWidth - hitw);
+      y = randomInt(0, kHeight - hith);
+
+      if (!hitTest(x, y, hitw, hith, imgdata))
 	break;
     }
-    drawWord(cx, word, x, y, false, size, width, imgdata);
+    drawWord(cx, word, x, y, vertical, size, width, imgdata);
 
-    let testdata = cx.getImageData(0, 0, kWidth, kHeight);
-    compareArrays(imgdata.data, testdata.data);
+    if (kParanoid) {
+      let testdata = cx.getImageData(0, 0, kWidth, kHeight);
+      compareArrays(imgdata.data, testdata.data);
+    }
   }
 }
 
 /*
 
   for each ([word, size] in words) {
     cx.font = size + 'px Utopia';
     let twidth = cx.measureText(word).width;