Skip to content
Advertisement

Making a analog clock by using html canvas

Below is my preliminary Javascript code for making a analog clock. My main problem is I don’t know how to clear the “previous second lines” on the clock surface:

JavaScript

I have tried to use “ctx.globalCompositeOperation = “destination-over”;”, however not successful:

JavaScript

Could you tell me how to clear these “previous second lines” by using globalCompositeOperation if such function can really do in my case? Thanks.

The reason i believe it is possible to do it through globalCompositeOperation, is because i had tried some test as below:

JavaScript

Advertisement

Answer

The globalCompositeOperation property cannot really be used for this purpose.

You can however do this:

  • Create a second canvas element that overlays the first (using position: absolute). It is transparent, so the other canvas will be seen through it.
  • After drawing the background on the original canvas, switch the context (ctx) to the second canvas, so that the timing function will only deal with the overlayed canvas
  • In the timing function, start by clearing that overlay canvas

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement