简体   繁体   English

是否可以将Canvas ID的width和height属性设置为窗口大小的90%?

[英]Is it possible to set the width and height attributes of a Canvas ID to 90% of the window size?

I'm having trouble getting the window size to be 90% of the size of the screen with Sketch.js. 我无法使用Sketch.js将窗口大小设为屏幕大小的90%。

I tried using Javascript but it starts the canvas size as huge (way bigger then the specified size) then after I start drawing it gets to the expected size. 我尝试使用Javascript,但它开始时的画布大小很大(比指定的大小要大得多),然后在我开始绘制后,画布大小便达到了预期的大小。

I have a cancel drawing button that closes out the drawing and it is on a touch-pad. 我有一个取消绘图按钮,可以关闭绘图,它位于触摸板上。 Because of this, until the user starts drawing the cancel button is not visible. 因此,在用户开始绘制之前,取消按钮是不可见的。

Here is the JS Fiddle 这是JS小提琴


 $('#mysketch').sketch(); $('canvas').attr('height', $(window).height() + 'px'); 
 <script src="http://intridea.github.io/sketch.js/lib/sketch.js"></script> <canvas id='mysketch' style="height:90%; width:100%; border: 1px solid black; "></canvas> 

Try using css , setting canvas height to 90vh , width to 90vw . 尝试使用css ,将canvas height设置为90vhwidth90vw See Viewport units: vw, vh, vmin, vmax , <length> 请参见视口单位:vw,vh,vmin,vmax<length>

 $("#mysketch").sketch(); 
 canvas { height:90vh; width:90vw; } 
 <script src="http://intridea.github.io/sketch.js/lib/sketch.js"></script> <canvas id='mysketch' style="height:90%; width:100%; border: 1px solid black; "></canvas> 

jsfiddle http://jsfiddle.net/mqzge8wd/3/ jsfiddle http://jsfiddle.net/mqzge8wd/3/

You can simply declare a variable like this 您可以像这样简单地声明一个变量

var procentOfHeight = $(window).height() * 0.9;

$('#mysketch').sketch();
$('canvas').css('height', procentOfHeight + 'px');

I have edited the code and changed attr to css when you set the height. 设置高度后,我已经编辑了代码并将attr更改为css。 Now its working. 现在可以正常工作了。 Try the snippet below where I have a hight of 50%. 请尝试以下我最高达到50%的代码段。

 var procentOfHeight = $(window).height() * 0.5; $('#mysketch').sketch(); $('canvas').css('height', procentOfHeight + 'px'); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="http://intridea.github.io/sketch.js/lib/sketch.js"></script> <canvas id='mysketch' style=" width:100%; border: 1px solid black; "></canvas> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM