1

I am trying to resize a CSS Canvas, and it is controlled with this Javascript currently.

I'm not best at Javascript : but is there a way to get the screenwidth and then just use that data rather than a less than and else.

So say screen size is 390x500. Can I not just get the screen.width and say -5 from it. Make it that size? So 390x500 would be '385'.

        screenwidth = screen.width;

            if (screenwidth < 400) {
                canvas.width = 375;
                canvas.height = 200;
            } else {
                canvas.width = 500;
                canvas.height = 300;
            }

Sorry, I didn't clarify. This is for sizing a signature pad. Messing with the CSS or HTML mucks everything. The sizing is handled within a large Javascript with coordinates etc...

Answer is: canvas.width = screen.width - 5;

Thx, don't know why I didn't see that was what I should have tried.

5
  • 2
    canvas.width = screen.width - 5? Commented Nov 27, 2018 at 2:50
  • CSS Canvas? You mean <canvas>? That's HTML. Commented Nov 27, 2018 at 2:52
  • Can I not just get the screen.width and say -5 from it. Make it that size? So 390x500 would be '385'. Have you tried? Commented Nov 27, 2018 at 2:53
  • 1
    Conditional styles are what CSS Media Queries are for. Also, in CSS, you can just say canvas { width:calc(100% - 5px); }, to get your result. No JavaScript needed. Commented Nov 27, 2018 at 2:55
  • Phix thx : that worked. I tweaked a bit to be - 25. But seems to work fine. Dacre - havent tried urs but may. Its for a signature pad and playing with the CSS wrecks everything. The width call only seems to work in the Javascript. Don't know why I didn't see that as plain as day. Commented Nov 27, 2018 at 17:31

3 Answers 3

1

Yes this is possible - you might find document.body.clientWidth is more useful for what you are trying to do here, as this will give you the dimensions of your current document (webpage) rather than dimenions of your entire phyiscal display in pixels (ie via screen).

So for instance, if you wanted the canvas' width to dynamically update to be 5px less than the current webpage width, you could do this:

function updateCanvasSize() {
  
  var canvas = document.body.querySelector('canvas');
  
  // Set canvas width to document width, minus 5
  canvas.width = document.body.clientWidth - 5;
}

// Causes canvas resize to happen when window is resized
window.addEventListener('resize', updateCanvasSize);

// Init canvas size on startup
updateCanvasSize();
canvas {
border:1px solid red;
}
<canvas></canvas>

Alternatively, you could take the following pure CSS approach to achieve the same result:

canvas {
  border:1px solid red;
  width:calc(100% - 5px);
}
<canvas></canvas>

Sign up to request clarification or add additional context in comments.

2 Comments

How about just canvas { width:calc(100% - 5px); } and be done?
@ScottMarcus yes, good point - just updated the answer, thanks for that!
1

Conditional styles are what CSS Media Queries are for.

And they make the need for JavaScript go away:

/* Default styling: */
canvas { 
  border:2px solid red;
  width:calc(100% - 5px);  /* Does exactly what it looks like it does */
  height:200px;
}

/* Conditional style that only kicks in when the viewport is at least 400px wide              */
@media (min-width:400px){
  canvas { height:300px; }  /* Only define what needs to be changed */
}
<canvas></canvas>

Comments

0

Sorry, I didn't clarify. This is for sizing a signature pad. Messing with the CSS or HTML mucks everything. The sizing is handled within a large Javascript with coordinates etc...

Answer is: canvas.width = screen.width - 5;

Thx, don't know why I didn't see that was what I should have tried.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.