gpt4 book ai didi

javascript - HTML Canvas 全屏

转载 作者:IT王子 更新时间:2023-10-29 02:55:08 26 4
gpt4 key购买 nike

我正在使用 HTML Canvas 玩以下应用程序:http://driz.co.uk/particles/

目前它设置为 640x480 像素,但我想让它全屏显示,因为它会被投影仪显示。但是据我所知,我不能将 Canvas 大小设置为 100% 作为变量,除了数字而不是 %。使用 CSS 只是拉伸(stretch)它而不是让它真正全屏显示。

有什么想法吗?

编辑:尝试使用 jQuery 查找高度和宽度,但它打破了 Canvas 任何想法为什么?

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


// ==========================================================================================

最佳答案

javascript有

var canvasW     = 640;
var canvasH = 480;

在里面。尝试更改这些以及 Canvas 的 css。

或者更好的是,让初始化函数根据 css 确定 Canvas 的大小!

为响应您的编辑,更改您的初始化函数:

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

同时从包装器中删除所有的 css,那只会把东西弄得乱七八糟。你必须编辑 js 以完全摆脱它们......不过我能够全屏显示它。

html, body {
overflow: hidden;
}

编辑:document.widthdocument.height are obsolete .替换为 document.body.clientWidthdocument.body.clientHeight

关于javascript - HTML Canvas 全屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4037212/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com