我必须根据可用的屏幕分辨率为画布元素设置属性“宽度”和“高度”。从 html 中静态设置它们(例如
$('#canv').setAttribute('width',screen.availWidth);
$('#canv').attr('width',screen.availWidth);
$('#canv').data('width',screen.availWidth);
$('#canv').width = screen.availWidth;
$('#canv').css('width',screen.availWidth);
我做了同样的操作 var cv = $('#canv')[0].getContext("2d"); 并用 cv
更改 $('#canv')
但没有结果。
BB
I have to set the attributes 'width' and 'height' for a canvas element, depending on the available screen resolution. Setting them statically from html works (ex. <canvas id="canv" width="1600" ...
). From Javascript I can't do it. I've googled and searched in SO but I didn't find anything that works. I am using JQuery and I've tried unsuccessfully the following (the same for height attribute)
$('#canv').setAttribute('width',screen.availWidth);
$('#canv').attr('width',screen.availWidth);
$('#canv').data('width',screen.availWidth);
$('#canv').width = screen.availWidth;
$('#canv').css('width',screen.availWidth);
I did the same putting var cv = $('#canv')[0].getContext("2d");
and changing $('#canv')
with cv
but with no result.
bb
发布评论
评论(2)
我将 jsfiddle.net/pUcjV/2 上的代码更改为
from
这在整个画布上产生了星星。这是想要的结果吗?
我使用的是 Chrome 5.0.375.99。
I changed the code on jsfiddle.net/pUcjV/2 to
from
This produced stars over the whole canvas. Is this the desired result?
I'm using Chrome 5.0.375.99.
您可以这样做,但我不确定屏幕尺寸是否是您想要的:
您可以在此处查看演示
You can do it like this, but I'm not sure if the screen size is what you're after:
You can see a demo here