Ask coding questions

← Back to all posts
My clearRect Code isn't working
dnilem26 (8)

For some reason ctx.clearRect(x, y, width, height) isn't working
If anyone knows what I did wrong could you help me

Answered by realTronsi (926) [earned 5 cycles]
View Answer
Comments
hotnewtop
Coder100 (18222)

Also do not forget to call the functions like clear()

dnilem26 (8)

@Coder100 I believe I called it

Coder100 (18222)

@dnilem26 you never defined canvas change it to

var canvas = document.getElementById("canvas");
dnilem26 (8)

@Coder100 i did but just have it a a different varible name

Coder100 (18222)

@dnilem26 no look
line 5:

you forgot to change canvas to c

dnilem26 (8)

@Coder100 2 things
1. it shouldn't make a difference because canvas.width and canvas.height is a common command used in coding
2. I tried that it made no difference

realTronsi (926)

@dnilem26 clear is a reserved word, use another function name such as "clear_canvas"

Coder100 (18222)

no you never defined the variable @dnilem26

dnilem26 (8)

@Coder100 it doesn't matter anymore the code works

Coder100 (18222)

idc you should use good practices @dnilem26

dnilem26 (8)

@Coder100 in what ways does my code not display good practices

Coder100 (18222)

you are using canvas which is a variable you haven't defined?? @dnilem26

dnilem26 (8)

@Coder100 canvas.width/canvas.height are both commands that can be used in js

I don't use it as a variable I use it as a command

Coder100 (18222)

Clear rect will never draw anything it is like an eraser

dnilem26 (8)

@Coder100 yeah I need it to clear the canvas