Jacob_DiDiodato

JS Survey First Edit

Aug 29th, 2017
322
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var c = document.getElementById('myCanvas');
  2. var elemLeft = c.offsetLeft;
  3. var elemTop = c.offsetTop;
  4. var ctx = c.getContext('2d');
  5.  
  6. var canvasWidth = c.width;
  7. var canvasHeight = c.height;
  8.  
  9. var attributesList = [];
  10. var levelsDict = [];
  11. var numTrials = 0;
  12. var attributesPerPage = 0;
  13. returnedText = readTextFile("./surveyinfo.txt").split("\n");
  14. for (i = 1; i < returnedText.length; i++)
  15. {
  16.     if (i != returnedText.length - 1)
  17.     {
  18.         attributesList.push(returnedText[i].split(":")[0])
  19.         levelsDict.push(returnedText[i].split(":")[1].split(","))
  20.     }
  21.     else
  22.     {
  23.         numTrials = Number(returnedText[i])
  24.     }
  25. }
  26.  
  27. if (attributesList.length > 8)
  28. {
  29.     attributesPerPage = 8
  30. }
  31. else
  32. {
  33.     attributesPerPage = attributesList.length
  34. }
  35.  
  36. console.log(attributesList);
  37. console.log(levelsDict);
  38.  
  39. ctx.font = "30px Times";
  40.  
  41. redraw();
  42.  
  43. function readTextFile(file)
  44. {  
  45.     var allText;
  46.     var rawFile = new XMLHttpRequest();
  47.     rawFile.open("GET", file, false);
  48.     rawFile.onreadystatechange = function ()
  49.     {
  50.         if(rawFile.readyState === 4)
  51.         {
  52.             if(rawFile.status === 200 || rawFile.status == 0)
  53.             {
  54.                 allText = rawFile.responseText;
  55.                 //alert(allText);
  56.             }
  57.         }
  58.     }
  59.     rawFile.send(null);
  60.     return allText;
  61. }
  62.  
  63. function ellipse(context, cx, cy, rx, ry){
  64.     context.save(); // save state
  65.     context.beginPath();
  66.  
  67.     context.translate(cx-rx, cy-ry);
  68.     context.scale(rx, ry);
  69.     context.arc(1, 1, 1, 0, 2 * Math.PI, false);
  70.  
  71.     context.restore(); // restore to original state
  72.     context.stroke();
  73. }
  74.  
  75. function redraw() {
  76.     ctx.font = "30px Times";
  77.  
  78.     ctx.beginPath();
  79.     ctx.fillStyle = "#D3D3D3";
  80.     ctx.fillRect(0,0,canvasWidth,canvasHeight);
  81.    
  82.     var aFirst = true;
  83.     var bFirst = false;
  84.    
  85.     var isAFirst = Math.floor(Math.random() * 2);
  86.     if (isAFirst === 1)
  87.     {
  88.         aFirst = true;
  89.         bFirst = false;
  90.     }
  91.     else
  92.     {
  93.         aFirst = false;
  94.         bFirst = true;
  95.     }
  96.    
  97.     ctx.beginPath();
  98.     ctx.fillStyle = "black";
  99.     ctx.fillText("Choice A", 303 + Number(aFirst)*187, 40);
  100.     ctx.fillText("Choice B", 303 + Number(bFirst)*187, 40);
  101.     ctx.fillText("Pick one ->", 60, canvasHeight - 30)
  102.     ctx.moveTo(453,0);
  103.     ctx.lineTo(453,720);
  104.     ctx.stroke();
  105.     ctx.moveTo(266,0);
  106.     ctx.lineTo(266,720);
  107.     ctx.stroke();
  108.     for (i = 0; i < 9; i++)
  109.     {
  110.         ctx.moveTo(0,72*(i+1));
  111.         ctx.lineTo(640,72*(i+1));
  112.         ctx.stroke();
  113.     }
  114.     ellipse(ctx, 360,canvasHeight - 40,10,10)
  115.     ellipse(ctx, 545,canvasHeight - 40,10,10)
  116. }
  117.  
  118. // Add event listener for `click` events.
  119. c.addEventListener('click', function(event) {
  120.     var x = event.pageX - elemLeft;
  121.     var y = event.pageY - elemTop;
  122.        
  123.     console.log(x)
  124.     console.log(y)
  125.    
  126.     redraw();
  127. }, false);
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment