Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Responsive Canvas in Bootstrap column?

I'm new to bootstrap and working on a project to help figure it out. I also don't know much LESS yet.

I have 2 columns: col-xs-3 and col-xs-9. In the 9 column, I have a canvas that I would like to retain a particular aspect ratio. I don't actually care what the aspect ratio is, as long as it's the same everywhere. Specifically, as wide as the column and a proper height for the given width.

I've tried using percentages for width and height, but even if that did work, it'd be less than ideal in a dynamic height column.

like image 778
Corey Ogburn Avatar asked May 31 '14 19:05

Corey Ogburn


People also ask

Can we make canvas responsive?

You can set any dimensions you like, VBCanvas will take care of all the scaling for you. This makes creative truly responsive <canvas> elements extremely easy! Here is a simple example in which the canvas is always scaled to fit it's container. Much like background-size: contain; in CSS or xMidYMid meet for SVG.

Can you use bootstrap with canvas?

In Canvas, you can leverage the bootstrap grid system to layout your content, whether you are organizing images, buttons, divs, text, etc. Bootstrap is a layout that allows you to organize your content using the concept of columns and will adjust depending on the screen size.

How do you change the height and width of a canvas?

The width attribute specifies the width of the <canvas> element, in pixels. Tip: Use the height attribute to specify the height of the <canvas> element, in pixels. Tip: Each time the height or width of a canvas is re-set, the canvas content will be cleared (see example at bottom of page).


1 Answers

This example works fine for me. I think you need to do two things: remember to set the width and height properties on the canvas, and then you can set the canvas width to 100% and its height to auto. It should cause the canvas to always be the full width of its container and force its height to remain in proportion.

CSS:

canvas {   background-color: blue;    width: 100%;   height: auto; } 

HTML:

<div class="container">       <div class="row">         <div class="col-md-4">             <canvas id="canvas" width="300" height="300">               Sorry, your browser doesn't support the &lt;canvas&gt; element.             </canvas>         </div> <!-- /col-md-4 -->         <div class="col-md-4">             <p>item 1: Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>         </div> <!-- /col-md-4 -->         <div class="col-md-4">             <p>item 2: Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>         </div> <!-- /col-md-4 -->       </div> <!-- /row -->     </div> 

JS:

$( document ).ready(function() {      var c=document.getElementById("canvas");     var ctx=c.getContext("2d");     ctx.beginPath();     ctx.arc(95,50,40,0,2*Math.PI);     ctx.stroke();  }); 

A fiddle demo

like image 188
jme11 Avatar answered Sep 30 '22 13:09

jme11