Pixels between rects instead of drawn lines in HTML5 Canvas












1















Basically I want background to be seen between rects. Right now I'm trying to leave a distance which is dependable on rect size between them, can I make that size to be exact number of pixels?



Because right now these empty spaces merge, sometimes are not seen at all, etc.






function getMousePos(canvas, evt) {
var rect = canvas.getBoundingClientRect();
return {
x: evt.clientX - rect.left,
y: evt.clientY - rect.top
};
}

var canvas = document.getElementById("posHourCanvas");
var context = canvas.getContext('2d');

var boxes = ;

function init() {
context.clearRect(0, 0, canvas.width, canvas.height);
boxes.length = 0;
const strokeWidth = 0.2;
canvas.width = $('#two')[0].clientWidth;
var cellSize = canvas.width / 27;
canvas.height = 7 / 27 * canvas.width;
var x = y = 0;
draw(x, y, canvas.width, canvas.height, cellSize, strokeWidth);
}

function Box(x, y, day, hour) {
this.x = x;
this.y = y;
this.day = day;
this.hour = hour;
}

function draw(x, y, w, h, cellSize, strokeWidth) {

let onePixel = cellSize * strokeWidth;
cellSize = cellSize * (1 - strokeWidth);
context.beginPath();
context.lineWidth = 0.01;
context.strokeStyle = 'rgba(255, 255, 255, 0)';

for (let i = 0; i < 7; i++) {
context.beginPath();
dayRect(context, cellSize, i, onePixel);
let startX = 3 * cellSize + onePixel;
for (let j = 0; j < 25; j++) {
context.rect(startX, i * cellSize + i * onePixel, cellSize, cellSize);
context.fillStyle = "white";
context.fill();
startX += cellSize + onePixel;
}
}
}

function dayRect(context, cellSize, day, onePixel) {
var offSet = day * onePixel;
context.rect(0, day * cellSize + offSet, 3 * cellSize, cellSize);
context.fillStyle = "white";
context.fill();
}

init();
window.addEventListener("resize", init, false);

body {
margin: auto;
color: white;
background-color: black;
}

div#two {
margin-left: 5%;
width: 10%;
height: 30%;
}

<script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous"></script>

<div id="parent">
<div>text above</div>

<div id="two">
<canvas id="posHourCanvas" width="600" height="300"></canvas>
</div>

<div>text under</div>
</div>





JSFIDDLE: http://jsfiddle.net/kgbh9352/










share|improve this question





























    1















    Basically I want background to be seen between rects. Right now I'm trying to leave a distance which is dependable on rect size between them, can I make that size to be exact number of pixels?



    Because right now these empty spaces merge, sometimes are not seen at all, etc.






    function getMousePos(canvas, evt) {
    var rect = canvas.getBoundingClientRect();
    return {
    x: evt.clientX - rect.left,
    y: evt.clientY - rect.top
    };
    }

    var canvas = document.getElementById("posHourCanvas");
    var context = canvas.getContext('2d');

    var boxes = ;

    function init() {
    context.clearRect(0, 0, canvas.width, canvas.height);
    boxes.length = 0;
    const strokeWidth = 0.2;
    canvas.width = $('#two')[0].clientWidth;
    var cellSize = canvas.width / 27;
    canvas.height = 7 / 27 * canvas.width;
    var x = y = 0;
    draw(x, y, canvas.width, canvas.height, cellSize, strokeWidth);
    }

    function Box(x, y, day, hour) {
    this.x = x;
    this.y = y;
    this.day = day;
    this.hour = hour;
    }

    function draw(x, y, w, h, cellSize, strokeWidth) {

    let onePixel = cellSize * strokeWidth;
    cellSize = cellSize * (1 - strokeWidth);
    context.beginPath();
    context.lineWidth = 0.01;
    context.strokeStyle = 'rgba(255, 255, 255, 0)';

    for (let i = 0; i < 7; i++) {
    context.beginPath();
    dayRect(context, cellSize, i, onePixel);
    let startX = 3 * cellSize + onePixel;
    for (let j = 0; j < 25; j++) {
    context.rect(startX, i * cellSize + i * onePixel, cellSize, cellSize);
    context.fillStyle = "white";
    context.fill();
    startX += cellSize + onePixel;
    }
    }
    }

    function dayRect(context, cellSize, day, onePixel) {
    var offSet = day * onePixel;
    context.rect(0, day * cellSize + offSet, 3 * cellSize, cellSize);
    context.fillStyle = "white";
    context.fill();
    }

    init();
    window.addEventListener("resize", init, false);

    body {
    margin: auto;
    color: white;
    background-color: black;
    }

    div#two {
    margin-left: 5%;
    width: 10%;
    height: 30%;
    }

    <script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous"></script>

    <div id="parent">
    <div>text above</div>

    <div id="two">
    <canvas id="posHourCanvas" width="600" height="300"></canvas>
    </div>

    <div>text under</div>
    </div>





    JSFIDDLE: http://jsfiddle.net/kgbh9352/










    share|improve this question



























      1












      1








      1








      Basically I want background to be seen between rects. Right now I'm trying to leave a distance which is dependable on rect size between them, can I make that size to be exact number of pixels?



      Because right now these empty spaces merge, sometimes are not seen at all, etc.






      function getMousePos(canvas, evt) {
      var rect = canvas.getBoundingClientRect();
      return {
      x: evt.clientX - rect.left,
      y: evt.clientY - rect.top
      };
      }

      var canvas = document.getElementById("posHourCanvas");
      var context = canvas.getContext('2d');

      var boxes = ;

      function init() {
      context.clearRect(0, 0, canvas.width, canvas.height);
      boxes.length = 0;
      const strokeWidth = 0.2;
      canvas.width = $('#two')[0].clientWidth;
      var cellSize = canvas.width / 27;
      canvas.height = 7 / 27 * canvas.width;
      var x = y = 0;
      draw(x, y, canvas.width, canvas.height, cellSize, strokeWidth);
      }

      function Box(x, y, day, hour) {
      this.x = x;
      this.y = y;
      this.day = day;
      this.hour = hour;
      }

      function draw(x, y, w, h, cellSize, strokeWidth) {

      let onePixel = cellSize * strokeWidth;
      cellSize = cellSize * (1 - strokeWidth);
      context.beginPath();
      context.lineWidth = 0.01;
      context.strokeStyle = 'rgba(255, 255, 255, 0)';

      for (let i = 0; i < 7; i++) {
      context.beginPath();
      dayRect(context, cellSize, i, onePixel);
      let startX = 3 * cellSize + onePixel;
      for (let j = 0; j < 25; j++) {
      context.rect(startX, i * cellSize + i * onePixel, cellSize, cellSize);
      context.fillStyle = "white";
      context.fill();
      startX += cellSize + onePixel;
      }
      }
      }

      function dayRect(context, cellSize, day, onePixel) {
      var offSet = day * onePixel;
      context.rect(0, day * cellSize + offSet, 3 * cellSize, cellSize);
      context.fillStyle = "white";
      context.fill();
      }

      init();
      window.addEventListener("resize", init, false);

      body {
      margin: auto;
      color: white;
      background-color: black;
      }

      div#two {
      margin-left: 5%;
      width: 10%;
      height: 30%;
      }

      <script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous"></script>

      <div id="parent">
      <div>text above</div>

      <div id="two">
      <canvas id="posHourCanvas" width="600" height="300"></canvas>
      </div>

      <div>text under</div>
      </div>





      JSFIDDLE: http://jsfiddle.net/kgbh9352/










      share|improve this question
















      Basically I want background to be seen between rects. Right now I'm trying to leave a distance which is dependable on rect size between them, can I make that size to be exact number of pixels?



      Because right now these empty spaces merge, sometimes are not seen at all, etc.






      function getMousePos(canvas, evt) {
      var rect = canvas.getBoundingClientRect();
      return {
      x: evt.clientX - rect.left,
      y: evt.clientY - rect.top
      };
      }

      var canvas = document.getElementById("posHourCanvas");
      var context = canvas.getContext('2d');

      var boxes = ;

      function init() {
      context.clearRect(0, 0, canvas.width, canvas.height);
      boxes.length = 0;
      const strokeWidth = 0.2;
      canvas.width = $('#two')[0].clientWidth;
      var cellSize = canvas.width / 27;
      canvas.height = 7 / 27 * canvas.width;
      var x = y = 0;
      draw(x, y, canvas.width, canvas.height, cellSize, strokeWidth);
      }

      function Box(x, y, day, hour) {
      this.x = x;
      this.y = y;
      this.day = day;
      this.hour = hour;
      }

      function draw(x, y, w, h, cellSize, strokeWidth) {

      let onePixel = cellSize * strokeWidth;
      cellSize = cellSize * (1 - strokeWidth);
      context.beginPath();
      context.lineWidth = 0.01;
      context.strokeStyle = 'rgba(255, 255, 255, 0)';

      for (let i = 0; i < 7; i++) {
      context.beginPath();
      dayRect(context, cellSize, i, onePixel);
      let startX = 3 * cellSize + onePixel;
      for (let j = 0; j < 25; j++) {
      context.rect(startX, i * cellSize + i * onePixel, cellSize, cellSize);
      context.fillStyle = "white";
      context.fill();
      startX += cellSize + onePixel;
      }
      }
      }

      function dayRect(context, cellSize, day, onePixel) {
      var offSet = day * onePixel;
      context.rect(0, day * cellSize + offSet, 3 * cellSize, cellSize);
      context.fillStyle = "white";
      context.fill();
      }

      init();
      window.addEventListener("resize", init, false);

      body {
      margin: auto;
      color: white;
      background-color: black;
      }

      div#two {
      margin-left: 5%;
      width: 10%;
      height: 30%;
      }

      <script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous"></script>

      <div id="parent">
      <div>text above</div>

      <div id="two">
      <canvas id="posHourCanvas" width="600" height="300"></canvas>
      </div>

      <div>text under</div>
      </div>





      JSFIDDLE: http://jsfiddle.net/kgbh9352/






      function getMousePos(canvas, evt) {
      var rect = canvas.getBoundingClientRect();
      return {
      x: evt.clientX - rect.left,
      y: evt.clientY - rect.top
      };
      }

      var canvas = document.getElementById("posHourCanvas");
      var context = canvas.getContext('2d');

      var boxes = ;

      function init() {
      context.clearRect(0, 0, canvas.width, canvas.height);
      boxes.length = 0;
      const strokeWidth = 0.2;
      canvas.width = $('#two')[0].clientWidth;
      var cellSize = canvas.width / 27;
      canvas.height = 7 / 27 * canvas.width;
      var x = y = 0;
      draw(x, y, canvas.width, canvas.height, cellSize, strokeWidth);
      }

      function Box(x, y, day, hour) {
      this.x = x;
      this.y = y;
      this.day = day;
      this.hour = hour;
      }

      function draw(x, y, w, h, cellSize, strokeWidth) {

      let onePixel = cellSize * strokeWidth;
      cellSize = cellSize * (1 - strokeWidth);
      context.beginPath();
      context.lineWidth = 0.01;
      context.strokeStyle = 'rgba(255, 255, 255, 0)';

      for (let i = 0; i < 7; i++) {
      context.beginPath();
      dayRect(context, cellSize, i, onePixel);
      let startX = 3 * cellSize + onePixel;
      for (let j = 0; j < 25; j++) {
      context.rect(startX, i * cellSize + i * onePixel, cellSize, cellSize);
      context.fillStyle = "white";
      context.fill();
      startX += cellSize + onePixel;
      }
      }
      }

      function dayRect(context, cellSize, day, onePixel) {
      var offSet = day * onePixel;
      context.rect(0, day * cellSize + offSet, 3 * cellSize, cellSize);
      context.fillStyle = "white";
      context.fill();
      }

      init();
      window.addEventListener("resize", init, false);

      body {
      margin: auto;
      color: white;
      background-color: black;
      }

      div#two {
      margin-left: 5%;
      width: 10%;
      height: 30%;
      }

      <script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous"></script>

      <div id="parent">
      <div>text above</div>

      <div id="two">
      <canvas id="posHourCanvas" width="600" height="300"></canvas>
      </div>

      <div>text under</div>
      </div>





      function getMousePos(canvas, evt) {
      var rect = canvas.getBoundingClientRect();
      return {
      x: evt.clientX - rect.left,
      y: evt.clientY - rect.top
      };
      }

      var canvas = document.getElementById("posHourCanvas");
      var context = canvas.getContext('2d');

      var boxes = ;

      function init() {
      context.clearRect(0, 0, canvas.width, canvas.height);
      boxes.length = 0;
      const strokeWidth = 0.2;
      canvas.width = $('#two')[0].clientWidth;
      var cellSize = canvas.width / 27;
      canvas.height = 7 / 27 * canvas.width;
      var x = y = 0;
      draw(x, y, canvas.width, canvas.height, cellSize, strokeWidth);
      }

      function Box(x, y, day, hour) {
      this.x = x;
      this.y = y;
      this.day = day;
      this.hour = hour;
      }

      function draw(x, y, w, h, cellSize, strokeWidth) {

      let onePixel = cellSize * strokeWidth;
      cellSize = cellSize * (1 - strokeWidth);
      context.beginPath();
      context.lineWidth = 0.01;
      context.strokeStyle = 'rgba(255, 255, 255, 0)';

      for (let i = 0; i < 7; i++) {
      context.beginPath();
      dayRect(context, cellSize, i, onePixel);
      let startX = 3 * cellSize + onePixel;
      for (let j = 0; j < 25; j++) {
      context.rect(startX, i * cellSize + i * onePixel, cellSize, cellSize);
      context.fillStyle = "white";
      context.fill();
      startX += cellSize + onePixel;
      }
      }
      }

      function dayRect(context, cellSize, day, onePixel) {
      var offSet = day * onePixel;
      context.rect(0, day * cellSize + offSet, 3 * cellSize, cellSize);
      context.fillStyle = "white";
      context.fill();
      }

      init();
      window.addEventListener("resize", init, false);

      body {
      margin: auto;
      color: white;
      background-color: black;
      }

      div#two {
      margin-left: 5%;
      width: 10%;
      height: 30%;
      }

      <script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous"></script>

      <div id="parent">
      <div>text above</div>

      <div id="two">
      <canvas id="posHourCanvas" width="600" height="300"></canvas>
      </div>

      <div>text under</div>
      </div>






      javascript html5-canvas






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 18 at 14:15









      Islam Elshobokshy

      2,3051428




      2,3051428










      asked Jan 18 at 14:11









      Ne NenneNe Nenne

      6119




      6119
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54255728%2fpixels-between-rects-instead-of-drawn-lines-in-html5-canvas%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54255728%2fpixels-between-rects-instead-of-drawn-lines-in-html5-canvas%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Liquibase includeAll doesn't find base path

          How to use setInterval in EJS file?

          Petrus Granier-Deferre