javascript Bootstrap 中的响应式画布

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26256217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 05:43:50  来源:igfitidea点击:

Responsive Canvas in Bootstrap

javascriptcsshtmlcanvastwitter-bootstrap-3

提问by Sergiodiaz53

I'm trying to do a responsive canvas. All my tests has been doing with a 600x600 canvas and with that height and width it works OK and paint every line correctly. The problem is that I have tried this:

我正在尝试做一个响应式画布。我所有的测试都是在 600x600 的画布上进行的,并且具有该高度和宽度,它可以正常工作并正确绘制每一行。问题是我试过这个:

 #myCanvas {
    background-color: #ffffff;
    border:1px solid #000000;
    min-height: 600px;
    height: 100%;
    width: 100%;
 }

Just for the record, myCanvas is inside a sm-col-8.

只是为了记录,myCanvas 在 sm-col-8 中。

And it looks nice on my laptop and looks nice on my phone but (because of my draw() function, because it was thinking for a square) the draw starts more like in the down-left corner (nearby) and it should start at up-right corner.

它在我的笔记本电脑上看起来不错,在我的手机上看起来也不错,但是(因为我的 draw() 函数,因为它正在考虑一个正方形)绘制更像是在左下角(附近)开始,它应该从右上角。

So, I don't want to change my draw() function but what I'm looking for is to reescale the canvas size. I mean: If I'm in a laptop/tablet.. with 600x600, show it at that size, but if I'm on my phone which has 384x640 show it like 300x300? I don't know if it could be a good solution.

所以,我不想改变我的 draw() 函数,但我正在寻找的是重新调整画布大小。我的意思是:如果我在笔记本电脑/平板电脑上......以 600x600 的尺寸显示它,但如果我使用的是 384x640 的手机,则以 300x300 的尺寸显示它?我不知道这是否是一个很好的解决方案。

My draw function:

我的绘图功能:

function drawLines(lines,i,table,xtotal,ytotal){

    var c = document.getElementById("myCanvas");

    var ctx = c.getContext("2d");

    var xIni;
    var xFin;
    var yIni;
    var yFin;

    xIni = (c.width*parseInt(lines[i][1])/xtotal);
    yIni = (c.height*parseInt(lines[i][2])/ytotal);
    xFin = (c.width*parseInt(lines[i][3])/xtotal);
    yFin = (c.height*parseInt(lines[i][4])/ytotal);

    ctx.beginPath();
    ctx.moveTo(xIni,c.height-yIni);
    ctx.lineTo(xFin,c.height-yFin);
    ctx.lineWidth=4;

    ctx.strokeStyle = colorAleatorio();

    ctx.stroke();

}

回答by Elton da Costa

With Bootstrap, use:

使用 Bootstrap,使用:

<canvas id="canvas" class='img-responsive' style="border: 1px solid black;"></canvas>

回答by markE

You can make your html Canvas responsive by using the context.scalecommand.

您可以使用该context.scale命令使您的 html Canvas 具有响应性。

The .scale command will scale the internal coordinates systemused by canvas.

.scale 命令将缩放画布使用的内部坐标系

This means you do not need to change any of your own drawing coordinates because canvas will automatically transform your coordinates into scaled canvas coordinates for you.

这意味着您不需要更改您自己的任何绘图坐标,因为画布会自动将您的坐标转换为缩放的画布坐标。

// save the original width,height used in drawLines()
var origWidth=600;
var origHeight=600;
var scale=1.00;


// reference to canvas and context
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");


// call this after resizing
// send in the new maximum width,height desired
function resizeAndRedraw(newMaxWidth,newMaxHeight){

    // calc the global scaling factor that fits into the new size
    // and also maintains the original aspect ratio
    scale=Math.min((newMaxWidth/origWidth),(newMaxHeight/origHeight))

    // resize the canvas while maintaining correct aspect ratio
    canvas.width=origWidth*scale;
    canvas.height=origHeight*scale;

    // Note: changing the canvas element's width or height will
    // erase the canvas so you must reissue all your drawing commands        
    drawLines(lines,i,table,xtotal,ytotal);

}


// call drawLines 
function drawLines(lines,i,table,xtotal,ytotal){

    // scale the canvas coordinate system to the current scale
    // Note: This scales the coordinates used internally
    // by canvas. It does not resize the canvas element
    ctx.scale(s,s);

    // now do your drawing commands
    // You do not need to adjust your drawing coordinates because
    // the Canvas will do that for you
    var xIni;
    var xFin;
    var yIni;
    var yFin;

    xIni = (c.width*parseInt(lines[i][1])/xtotal);
    yIni = (c.height*parseInt(lines[i][2])/ytotal);
    xFin = (c.width*parseInt(lines[i][3])/xtotal);
    yFin = (c.height*parseInt(lines[i][4])/ytotal);

    ctx.beginPath();
    ctx.moveTo(xIni,c.height-yIni);
    ctx.lineTo(xFin,c.height-yFin);
    ctx.lineWidth=4;

    ctx.strokeStyle = colorAleatorio();

    ctx.stroke();

    // restore the context to it's unscaled state
    ctx.scale(-s,-s);

}