简体   繁体   English

我如何使画布的宽度为100%

[英]How do I make my canvas 100% width

I'm struggling trying to set my canvas width to 100% width. 我正在努力将画布宽度设置为100%宽度。 It's currently defined in px . 当前在px定义。

Width defined by var width = 4000 . var width = 4000定义的var width = 4000

But changing it to 100% doesn't work. 但是将其更改为100%无效。 (var width = 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>

You can set canvas to be 100% width of window. 您可以将画布设置为窗口的100%宽度。

Use 采用

var canvas = document.getElementById('projector');
var context = canvas.getContext('2d');
context.canvas.width = window.innerWidth;

暂无
暂无

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

相关问题 为什么我将我的 canvas 宽度设置为 JS 中的 window innerWidth 和 canvas 宽度 100% 在 ZC7A628CBA2AEFA28EB61B7 中? - Why do i have set my canvas width to window innerWidth in JS and canvas width 100% in css? 如何使用javascript生成的画布为100%宽度? - How to make canvas generated with javascript to be 100% width? 如何输出宽度=“ 100%”或宽度=“”的iframe。 不,宽度=“ 100” - How do I output an iframe with width = “100%” or width = “”. Not, width = “100” 如何确定宽度为100%? - How would I make sure the width is 100%? 使用 css 或 js 将 Canvas 缩放到屏幕的最佳方法是什么? 以及如何使画布的宽度等于高度? - What is the best way to scale an Canvas with css or js to the screen? And how do I make the width of the canvas equal to the height? EJS:我怎样才能让我的内容占据整个屏幕(例如宽度:100%)? - EJS: How can I make my content take up the whole screen (e.g. width: 100%)? 如何在Ext.FormPanel中使按钮的宽度等于100%并带有红色文本? - How do I make a button in an Ext.FormPanel be width=100% with red text? 如何使元素具有页面(文档)的 100% 宽度和高度,而不仅仅是窗口(浏览器)? - How do I make an element 100% width and height of the page (document) and not just the window (browser)? 如何更改画布的宽度或高度? - How do I alter a canvas' width or height? 如何使JQuery图像滑块达到屏幕宽度? - How do I make my JQuery image slider the width of the screen?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM