Jump to content

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();
}

 

Hi I'm using a .html file

Languages: HTML, JavaScript
Level: Beginner

 

Problem:

Unable to toggle img inside canvas

I only want the img inside the canvas to disappear not the "image to use"

Spoiler

image.png.efc777fdf42ca978e96b13566b735285.png

 

HTML

Spoiler
 <h1>Draw Image</h1>

<p>Image to use:</p>
<img id="scream" src="../Images/img_the_scream.jpg" alt="The Scream" 
width="220" height="227">

<p>Canvas to fill:</p>
<canvas id="myCanvas" width="250" height="300" style="border: 1px solid #d3d3d3;">
Your browser does not support the HTML canvas tag.
</canvas>

<p><button onclick="myCanvas()">Try it</button></p>
<p><button onclick="toggle()">Toggle it</button></p>

 

Javascript

Spoiler
function myCanvas()
{
  var c = document.getElementById("myCanvas");
  var ctx = c.getContext("2d");
  var img = document.getElementById("scream");
  ctx.drawImage(img,10,10);
}      
function toggle()
{
  var c = document.getElementById("scream");

  if (c.style.display=="none")
{
  c.style.display="block";
}
else
{
  c.style.display="none";
}
}

 

PC  Specs 2022:

Spoiler
  • CPU
    AMD Ryzen R9 5900x @ 5.1GHz - Auto OC
  • Curve Optimizer Magnitude: -20
  • Motherboard
    ASUS ROG STRIX x570-F Gaming
  • RAM
                                        Kingston Fury 32GB DDR4 3200MHz 16x2GB
  • GPU
    MSI 3070 8GB Ventus 2x OC
  • Case
    LIAN LI LANCOOL MESH II Mesh RGB Black
  • Storage
    Kingston NV1 2TB M.2. NVMe
  • PSU
    Seasonic Focus GX 850w 
  • Display(s)
    MSI OPTIX MAG 251RX IPS 240hz & ASUS MG248Q Vertical 144hz & Dell 60hz
  • Cooling
    NZXT Kraken x73 360mm
  • Keyboard
    Tt eSports Meka G1
  • Mouse
    Logitech G Pro Wireless
  • Operating System
    -Windows 10 Professional 64bit
Link to comment
Share on other sites

Link to post
Share on other sites

51 minutes ago, c0d0ps said:

I only want the img inside the canvas to disappear not the "image to use"

 

51 minutes ago, c0d0ps said:
var c = document.getElementById("scream");

You're grabbing #scream instead of #myCanvas..

On 4/5/2024 at 10:13 PM, LAwLz said:

I am getting pretty fucking sick and tired of the "watch something else" responses. It's such a cop out answer because you could say that about basically anything, and it doesn't address the actual complaints. People use it as some kind of card they pull when they can't actually respond to the criticism raised but they still feel like they need to defend some company/person. If you don't like this thread then stop reading it. See how stupid it is? It's basically like telling someone "shut the fuck up". It's not a clever responsive, it doesn't address anything said, and it is rude. 

 ^

 

bruh switch to dark mode its at the bottom of this page

VPN Server Guide

Link to comment
Share on other sites

Link to post
Share on other sites

On 4/1/2022 at 3:34 PM, RockSolid1106 said:

 

You're grabbing #scream instead of #myCanvas..

It's definitely a lot better, thanks.

Spoiler

image.png.6e208f96056f5d5a886d5cb503e8ae48.png

However i'm looking to remove the picture in the canvas not the entire canvas.

I'm guessing I need to create a scream2 after it has been put in the canvas but I am not sure how.

PC  Specs 2022:

Spoiler
  • CPU
    AMD Ryzen R9 5900x @ 5.1GHz - Auto OC
  • Curve Optimizer Magnitude: -20
  • Motherboard
    ASUS ROG STRIX x570-F Gaming
  • RAM
                                        Kingston Fury 32GB DDR4 3200MHz 16x2GB
  • GPU
    MSI 3070 8GB Ventus 2x OC
  • Case
    LIAN LI LANCOOL MESH II Mesh RGB Black
  • Storage
    Kingston NV1 2TB M.2. NVMe
  • PSU
    Seasonic Focus GX 850w 
  • Display(s)
    MSI OPTIX MAG 251RX IPS 240hz & ASUS MG248Q Vertical 144hz & Dell 60hz
  • Cooling
    NZXT Kraken x73 360mm
  • Keyboard
    Tt eSports Meka G1
  • Mouse
    Logitech G Pro Wireless
  • Operating System
    -Windows 10 Professional 64bit
Link to comment
Share on other sites

Link to post
Share on other sites

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();
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

15 hours ago, C2dan88 said:

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

SNIP

 

Hi, thanks for answer.

Nothing happens when I press toggle or the "on-button" 

Update: I forgot this, it works now thank you.

Spoiler
var drawScream = 1

 

Javascript

Spoiler
<script title="Default">
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);
}
}
</script>
    
<script title="toggleScream">
function toggle()
{
drawScream = (drawScream == 1) ? 0 : 1;
// Call mycanvas
myCanvas();
}
</script>

 

 

PC  Specs 2022:

Spoiler
  • CPU
    AMD Ryzen R9 5900x @ 5.1GHz - Auto OC
  • Curve Optimizer Magnitude: -20
  • Motherboard
    ASUS ROG STRIX x570-F Gaming
  • RAM
                                        Kingston Fury 32GB DDR4 3200MHz 16x2GB
  • GPU
    MSI 3070 8GB Ventus 2x OC
  • Case
    LIAN LI LANCOOL MESH II Mesh RGB Black
  • Storage
    Kingston NV1 2TB M.2. NVMe
  • PSU
    Seasonic Focus GX 850w 
  • Display(s)
    MSI OPTIX MAG 251RX IPS 240hz & ASUS MG248Q Vertical 144hz & Dell 60hz
  • Cooling
    NZXT Kraken x73 360mm
  • Keyboard
    Tt eSports Meka G1
  • Mouse
    Logitech G Pro Wireless
  • Operating System
    -Windows 10 Professional 64bit
Link to comment
Share on other sites

Link to post
Share on other sites

15 hours ago, C2dan88 said:

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

SNIP

 

Bonus question (already marked answer for solution):

 What does this mean?

Spoiler
drawScream = (drawScream == 1) ? 0 : 1;

 

I know about if-else statements, for-while statements, but I haven't seen this before.
What should I read about to learn about this?

PC  Specs 2022:

Spoiler
  • CPU
    AMD Ryzen R9 5900x @ 5.1GHz - Auto OC
  • Curve Optimizer Magnitude: -20
  • Motherboard
    ASUS ROG STRIX x570-F Gaming
  • RAM
                                        Kingston Fury 32GB DDR4 3200MHz 16x2GB
  • GPU
    MSI 3070 8GB Ventus 2x OC
  • Case
    LIAN LI LANCOOL MESH II Mesh RGB Black
  • Storage
    Kingston NV1 2TB M.2. NVMe
  • PSU
    Seasonic Focus GX 850w 
  • Display(s)
    MSI OPTIX MAG 251RX IPS 240hz & ASUS MG248Q Vertical 144hz & Dell 60hz
  • Cooling
    NZXT Kraken x73 360mm
  • Keyboard
    Tt eSports Meka G1
  • Mouse
    Logitech G Pro Wireless
  • Operating System
    -Windows 10 Professional 64bit
Link to comment
Share on other sites

Link to post
Share on other sites

5 hours ago, c0d0ps said:

I know about if-else statements, for-while statements, but I haven't seen this before.

This is a ternary operator.

5 hours ago, c0d0ps said:
(drawScream == 1) ?

This corresponds to the if condition. Checking if drawScream equals 1.

 

5 hours ago, c0d0ps said:
(drawScream == 1) ? 0

The value after the question mark(?) is the value to be assigned if the condition is true.

5 hours ago, c0d0ps said:
0 : 1;

The value after the colon is what the value of that variable would be if the condition is false. Essentially, an if else statement.

On 4/5/2024 at 10:13 PM, LAwLz said:

I am getting pretty fucking sick and tired of the "watch something else" responses. It's such a cop out answer because you could say that about basically anything, and it doesn't address the actual complaints. People use it as some kind of card they pull when they can't actually respond to the criticism raised but they still feel like they need to defend some company/person. If you don't like this thread then stop reading it. See how stupid it is? It's basically like telling someone "shut the fuck up". It's not a clever responsive, it doesn't address anything said, and it is rude. 

 ^

 

bruh switch to dark mode its at the bottom of this page

VPN Server Guide

Link to comment
Share on other sites

Link to post
Share on other sites

On 4/5/2022 at 3:41 PM, RockSolid1106 said:

This is a ternary operator.

This corresponds to the if condition. Checking if drawScream equals 1.

 

The value after the question mark(?) is the value to be assigned if the condition is true.

The value after the colon is what the value of that variable would be if the condition is false. Essentially, an if else statement.

Found it on Mozilla, thank you for the explanation.

PC  Specs 2022:

Spoiler
  • CPU
    AMD Ryzen R9 5900x @ 5.1GHz - Auto OC
  • Curve Optimizer Magnitude: -20
  • Motherboard
    ASUS ROG STRIX x570-F Gaming
  • RAM
                                        Kingston Fury 32GB DDR4 3200MHz 16x2GB
  • GPU
    MSI 3070 8GB Ventus 2x OC
  • Case
    LIAN LI LANCOOL MESH II Mesh RGB Black
  • Storage
    Kingston NV1 2TB M.2. NVMe
  • PSU
    Seasonic Focus GX 850w 
  • Display(s)
    MSI OPTIX MAG 251RX IPS 240hz & ASUS MG248Q Vertical 144hz & Dell 60hz
  • Cooling
    NZXT Kraken x73 360mm
  • Keyboard
    Tt eSports Meka G1
  • Mouse
    Logitech G Pro Wireless
  • Operating System
    -Windows 10 Professional 64bit
Link to comment
Share on other sites

Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

×