2

I am encountering immediate issues in the early stages of this particle system for HTML 5 canvas. When I try to retrieve properties of my Particle class object, it returns undefined and I can't figure out why!

class Particle {
  contructor(context, width, height) {
    this.x = width / 2;
    this.y = height / 2;
    this.radius = Math.random() * 5 + 5;
  }
};

var App = {
  canvas: document.getElementById('canvas'),
  ctx: canvas.getContext('2d'),
  initialize: function() {
    this.canvas.width = window.innerWidth;
    this.canvas.height = window.innerHeight;
  },
  draw: function() {
    var P = new Particle(this.ctx, this.canvas.width, this.canvas.height);

    alert(P.x); // Why does this return undefined?

    this.ctx.beginPath();
    this.ctx.arc(P.x,P.y,P.radius,0,2*Math.PI);
    this.ctx.stroke()
  }
};

App.initialize();
App.draw();
1
  • I was trying to figure it out all this time until I saw a red underline saying spelling mistake. It is a constructor not contructor Commented May 9, 2017 at 23:29

1 Answer 1

4

I think you just have a silly typo in your Particle class constructor: it should read constructor. For instance:

class Particle {
    constructor(context, width, height) {
        ...
    }
};

Since you didn't actually initialized your P variable, all properties are undefined by design.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.