How to choose the picture(Object) in Canvas HTML5
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
add a comment |
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
add a comment |
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
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
javascript events canvas
asked Nov 20 at 23:47
Veysel BAYAR
11
11
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Changes I've made:
instead of
clearInterval(interval);
I useclearInterval(game.timer);
Theinterval
is not defined. You have to use the handler you created for thesetInterval
(line 24 in my code)You call the
game.update
function but in your code is an empty function. I've changed that. Now thegame.update()
calls theplayer.update()
.To reanimate the player again on
mousedown
you need to reset theiterations = 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);
}
});
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Changes I've made:
instead of
clearInterval(interval);
I useclearInterval(game.timer);
Theinterval
is not defined. You have to use the handler you created for thesetInterval
(line 24 in my code)You call the
game.update
function but in your code is an empty function. I've changed that. Now thegame.update()
calls theplayer.update()
.To reanimate the player again on
mousedown
you need to reset theiterations = 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);
}
});
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
add a comment |
Changes I've made:
instead of
clearInterval(interval);
I useclearInterval(game.timer);
Theinterval
is not defined. You have to use the handler you created for thesetInterval
(line 24 in my code)You call the
game.update
function but in your code is an empty function. I've changed that. Now thegame.update()
calls theplayer.update()
.To reanimate the player again on
mousedown
you need to reset theiterations = 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);
}
});
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
add a comment |
Changes I've made:
instead of
clearInterval(interval);
I useclearInterval(game.timer);
Theinterval
is not defined. You have to use the handler you created for thesetInterval
(line 24 in my code)You call the
game.update
function but in your code is an empty function. I've changed that. Now thegame.update()
calls theplayer.update()
.To reanimate the player again on
mousedown
you need to reset theiterations = 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);
}
});
Changes I've made:
instead of
clearInterval(interval);
I useclearInterval(game.timer);
Theinterval
is not defined. You have to use the handler you created for thesetInterval
(line 24 in my code)You call the
game.update
function but in your code is an empty function. I've changed that. Now thegame.update()
calls theplayer.update()
.To reanimate the player again on
mousedown
you need to reset theiterations = 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);
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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