如何使画布的宽度达到100%
How do I make my canvas 100% width
我很难将画布宽度设置为100%宽度。它目前在px
中定义。
宽度由var width = 4000
定义。
但将其更改为100%是行不通的。(无功宽度=100%)
var VELOCITY = 0.1;
var PARTICLES = 600;
var mouse = {x:0, y:0};
var particles = [];
var colors = [ "blue","white","yellow" ];
var canvas = document.getElementById('projector');
var context;
var width = 4000;
var height = 750;
if (canvas && canvas.getContext) {
context = canvas.getContext('2d');
for( var i = 0; i < PARTICLES; i++ ) {
particles.push( {
x: Math.random()*width,
y: Math.random()*height,
vx: ((Math.random()*(VELOCITY*2))-VELOCITY),
vy: ((Math.random()*(VELOCITY*2))-VELOCITY),
size: 1+Math.random()*3,
color: colors[ Math.floor( Math.random() * colors.length ) ]
} );
}
Initialize();
}
function Initialize() {
canvas.addEventListener('mousemove', MouseMove, false);
window.addEventListener('mousedown', MouseDown, false);
window.addEventListener('resize', ResizeCanvas, false);
setInterval( TimeUpdate, 40 );
ResizeCanvas();
}
function TimeUpdate(e) {
context.clearRect(0, 0, width, height);
var len = particles.length;
var particle;
for( var i = 0; i < len; i++ ) {
particle = particles[i];
if (!particle.frozen) {
particle.x += particle.vx;
particle.y += particle.vy;
if (particle.x > width) {
particle.vx = -VELOCITY - Math.random();
}
else if (particle.x < 0) {
particle.vx = VELOCITY + Math.random();
}
else {
particle.vx *= 1 + (Math.random() * 0.005);
}
if (particle.y > height) {
particle.vy = -VELOCITY - Math.random();
}
else if (particle.y < 0) {
particle.vy = VELOCITY + Math.random();
}
else {
particle.vy *= 1 + (Math.random() * 0.005);
}
var distanceFactor = DistanceBetween( mouse, particle );
distanceFactor = Math.max( Math.min( 15 - ( distanceFactor / 10 ), 10 ), 1 );
particle.currentSize = particle.size*distanceFactor;
}
context.fillStyle = particle.color;
context.beginPath();
context.arc(particle.x,particle.y,particle.currentSize,0,Math.PI*2,true);
context.closePath();
context.fill();
}
}
function MouseMove(e) {
mouse.x = e.layerX;
mouse.y = e.layerY;
}
function MouseDown(e) {
var len = particles.length;
var closestIndex = 0;
var closestDistance = 1000;
for( var i = 0; i < len; i++ ) {
var thisDistance = DistanceBetween( particles[i], mouse );
if( thisDistance < closestDistance ) {
closestDistance = thisDistance;
closestIndex = i;
}
}
if (closestDistance < particles[closestIndex].currentSize) {
particles[closestIndex].frozen = true;
}
}
function ResizeCanvas(e) {
canvas.width = width;
canvas.height = height;
}
function DistanceBetween(p1,p2) {
var dx = p2.x-p1.x;
var dy = p2.y-p1.y;
return Math.sqrt(dx*dx + dy*dy);
}
</script>
您可以将画布设置为窗口的100%宽度。
使用
var canvas = document.getElementById('projector');
var context = canvas.getContext('2d');
context.canvas.width = window.innerWidth;
相关文章:
- 普通JS:使80%宽度元素100%IF包含一个图像
- 如何使画布的宽度达到100%
- 使用css中设置的表宽度:100%,使表内容适合宽度
- ExtJS使网格高度达到100%
- 使页面高度为浏览器的100%
- 试图使循环退出,但它目前只是继续循环100次
- 使jquery滑动滑块宽度为100%
- 使用JQuery使滚动条为@bottom时DIV的宽度为100%
- 如何使元素的宽度和高度为页面(文档)的100%,而不仅仅是窗口(浏览器)
- 按下按钮使$(“身体”).css(“高度”:“100%”)
- 如何使脸书评论框宽度为100%
- 使浮动元素的高度为100%以填充页面
- 是否有可能通过跳过前 100 帧来使 SWIFFY 对象开始播放
- 如何在javascript中使其具有100%的宽度和高度
- 如何使文本和宽度100%的分辨率和输入集中
- 是否有可能使我画的每条线都可以在画布中拖动
- 用特殊行为使二级UL占100%
- 如何使所有的输入字段(与100+输入字段已经存在)在单一形式作为“需要”
- 如何使我画的矩形(用鼠标)完全相同,我的鼠标指示
- 调整isscroll 4的大小,使其100%适合任何设备