JavaScript margin right not working on canvas html5 and trouble with scroll bar removal margin-right not working,margin right not working css,margin

I have the following canvas:

Codepen link

What I want: Equal margin on both sides of canvas without any horizontal scroll bars.

Problem: margin-right property does not to work. I have seen some solutions that solve this problem by specifying a fixed width, but I cannot have a fixed width in my case. I want my canvas to adjust its width height according to the size of the window.

The following Javascript takes care of that:

window.addEventListener('resize' , resizeCanvas , false);

function resizeCanvas(){
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight/1.2;
}

So is there a different solution?

For the overflow problem, if I put overflow-x: hidden inside the body then only the scrollbar disappears but the problem persists. The canvas still extends past the screen hence the right border of the canvas is No longer visible. See here

Here is my code:

HTML

<body onload="start()">
    <canvas id="myCanvas"></canvas>
</body>

CSS

body{

}

canvas{

    border: 1px solid black;
    border-radius: 5px;
    background-color: #fff;

    margin: auto 50px auto 50px; /* works for left margin but not for right */

}

Thanks!

Another thing:

I have not set width: 100% for the canvas because it distorts the content inside it.

Answer:1

As Chris is saying you need to set the width of the canvas lower than the full width of the page:

canvas.width = window.innerWidth - 100;

Note that you need to take the border-width of the canvas and in your codepen the body also has a margin of 8px into account as well:

canvas.width = window.innerWidth - 118;
Answer:2

CSS calc() method is what you need. Just subtract the margins from 100% and you get the desired result. See the demo below. CSS calc() reference

function start() {

  var canvas = document.getElementById('myCanvas');
  canvas.width = window.innerWidth;
  canvas.height = window.innerHeight / 1.2;
  var ctx = canvas.getContext('2d');

  function rand(min, max) {
    return parseInt(Math.random() * (max - min + 1), 10) + min;
  }

  function get_random_color() {
    var h = rand(1, 360);
    var s = rand(30, 100);
    var l = rand(30, 70);
    return 'hsl(' + h + ',' + s + '%,' + l + '%)';
  }

  function getRandomInt(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
  }

  var balls = [];
  var ballCount = getRandomInt(2, 10);
  //document.getElementById('ballCountInfo').innerHTML = ballCount;
  //document.getElementById('box').innerHTML = ballCount;
  var startpointX = 100;
  var startpointY = 50;

  for (var i = 0; i < ballCount; i++) {

    var randValue = getRandomInt(20, 30);
    balls.push({
      x: startpointX,
      y: startpointY,
      vx: getRandomInt(3, 3) * direction(),
      vy: getRandomInt(1, 1) * direction(),
      radius: randValue,
      mass: randValue,
      color: get_random_color(),

      draw: function() {
        ctx.beginPath();
        ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, true);
        ctx.closePath();
        ctx.fillStyle = this.color;
        ctx.fill();
      }
    });

    startpointX = startpointX + 50;
    startpointY = startpointY + 40;
  }


  function direction() {
    var chosenValue = Math.random() < 0.5 ? 1 : -1;
    return chosenValue;
  }

  function draw() {

    ctx.clearRect(0, 0, canvas.width, canvas.height);
    for (var i = 0; i < ballCount; i++) {

      balls[i].draw();
      balls[i].x += balls[i].vx;
      balls[i].y += balls[i].vy;

      if ((balls[i].y + balls[i].vy + balls[i].radius) > canvas.height || (balls[i].y + balls[i].vy - balls[i].radius) < 0) {
        balls[i].vy = -balls[i].vy;
      }
      if ((balls[i].x + balls[i].vx + balls[i].radius) > canvas.width || (balls[i].x + balls[i].vx - balls[i].radius) < 0) {
        balls[i].vx = -balls[i].vx;
      }
    }

    //		onBoxTouched();

    //collision check
    for (var i = 0; i < ballCount; i++) {
      for (var j = i + 1; j < ballCount; j++) {

        var distance = Math.sqrt(
          (balls[i].x - balls[j].x) * (balls[i].x - balls[j].x) +
          (balls[i].y - balls[j].y) * (balls[i].y - balls[j].y)
        );

        if (distance < (balls[i].radius + balls[j].radius)) {

          var ax = (balls[i].vx * (balls[i].mass - balls[j].mass) + (2 * balls[j].mass * balls[j].vx)) / (balls[i].mass + balls[j].mass);
          var ay = (balls[i].vy * (balls[i].mass - balls[j].mass) + (2 * balls[j].mass * balls[j].vy)) / (balls[i].mass + balls[j].mass);
          balls[j].vx = (balls[j].vx * (balls[j].mass - balls[i].mass) + (2 * balls[i].mass * balls[i].vx)) / (balls[i].mass + balls[j].mass);
          balls[j].vy = (balls[j].vy * (balls[j].mass - balls[i].mass) + (2 * balls[i].mass * balls[i].vy)) / (balls[i].mass + balls[j].mass);
          balls[i].vx = ax;
          balls[i].vy = ay;
        }
      }
    }

    raf = window.requestAnimationFrame(draw);
  }


  function onBoxTouched() {

    for (var i = 0; i < ballCount; i++) {

      if (balls[i].x + balls[i].radius > 600 && balls[i].x + balls[i].radius < 750 &&
        balls[i].y + balls[i].radius > 200 && balls[i].y + balls[i].radius < 350) {

        //var ele = document.getElementById("box");
        ele.style.backgroundColor = balls[i].color;

        balls.splice(i, 1);
        ballCount = ballCount - 1;

        if (ballCount == 0) {
          ele.style.fontSize = "x-large";
          ele.innerHTML = "Over";
        } else {
          ele.innerHTML = ballCount;
        }

        //document.getElementById('ballCountInfo').innerHTML=" "+ballCount;
      }
    }
  }

  window.requestAnimationFrame(draw);

  window.addEventListener('resize', resizeCanvas, false);

  function resizeCanvas() {
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight / 1.2;
  }



}
* {}

html,
body {}

canvas {
  border: 1px solid black;
  border-radius: 5px;
  background-color: #fff;
  width: calc(100% - 40px);
  /*substract the total margin from 100% and will automoatically adjuts accordint to your need*/
  margin: auto 20px auto 20px;
  /* works for left margin but not for right */
}

#box {
  width: 150px;
  height: 150px;
  background-color: plum;
  border-radius: 5px;
  position: absolute;
  top: 200px;
  left: 600px;
  font-size: 72px;
  font-weight: bold;
  color: white;
  line-height: 150px;
  text-align: center;
}

#info {
  float: left;
  font-size: 24px;
  color: #6D8390;
  margin-top: 20px;
}
<body onload="start()">
  <canvas id="myCanvas"></canvas>
</body>
Answer:3

I need to convert megabytes to the appropriate unit. parseInt(10000) * 1024 * 1024 //10485760000 byte filesize(parseInt(10000) * 1024 * 1024); // "9.77 GB" 9.77 GB seems to be wrong. When I convert ...

I need to convert megabytes to the appropriate unit. parseInt(10000) * 1024 * 1024 //10485760000 byte filesize(parseInt(10000) * 1024 * 1024); // "9.77 GB" 9.77 GB seems to be wrong. When I convert ...

  1. bytes human readable
  2. bytes human readable online
  3. bytes human readable javascript
  4. java byte human readable
  5. bytes to human readable python
  6. golang bytes human readable
  7. bytes to human readable bash
  8. bytes to human readable php
  9. bytes to human readable c#
  10. bytes to human readable calculator
  11. postgres bytes human readable
  12. byte array to human readable string
  13. java byte size human readable
  14. human readable byte count
  15. python format bytes human readable
  16. javascript format bytes human readable
  17. golang format bytes human readable
  18. python print bytes human readable
  19. c# format bytes human readable
  20. java format bytes human readable

I'm trying to crete 3 HTML collections containing all my links on a page, so I can attach 3 separate function to each categories of links. My first HTML collection is "header links", the second is "...

I'm trying to crete 3 HTML collections containing all my links on a page, so I can attach 3 separate function to each categories of links. My first HTML collection is "header links", the second is "...

  1. remove items from list python
  2. remove items from array javascript
  3. remove items from list
  4. remove items from amazon history
  5. remove items from startup windows 10
  6. remove items from apple wallet
  7. remove items from launchpad
  8. remove items from dictionary python
  9. remove items from icloud
  10. remove items from kindle library
  11. remove items from list c#
  12. remove items from etsy cart
  13. remove items from menu bar mac
  14. remove items from dock
  15. remove items from google search
  16. remove items from google drive
  17. remove items from photos
  18. remove items from pictures
  19. remove items from buy again amazon
  20. remove items from quick access

I wanted to have a way so users can choose images to send with a review. so I use dropzone.js . It seems like having problems sending multiple images in one request. I thought that req.files would ...

I wanted to have a way so users can choose images to send with a review. so I use dropzone.js . It seems like having problems sending multiple images in one request. I thought that req.files would ...

  1. error unexpected field multer
  2. error unexpected field
  3. error unexpected field at makeerror
  4. multer error unexpected field postman
  5. multer error unexpected field node js
  6. nodejs multer error unexpected field
  7. node multer error unexpected field
  8. express multer error unexpected field
  9. npm multer error unexpected field
  10. error unexpected value for instancetype field
  11. jq error syntax error unexpected field
  12. syntax error unexpected parseop_field
  13. multer error unexpected field at wrapped filefilter

Trying to get post tags with wordpress API - api call is /wp-json/wp/v2/posts ourHTMLString += '<i class="fa fa-tags">"' + postsData[i].tags + '"</i>'; It is returning these values "...

Trying to get post tags with wordpress API - api call is /wp-json/wp/v2/posts ourHTMLString += '<i class="fa fa-tags">"' + postsData[i].tags + '"</i>'; It is returning these values "...

  1. post tags and categories for pages
  2. post tags wordpress
  3. post tags and categories for pages plugin
  4. post tags and categories for pages wordpress
  5. post_tags_meta_box
  6. post tags mod apk
  7. post tags facebook
  8. post tags reddit
  9. post tags css
  10. post tags seo
  11. post tags laravel
  12. post tags wp
  13. post tags jekyll
  14. post tag shortcode
  15. pos tags list
  16. pos tags nltk
  17. post tags html
  18. post tags widget wordpress
  19. post tags wordpress php
  20. post tags in php