I use bootstrap. I want the user to be able to choose the canvas size while keeping the design screen responsive within the div.
<div class="row"> <div class="col-xs-2 col-sm-2" id="border">content left</div> <div class="col-xs-6 col-sm-6" id="border"> Width <input type="number" class="form-control"><br> Height <input type="number" class="form-control"><br> canvas <canvas id="canvas" width="500" height="300"> </canvas> </div> <div class="col-xs-2 col-sm-2" id="border">content right</div>
How can I limit the size of the canvas to the size of the div?
I do not know if it will be necessary to use JavaScript.
Edit
It should be taken into account that the width and height values are entered by the user and the canvas must be in div proportional in size
https://jsfiddle.net/1a11p3ng/2/
Resizing the canvas on the fly is quite easy. To do it, simply set the width and height properties of the Canvas object, and then redraw the canvas contents: Canvas . width = 600 ; Canvas .
Code to make canvas occupy full page :innerWidth; canvas. height = window. innerHeight; //Done! Enjoy full page canvas!
You can have a responsive canvas in 3 short and simple steps:
Remove the width
and height
attributes from your <canvas>
.
<canvas id="responsive-canvas"></canvas>
Using CSS, set the width
of your canvas to 100%
.
#responsive-canvas { width: 100%; }
Using JavaScript, set the height to some ratio of the width.
var canvas = document.getElementById('responsive-canvas'); var heightRatio = 1.5; canvas.height = canvas.width * heightRatio;
To change width is not that hard. Just remove the width
attribute from the tag and add width: 100%;
in the css for #canvas
#canvas{ border: solid 1px blue; width: 100%; }
Changing height is a bit harder: you need javascript. I have used jQuery because i'm more comfortable with.
you need to remove the height
attribute from the canvas tag and add this script:
<script> function resize(){ $("#canvas").outerHeight($(window).height()-$("#canvas").offset().top- Math.abs($("#canvas").outerHeight(true) - $("#canvas").outerHeight())); } $(document).ready(function(){ resize(); $(window).on("resize", function(){ resize(); }); }); </script>
You can see this fiddle: https://jsfiddle.net/1a11p3ng/3/
EDIT:
To answer your second question. You need javascript
0) First of all i changed your #border id into a class since ids must be unique for an element inside an html page (you can't have 2 tags with the same id)
.border{ border: solid 1px black; } #canvas{ border: solid 1px blue; width: 100%; }
1) Changed your HTML to add ids where needed, two inputs and a button to set the values
<div class="row"> <div class="col-xs-2 col-sm-2 border">content left</div> <div class="col-xs-6 col-sm-6 border" id="main-content"> <div class="row"> <div class="col-xs-6"> Width <input id="w-input" type="number" class="form-control"> </div> <div class="col-xs-6"> Height <input id="h-input" type="number" class="form-control"> </div> <div class="col-xs-12 text-right" style="padding: 3px;"> <button id="set-size" class="btn btn-primary">Set</button> </div> </div> canvas <canvas id="canvas"></canvas> </div> <div class="col-xs-2 col-sm-2 border">content right</div> </div>
2) Set the canvas height and width so that it fits inside the container
$("#canvas").outerHeight($(window).height()-$("#canvas").offset().top-Math.abs( $("#canvas").outerHeight(true) - $("#canvas").outerHeight()));
3) Set the values of the width and height forms
$("#h-input").val($("#canvas").outerHeight()); $("#w-input").val($("#canvas").outerWidth());
4) Finally, whenever you click on the button you set the canvas width and height to the values set. If the width value is bigger than the container's width then it will resize the canvas to the container's width instead (otherwise it will break your layout)
$("#set-size").click(function(){ $("#canvas").outerHeight($("#h-input").val()); $("#canvas").outerWidth(Math.min($("#w-input").val(), $("#main-content").width())); });
See a full example here https://jsfiddle.net/1a11p3ng/7/
UPDATE 2:
To have full control over the width you can use this:
<div class="container-fluid"> <div class="row"> <div class="col-xs-2 border">content left</div> <div class="col-xs-8 border" id="main-content"> <div class="row"> <div class="col-xs-6"> Width <input id="w-input" type="number" class="form-control"> </div> <div class="col-xs-6"> Height <input id="h-input" type="number" class="form-control"> </div> <div class="col-xs-12 text-right" style="padding: 3px;"> <button id="set-size" class="btn btn-primary">Set</button> </div> </div> canvas <canvas id="canvas"> </canvas> </div> <div class="col-xs-2 border">content right</div> </div> </div> <script> $(document).ready(function(){ $("#canvas").outerHeight($(window).height()-$("#canvas").offset().top-Math.abs( $("#canvas").outerHeight(true) - $("#canvas").outerHeight())); $("#h-input").val($("#canvas").outerHeight()); $("#w-input").val($("#canvas").outerWidth()); $("#set-size").click(function(){ $("#canvas").outerHeight($("#h-input").val()); $("#main-content").width($("#w-input").val()); $("#canvas").outerWidth($("#main-content").width()); }); }); </script>
https://jsfiddle.net/1a11p3ng/8/
the content left and content right columns will move above and belove the central div if the width is too high, but this can't be helped if you are using bootstrap. This is not, however, what responsive means. a truly responsive site will adapt its size to the user screen to keep the layout as you have intended without any external input, letting the user set any size which may break your layout does not mean making a responsive site.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With