I need to run a javascript code in my angular project, and following other questions on SO I could run a hello world js file. However, there is no example for usage of more advanced js files like cases in which js needs html elements of the angular component.
Following is my simplified myGame.js file:
export function greeting(){
alert("Greeting from javascript");
}
var canvas, context;
export function initializeScenes() {
canvas = document.getElementById("canvas");
if (canvas)
context = canvas.getContext("2d");
else
alert("No canvas");
}
and my runjavascript.component.ts:
import * as myGame from "../../../../assets/myGame.js"
.
.
.
export class RunJavascriptComponent {
constructor() {
myGame.greeting();
myGame.initializeScenes()
}
}
and my runjavascript.component.htm:
<div class="container">
<canvas id="canvas" width="600" height="400"></canvas>
</div>
When I run ng serve, I see "Greeting from javascript", which means greeting() is called successfully, but then I get the alert "No canvas", which I think means canvas element of my angular component.html is not recognized!
Does anyone know how to solve the problem? Thank you in advance.