I’m using the p5.js library so I can practise some thing I’ve learned at freeCodeCamp. I’ve created a sketch.js file with this (related to my problem):
import laser from "./laser";
let square = {
x: 300,
y: 300,
a: 40,
speed: 4,
r: 255,
g: 0,
b: 0
...
function mouseClicked () {
let direction;
let angle;
if (mouseX - square.x != 0){
direction = (mouseY - square.y) / (mouseX - square.x);
angle = Math.atan(direction);
} else {
angle = Math.PI / 2;
}
strokeWeight(2);
stroke(255, 0, 0);
let laser = new laser(square.x, square.y, angle);
Uncaught SyntaxError: Unexpected token export ( at laser.js:1)
Uncaught SyntaxError: Unexpected identifier (at sketch.js:1)
I’ve double checked in freeCodeCamp that my syntax is correct and I don’t understand why do I get those messages… Any suggestions would be appreciated!
Where are you running this code? Wherever it is doesn’t understand imports/exports looks to be the issue - the code itself looks fine, just need a bit more context to debug it
In this case, should work - p5 is browser, not Node, and the code there should work fine in every new browser except IE as long as the script tag is correct (and as long as modules have been turned on if they’re behind a flag). I mean if it is being bundled then yup, probably missing Babel, but shouldn’t need that
Haha, I wouldn’t have thought about it had I not been playing around with p5 a couple of months ago, running it in the browser and using modules for my code (p5 is awesome as well, was doing the Khan academy course on natural algorithms and need to get back into it, it’s really interesting)
Which I’d meant to read for ages. Goes into all kinds of electronic life simulation stuff (game of life, ants etc), is all super interesting. The JS itself is actually really simple, p5 has a really easy-to-use API, but it teaches a lot about randomness and distributions and stuff in a really nice way (it looks awesome when you have stuff growing and breeding and multiplying and wandering around the screen). As I say, I really want to get back on it, only got a few lessons in before work stuff dragged me away