I am exporting a canvas element object from my main.js file to a separate file that I would like to handle the events of the canvas object. I have imported it before when it would be referenced in a function of the other file, however whenever I try to access the events such as "onmouseup" of the canvas object it gives the above error on the first line that it is implemented. Referencing does work when it is in a function just not when it is outside of a function. this other file is meant to be imported into main.js.
I assumed the problem was that I was importing other file in main.js before I defined what canv was in main.js so I defined canv before imported the other file.
like this
export const canv = document.getElementById('window');
import {resize} from "/scripts/otherfile.js";
resize(10,80);
This still gave to same error so I am thoroughly stumped.
main.js
import {resize} from "/scripts/otherfile.js";
export const canv = document.getElementById('window');
resize(10,80);
otherfile.js
import {canv} from "../main.js";
let focus = false;
canv.onmouseenter = () => focus = true;
export const resize = (x, y) =>{
canv.height = y;
canv.width = x;
}
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Graphing Calculator</title>
</head>
<body>
<canvas id="window" style="border:dotted"></canvas>
<script type="module" src="main.js"></script>
</body>
</html>
line 3 of otherfile.js throws the error. however no error occurs by having resize in the other file or calling resize in main.js
I expect for the other file to be able to reference canv and its events. however canv is apparently not defined in that case.