I'm new to Vue and JavaScript, so please help me understand this problem:
I want to dynamically showload different canvas elements on a page. The drawing of the canvas is defined in a seperate script.js file.
Here's a section of my code:
<canvas v-if="checkedBoxes.includes('Heart')" onload="drawHeart();" id="heartCanvas" width="300" height="240"></canvas>
<canvas v-if="checkedBoxes.includes('Star')" onload="drawStar();" id="starCanvas" width="240" height="240"></canvas>
script.js:
function drawHeart() {
let heartCanvas = document.getElementById("heartCanvas");
let c = heartCanvas.getContext('2d');
c.beginPath();
... }
However, when a canvas element appears, it is not drawn by the Javascript function drawXY(). It seems to me that the onload event is not triggered when the element appears on the page. I also tried it with the Vue expression v-on:load="drawXY"
without success. When i change the event trigger to onclick
, it works, but that's not what I need.