JavaScript Get canvas width and height using javascript canvas width height css,canvas width height 100,canvas width

I'm currently trying to receive the width and height of a canvas. The canvas is using a style parameter as you can see in the following code:

<canvas id="ctx" style="height: calc(100vh - 142px); width: 100vw; display: block;"></canvas>

<script type="text/javascript">
    var ctx = document.getElementById("ctx").getContext("2d");

    console.log(ctx.width);

    var socket = io();

    socket.on('newPosition', function(data){
        ctx.clearRect(0, 0, ctx.width, ctx.height);
        ctx.fillText('P', data.x, data.y);
    })
</script>

I've tried to get the value using console.log(ctx.width); which returned a undefined value. I think the problem occurs because im using the style tag to set the canvas to a autosize.

How could i receive the canvas width and height in another way?

I appreciate any kind of suggestions.

Answer:1

The canvas is a special case when it comes to the width and height.

The canvas.style.width and canvas.style.height define the width and height of the canvas as displayed on the page.

The canvas.width and canvas.height properties define the size of the canvas in pixels or the resolution of the canvas. The canvas resolution does not always match the canvas display size.

You can get the canvas resolution by just getting the width and height properties.

var width = canvas.width;
var height = canvas.height;

The canvas resolution will not have any units at the end so can be directly converted to a Number. The canvas.width and canvas.height properties are always in pixels.

If you do not set the canvas.width and canvas.height properties the canvas resolution will default to 300 by 150.

You can access the 2D context canvas via the context.canvas property. The context does not have a width and height property.

 // get resolution
 var width = ctx.canvas.width;
 var height = ctx.canvas.height;

 // get display size. Note values will have units eg px,em,%
 var displayWidth = ctx.canvas.style.width;
 var displayHeight = ctx.canvas.style.height;

Oh and as pointed out in the comments. Style attributes will only be available if set.

Answer:2

What is the difference between the two? // Chrome console Array(2); // [undefined × 2] Array(2).map(() => 1); // [undefined × 2] [...Array(2)]; // [undefined, ...

What is the difference between the two? // Chrome console Array(2); // [undefined × 2] Array(2).map(() => 1); // [undefined × 2] [...Array(2)]; // [undefined, ...

  1. what difference between bourbon and whiskey
  2. what difference between vegan and vegetarian
  3. what difference between psoriasis and eczema
  4. what difference between flu a and b
  5. what difference between ipad and ipad air
  6. what difference between republic and democracy
  7. what difference between inc and llc
  8. what difference between chromebook and laptop
  9. what difference between md and do
  10. what difference between virus and bacteria
  11. what difference between w2 and w4
  12. what difference between perfume and eau de toilette
  13. what difference between cold and flu
  14. what difference between catholic and christian
  15. what difference between diabetes 1 and 2
  16. what difference between california king and king
  17. what difference between macbook air and pro
  18. what difference between airpod 1 and 2
  19. what difference between medicare and medicaid
  20. what difference between bipolar 1 and 2

I need to build two force graphs using d3 and make them both zoom/pan at the same time. I'm acheving this by forcing the d3.event.scale() and d3.event.translate() properties from within the zoom event ...

I need to build two force graphs using d3 and make them both zoom/pan at the same time. I'm acheving this by forcing the d3.event.scale() and d3.event.translate() properties from within the zoom event ...

I have component that looks something like this: <text-field name=myusername>Username</text-field> I'd like to place "Username" in the placeholder attribute of an input, kind of like ...

I have component that looks something like this: <text-field name=myusername>Username</text-field> I'd like to place "Username" in the placeholder attribute of an input, kind of like ...

  1. angular ng-content attribute
  2. angular ng-content select attribute
  3. angular 2 ng-content attribute

I've implemented GTM on an ecommerce site and have some tags set up for a few thins (page impressions, add to cart etc.) These all work and track in Analytics as expected however I've now added the ...

I've implemented GTM on an ecommerce site and have some tags set up for a few thins (page impressions, add to cart etc.) These all work and track in Analytics as expected however I've now added the ...

  1. google tag manager enhanced ecommerce
  2. google tag manager enhanced ecommerce tutorial
  3. google tag manager enhanced ecommerce data layer
  4. google tag manager enhanced ecommerce tracking
  5. google tag manager enhanced ecommerce purchase
  6. magento google tag manager enhanced ecommerce
  7. shopify google tag manager enhanced ecommerce
  8. google tag manager enable enhanced ecommerce
  9. google tag manager enhanced ecommerce (ua) - pro
  10. google tag manager enhanced ecommerce prestashop
  11. cdc google tag manager enhanced ecommerce