Drag and drop a moving ball around canvas












1















I have a red ball which moves in a specific path and 2 buttons to start and stop it's movement. I want to stop sometime the ball an then move it in another position of the path and then start it's movement again. Here is what i have done until now, but i did not managed to make it. My code runs and no errors appear but i can't drag or drop my ball to the position i want. Does anyone knows what i am doing wrong or why this happens??
Thanks in advance!!!



<!DOCTYPE html>
<html>
<head>
<title> example </title>

</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<button type='button' id='start'>START</button>
<button type='button' id='stop'>STOP</button>
<br>
<canvas id="myCanvas" width="1515" height="950" style="border:1px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.</canvas>

</body>
</html>


script



<script>
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");

window.requestAnimFrame = (function (callback) {
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function (callback) {
window.setTimeout(callback, 1000 / 60);
};
})();

var dragok = false;

function MouseDown(e){
x = e.pageX - canvas.offsetLeft;
y = e.pageY - canvas.offsetTop;
dragok = true;
canvas.onmousemove = MouseMove;
}

function MouseUp(e){
dragok = false;
canvas.onmousemove = null;
}

function MouseMove(e){
if (dragok){
x = e.pageX - canvas.offsetLeft;
y = e.pageY - canvas.offsetTop;
}
}

canvas.onmousedown = MouseDown;
canvas.onmouseup = MouseUp;

var pathArray = ;
pathArray.push({
x: 140,
y: 100
});
pathArray.push({
x: 1375,
y: 100
});
pathArray.push({
x: 1375,
y: 230
});
pathArray.push({
x: 140,
y: 230
});
pathArray.push({
x: 140,
y: 320
});
pathArray.push({
x: 1375,
y: 320
});
pathArray.push({
x: 1375,
y: 450
});
pathArray.push({
x: 140,
y: 450
});
pathArray.push({
x: 140,
y: 540
});
pathArray.push({
x: 1375,
y: 540
});
pathArray.push({
x: 1375,
y: 670
});
pathArray.push({
x: 140,
y: 670
});

var polypoints = makePolyPoints(pathArray, 5);
var width = 15;
var height = 30;
var speed = 1/2;
var stopAnimation = true;
var position = 0;


function drawBall(){
ctx.beginPath();
ctx.arc(150, 85, 12, 0, 2*Math.PI);
ctx.fillStyle = "#B22222";
ctx.fill();
}

drawBall();

// start animation
$('#start').on('click',function(){
stopAnimation = false;
requestAnimFrame(animate);
});

// stop animation
$('#stop').on('click',function(){
stopAnimation = true;
});

animate();

function animate(){
if(stopAnimation){return;}
setTimeout(function () {
requestAnimFrame(animate);
position += speed;
if (position> polypoints.length - 1) {
return;
}
var pt = polypoints[position];
if(pt){
ctx.save();
ctx.beginPath();
ctx.translate(pt.x, pt.y);
ctx.arc(-width / 2, -height / 2, 12, 0, 2*Math.PI);
ctx.fillStyle = "#B22222";
ctx.fill();
ctx.restore();
}
}, 1000 / 60);
}

function makePolyPoints(pathArray, speedFactor) {
var points = ;
for (var i = 1; i < pathArray.length; i++) {
var startPt = pathArray[i - 1];
var endPt = pathArray[i];
var dx = endPt.x - startPt.x;
var dy = endPt.y - startPt.y;
var distance = Math.sqrt(dx*dx+dy*dy)
var steps = distance/speedFactor
for (var n = 0; n <= steps; n++) {
var x = startPt.x + dx * n / steps;
var y = startPt.y + dy * n / steps;
points.push({
x: x,
y: y
});
}
}
return (points);
}


</script>









share|improve this question

























  • One of the things you are doing wrong is that you try to push your balls in an unexistent array pathArrayBall1

    – enxaneta
    Nov 13 '18 at 20:11











  • That was a copy-paste mistake, but thank you for your note!

    – dim109
    Nov 13 '18 at 20:36
















1















I have a red ball which moves in a specific path and 2 buttons to start and stop it's movement. I want to stop sometime the ball an then move it in another position of the path and then start it's movement again. Here is what i have done until now, but i did not managed to make it. My code runs and no errors appear but i can't drag or drop my ball to the position i want. Does anyone knows what i am doing wrong or why this happens??
Thanks in advance!!!



<!DOCTYPE html>
<html>
<head>
<title> example </title>

</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<button type='button' id='start'>START</button>
<button type='button' id='stop'>STOP</button>
<br>
<canvas id="myCanvas" width="1515" height="950" style="border:1px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.</canvas>

</body>
</html>


script



<script>
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");

window.requestAnimFrame = (function (callback) {
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function (callback) {
window.setTimeout(callback, 1000 / 60);
};
})();

var dragok = false;

function MouseDown(e){
x = e.pageX - canvas.offsetLeft;
y = e.pageY - canvas.offsetTop;
dragok = true;
canvas.onmousemove = MouseMove;
}

function MouseUp(e){
dragok = false;
canvas.onmousemove = null;
}

function MouseMove(e){
if (dragok){
x = e.pageX - canvas.offsetLeft;
y = e.pageY - canvas.offsetTop;
}
}

canvas.onmousedown = MouseDown;
canvas.onmouseup = MouseUp;

var pathArray = ;
pathArray.push({
x: 140,
y: 100
});
pathArray.push({
x: 1375,
y: 100
});
pathArray.push({
x: 1375,
y: 230
});
pathArray.push({
x: 140,
y: 230
});
pathArray.push({
x: 140,
y: 320
});
pathArray.push({
x: 1375,
y: 320
});
pathArray.push({
x: 1375,
y: 450
});
pathArray.push({
x: 140,
y: 450
});
pathArray.push({
x: 140,
y: 540
});
pathArray.push({
x: 1375,
y: 540
});
pathArray.push({
x: 1375,
y: 670
});
pathArray.push({
x: 140,
y: 670
});

var polypoints = makePolyPoints(pathArray, 5);
var width = 15;
var height = 30;
var speed = 1/2;
var stopAnimation = true;
var position = 0;


function drawBall(){
ctx.beginPath();
ctx.arc(150, 85, 12, 0, 2*Math.PI);
ctx.fillStyle = "#B22222";
ctx.fill();
}

drawBall();

// start animation
$('#start').on('click',function(){
stopAnimation = false;
requestAnimFrame(animate);
});

// stop animation
$('#stop').on('click',function(){
stopAnimation = true;
});

animate();

function animate(){
if(stopAnimation){return;}
setTimeout(function () {
requestAnimFrame(animate);
position += speed;
if (position> polypoints.length - 1) {
return;
}
var pt = polypoints[position];
if(pt){
ctx.save();
ctx.beginPath();
ctx.translate(pt.x, pt.y);
ctx.arc(-width / 2, -height / 2, 12, 0, 2*Math.PI);
ctx.fillStyle = "#B22222";
ctx.fill();
ctx.restore();
}
}, 1000 / 60);
}

function makePolyPoints(pathArray, speedFactor) {
var points = ;
for (var i = 1; i < pathArray.length; i++) {
var startPt = pathArray[i - 1];
var endPt = pathArray[i];
var dx = endPt.x - startPt.x;
var dy = endPt.y - startPt.y;
var distance = Math.sqrt(dx*dx+dy*dy)
var steps = distance/speedFactor
for (var n = 0; n <= steps; n++) {
var x = startPt.x + dx * n / steps;
var y = startPt.y + dy * n / steps;
points.push({
x: x,
y: y
});
}
}
return (points);
}


</script>









share|improve this question

























  • One of the things you are doing wrong is that you try to push your balls in an unexistent array pathArrayBall1

    – enxaneta
    Nov 13 '18 at 20:11











  • That was a copy-paste mistake, but thank you for your note!

    – dim109
    Nov 13 '18 at 20:36














1












1








1








I have a red ball which moves in a specific path and 2 buttons to start and stop it's movement. I want to stop sometime the ball an then move it in another position of the path and then start it's movement again. Here is what i have done until now, but i did not managed to make it. My code runs and no errors appear but i can't drag or drop my ball to the position i want. Does anyone knows what i am doing wrong or why this happens??
Thanks in advance!!!



<!DOCTYPE html>
<html>
<head>
<title> example </title>

</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<button type='button' id='start'>START</button>
<button type='button' id='stop'>STOP</button>
<br>
<canvas id="myCanvas" width="1515" height="950" style="border:1px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.</canvas>

</body>
</html>


script



<script>
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");

window.requestAnimFrame = (function (callback) {
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function (callback) {
window.setTimeout(callback, 1000 / 60);
};
})();

var dragok = false;

function MouseDown(e){
x = e.pageX - canvas.offsetLeft;
y = e.pageY - canvas.offsetTop;
dragok = true;
canvas.onmousemove = MouseMove;
}

function MouseUp(e){
dragok = false;
canvas.onmousemove = null;
}

function MouseMove(e){
if (dragok){
x = e.pageX - canvas.offsetLeft;
y = e.pageY - canvas.offsetTop;
}
}

canvas.onmousedown = MouseDown;
canvas.onmouseup = MouseUp;

var pathArray = ;
pathArray.push({
x: 140,
y: 100
});
pathArray.push({
x: 1375,
y: 100
});
pathArray.push({
x: 1375,
y: 230
});
pathArray.push({
x: 140,
y: 230
});
pathArray.push({
x: 140,
y: 320
});
pathArray.push({
x: 1375,
y: 320
});
pathArray.push({
x: 1375,
y: 450
});
pathArray.push({
x: 140,
y: 450
});
pathArray.push({
x: 140,
y: 540
});
pathArray.push({
x: 1375,
y: 540
});
pathArray.push({
x: 1375,
y: 670
});
pathArray.push({
x: 140,
y: 670
});

var polypoints = makePolyPoints(pathArray, 5);
var width = 15;
var height = 30;
var speed = 1/2;
var stopAnimation = true;
var position = 0;


function drawBall(){
ctx.beginPath();
ctx.arc(150, 85, 12, 0, 2*Math.PI);
ctx.fillStyle = "#B22222";
ctx.fill();
}

drawBall();

// start animation
$('#start').on('click',function(){
stopAnimation = false;
requestAnimFrame(animate);
});

// stop animation
$('#stop').on('click',function(){
stopAnimation = true;
});

animate();

function animate(){
if(stopAnimation){return;}
setTimeout(function () {
requestAnimFrame(animate);
position += speed;
if (position> polypoints.length - 1) {
return;
}
var pt = polypoints[position];
if(pt){
ctx.save();
ctx.beginPath();
ctx.translate(pt.x, pt.y);
ctx.arc(-width / 2, -height / 2, 12, 0, 2*Math.PI);
ctx.fillStyle = "#B22222";
ctx.fill();
ctx.restore();
}
}, 1000 / 60);
}

function makePolyPoints(pathArray, speedFactor) {
var points = ;
for (var i = 1; i < pathArray.length; i++) {
var startPt = pathArray[i - 1];
var endPt = pathArray[i];
var dx = endPt.x - startPt.x;
var dy = endPt.y - startPt.y;
var distance = Math.sqrt(dx*dx+dy*dy)
var steps = distance/speedFactor
for (var n = 0; n <= steps; n++) {
var x = startPt.x + dx * n / steps;
var y = startPt.y + dy * n / steps;
points.push({
x: x,
y: y
});
}
}
return (points);
}


</script>









share|improve this question
















I have a red ball which moves in a specific path and 2 buttons to start and stop it's movement. I want to stop sometime the ball an then move it in another position of the path and then start it's movement again. Here is what i have done until now, but i did not managed to make it. My code runs and no errors appear but i can't drag or drop my ball to the position i want. Does anyone knows what i am doing wrong or why this happens??
Thanks in advance!!!



<!DOCTYPE html>
<html>
<head>
<title> example </title>

</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<button type='button' id='start'>START</button>
<button type='button' id='stop'>STOP</button>
<br>
<canvas id="myCanvas" width="1515" height="950" style="border:1px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.</canvas>

</body>
</html>


script



<script>
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");

window.requestAnimFrame = (function (callback) {
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function (callback) {
window.setTimeout(callback, 1000 / 60);
};
})();

var dragok = false;

function MouseDown(e){
x = e.pageX - canvas.offsetLeft;
y = e.pageY - canvas.offsetTop;
dragok = true;
canvas.onmousemove = MouseMove;
}

function MouseUp(e){
dragok = false;
canvas.onmousemove = null;
}

function MouseMove(e){
if (dragok){
x = e.pageX - canvas.offsetLeft;
y = e.pageY - canvas.offsetTop;
}
}

canvas.onmousedown = MouseDown;
canvas.onmouseup = MouseUp;

var pathArray = ;
pathArray.push({
x: 140,
y: 100
});
pathArray.push({
x: 1375,
y: 100
});
pathArray.push({
x: 1375,
y: 230
});
pathArray.push({
x: 140,
y: 230
});
pathArray.push({
x: 140,
y: 320
});
pathArray.push({
x: 1375,
y: 320
});
pathArray.push({
x: 1375,
y: 450
});
pathArray.push({
x: 140,
y: 450
});
pathArray.push({
x: 140,
y: 540
});
pathArray.push({
x: 1375,
y: 540
});
pathArray.push({
x: 1375,
y: 670
});
pathArray.push({
x: 140,
y: 670
});

var polypoints = makePolyPoints(pathArray, 5);
var width = 15;
var height = 30;
var speed = 1/2;
var stopAnimation = true;
var position = 0;


function drawBall(){
ctx.beginPath();
ctx.arc(150, 85, 12, 0, 2*Math.PI);
ctx.fillStyle = "#B22222";
ctx.fill();
}

drawBall();

// start animation
$('#start').on('click',function(){
stopAnimation = false;
requestAnimFrame(animate);
});

// stop animation
$('#stop').on('click',function(){
stopAnimation = true;
});

animate();

function animate(){
if(stopAnimation){return;}
setTimeout(function () {
requestAnimFrame(animate);
position += speed;
if (position> polypoints.length - 1) {
return;
}
var pt = polypoints[position];
if(pt){
ctx.save();
ctx.beginPath();
ctx.translate(pt.x, pt.y);
ctx.arc(-width / 2, -height / 2, 12, 0, 2*Math.PI);
ctx.fillStyle = "#B22222";
ctx.fill();
ctx.restore();
}
}, 1000 / 60);
}

function makePolyPoints(pathArray, speedFactor) {
var points = ;
for (var i = 1; i < pathArray.length; i++) {
var startPt = pathArray[i - 1];
var endPt = pathArray[i];
var dx = endPt.x - startPt.x;
var dy = endPt.y - startPt.y;
var distance = Math.sqrt(dx*dx+dy*dy)
var steps = distance/speedFactor
for (var n = 0; n <= steps; n++) {
var x = startPt.x + dx * n / steps;
var y = startPt.y + dy * n / steps;
points.push({
x: x,
y: y
});
}
}
return (points);
}


</script>






javascript html5 canvas javascript-events html5-canvas






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 20:43







dim109

















asked Nov 13 '18 at 19:32









dim109dim109

84




84













  • One of the things you are doing wrong is that you try to push your balls in an unexistent array pathArrayBall1

    – enxaneta
    Nov 13 '18 at 20:11











  • That was a copy-paste mistake, but thank you for your note!

    – dim109
    Nov 13 '18 at 20:36



















  • One of the things you are doing wrong is that you try to push your balls in an unexistent array pathArrayBall1

    – enxaneta
    Nov 13 '18 at 20:11











  • That was a copy-paste mistake, but thank you for your note!

    – dim109
    Nov 13 '18 at 20:36

















One of the things you are doing wrong is that you try to push your balls in an unexistent array pathArrayBall1

– enxaneta
Nov 13 '18 at 20:11





One of the things you are doing wrong is that you try to push your balls in an unexistent array pathArrayBall1

– enxaneta
Nov 13 '18 at 20:11













That was a copy-paste mistake, but thank you for your note!

– dim109
Nov 13 '18 at 20:36





That was a copy-paste mistake, but thank you for your note!

– dim109
Nov 13 '18 at 20:36












0






active

oldest

votes











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%2f53288281%2fdrag-and-drop-a-moving-ball-around-canvas%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53288281%2fdrag-and-drop-a-moving-ball-around-canvas%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

Florida Star v. B. J. F.

Error while running script in elastic search , gateway timeout

Adding quotations to stringified JSON object values