Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <title>JavaScript</title>
- </head>
- <body>
- <canvas width="256" height="256" id="c"></canvas>
- <script type="text/javascript">
- var
- ctx = document.getElementById('c').getContext('2d'),
- xpos = 0, t,
- canvasWidth = 256,
- direction = "goright"
- ;
- function drawSquare(ctx) {
- ctx.clearRect(0,0,256,256);
- if(direction==="goright"){
- if(xpos<canvasWidth - 20){xpos = xpos + 5;}else{direction = "left";}
- } if(direction==="left"){
- if(xpos>0){xpos = xpos - 5;}else{direction = "goright";}
- }
- if(direction==="goright"){
- if(xpos<canvasWidth - 20){xpos = xpos + 5;}else{direction = "left";}
- } if(direction==="left"){
- if(xpos>0){xpos = xpos - 5;}else{direction = "goright";}
- }
- ctx.strokeStyle = "black";
- t = window.performance.now();
- ctx.strokeRect(xpos + Math.cos(t * 0.001) * 32, Math.sin(t * 0.001) * 118 + 128, 20, 20)
- }
- window.requestAnimationFrame(function draw() {
- drawSquare(ctx);
- window.requestAnimationFrame(draw);
- });
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement