This is my āfinal codeā:
index.html
<!doctype html>
<html class="no-js" lang="">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
<link rel="stylesheet" href="css/style.css">
<meta name="description" content="">
<meta property="og:title" content="">
<meta property="og:type" content="">
<meta property="og:url" content="">
<meta property="og:image" content="">
<meta property="og:image:alt" content="">
<link rel="icon" href="/favicon.ico" sizes="any">
<link rel="icon" href="/icon.svg" type="image/svg+xml">
<link rel="apple-touch-icon" href="icon.png">
<link rel="manifest" href="site.webmanifest">
<meta name="theme-color" content="#fafafa">
<!--
Si usamos async para cargar la biblioteca "p5.js", el navegador ejecutarĆ” ese archivo inmediatamente cuando termine de cargarlo.
That will make the variable p5 globally available. -->
<script defer type="module" src="js/libraries/p5.min.js"></script>
<script defer type="module" src="js/sketch.mjs"></script>
<script defer type="module" src="js/secondsketch.mjs"></script>
</head>
<body>
<!-- Add your site or application content here -->
<p>Hello world! This is HTML5 Boilerplate.</p>
<div id="first-sketch-holder"></div>
<p>Hello world! This is HTML5 Boilerplate.</p>
<div id="second-sketch-holder"></div>
</body>
</html>
sketch.mjs
import {Rectangle} from "./classes/Rectangle.mjs";
globalThis.a=5;
let b=8;
const s = ( sketch ) => {
let myrect = new Rectangle(10, 20);
sketch.setup = () => {
// Creates a canvas 600 pixels wide
// and 400 pixels high.
sketch.createCanvas(600, 400);
globalThis.a++;
b++;
console.log(myrect.area);
}
sketch.draw = () => {
sketch.background(135, 206, 235);
// sun in top right
sketch.fill("yellow"); // yellow
sketch.stroke("orange"); // orange outline
sketch.strokeWeight(20); // large outline
sketch.circle(550, 50, 100);
};
sketch.keyPressed = () => {
if (sketch.keyCode === sketch.RIGHT_ARROW) {
b++;
}
else if (sketch.keyCode === sketch.LEFT_ARROW) {
b--;
}
}//ends function keyPressed
};
let myp5 = new p5(s, document.getElementById('first-sketch-holder'));
export {b}; // Exporting 'b' to be used in "secondsketch.mjs"
secondsketch.mjs
"use strict";
import {b} from "./sketch.mjs";
const s2 = (secondsketch) => {
secondsketch.setup = () => {
secondsketch.createCanvas(400, 200);
secondsketch.textSize(12);
};
secondsketch.draw = ()=> {
secondsketch.background(220);
secondsketch.text("Valor de a: " + globalThis.a, 20, 50); // var 'a' was defined in "sketch.mjs"
secondsketch.text("Valor de b: " + b, 20, 80); // var 'a' was defined in "sketch.mjs"
};
};
let mysecondp5 = new p5(s2, document.getElementById('second-sketch-holder'));
Rectangle.mjs
export class Rectangle {
constructor(alto, ancho) {
this.alto = alto;
this.ancho = ancho;
}
// Getter
get area() {
return this.calcArea();
}
// MĆ©todos
calcArea() {
return this.alto * this.ancho;
}
}
1 - As told you seems that let or var is not the problemā¦but is a little detail using var or letā¦
2 - if put async it doesnt work:
<script async type="module" src="js/libraries/p5.min.js"></script>
<script defer type="module" src="js/sketch.mjs"></script>
<script defer type="module" src="js/secondsketch.mjs"></script>
Which i dont understandā¦as p5.min.js doesnt rely or depend on other libraries or files or modulesā¦
Uncaught ReferenceError: p5 is not defined
<anonymous> http://localhost:63342/MeuPacman/js/sketch.mjs:41
2 [sketch.mjs:41:12](http://localhost:63342/MeuPacman/js/sketch.mjs)
3 - This also works: all modules to asyncā¦i dont understand because seconsketch depends of variable b in first sketchā¦
<script async type="module" src="js/libraries/p5.min.js"></script>
<script async type="module" src="js/sketch.mjs"></script>
<script async type="module" src="js/secondsketch.mjs"></script>
4- Without anything it works alsoā¦
<script type="module" src="js/libraries/p5.min.js"></script>
<script type="module" src="js/sketch.mjs"></script>
<script type="module" src="js/secondsketch.mjs"></script>
5 -This also works:
<script src="js/libraries/p5.min.js"></script>
<script src="js/sketch.mjs"></script>
<script type="module" src="js/secondsketch.mjs"></script>
So do you ālikeā this code ? is more modern and flexibleā¦without using globalThis and using import/export modules in ESM.
Also if i add which it should not hurt after the other scriptsā¦it always failsā¦ I dont know whyā¦
<script type="module" src="js/classes/Rectangle.mjs"></script>
By now id try to test this in Vite, and then in Vercel.
BTW. Is recommended use strict mode? or coding with Eslint is enough ?