javascript - 调整 <canvas> 元素的大小

标签 javascript html css canvas

我有以下 Canvas 。我试图使用 widthheight 属性使其更小,但它不起作用。这是我的代码:

<!DOCTYPE HTML>
<html>
  <head>
    <style>
      body {
        margin: 0px;
        padding: 0px;
      }
    </style>
  </head>
  <body>
  	<canvas id="canvas" width="200" height="50"></canvas>

    <script>

//set the variables
var a = document.getElementById('canvas'),
    c = a.getContext('2d'),
    w = a.width = window.innerWidth,
    h = a.height = window.innerHeight,
    area = w * h,
    particleNum = 300,
    ANIMATION;

var particles = [];


//create the particles
function Particle(i) {
  this.id = i;
  this.hue =  rand(50, 0, 1);
  this.active = false;
}

Particle.prototype.build = function() {
  this.x = w / 2;
  this.y = h / 2;
  this.r = rand(7, 2, 1);
  this.vx = Math.random() * 10 - 5;
  this.vy = Math.random() * 10 - 5;
  this.gravity = .01;
  this.opacity = Math.random() + .5;
  this.active = true;

  c.beginPath();
      c.arc(this.x, this.y, this.r, 0, 2 * Math.PI, false);
  c.fillStyle = "hsla(" + this.hue + ",100%,50%,1)";
  c.fill();
};

Particle.prototype.draw = function() {
  this.active = true;
  this.x += this.vx;
  this.y += this.vy;
  this.vy += this.gravity;
  this.hue -= 0.5;
  this.r = Math.abs(this.r - .05);

  c.beginPath();
      c.arc(this.x, this.y, this.r, 0, 2 * Math.PI, false);
  c.fillStyle = "hsla(" + this.hue + ",100%,50%,1)";
  c.fill();

  // reset particle
  if(this.r <= .05) {
    this.active = false;
  }
};


//functionality
function drawScene() {
  c.fillStyle = "black";
  c.fillRect(0,0,w,h);

  for(var i = 0; i < particles.length; i++) {
    if(particles[i].active === true) {
      particles[i].draw();
    } else {
      particles[i].build();
    }
  }

      ANIMATION = requestAnimationFrame(drawScene);
}

function initCanvas() {
  var s = getComputedStyle(a);

  if(particles.length) {
    particles = [];
    cancelAnimationFrame(ANIMATION);
    ANIMATION;
    console.log(ANIMATION);
  }

  w = a.width = window.innerWidth;
    h = a.height = window.innerHeight;

  for(var i = 0; i < particleNum; i++) {
    particles.push(new Particle(i));
  }

  drawScene();
  console.log(ANIMATION);
}


//init
(function() {
  initCanvas();
  addEventListener('resize', initCanvas, false);
})();


//helper functions
function rand(max, min, _int) {
  var max = (max === 0 || max)?max:1, 
      min = min || 0, 
      gen = min + (max - min) * Math.random();

  return (_int) ? Math.round(gen) : gen;
};

    </script>
  </body>
</html>      

      

最佳答案

多注意这里:

var a = document.getElementById('canvas'),
    c = a.getContext('2d'),
    w = a.width = window.innerWidth,
    h = a.height = window.innerHeight,
    area = w * h,

如您所见,w 是窗口的宽度,h 是窗口的高度。尝试使用自定义值编辑 wh 变量并告诉我它是否有效;)

编辑:

只需在您的脚本标记中将 hw 替换为:

a.style.width = '500px'; //random value, insert custom here
a.style.height = '500px';

关于javascript - 调整 <canvas> 元素的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41666211/

相关文章:

javascript - 在 chrome speechSynthesis 中获取额外的声音列表

html - 图像在文本上重叠,同时使用 TweenMax 缩放滚动

javascript - 使用 angularJS 操作同级 DOM 元素

asp.net - 支持CMS系统中的列插入

html - CSS偏移但保持全宽

javascript - 将 Javascript 函数转换为 WebAssembly.Table 的 "funcref"

javascript - 如何让模数运算符在 JavaScript 中的两个数字之间添加奇数?

javascript - 如何将非模块(!)JavaScript 导入 Polymer 3.0

html - Sass 规则不适用

html - 悬停一个 td block