110
Views
2
Comments
Solved
JavaScript: canvas.getContext is not a function
Application Type
Reactive

I am trying to draw a rectangle on a container using JavaScript, however I keep encountering the below error:

canvas.getContext is not a function


Please see the JavaScript I am using:


console.log("START");

var canvas = document.getElementById($parameters.Id);

console.log(canvas);

var ctx = canvas.getContext("2d");

console.log(ctx);

ctx.fillStyle = "#FF0000";

ctx.fillRect(0, 0, 150, 75);

console.log("END");


I pass in the container id, which from the log I can see is passed successfully. Has anyone got a suggestion as to where I have gone wrong in the code?

2021-09-06 15-09-53
Dorine Boudry
 
MVP
Solution

Hi Roisin,

a container becomes a <div> element in the dom, but what you want is a <canvas> element.

So you can use the html widget with tag canvas on your screen, or you can add the canvas element with javascript inside your container.

see attached oml for both options

Dorine

QDR_Canvas.oml
UserImage.jpg
Roisin Woolley

This works perfectly! Thank you for example and explanation :)

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.