Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- <style>
- body{
- /*margin:0px;*/
- }
- </style>
- <script>
- document.addEventListener('DOMContentLoaded', function(){
- const canvas = document.getElementById("canvasId");
- const context = canvas.getContext("2d");
- let x = 50;
- let y = 50;
- let moveX = 10;
- let moveY = 14;
- let radius = 20;
- canvas.addEventListener('mousemove', (e) => {
- let mouse = mouse_player1(e);
- context.beginPath();
- context.fillStyle = "red";
- context.arc(mouse.x, mouse.y, 20, 0, 2*Math.PI);
- context.fill();
- context.closePath();
- });
- const mouse_player1 = (e) => {
- return {
- x: e.layerX,
- y: e.layerY,
- }
- }
- const executeTasks = () =>{
- // moveTask();
- drawTask();
- }
- const moveTask = () => {
- x += moveX;
- y += moveY;
- if( x >= canvas.width - radius) {
- moveX = -moveX;
- }
- if( x <= 0 + radius ) {
- moveX = -moveX;
- }
- if(y >= canvas.height - radius){
- moveY = -moveY;
- }
- if(y <= 0 + radius ){
- moveY = -moveY;
- }
- }
- const drawTask = () => {
- context.clearRect(0, 0, canvas.width, canvas.height);
- context.fillStyle = "green";
- context.beginPath();
- context.arc(x, y, radius, 0, Math.PI*2);
- context.fill();
- context.closePath();
- }
- setInterval(executeTasks, 24);
- });
- </script>
- <style>
- #canvasId{
- background:black;
- }
- </style>
- </head>
- <body>
- <canvas id="canvasId" width="800" height="600"></canvas>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement