Advertisement
myarkqub

Canvas example

Nov 19th, 2024
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 0.66 KB | None | 0 0
  1. <canvas id="canvas"></canvas>
  2. <script>
  3.     const canvas = document.getElementById("canvas")
  4.     const ctx = canvas.getContext('2d')
  5.  
  6.     ctx.fillStyle = "#ff0000"
  7.     ctx.fillRect(20, 20, 100, 100)
  8.  
  9.     const canvasurl = canvas.toDataURL('image/png')
  10.     let bg = new Image()
  11.     bg.src = canvasurl
  12.     ctx.drawImage(bg, 0, 0)
  13.    
  14.     let x = 0
  15.     const main = () => {
  16.         ctx.fillStyle = "#000000"
  17.         ctx.fillRect(0, 0, canvas.width, canvas.height)
  18.         ctx.drawImage(bg, 0, 0)
  19.  
  20.         ctx.fillStyle = "#00ff00"
  21.         ctx.fillRect(x, 40, 20, 20)
  22.         x += 0.3
  23.        
  24.         requestAnimationFrame(main)
  25.     }
  26.     main()
  27. </script>
  28.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement