在Bootstrap列中使用响应式画布?

28

我对Bootstrap还不熟悉,并在处理一个项目以了解其使用方法。同时,我对LESS也不太了解。

我有两列:col-xs-3和col-xs-9。在第九列中,我有一个画布,希望保持特定的宽高比。实际上,我并不在意宽高比是什么,只要它在每个地方都一样就可以了。具体而言,它的宽度应与列宽相同,给定宽度合适的高度。

我尝试使用百分比来设置宽度和高度,但即使这样做成功了,在动态高度的列中也不太理想。

1个回答

54

这个例子对我来说很好用。我认为你需要做两件事:记得在画布上设置widthheight属性,然后可以将画布宽度设置为100%,高度设置为auto。它应该会使画布始终是其容器的完整宽度,并强制其高度保持成比例。

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();

});

这是一个示例程序,涉及到IT技术。


3
使用鼠标在这样的画布上绘图时,基本的鼠标光标位置检测并不起作用(如果保持画布预设的具体尺寸,则可以正常工作)。你有什么想法吗? - Youp Bernoulli

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接