Home > database >  importing a simple class, results in white screen, but no error
importing a simple class, results in white screen, but no error

Time:06-04

I have been experimenting with importing classes in Javascript and i got it to work using this exact fomular with adding type="module" to the script tag like this:

<script type="module" src="main.js"></script>

And then adding: "import" and "export default", as show down below. When i try to implement this in a working p5 sketch the screen goes white and both me and the console have no clue why?

import Tester from "./test.js";

let test;
let tenp;

function setup() {
  createCanvas(900, 900);
  tenp = new Tenprint();
  test = new Tester();
}

function draw(){
  tenp.drawit();
  lort.printlort();

}

//
class Tenprint {
  constructor() {
    this.xx = 0;
    this.yy = 0;
    this.spacing = 20;
  }
  drawit() {
    stroke(55);
    if (random(1) < 0.5) {
      fill(2, 24, 242)
      line(this.xx, this.yy, this.xx   this.spacing, this.yy   this.spacing);
    } else {
      line(this.xx, this.yy   this.spacing, this.xx   this.spacing, this.yy);
    }
    this.xx = this.xx   this.spacing;
    if (this.xx > width) {
      this.xx = 0;
      this.yy = this.yy   this.spacing;
    }
  }
}

diffrent file in same folder called: test.js

export default class Tester{
    constructor(){

    }

    printlort(){
        console.log("LOOORT");
    }
}

CodePudding user response:

replace the fill with a stroke because line has no body

CodePudding user response:

You can also create a new script above the main.js tag, with the path to the test.js file.
Then just creating a new isntance without the import statement, but just simply the variable.

This works because the test.js script is loaded before the main.js script.

  • Related