Cam*_*ron 62 javascript html5 canvas
我正在使用HTML Canvas使用以下应用程序:http://driz.co.uk/particles/
目前设置为640x480像素,但我想将其全屏显示,因为它将显示为投影仪.但是据我所知,除了数字而不是%之外,我不能将画布大小设置为100%作为变量.使用CSS只是拉伸它而不是使它实际全屏.
有任何想法吗?
编辑:尝试使用jQuery找到高度和宽度,但它打破画布任何想法为什么?
var $j = jQuery.noConflict();
var canvas;
var ctx;
var canvasDiv;
var outerDiv;
var canvasW = $j('body').width();
var canvasH = $j('body').height();
//var canvasW = 640;
//var canvasH = 480;
var numMovers = 550;
var movers = [];
var friction = .96;
var radCirc = Math.PI * 2;
var mouseX, mouseY, mouseVX, mouseVY, prevMouseX = 0, prevMouseY = 0;
var isMouseDown = true;
function init()
{
canvas = document.getElementById("mainCanvas");
if( canvas.getContext )
{
setup();
setInterval( run , 33 );
}
}
function setup()
{
outerDiv = document.getElementById("outer");
canvasDiv = document.getElementById("canvasContainer");
ctx = canvas.getContext("2d");
var i = numMovers;
while( i-- )
{
var m = new Mover();
m.x = canvasW * .5;
m.y = canvasH * .5;
m.vX = Math.cos(i) * Math.random() * 25;
m.vY = Math.sin(i) * Math.random() * 25;
m.size = 2;
movers[i] = m;
}
document.onmousedown = onDocMouseDown;
document.onmouseup = onDocMouseUp;
document.onmousemove = onDocMouseMove;
}
function run()
{
ctx.globalCompositeOperation = "source-over";
ctx.fillStyle = "rgba(8,8,12,.65)";
ctx.fillRect( 0 , 0 , canvasW , canvasH );
ctx.globalCompositeOperation = "lighter";
mouseVX = mouseX - prevMouseX;
mouseVY = mouseY - prevMouseY;
prevMouseX = mouseX;
prevMouseY = mouseY;
var toDist = canvasW / 1.15;
var stirDist = canvasW / 8;
var blowDist = canvasW / 2;
var Mrnd = Math.random;
var Mabs = Math.abs;
var Msqrt = Math.sqrt;
var Mcos = Math.cos;
var Msin = Math.sin;
var Matan2 = Math.atan2;
var Mmax = Math.max;
var Mmin = Math.min;
var i = numMovers;
while( i-- )
{
var m = movers[i];
var x = m.x;
var y = m.y;
var vX = m.vX;
var vY = m.vY;
var dX = x - mouseX;
var dY = y - mouseY;
var d = Msqrt( dX * dX + dY * dY );
var a = Matan2( dY , dX );
var cosA = Mcos( a );
var sinA = Msin( a );
if( isMouseDown )
{
if( d < blowDist )
{
var blowAcc = ( 1 - ( d / blowDist ) ) * 2;
vX += cosA * blowAcc + .5 - Mrnd();
vY += sinA * blowAcc + .5 - Mrnd();
}
}
if( d < toDist )
{
var toAcc = ( 1 - ( d / toDist ) ) * canvasW * .0014;
vX -= cosA * toAcc;
vY -= sinA * toAcc;
}
if( d < stirDist )
{
var mAcc = ( 1 - ( d / stirDist ) ) * canvasW * .00022;
vX += mouseVX * mAcc;
vY += mouseVY * mAcc;
}
vX *= friction;
vY *= friction;
var avgVX = Mabs( vX );
var avgVY = Mabs( vY );
var avgV = ( avgVX + avgVY ) * .5;
if( avgVX < .1 ) vX *= Mrnd() * 3;
if( avgVY < .1 ) vY *= Mrnd() * 3;
var sc = avgV * .45;
sc = Mmax( Mmin( sc , 3.5 ) , .4 );
var nextX = x + vX;
var nextY = y + vY;
if( nextX > canvasW )
{
nextX = canvasW;
vX *= -1;
}
else if( nextX < 0 )
{
nextX = 0;
vX *= -1;
}
if( nextY > canvasH )
{
nextY = canvasH;
vY *= -1;
}
else if( nextY < 0 )
{
nextY = 0;
vY *= -1;
}
m.vX = vX;
m.vY = vY;
m.x = nextX;
m.y = nextY;
ctx.fillStyle = m.color;
ctx.beginPath();
ctx.arc( nextX , nextY , sc , 0 , radCirc , true );
ctx.closePath();
ctx.fill();
}
//rect( ctx , mouseX - 3 , mouseY - 3 , 6 , 6 );
}
function onDocMouseMove( e )
{
var ev = e ? e : window.event;
mouseX = ev.clientX - outerDiv.offsetLeft - canvasDiv.offsetLeft;
mouseY = ev.clientY - outerDiv.offsetTop - canvasDiv.offsetTop;
}
function onDocMouseDown( e )
{
isMouseDown = true;
return false;
}
function onDocMouseUp( e )
{
isMouseDown = true;
return false;
}
// ==========================================================================================
function Mover()
{
this.color = "rgb(" + Math.floor( Math.random()*255 ) + "," + Math.floor( Math.random()*255 ) + "," + Math.floor( Math.random()*255 ) + ")";
this.y = 0;
this.x = 0;
this.vX = 0;
this.vY = 0;
this.size = 0;
}
// ==========================================================================================
function rect( context , x , y , w , h )
{
context.beginPath();
context.rect( x , y , w , h );
context.closePath();
context.fill();
}
// ==========================================================================================
Run Code Online (Sandbox Code Playgroud)
Nic*_*sen 52
这个javascript有
var canvasW = 640;
var canvasH = 480;
Run Code Online (Sandbox Code Playgroud)
在里面.尝试更改它们以及画布的CSS.
或者更好的是,让初始化函数从css确定画布的大小!
为了响应您的编辑,请更改您的init函数:
function init()
{
canvas = document.getElementById("mainCanvas");
canvas.width = document.body.clientWidth; //document.width is obsolete
canvas.height = document.body.clientHeight; //document.height is obsolete
canvasW = canvas.width;
canvasH = canvas.height;
if( canvas.getContext )
{
setup();
setInterval( run , 33 );
}
}
Run Code Online (Sandbox Code Playgroud)
同时从包装器中删除所有的css,这只是简单的东西.你必须编辑js才能完全摆脱它们......虽然我能够全屏显示它们.
html, body {
overflow: hidden;
}
Run Code Online (Sandbox Code Playgroud)
编辑:document.width
并且document.height
已过时.替换为document.body.clientWidth
和document.body.clientHeight
Mar*_*tyn 36
您只需将以下内容插入主html页面或函数:
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
Run Code Online (Sandbox Code Playgroud)
那应该做的
小智 16
最新的Chrome和Firefox支持全屏API,但设置为全屏就像一个窗口调整大小.收听window-object的onresize-Event:
$(window).bind("resize", function(){
var w = $(window).width();
var h = $(window).height();
$("#mycanvas").css("width", w + "px");
$("#mycanvas").css("height", h + "px");
});
//using HTML5 for fullscreen (only newest Chrome + FF)
$("#mycanvas")[0].webkitRequestFullScreen(Element.ALLOW_KEYBOARD_INPUT); //Chrome
$("#mycanvas")[0].mozRequestFullScreen(); //Firefox
//...
//now i want to cancel fullscreen
document.webkitCancelFullScreen(); //Chrome
document.mozCancelFullScreen(); //Firefox
Run Code Online (Sandbox Code Playgroud)
这在每个浏览器中都不起作用.您应检查函数是否存在,否则将引发js-error.
有关html5-fullscreen的更多信息请查看:http: //updates.html5rocks.com/2011/10/Let-Your-Content-Do-the-Talking-Fullscreen-API
Bli*_*xxy 10
您需要做的就是动态地将width和height属性设置为画布的大小.所以你使用CSS让它延伸到整个浏览器窗口,然后你在javascript中有一个小函数来测量宽度和高度,并分配它们.我对jQuery并不十分熟悉,所以请考虑这个伪代码:
window.onload = window.onresize = function() {
theCanvas.width = theCanvas.offsetWidth;
theCanvas.height = theCanvas.offsetHeight;
}
Run Code Online (Sandbox Code Playgroud)
元素的width和height属性决定了它在内部渲染缓冲区中使用的像素数.将这些更改为新数字会导致画布使用不同大小的空白缓冲区重新初始化.如果宽度和高度属性不符合实际的真实世界像素宽度和高度,浏览器将仅展开图形.
因为它尚未发布,并且是简单的CSS修复程序:
#canvas {
position:fixed;
left:0;
top:0;
width:100%;
height:100%;
}
Run Code Online (Sandbox Code Playgroud)
如果您想应用全屏画布背景(例如,使用Granim.js),则效果很好。
小智 6
如果您想在演示文稿中显示它,请考虑使用 requestFullscreen()方法
let canvas = document.getElementById("canvas_id");
canvas.requestFullscreen();
Run Code Online (Sandbox Code Playgroud)
无论当前情况如何,这都应该使其全屏显示。
另请检查支持表https://caniuse.com/?search=requestFullscreen
小智 5
在文档加载时设置
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
129471 次 |
最近记录: |