javascript - TypeError : this. Canvas 未定义

标签 javascript html5-canvas

我正在尝试用 javascript 手动编写一个 dart 演示。我收到一个奇怪的 TypeError: this.canvas is undefined 错误,该错误源自 SolarSystem 类中的 draw 函数,使用的是 firebug 和类似的在 Chrome 。我不明白为什么会这样。

这是我在 JS Bin 中的代码的链接,您可以使用它。 http://jsbin.com/udujep/1/edit

为了后代,这里是完整的 javascript 代码:

var main, fpsAverage, showFps, Point, SolarSystem, PlanetaryBody;
main = function(){
  var solarSystem;
  solarSystem = new SolarSystem(document.getElementById('container'));
  solarSystem.start();
};
showFps = function(fps){
  if (fpsAverage != null) {
    fpsAverage = fps;
  }
  fpsAverage = fps * 0.05 + fpsAverage * 0.95;
  document.getElementById('notes').textContent = Math.round(fpsAverage) + ' fps';
};
Point = (function(){
  Point.displayName = 'Point';
  var prototype = Point.prototype, constructor = Point;
  function Point(x, y){
    var ref$;
    ref$ = [x, y], this.x = ref$[0], this.y = ref$[1];
  }
  return Point;
}());
SolarSystem = (function(){
  SolarSystem.displayName = 'SolarSystem';
  var prototype = SolarSystem.prototype, constructor = SolarSystem;
  prototype.canvas = null;
  prototype.renderTime = null;
  function SolarSystem(canvas){
    this.canvas = canvas;
  }
  prototype.start = function(){
    this.width = this.canvas.parentNode.clientWidth;
    this.height = this.canvas.parentNode.clientWidth;
    this.canvas.width = this.width;
    this._start();
  };
  prototype._start = function(){
    var earth, f, h, g, jupiter;
    this.sun = new PlanetaryBody(this, 'Sun', '#ff2', 14.0);
    this.sun.addPlanet(new PlanetaryBody(this, 'Mercury', 'orange', 0.382, 0.387, 0.241));
    this.sun.addPlanet(new PlanetaryBody(this, 'Venus', 'green', 0.949, 0.723, 0.615));
    earth = new PlanetaryBody(this, 'Earth', '#33f', 1.0, 1.0, 1.0);
    this.sun.addPlanet(earth);
    earth.addPlanet(new PlanetaryBody(this, 'Moon', 'gray', 0.2, 0.14, 0.075));
    this.sun.addPlanet(new PlanetaryBody(this, 'Mars', 'red', 0.532, 1.524, 1.88));
    this.addAsteroidBelt(this.sun, 150);
    f = 0.1;
    h = 1 / 1500.0;
    g = 1 / 72.0;
    jupiter = new PlanetaryBody(this, 'Jupiter', 'gray', 4.0, 5.203, 11.86);
    this.sun.addPlanet(jupiter);
    jupiter.addPlanet(new PlanetaryBody(this, 'Io', 'gray', 3.6 * f, 421 * h, 1.769 * g));
    jupiter.addPlanet(new PlanetaryBody(this, 'Europa', 'gray', 3.1 * f, 671 * h, 3.551 * g));
    jupiter.addPlanet(new PlanetaryBody(this, 'Ganymede', 'gray', 5.3 * f, 1070 * h, 7.154 * g));
    jupiter.addPlanet(new PlanetaryBody(this, 'Callisto', 'gray', 4.8 * f, 1882 * h, 16.689 * g));
    this.requestRedraw();
  };
  prototype.draw = function(){
    var time, context;
    time = Date.now();
    if (this.renderTime != null) {
      showFps(Math.round(1000 / (time - this.renderTime)));
    }
    this.renderTime = time;
    context = this.canvas.getContext('2d');
    this.drawBackground(context);
    this.drawPlanets(context);
    this.requestRedraw();
  };
  prototype.drawBackground = function(context){
    var x$;
    x$ = context;
    x$.fillStyle = 'white';
    x$.rect(0, 0, this.width, this.height);
    x$.fill();
  };
  prototype.drawPlanets = function(context){
    this.sun.draw(context, this.width / 2, this.height / 2);
  };
  prototype.requestRedraw = function(){
    window.requestAnimationFrame(this.draw);
  };
  prototype.addAsteroidBelt = function(body, count){
    var i$, radius;
    for (i$ = 0; i$ < count; ++i$) {
      radius = 2.06 + Math.random() * (3.27 - 2.06);
      body.addPlanet(new PlanetaryBody(this, 'asteroid', '#777', 0.1 * Math.random(), radius, radius * 2));
    }
  };
  prototype.normalizeOrbitRadius = function(r){
    return r * (this.width / 10.0);
  };
  prototype.normalizePlanetSize = function(r){
    return Math.log(r + 1) * (this.width / 100.0);
  };
  return SolarSystem;
}());
PlanetaryBody = (function(){
  PlanetaryBody.displayName = 'PlanetaryBody';
  var prototype = PlanetaryBody.prototype, constructor = PlanetaryBody;
  prototype.planets = [];
  function PlanetaryBody(solarSystem, name, color, bodySize, orbitRadius, orbitPeriod){
    orbitRadius == null && (orbitRadius = 0.0);
    orbitPeriod == null && (orbitPeriod = 0.0);
    this.solarSystem = solarSystem;
    this.name = name;
    this.color = color;
    this.orbitPeriod = orbitPeriod;
    this.bodySize = solarSystem.normalizePlanetSize(bodySize);
    this.orbitRadius = solarSystem.normalizeOrbitRadius(orbitRadius);
    this.orbitSpeed = prototype._calculateSpeed(orbitPeriod);
  }
  prototype.addPlanet = function(planet){
    this.planets.push(planet);
  };
  prototype.draw = function(context, x, y){
    var pos;
    pos = this._calculatePos(x, y);
    this.drawSelf(context, pos.x, pos.y);
    this.drawChildren(context, pos.x, pos.y);
  };
  prototype.drawSelf = function(context, x, y){
    var x$;
    x$ = context;
    x$.save();
    try {
      x$.lineWidth = 0.5;
      x$.fillStyle = this.color;
      x$.strokeStyle = this.color;
      if (this.bodySize >= 2.0) {
        x$.shadowOffsetX = 2;
        x$.shadowOffsetY = 2;
        x$.shadowBlur = 2;
        x$.shadowColor = '#ddd';
      }
      x$.beginPath();
      x$.arc(x, y, this.bodySize, 0, Math.PI * 2, false);
      x$.fill();
      x$.closePath();
      x$.stroke();
      x$.shadowOffsetX = 0;
      x$.shadowOffsetY = 0;
      x$.shadowBlur = 0;
      x$.beginPath();
      x$.arc(x, y, this.bodySize, 0, Math.PI * 2, false);
      x$.fill();
      x$.closePath();
      x$.stroke();
    } finally {
      x$.restore();
    }
  };
  prototype.drawChildren = function(context, x, y){
    var i$, ref$, len$, planet;
    for (i$ = 0, len$ = (ref$ = this.planets).length; i$ < len$; ++i$) {
      planet = ref$[i$];
      planet.draw(context, x, y);
    }
  };
  prototype._calculateSpeed = function(period){
    if (period === 0.0) {
      return 0.0;
    } else {
      return 1 / (60.0 * 24.0 * 2 * period);
    }
  };
  prototype._calculatePos = function(x, y){
    var angle;
    if (this.orbitSpeed === 0.0) {
      return new Point(x, y);
    } else {
      angle = this.solarSystem.renderTime * this.orbitSpeed;
      return new Point(this.orbitRadius * Math.cos(angle) + x, this.orbitRadius * Math.sin(angle) + y);
    }
  };
  return PlanetaryBody;
}());
window.requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
window.onload = main;

最佳答案

问题是这里的这一行。

window.requestAnimationFrame(this.draw);

发生的事情是,当您将原始 draw 方法传递给 requestAnimationFrame 时,您会丢失 SolarSystem 的上下文。例如,如果您在 draw 中尝试这样做,它将返回 true。

alert(this === window)

解决方案很简单,使用闭包更改上下文。

prototype.requestRedraw = function(){
    var self = this;
    window.requestAnimationFrame(function () {self.draw()});
};

关于javascript - TypeError : this. Canvas 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13302796/

相关文章:

Javascript 在 Canvas 上绘制并将其与视频合并并一起保存

javascript - 如何使用 Cypress 断言 HTML 元素的高度或宽度大于指定数量?

javascript - 根据嵌套键值对对象数组进行排序的最快方法

javascript - 用户输入详细信息后从 JavaScript 打开 URL

javascript - 与其他函数一起返回变量

javascript - 使用 `react-hook-form` 库向组件编写测试

javascript - 在 HTML5 Canvas 上绘制矩形

javascript - 在 Javascript 中以编程方式将 SVG 转换为图像

javascript - 将 Canvas 内容序列化到 ArrayBuffer 并再次反序列化

html5-canvas - 通过 Canvas 在 antmedia 直播中嵌入 logo