How to choose the picture(Object) in Canvas HTML5












0














I just want to start the process by clicking on the ball.gif. I couldn't do it. Also at the end of the animation my picture disappears.
Here are my codes;



Select the object in Canvas and add it to the event.
I looked at almost all the issues. But I didn't find the answer.



var player, mouse;
var iterations = 0;


var game = {
canvas: document.createElement("canvas"),
start: function () {

game.canvas.width = 357;
game.canvas.height = 500;
game.canvas.style.border = "1px solid red";
game.ctx = game.canvas.getContext("2d");
document.body.appendChild(game.canvas);
player=new game.nesne(150,400,50,50);

game.canvas.addEventListener("mousedown", function(event) {
game.nesne(150,400,50,50);
});

game.timer = setInterval(game.animate.bind(this), 30);
},
draw: function () {

player.draw();
},
update: function () {
},
nesne: function (x, y, w, h) {

this.x = x;
this.y = y;
this.w = w;
this.h = h;

this.image=new Image();
this.image.src="ball.gif";
this.vx = 1;
this.vy = -1;
this.draw = function () {

game.ctx.drawImage(this.image,this.x,this.y,this.w,this.h);


}


this.update = function () {
this.x += this.vx;
this.y += this.vy;

}
},

animate: function () {
game.ctx.clearRect(0,0,game.canvas.width,game.canvas.height);

iterations++
if (iterations >= 100)
clearInterval(interval);
game.update();
game.draw();

},
}
window.addEventListener("load", game.start, false);









share|improve this question



























    0














    I just want to start the process by clicking on the ball.gif. I couldn't do it. Also at the end of the animation my picture disappears.
    Here are my codes;



    Select the object in Canvas and add it to the event.
    I looked at almost all the issues. But I didn't find the answer.



    var player, mouse;
    var iterations = 0;


    var game = {
    canvas: document.createElement("canvas"),
    start: function () {

    game.canvas.width = 357;
    game.canvas.height = 500;
    game.canvas.style.border = "1px solid red";
    game.ctx = game.canvas.getContext("2d");
    document.body.appendChild(game.canvas);
    player=new game.nesne(150,400,50,50);

    game.canvas.addEventListener("mousedown", function(event) {
    game.nesne(150,400,50,50);
    });

    game.timer = setInterval(game.animate.bind(this), 30);
    },
    draw: function () {

    player.draw();
    },
    update: function () {
    },
    nesne: function (x, y, w, h) {

    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;

    this.image=new Image();
    this.image.src="ball.gif";
    this.vx = 1;
    this.vy = -1;
    this.draw = function () {

    game.ctx.drawImage(this.image,this.x,this.y,this.w,this.h);


    }


    this.update = function () {
    this.x += this.vx;
    this.y += this.vy;

    }
    },

    animate: function () {
    game.ctx.clearRect(0,0,game.canvas.width,game.canvas.height);

    iterations++
    if (iterations >= 100)
    clearInterval(interval);
    game.update();
    game.draw();

    },
    }
    window.addEventListener("load", game.start, false);









    share|improve this question

























      0












      0








      0







      I just want to start the process by clicking on the ball.gif. I couldn't do it. Also at the end of the animation my picture disappears.
      Here are my codes;



      Select the object in Canvas and add it to the event.
      I looked at almost all the issues. But I didn't find the answer.



      var player, mouse;
      var iterations = 0;


      var game = {
      canvas: document.createElement("canvas"),
      start: function () {

      game.canvas.width = 357;
      game.canvas.height = 500;
      game.canvas.style.border = "1px solid red";
      game.ctx = game.canvas.getContext("2d");
      document.body.appendChild(game.canvas);
      player=new game.nesne(150,400,50,50);

      game.canvas.addEventListener("mousedown", function(event) {
      game.nesne(150,400,50,50);
      });

      game.timer = setInterval(game.animate.bind(this), 30);
      },
      draw: function () {

      player.draw();
      },
      update: function () {
      },
      nesne: function (x, y, w, h) {

      this.x = x;
      this.y = y;
      this.w = w;
      this.h = h;

      this.image=new Image();
      this.image.src="ball.gif";
      this.vx = 1;
      this.vy = -1;
      this.draw = function () {

      game.ctx.drawImage(this.image,this.x,this.y,this.w,this.h);


      }


      this.update = function () {
      this.x += this.vx;
      this.y += this.vy;

      }
      },

      animate: function () {
      game.ctx.clearRect(0,0,game.canvas.width,game.canvas.height);

      iterations++
      if (iterations >= 100)
      clearInterval(interval);
      game.update();
      game.draw();

      },
      }
      window.addEventListener("load", game.start, false);









      share|improve this question













      I just want to start the process by clicking on the ball.gif. I couldn't do it. Also at the end of the animation my picture disappears.
      Here are my codes;



      Select the object in Canvas and add it to the event.
      I looked at almost all the issues. But I didn't find the answer.



      var player, mouse;
      var iterations = 0;


      var game = {
      canvas: document.createElement("canvas"),
      start: function () {

      game.canvas.width = 357;
      game.canvas.height = 500;
      game.canvas.style.border = "1px solid red";
      game.ctx = game.canvas.getContext("2d");
      document.body.appendChild(game.canvas);
      player=new game.nesne(150,400,50,50);

      game.canvas.addEventListener("mousedown", function(event) {
      game.nesne(150,400,50,50);
      });

      game.timer = setInterval(game.animate.bind(this), 30);
      },
      draw: function () {

      player.draw();
      },
      update: function () {
      },
      nesne: function (x, y, w, h) {

      this.x = x;
      this.y = y;
      this.w = w;
      this.h = h;

      this.image=new Image();
      this.image.src="ball.gif";
      this.vx = 1;
      this.vy = -1;
      this.draw = function () {

      game.ctx.drawImage(this.image,this.x,this.y,this.w,this.h);


      }


      this.update = function () {
      this.x += this.vx;
      this.y += this.vy;

      }
      },

      animate: function () {
      game.ctx.clearRect(0,0,game.canvas.width,game.canvas.height);

      iterations++
      if (iterations >= 100)
      clearInterval(interval);
      game.update();
      game.draw();

      },
      }
      window.addEventListener("load", game.start, false);






      javascript events canvas






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 at 23:47









      Veysel BAYAR

      11




      11
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Changes I've made:




          1. instead of clearInterval(interval); I use clearInterval(game.timer);
            The interval is not defined. You have to use the handler you created for the setInterval(line 24 in my code)


          2. You call the game.update function but in your code is an empty function. I've changed that. Now the game.update() calls the player.update().


          3. To reanimate the player again on mousedown you need to reset the iterations = 0;






          var player, mouse;
          var iterations = 0;


          var game = {
          canvas: document.createElement("canvas"),
          start: function () {

          game.canvas.width = 357;
          game.canvas.height = 500;
          game.canvas.style.border = "1px solid red";
          game.ctx = game.canvas.getContext("2d");


          document.body.appendChild(game.canvas);

          player=new game.nesne(150,400,50,50);


          game.canvas.addEventListener("mousedown",
          function(event) {

          if (game.timer) clearInterval(game.timer);
          game.nesne(150, 400, 50, 50);
          iterations = 0;
          game.timer = setInterval(game.animate.bind(this), 30);


          });

          game.timer = setInterval(game.animate.bind(this), 30);
          },


          draw: function () {
          player.draw();
          },

          update: function () {
          player.update();///////////////
          },


          nesne: function (x, y, w, h) {

          this.x = x;
          this.y = y;
          this.w = w;
          this.h = h;

          this.image=new Image();
          this.image.src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/222579/puppy150x150.jpg";
          this.vx = 1;
          this.vy = -1;
          this.draw = function () {

          game.ctx.drawImage(this.image,this.x,this.y,this.w,this.h);


          }


          this.update = function () {
          this.x += this.vx;
          this.y += this.vy;

          }
          },

          animate: function () {
          game.ctx.clearRect(0,0,game.canvas.width,game.canvas.height);

          iterations++
          if (iterations >= 100)
          //clearInterval(interval);
          clearInterval(game.timer);
          game.update();
          game.draw();

          }
          }

          window.addEventListener("load", game.start, false);





          UPDATE



          The OP commented:




          So how do I do this when I just click on the picture.




          In this case you need to detect the position of the mouse when clicking the canvas. I've added a function to detect the mouse position:



          function oMousePos(canvas, evt) {
          var ClientRect = canvas.getBoundingClientRect();
          return {
          x: Math.round(evt.clientX - ClientRect.left),
          y: Math.round(evt.clientY - ClientRect.top)
          }
          }


          Next you need to change the function for the mousedown. You need to use the method isPointInPath to check if the mouse is inside the nesne thing.



          function(event) {
          mouse = oMousePos(game.canvas, event);

          game.ctx.beginPath();
          game.ctx.rect(player.x,player.y,player.w,player.h);
          if (game.ctx.isPointInPath(mouse.x, mouse.y)) {


          if (game.timer) clearInterval(game.timer);
          game.nesne(150, 400, 50, 50);
          iterations = 0;
          game.timer = setInterval(game.animate.bind(this), 30);
          }

          });





          share|improve this answer























          • Thanks for the changes you made. So how do I do this when I just click on the picture. And one of the things I can't do is I'm really freaking out :) –
            – Veysel BAYAR
            Nov 21 at 10:49










          • The picture disappeared when I changed the codes according to ispointPath. I must have made a mistake again somewhere.Also in console console 22 says there is an error. mouse = oMousePos(game.canvas, event); ==> error code
            – Veysel BAYAR
            Nov 21 at 11:43












          • Thank you. I wrote missing codes
            – Veysel BAYAR
            Nov 21 at 12:26











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403304%2fhow-to-choose-the-pictureobject-in-canvas-html5%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Changes I've made:




          1. instead of clearInterval(interval); I use clearInterval(game.timer);
            The interval is not defined. You have to use the handler you created for the setInterval(line 24 in my code)


          2. You call the game.update function but in your code is an empty function. I've changed that. Now the game.update() calls the player.update().


          3. To reanimate the player again on mousedown you need to reset the iterations = 0;






          var player, mouse;
          var iterations = 0;


          var game = {
          canvas: document.createElement("canvas"),
          start: function () {

          game.canvas.width = 357;
          game.canvas.height = 500;
          game.canvas.style.border = "1px solid red";
          game.ctx = game.canvas.getContext("2d");


          document.body.appendChild(game.canvas);

          player=new game.nesne(150,400,50,50);


          game.canvas.addEventListener("mousedown",
          function(event) {

          if (game.timer) clearInterval(game.timer);
          game.nesne(150, 400, 50, 50);
          iterations = 0;
          game.timer = setInterval(game.animate.bind(this), 30);


          });

          game.timer = setInterval(game.animate.bind(this), 30);
          },


          draw: function () {
          player.draw();
          },

          update: function () {
          player.update();///////////////
          },


          nesne: function (x, y, w, h) {

          this.x = x;
          this.y = y;
          this.w = w;
          this.h = h;

          this.image=new Image();
          this.image.src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/222579/puppy150x150.jpg";
          this.vx = 1;
          this.vy = -1;
          this.draw = function () {

          game.ctx.drawImage(this.image,this.x,this.y,this.w,this.h);


          }


          this.update = function () {
          this.x += this.vx;
          this.y += this.vy;

          }
          },

          animate: function () {
          game.ctx.clearRect(0,0,game.canvas.width,game.canvas.height);

          iterations++
          if (iterations >= 100)
          //clearInterval(interval);
          clearInterval(game.timer);
          game.update();
          game.draw();

          }
          }

          window.addEventListener("load", game.start, false);





          UPDATE



          The OP commented:




          So how do I do this when I just click on the picture.




          In this case you need to detect the position of the mouse when clicking the canvas. I've added a function to detect the mouse position:



          function oMousePos(canvas, evt) {
          var ClientRect = canvas.getBoundingClientRect();
          return {
          x: Math.round(evt.clientX - ClientRect.left),
          y: Math.round(evt.clientY - ClientRect.top)
          }
          }


          Next you need to change the function for the mousedown. You need to use the method isPointInPath to check if the mouse is inside the nesne thing.



          function(event) {
          mouse = oMousePos(game.canvas, event);

          game.ctx.beginPath();
          game.ctx.rect(player.x,player.y,player.w,player.h);
          if (game.ctx.isPointInPath(mouse.x, mouse.y)) {


          if (game.timer) clearInterval(game.timer);
          game.nesne(150, 400, 50, 50);
          iterations = 0;
          game.timer = setInterval(game.animate.bind(this), 30);
          }

          });





          share|improve this answer























          • Thanks for the changes you made. So how do I do this when I just click on the picture. And one of the things I can't do is I'm really freaking out :) –
            – Veysel BAYAR
            Nov 21 at 10:49










          • The picture disappeared when I changed the codes according to ispointPath. I must have made a mistake again somewhere.Also in console console 22 says there is an error. mouse = oMousePos(game.canvas, event); ==> error code
            – Veysel BAYAR
            Nov 21 at 11:43












          • Thank you. I wrote missing codes
            – Veysel BAYAR
            Nov 21 at 12:26
















          0














          Changes I've made:




          1. instead of clearInterval(interval); I use clearInterval(game.timer);
            The interval is not defined. You have to use the handler you created for the setInterval(line 24 in my code)


          2. You call the game.update function but in your code is an empty function. I've changed that. Now the game.update() calls the player.update().


          3. To reanimate the player again on mousedown you need to reset the iterations = 0;






          var player, mouse;
          var iterations = 0;


          var game = {
          canvas: document.createElement("canvas"),
          start: function () {

          game.canvas.width = 357;
          game.canvas.height = 500;
          game.canvas.style.border = "1px solid red";
          game.ctx = game.canvas.getContext("2d");


          document.body.appendChild(game.canvas);

          player=new game.nesne(150,400,50,50);


          game.canvas.addEventListener("mousedown",
          function(event) {

          if (game.timer) clearInterval(game.timer);
          game.nesne(150, 400, 50, 50);
          iterations = 0;
          game.timer = setInterval(game.animate.bind(this), 30);


          });

          game.timer = setInterval(game.animate.bind(this), 30);
          },


          draw: function () {
          player.draw();
          },

          update: function () {
          player.update();///////////////
          },


          nesne: function (x, y, w, h) {

          this.x = x;
          this.y = y;
          this.w = w;
          this.h = h;

          this.image=new Image();
          this.image.src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/222579/puppy150x150.jpg";
          this.vx = 1;
          this.vy = -1;
          this.draw = function () {

          game.ctx.drawImage(this.image,this.x,this.y,this.w,this.h);


          }


          this.update = function () {
          this.x += this.vx;
          this.y += this.vy;

          }
          },

          animate: function () {
          game.ctx.clearRect(0,0,game.canvas.width,game.canvas.height);

          iterations++
          if (iterations >= 100)
          //clearInterval(interval);
          clearInterval(game.timer);
          game.update();
          game.draw();

          }
          }

          window.addEventListener("load", game.start, false);





          UPDATE



          The OP commented:




          So how do I do this when I just click on the picture.




          In this case you need to detect the position of the mouse when clicking the canvas. I've added a function to detect the mouse position:



          function oMousePos(canvas, evt) {
          var ClientRect = canvas.getBoundingClientRect();
          return {
          x: Math.round(evt.clientX - ClientRect.left),
          y: Math.round(evt.clientY - ClientRect.top)
          }
          }


          Next you need to change the function for the mousedown. You need to use the method isPointInPath to check if the mouse is inside the nesne thing.



          function(event) {
          mouse = oMousePos(game.canvas, event);

          game.ctx.beginPath();
          game.ctx.rect(player.x,player.y,player.w,player.h);
          if (game.ctx.isPointInPath(mouse.x, mouse.y)) {


          if (game.timer) clearInterval(game.timer);
          game.nesne(150, 400, 50, 50);
          iterations = 0;
          game.timer = setInterval(game.animate.bind(this), 30);
          }

          });





          share|improve this answer























          • Thanks for the changes you made. So how do I do this when I just click on the picture. And one of the things I can't do is I'm really freaking out :) –
            – Veysel BAYAR
            Nov 21 at 10:49










          • The picture disappeared when I changed the codes according to ispointPath. I must have made a mistake again somewhere.Also in console console 22 says there is an error. mouse = oMousePos(game.canvas, event); ==> error code
            – Veysel BAYAR
            Nov 21 at 11:43












          • Thank you. I wrote missing codes
            – Veysel BAYAR
            Nov 21 at 12:26














          0












          0








          0






          Changes I've made:




          1. instead of clearInterval(interval); I use clearInterval(game.timer);
            The interval is not defined. You have to use the handler you created for the setInterval(line 24 in my code)


          2. You call the game.update function but in your code is an empty function. I've changed that. Now the game.update() calls the player.update().


          3. To reanimate the player again on mousedown you need to reset the iterations = 0;






          var player, mouse;
          var iterations = 0;


          var game = {
          canvas: document.createElement("canvas"),
          start: function () {

          game.canvas.width = 357;
          game.canvas.height = 500;
          game.canvas.style.border = "1px solid red";
          game.ctx = game.canvas.getContext("2d");


          document.body.appendChild(game.canvas);

          player=new game.nesne(150,400,50,50);


          game.canvas.addEventListener("mousedown",
          function(event) {

          if (game.timer) clearInterval(game.timer);
          game.nesne(150, 400, 50, 50);
          iterations = 0;
          game.timer = setInterval(game.animate.bind(this), 30);


          });

          game.timer = setInterval(game.animate.bind(this), 30);
          },


          draw: function () {
          player.draw();
          },

          update: function () {
          player.update();///////////////
          },


          nesne: function (x, y, w, h) {

          this.x = x;
          this.y = y;
          this.w = w;
          this.h = h;

          this.image=new Image();
          this.image.src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/222579/puppy150x150.jpg";
          this.vx = 1;
          this.vy = -1;
          this.draw = function () {

          game.ctx.drawImage(this.image,this.x,this.y,this.w,this.h);


          }


          this.update = function () {
          this.x += this.vx;
          this.y += this.vy;

          }
          },

          animate: function () {
          game.ctx.clearRect(0,0,game.canvas.width,game.canvas.height);

          iterations++
          if (iterations >= 100)
          //clearInterval(interval);
          clearInterval(game.timer);
          game.update();
          game.draw();

          }
          }

          window.addEventListener("load", game.start, false);





          UPDATE



          The OP commented:




          So how do I do this when I just click on the picture.




          In this case you need to detect the position of the mouse when clicking the canvas. I've added a function to detect the mouse position:



          function oMousePos(canvas, evt) {
          var ClientRect = canvas.getBoundingClientRect();
          return {
          x: Math.round(evt.clientX - ClientRect.left),
          y: Math.round(evt.clientY - ClientRect.top)
          }
          }


          Next you need to change the function for the mousedown. You need to use the method isPointInPath to check if the mouse is inside the nesne thing.



          function(event) {
          mouse = oMousePos(game.canvas, event);

          game.ctx.beginPath();
          game.ctx.rect(player.x,player.y,player.w,player.h);
          if (game.ctx.isPointInPath(mouse.x, mouse.y)) {


          if (game.timer) clearInterval(game.timer);
          game.nesne(150, 400, 50, 50);
          iterations = 0;
          game.timer = setInterval(game.animate.bind(this), 30);
          }

          });





          share|improve this answer














          Changes I've made:




          1. instead of clearInterval(interval); I use clearInterval(game.timer);
            The interval is not defined. You have to use the handler you created for the setInterval(line 24 in my code)


          2. You call the game.update function but in your code is an empty function. I've changed that. Now the game.update() calls the player.update().


          3. To reanimate the player again on mousedown you need to reset the iterations = 0;






          var player, mouse;
          var iterations = 0;


          var game = {
          canvas: document.createElement("canvas"),
          start: function () {

          game.canvas.width = 357;
          game.canvas.height = 500;
          game.canvas.style.border = "1px solid red";
          game.ctx = game.canvas.getContext("2d");


          document.body.appendChild(game.canvas);

          player=new game.nesne(150,400,50,50);


          game.canvas.addEventListener("mousedown",
          function(event) {

          if (game.timer) clearInterval(game.timer);
          game.nesne(150, 400, 50, 50);
          iterations = 0;
          game.timer = setInterval(game.animate.bind(this), 30);


          });

          game.timer = setInterval(game.animate.bind(this), 30);
          },


          draw: function () {
          player.draw();
          },

          update: function () {
          player.update();///////////////
          },


          nesne: function (x, y, w, h) {

          this.x = x;
          this.y = y;
          this.w = w;
          this.h = h;

          this.image=new Image();
          this.image.src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/222579/puppy150x150.jpg";
          this.vx = 1;
          this.vy = -1;
          this.draw = function () {

          game.ctx.drawImage(this.image,this.x,this.y,this.w,this.h);


          }


          this.update = function () {
          this.x += this.vx;
          this.y += this.vy;

          }
          },

          animate: function () {
          game.ctx.clearRect(0,0,game.canvas.width,game.canvas.height);

          iterations++
          if (iterations >= 100)
          //clearInterval(interval);
          clearInterval(game.timer);
          game.update();
          game.draw();

          }
          }

          window.addEventListener("load", game.start, false);





          UPDATE



          The OP commented:




          So how do I do this when I just click on the picture.




          In this case you need to detect the position of the mouse when clicking the canvas. I've added a function to detect the mouse position:



          function oMousePos(canvas, evt) {
          var ClientRect = canvas.getBoundingClientRect();
          return {
          x: Math.round(evt.clientX - ClientRect.left),
          y: Math.round(evt.clientY - ClientRect.top)
          }
          }


          Next you need to change the function for the mousedown. You need to use the method isPointInPath to check if the mouse is inside the nesne thing.



          function(event) {
          mouse = oMousePos(game.canvas, event);

          game.ctx.beginPath();
          game.ctx.rect(player.x,player.y,player.w,player.h);
          if (game.ctx.isPointInPath(mouse.x, mouse.y)) {


          if (game.timer) clearInterval(game.timer);
          game.nesne(150, 400, 50, 50);
          iterations = 0;
          game.timer = setInterval(game.animate.bind(this), 30);
          }

          });





          var player, mouse;
          var iterations = 0;


          var game = {
          canvas: document.createElement("canvas"),
          start: function () {

          game.canvas.width = 357;
          game.canvas.height = 500;
          game.canvas.style.border = "1px solid red";
          game.ctx = game.canvas.getContext("2d");


          document.body.appendChild(game.canvas);

          player=new game.nesne(150,400,50,50);


          game.canvas.addEventListener("mousedown",
          function(event) {

          if (game.timer) clearInterval(game.timer);
          game.nesne(150, 400, 50, 50);
          iterations = 0;
          game.timer = setInterval(game.animate.bind(this), 30);


          });

          game.timer = setInterval(game.animate.bind(this), 30);
          },


          draw: function () {
          player.draw();
          },

          update: function () {
          player.update();///////////////
          },


          nesne: function (x, y, w, h) {

          this.x = x;
          this.y = y;
          this.w = w;
          this.h = h;

          this.image=new Image();
          this.image.src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/222579/puppy150x150.jpg";
          this.vx = 1;
          this.vy = -1;
          this.draw = function () {

          game.ctx.drawImage(this.image,this.x,this.y,this.w,this.h);


          }


          this.update = function () {
          this.x += this.vx;
          this.y += this.vy;

          }
          },

          animate: function () {
          game.ctx.clearRect(0,0,game.canvas.width,game.canvas.height);

          iterations++
          if (iterations >= 100)
          //clearInterval(interval);
          clearInterval(game.timer);
          game.update();
          game.draw();

          }
          }

          window.addEventListener("load", game.start, false);





          var player, mouse;
          var iterations = 0;


          var game = {
          canvas: document.createElement("canvas"),
          start: function () {

          game.canvas.width = 357;
          game.canvas.height = 500;
          game.canvas.style.border = "1px solid red";
          game.ctx = game.canvas.getContext("2d");


          document.body.appendChild(game.canvas);

          player=new game.nesne(150,400,50,50);


          game.canvas.addEventListener("mousedown",
          function(event) {

          if (game.timer) clearInterval(game.timer);
          game.nesne(150, 400, 50, 50);
          iterations = 0;
          game.timer = setInterval(game.animate.bind(this), 30);


          });

          game.timer = setInterval(game.animate.bind(this), 30);
          },


          draw: function () {
          player.draw();
          },

          update: function () {
          player.update();///////////////
          },


          nesne: function (x, y, w, h) {

          this.x = x;
          this.y = y;
          this.w = w;
          this.h = h;

          this.image=new Image();
          this.image.src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/222579/puppy150x150.jpg";
          this.vx = 1;
          this.vy = -1;
          this.draw = function () {

          game.ctx.drawImage(this.image,this.x,this.y,this.w,this.h);


          }


          this.update = function () {
          this.x += this.vx;
          this.y += this.vy;

          }
          },

          animate: function () {
          game.ctx.clearRect(0,0,game.canvas.width,game.canvas.height);

          iterations++
          if (iterations >= 100)
          //clearInterval(interval);
          clearInterval(game.timer);
          game.update();
          game.draw();

          }
          }

          window.addEventListener("load", game.start, false);






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 21 at 10:49

























          answered Nov 21 at 8:49









          enxaneta

          5,3562415




          5,3562415












          • Thanks for the changes you made. So how do I do this when I just click on the picture. And one of the things I can't do is I'm really freaking out :) –
            – Veysel BAYAR
            Nov 21 at 10:49










          • The picture disappeared when I changed the codes according to ispointPath. I must have made a mistake again somewhere.Also in console console 22 says there is an error. mouse = oMousePos(game.canvas, event); ==> error code
            – Veysel BAYAR
            Nov 21 at 11:43












          • Thank you. I wrote missing codes
            – Veysel BAYAR
            Nov 21 at 12:26


















          • Thanks for the changes you made. So how do I do this when I just click on the picture. And one of the things I can't do is I'm really freaking out :) –
            – Veysel BAYAR
            Nov 21 at 10:49










          • The picture disappeared when I changed the codes according to ispointPath. I must have made a mistake again somewhere.Also in console console 22 says there is an error. mouse = oMousePos(game.canvas, event); ==> error code
            – Veysel BAYAR
            Nov 21 at 11:43












          • Thank you. I wrote missing codes
            – Veysel BAYAR
            Nov 21 at 12:26
















          Thanks for the changes you made. So how do I do this when I just click on the picture. And one of the things I can't do is I'm really freaking out :) –
          – Veysel BAYAR
          Nov 21 at 10:49




          Thanks for the changes you made. So how do I do this when I just click on the picture. And one of the things I can't do is I'm really freaking out :) –
          – Veysel BAYAR
          Nov 21 at 10:49












          The picture disappeared when I changed the codes according to ispointPath. I must have made a mistake again somewhere.Also in console console 22 says there is an error. mouse = oMousePos(game.canvas, event); ==> error code
          – Veysel BAYAR
          Nov 21 at 11:43






          The picture disappeared when I changed the codes according to ispointPath. I must have made a mistake again somewhere.Also in console console 22 says there is an error. mouse = oMousePos(game.canvas, event); ==> error code
          – Veysel BAYAR
          Nov 21 at 11:43














          Thank you. I wrote missing codes
          – Veysel BAYAR
          Nov 21 at 12:26




          Thank you. I wrote missing codes
          – Veysel BAYAR
          Nov 21 at 12:26


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403304%2fhow-to-choose-the-pictureobject-in-canvas-html5%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          TypeError: fit_transform() missing 1 required positional argument: 'X'