1

Here is my code, I feel like I have tried everything but nothing is affecting the canvas. I have been researching for the last hour and I tried just plain javascript, but for some reason the canvas is not changing. This same code works for an iframe on a different page.

<canvas id="canvas" height="400" width="400"></canvas>
<script type="text/javascript">
$(document).ready(function () {
        $("#canvas").attr('width', 800);
    });
</script>

What I want to do is set the width equal to the window size minus 100 px, but right now I have it set as 800 so I can try to figure out why this is not working.

3
  • 1
    $("#canvas").attr('width', 800); ? Commented Dec 8, 2013 at 3:17
  • that does not work either Commented Dec 8, 2013 at 3:18
  • You have included jQuery somewhere right? The code works fine for me... using $("#canvas").attr('width,800); instead of $("canvas").width(800). (This second command did resize the canvas, it just also maintained the original aspect ratio, so the resized canvas was 800 by 800). Commented Dec 8, 2013 at 3:41

2 Answers 2

6

You have to redraw the canvas once you've resized it. First, grab the 2d context with getContext('2d'), then call the necessary functions on the context, most likely strokeRect()

var myCanvas = $('#canvas').attr('width',800);
var myContext = myCanvas.getContext('2d');
myContext.strokeRect(0,0,myCanvas.attr('width'),myCanvas.attr('height'));

I think that's right, but there could be some code errors, hopefully you get the idea.

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

2 Comments

Off-topic, is your username related to Zarathustra?
Thanks, all I had to do was put it before the chart was drawn by the script
1

How about this:

#canvas {
    width:400px;
    height:400px;
}

<canvas id="canvas"></canvas>

<script type="text/javascript">
$(document).ready(function () {
        $("#canvas").css('width','800px');
    });
</script>

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.