Discuss Scratch

TheCreatorOfUnTV
Scratcher
1000+ posts

I can't get JavaScript to work with HTML.

Hello! My problem is that I can't get JavaScript to work with HTML. I know how to use it:
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<script>
</script>
</body>
</html>
but all I can do is code HTML elements and code simple games which change text and use window.prompt, window.alert, variables and if conditions to play (So only text-based games) While this is enough for static website design, this is not enough for the kinds of things I want to make, and the reason why I can't do anything is because the Canvas element doesn't seem to work when I use it. It works fine on code by other people (I can run Scratch perfectly fine, and the website uses Canvas element a lot) but not on code I make, which does not feel fun and is hard.

EDIT: Here's the code:
<!DOCTYPE html>

<html>

<head>
</head>

<body>
<button id='greenFlag' type='button'>Green Flag</button>
<canvas id='canvas' width=480 height=360></canvas>
<script>
ctx.strokeStyle = "yellow";
ctx.strokeRect(0,0,10,10);
document.body.appendChild(canvas);
</script>
<script>
window.addEventListener("keypress", function(event) {
if(event.key.toLowerCase == "a") {
window.alert("You pressed the A key!")
}
});
</script>

</body>

</html>
The canvas exists but doesn't have anything on it at all, and the keys don't work.
EDIT 2: Resolved.

Last edited by TheCreatorOfUnTV (Aug. 14, 2024 23:11:42)


This is the start of my signature. Ctrl+Shift+Down to see all of it.

Check out my about me project here! (I'm allowed to advertise here)
1st post
1000th post

;
blubby4
Scratcher
100+ posts

I can't get JavaScript to work with HTML.

What's it doing/not doing? Is there an error? What's the code you're using? The canvas element works fine for me.

I think about things. Sometimes I think about things too much, and miss obvious simple things
AHypnoman
Scratcher
1000+ posts

I can't get JavaScript to work with HTML.

Could you provide some of your code?

~ AHypnoman (Use Shift+Down Arrow to see the rest of my signature)










I am an evil kumquat, and your siggy looks tasty…
Hello there, I'm AHypnoman, If you write my name remember to capitalize the “H”. “When you don't create things, you become defined by your tastes rather than ability. your tastes only narrow & exclude people. so create.” I write though as tho.
According to the Chrome Console, my viewbox attribute is “Garbage”. Very nice, Chrome.
#TrendyHashtag
Gib internneeet
london11gm
Scratcher
42 posts

I can't get JavaScript to work with HTML.

To me it sounds like you want to create 2D objects on the screen for an HTML5 game instead of text?

In that case, consider using CanvasRenderingContext2D or p5.js.

CTX2: https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D

P5.JS: https://p5js.org/reference/

If you've already coded the game in scratch, you can use the <iframe> element with scratch.mit.edu/projects/YOUR_ID/embed as long as the project is shared.

I will sometimes make mistakes with my information. Correct me if necessary.
breakfast_for_dinner
Scratcher
1000+ posts

I can't get JavaScript to work with HTML.

any errors? make sure to use getContext.
let canvasElement = document.getElementById("put_the_id_here");
let ctx = canvasElement.getContext("2d");

breakfast // web dev // 15yo // she/they // scratching since october 9th, 2018
ocular profile // scratch wiki page // git

i have cookies for brains

i'm a blunt person who may accidentally hurt your feelings.





the hall of my silly forum posts
[[ 500th post (no edit >:3) ]]
diarrhea isn't copyrighted
i_eat_coffee's birthday surprise
the jeffalo bug
mess cat
semicolon language translator
TheCreatorOfUnTV
Scratcher
1000+ posts

I can't get JavaScript to work with HTML.

breakfast_for_dinner wrote:

any errors? make sure to use getContext.
let canvasElement = document.getElementById("put_the_id_here");
let ctx = canvasElement.getContext("2d");
It worked, thank you! (The keys don't work, though)

Last edited by TheCreatorOfUnTV (Aug. 14, 2024 22:59:18)


This is the start of my signature. Ctrl+Shift+Down to see all of it.

Check out my about me project here! (I'm allowed to advertise here)
1st post
1000th post

;
blubby4
Scratcher
100+ posts

I can't get JavaScript to work with HTML.

It's still hard to help if we don't have any code, but to get keyboard input you would do something like:
addEventListener("keydown", function(event) {
// event.key is what key you're pressing, e.g. "ArrowUp", " ", "f"
})

Last edited by blubby4 (Aug. 14, 2024 23:10:55)


I think about things. Sometimes I think about things too much, and miss obvious simple things
TheCreatorOfUnTV
Scratcher
1000+ posts

I can't get JavaScript to work with HTML.

blubby4 wrote:

It's still hard to help if we don't have any code, but to get keyboard input you would do something like:
addEventListener("keydown", function(event) {
// event.key is what key you're pressing, e.g. "ArrowUp", " ", "f"
})
Added the code.

This is the start of my signature. Ctrl+Shift+Down to see all of it.

Check out my about me project here! (I'm allowed to advertise here)
1st post
1000th post

;
josueart
Scratcher
100+ posts

I can't get JavaScript to work with HTML.

There are two issues with your event listener:
  • The keypress event does not exist. You can use keydown instead
  • You're apparently trying to convert the key to lowercase, but you're not calling the toLowerCase function, so you're literally comparing if the toLowerCase function equals “a”. Add () at the end to call it, like so:
    window.addEventListener("keydown", function(event) {
        if (event.key.toLowerCase() == "a") {
            window.alert("You pressed the A key!")
        }
    });
    

Edit: my bad. Keypress does indeed exist, but it's deprecated and may not work on modern browsers.

Last edited by josueart (Aug. 16, 2024 10:14:13)


https://notfenixio.is-a.dev/
My hot take: Rust is overrated.
BigNate469
Scratcher
1000+ posts

I can't get JavaScript to work with HTML.

While I don't have anything major to add (everything I was going to say was already said), do note that
window.alert();
and just
alert();
both function identically (the removal of the window object reference there is optional and designed for ease of use and memory minimization).

As another example, the document object is technically a property of window, but you can call it on its own- again, for ease of use and minimizing the total size of your file. Both of these are intentional features and present in all major (and most less well-known) browsers.

This signature is designed to be as useful as possible.
How to make a signature & other useful info about them
The Official List of Rejected Suggestions (TOLORS)
The Announcements Directory
Lesser-known Scratch URLs: https://scratch-mit-edu.ezproxy.canberra.edu.au/discuss/topic/542480/
Why @Paddle2See's responses are so often identical: https://scratch-mit-edu.ezproxy.canberra.edu.au/discuss/topic/762351/

Ads Useful projects:
Raycaster & Maze 1.4.1 | Don't Break The Ice | Procedurally Generated Terrain | Basic Trigonometry | Comparing the fastest list sorters on Scratch

“if nobody can learn the programming language, it's just gibberish that does math.” -me, in a forum post

The original name of “loves” was “love-its”. Technically speaking, this hasn't changed.

© @BigNate469, some rights reserved
blubby4
Scratcher
100+ posts

I can't get JavaScript to work with HTML.

BigNate469 wrote:

While I don't have anything major to add (everything I was going to say was already said), do note that
window.alert();
and just
alert();
both function identically (the removal of the window object reference there is optional and designed for ease of use and memory minimization).

As another example, the document object is technically a property of window, but you can call it on its own- again, for ease of use and minimizing the total size of your file. Both of these are intentional features and present in all major (and most less well-known) browsers.
same goes for addEventListener and setTimeout/setInterval. Note that you can call addEventListener on a specific element, such as getting the mouse position relative to said element.

I think about things. Sometimes I think about things too much, and miss obvious simple things

Powered by DjangoBB