Unable to toggle img inside canvas (HTML&JavaScript Beginner)
Go to solution
Solved by C2dan88,
You want the toggle function to toggle a variable between 1 and 0. When the value is 1 draw the image. When it is 0 reset the canvas in the myCanvas function
var drawScream = 1 function myCanvas() { var c = document.getElementById("myCanvas"); var ctx = c.getContext("2d"); if(drawScream) { var img = document.getElementById("scream"); ctx.drawImage(img,10,10); } else ctx.clearRect(0, 0, c.width, c.height); } function toggleScream() { drawScream = (drawScream == 1) ? 0 : 1; // call mycanvas myCanvas(); }
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now