Html Canvas random start position to move afterwards











up vote
1
down vote

favorite












I am currently learning JavaScript.



I want the random start position of the cube to go up and down. This will cause the cubes to jump up and down on the Y axis.



I want the cubes to move up and down



Picture



maybe like that:



Y = Y + speed;
if(Y <= 0) {
speed = -speed;
}
if(Y >= canvas.width) {
speed = -speed;
}


So that's my code:



window.onload = function() {
canvas = document.getElementById('gameCanvas');
canvasContext = canvas.getContext('2d');
draw();
}
function draw(){
canvasContext.fillStyle = "white";
canvasContext.fillRect(0,0,canvas.width,canvas.height, "#cc33ff");
for (var i=0; i <= 50; i++) {
var randX = Math.floor(Math.random() * 800);
X = randX;
var randY = Math.floor(Math.random() * 600);
Y = randY;
speed = 10;
var colorArray = ['#2185C5', '#7ECEFD', '#FFF6E5', '#FF6666'];
var randColor = colorArray[Math.floor(Math.random() * colorArray.length)];
canvasContext.beginPath();
canvasContext.rect(X, Y, 20, 20);
canvasContext.fillStyle = randColor;
canvasContext.fill();
canvasContext.strokeStyle = "black";
canvasContext.stroke();
canvasContext.closePath();
}
}









share|improve this question
























  • What's the problem you're facing?
    – rv7
    Nov 7 at 18:20










  • I want the cubes to move up and down
    – MorpheusCode
    Nov 7 at 18:31















up vote
1
down vote

favorite












I am currently learning JavaScript.



I want the random start position of the cube to go up and down. This will cause the cubes to jump up and down on the Y axis.



I want the cubes to move up and down



Picture



maybe like that:



Y = Y + speed;
if(Y <= 0) {
speed = -speed;
}
if(Y >= canvas.width) {
speed = -speed;
}


So that's my code:



window.onload = function() {
canvas = document.getElementById('gameCanvas');
canvasContext = canvas.getContext('2d');
draw();
}
function draw(){
canvasContext.fillStyle = "white";
canvasContext.fillRect(0,0,canvas.width,canvas.height, "#cc33ff");
for (var i=0; i <= 50; i++) {
var randX = Math.floor(Math.random() * 800);
X = randX;
var randY = Math.floor(Math.random() * 600);
Y = randY;
speed = 10;
var colorArray = ['#2185C5', '#7ECEFD', '#FFF6E5', '#FF6666'];
var randColor = colorArray[Math.floor(Math.random() * colorArray.length)];
canvasContext.beginPath();
canvasContext.rect(X, Y, 20, 20);
canvasContext.fillStyle = randColor;
canvasContext.fill();
canvasContext.strokeStyle = "black";
canvasContext.stroke();
canvasContext.closePath();
}
}









share|improve this question
























  • What's the problem you're facing?
    – rv7
    Nov 7 at 18:20










  • I want the cubes to move up and down
    – MorpheusCode
    Nov 7 at 18:31













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I am currently learning JavaScript.



I want the random start position of the cube to go up and down. This will cause the cubes to jump up and down on the Y axis.



I want the cubes to move up and down



Picture



maybe like that:



Y = Y + speed;
if(Y <= 0) {
speed = -speed;
}
if(Y >= canvas.width) {
speed = -speed;
}


So that's my code:



window.onload = function() {
canvas = document.getElementById('gameCanvas');
canvasContext = canvas.getContext('2d');
draw();
}
function draw(){
canvasContext.fillStyle = "white";
canvasContext.fillRect(0,0,canvas.width,canvas.height, "#cc33ff");
for (var i=0; i <= 50; i++) {
var randX = Math.floor(Math.random() * 800);
X = randX;
var randY = Math.floor(Math.random() * 600);
Y = randY;
speed = 10;
var colorArray = ['#2185C5', '#7ECEFD', '#FFF6E5', '#FF6666'];
var randColor = colorArray[Math.floor(Math.random() * colorArray.length)];
canvasContext.beginPath();
canvasContext.rect(X, Y, 20, 20);
canvasContext.fillStyle = randColor;
canvasContext.fill();
canvasContext.strokeStyle = "black";
canvasContext.stroke();
canvasContext.closePath();
}
}









share|improve this question















I am currently learning JavaScript.



I want the random start position of the cube to go up and down. This will cause the cubes to jump up and down on the Y axis.



I want the cubes to move up and down



Picture



maybe like that:



Y = Y + speed;
if(Y <= 0) {
speed = -speed;
}
if(Y >= canvas.width) {
speed = -speed;
}


So that's my code:



window.onload = function() {
canvas = document.getElementById('gameCanvas');
canvasContext = canvas.getContext('2d');
draw();
}
function draw(){
canvasContext.fillStyle = "white";
canvasContext.fillRect(0,0,canvas.width,canvas.height, "#cc33ff");
for (var i=0; i <= 50; i++) {
var randX = Math.floor(Math.random() * 800);
X = randX;
var randY = Math.floor(Math.random() * 600);
Y = randY;
speed = 10;
var colorArray = ['#2185C5', '#7ECEFD', '#FFF6E5', '#FF6666'];
var randColor = colorArray[Math.floor(Math.random() * colorArray.length)];
canvasContext.beginPath();
canvasContext.rect(X, Y, 20, 20);
canvasContext.fillStyle = randColor;
canvasContext.fill();
canvasContext.strokeStyle = "black";
canvasContext.stroke();
canvasContext.closePath();
}
}






javascript html canvas random






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 7 at 18:31

























asked Nov 7 at 18:17









MorpheusCode

114




114












  • What's the problem you're facing?
    – rv7
    Nov 7 at 18:20










  • I want the cubes to move up and down
    – MorpheusCode
    Nov 7 at 18:31


















  • What's the problem you're facing?
    – rv7
    Nov 7 at 18:20










  • I want the cubes to move up and down
    – MorpheusCode
    Nov 7 at 18:31
















What's the problem you're facing?
– rv7
Nov 7 at 18:20




What's the problem you're facing?
– rv7
Nov 7 at 18:20












I want the cubes to move up and down
– MorpheusCode
Nov 7 at 18:31




I want the cubes to move up and down
– MorpheusCode
Nov 7 at 18:31












3 Answers
3






active

oldest

votes

















up vote
0
down vote













I think what you need to do is have an initialisation routine to determine the starting random position and then another to handle the up/down movement. What you have now is merely handling initialisation but not handling the next animation frame etc.






share|improve this answer





















  • Can you edit the code so that it is correct?
    – MorpheusCode
    Nov 7 at 19:20


















up vote
0
down vote













In this case you would need to save your squares in an array. You will also need a function to update the squares position. In my answer I'm using a function that updates all the squares and an other one that draws all the squares. A better solution would be to use the a function that draws or updates a single square, and to call that function for each square.






var cw,ch,w = 20;
var squares =

canvas = document.getElementById('gameCanvas');
// set the canvas width and height
cw = canvas.width = 800;
ch = canvas.height = 600;
canvasContext = canvas.getContext('2d');

for (var i=0; i <= 50; i++) {
// make a new square object
var sq = {}
var randX = Math.floor(Math.random() * (cw - w));
sq.X = randX;
var randY = Math.floor(Math.random() * (ch - w));
sq.Y = randY;
sq.speed = 1;
var colorArray = ['#2185C5', '#7ECEFD', '#FFF6E5', '#FF6666'];
var randColor = colorArray[Math.floor(Math.random() * colorArray.length)];
sq.color = randColor;
// push the new square object into the squares array
squares.push(sq);

}

function Draw() {
// to animate the squares you will need to use the requestAnimationFrame method
window.requestAnimationFrame(Draw);
canvasContext.clearRect(0,0,cw,ch);

draw();
update();
}

Draw();

function draw(){
canvasContext.fillStyle = "white";
canvasContext.fillRect(0,0,canvas.width,canvas.height);
for (var i=0; i < squares.length; i++) {
var sq = squares[i];
canvasContext.beginPath();
canvasContext.rect(sq.X, sq.Y, w, w);
canvasContext.fillStyle =sq.color;
canvasContext.fill();
canvasContext.strokeStyle = "black";
canvasContext.stroke();
canvasContext.closePath();
}
}

function update(){
// a function to update the squares position with every frame.
for (var i=0; i < squares.length; i++) {
var sq = squares[i];
if(sq.Y >= ch - w || sq.Y <= 0){sq.speed *= -1;}
sq.Y += sq.speed;
}
}

<canvas id="gameCanvas"></canvas>








share|improve this answer





















  • Update all draw all is actually netter than update single draw single, easier for browser to optimize, but you'll want to make it smarter by conposing the less pathes as possible (i.e when consecutive rects share the same color, don't call beginPath, nor fill nor stroke). Also, what is closePath doing here?
    – Kaiido
    Nov 8 at 0:31


















up vote
0
down vote













It doesnt't work on my Computer but on JsFidlle:



The Problem(Picture)






share|improve this answer





















    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',
    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%2f53195444%2fhtml-canvas-random-start-position-to-move-afterwards%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    I think what you need to do is have an initialisation routine to determine the starting random position and then another to handle the up/down movement. What you have now is merely handling initialisation but not handling the next animation frame etc.






    share|improve this answer





















    • Can you edit the code so that it is correct?
      – MorpheusCode
      Nov 7 at 19:20















    up vote
    0
    down vote













    I think what you need to do is have an initialisation routine to determine the starting random position and then another to handle the up/down movement. What you have now is merely handling initialisation but not handling the next animation frame etc.






    share|improve this answer





















    • Can you edit the code so that it is correct?
      – MorpheusCode
      Nov 7 at 19:20













    up vote
    0
    down vote










    up vote
    0
    down vote









    I think what you need to do is have an initialisation routine to determine the starting random position and then another to handle the up/down movement. What you have now is merely handling initialisation but not handling the next animation frame etc.






    share|improve this answer












    I think what you need to do is have an initialisation routine to determine the starting random position and then another to handle the up/down movement. What you have now is merely handling initialisation but not handling the next animation frame etc.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 7 at 18:52









    schlock

    136129




    136129












    • Can you edit the code so that it is correct?
      – MorpheusCode
      Nov 7 at 19:20


















    • Can you edit the code so that it is correct?
      – MorpheusCode
      Nov 7 at 19:20
















    Can you edit the code so that it is correct?
    – MorpheusCode
    Nov 7 at 19:20




    Can you edit the code so that it is correct?
    – MorpheusCode
    Nov 7 at 19:20












    up vote
    0
    down vote













    In this case you would need to save your squares in an array. You will also need a function to update the squares position. In my answer I'm using a function that updates all the squares and an other one that draws all the squares. A better solution would be to use the a function that draws or updates a single square, and to call that function for each square.






    var cw,ch,w = 20;
    var squares =

    canvas = document.getElementById('gameCanvas');
    // set the canvas width and height
    cw = canvas.width = 800;
    ch = canvas.height = 600;
    canvasContext = canvas.getContext('2d');

    for (var i=0; i <= 50; i++) {
    // make a new square object
    var sq = {}
    var randX = Math.floor(Math.random() * (cw - w));
    sq.X = randX;
    var randY = Math.floor(Math.random() * (ch - w));
    sq.Y = randY;
    sq.speed = 1;
    var colorArray = ['#2185C5', '#7ECEFD', '#FFF6E5', '#FF6666'];
    var randColor = colorArray[Math.floor(Math.random() * colorArray.length)];
    sq.color = randColor;
    // push the new square object into the squares array
    squares.push(sq);

    }

    function Draw() {
    // to animate the squares you will need to use the requestAnimationFrame method
    window.requestAnimationFrame(Draw);
    canvasContext.clearRect(0,0,cw,ch);

    draw();
    update();
    }

    Draw();

    function draw(){
    canvasContext.fillStyle = "white";
    canvasContext.fillRect(0,0,canvas.width,canvas.height);
    for (var i=0; i < squares.length; i++) {
    var sq = squares[i];
    canvasContext.beginPath();
    canvasContext.rect(sq.X, sq.Y, w, w);
    canvasContext.fillStyle =sq.color;
    canvasContext.fill();
    canvasContext.strokeStyle = "black";
    canvasContext.stroke();
    canvasContext.closePath();
    }
    }

    function update(){
    // a function to update the squares position with every frame.
    for (var i=0; i < squares.length; i++) {
    var sq = squares[i];
    if(sq.Y >= ch - w || sq.Y <= 0){sq.speed *= -1;}
    sq.Y += sq.speed;
    }
    }

    <canvas id="gameCanvas"></canvas>








    share|improve this answer





















    • Update all draw all is actually netter than update single draw single, easier for browser to optimize, but you'll want to make it smarter by conposing the less pathes as possible (i.e when consecutive rects share the same color, don't call beginPath, nor fill nor stroke). Also, what is closePath doing here?
      – Kaiido
      Nov 8 at 0:31















    up vote
    0
    down vote













    In this case you would need to save your squares in an array. You will also need a function to update the squares position. In my answer I'm using a function that updates all the squares and an other one that draws all the squares. A better solution would be to use the a function that draws or updates a single square, and to call that function for each square.






    var cw,ch,w = 20;
    var squares =

    canvas = document.getElementById('gameCanvas');
    // set the canvas width and height
    cw = canvas.width = 800;
    ch = canvas.height = 600;
    canvasContext = canvas.getContext('2d');

    for (var i=0; i <= 50; i++) {
    // make a new square object
    var sq = {}
    var randX = Math.floor(Math.random() * (cw - w));
    sq.X = randX;
    var randY = Math.floor(Math.random() * (ch - w));
    sq.Y = randY;
    sq.speed = 1;
    var colorArray = ['#2185C5', '#7ECEFD', '#FFF6E5', '#FF6666'];
    var randColor = colorArray[Math.floor(Math.random() * colorArray.length)];
    sq.color = randColor;
    // push the new square object into the squares array
    squares.push(sq);

    }

    function Draw() {
    // to animate the squares you will need to use the requestAnimationFrame method
    window.requestAnimationFrame(Draw);
    canvasContext.clearRect(0,0,cw,ch);

    draw();
    update();
    }

    Draw();

    function draw(){
    canvasContext.fillStyle = "white";
    canvasContext.fillRect(0,0,canvas.width,canvas.height);
    for (var i=0; i < squares.length; i++) {
    var sq = squares[i];
    canvasContext.beginPath();
    canvasContext.rect(sq.X, sq.Y, w, w);
    canvasContext.fillStyle =sq.color;
    canvasContext.fill();
    canvasContext.strokeStyle = "black";
    canvasContext.stroke();
    canvasContext.closePath();
    }
    }

    function update(){
    // a function to update the squares position with every frame.
    for (var i=0; i < squares.length; i++) {
    var sq = squares[i];
    if(sq.Y >= ch - w || sq.Y <= 0){sq.speed *= -1;}
    sq.Y += sq.speed;
    }
    }

    <canvas id="gameCanvas"></canvas>








    share|improve this answer





















    • Update all draw all is actually netter than update single draw single, easier for browser to optimize, but you'll want to make it smarter by conposing the less pathes as possible (i.e when consecutive rects share the same color, don't call beginPath, nor fill nor stroke). Also, what is closePath doing here?
      – Kaiido
      Nov 8 at 0:31













    up vote
    0
    down vote










    up vote
    0
    down vote









    In this case you would need to save your squares in an array. You will also need a function to update the squares position. In my answer I'm using a function that updates all the squares and an other one that draws all the squares. A better solution would be to use the a function that draws or updates a single square, and to call that function for each square.






    var cw,ch,w = 20;
    var squares =

    canvas = document.getElementById('gameCanvas');
    // set the canvas width and height
    cw = canvas.width = 800;
    ch = canvas.height = 600;
    canvasContext = canvas.getContext('2d');

    for (var i=0; i <= 50; i++) {
    // make a new square object
    var sq = {}
    var randX = Math.floor(Math.random() * (cw - w));
    sq.X = randX;
    var randY = Math.floor(Math.random() * (ch - w));
    sq.Y = randY;
    sq.speed = 1;
    var colorArray = ['#2185C5', '#7ECEFD', '#FFF6E5', '#FF6666'];
    var randColor = colorArray[Math.floor(Math.random() * colorArray.length)];
    sq.color = randColor;
    // push the new square object into the squares array
    squares.push(sq);

    }

    function Draw() {
    // to animate the squares you will need to use the requestAnimationFrame method
    window.requestAnimationFrame(Draw);
    canvasContext.clearRect(0,0,cw,ch);

    draw();
    update();
    }

    Draw();

    function draw(){
    canvasContext.fillStyle = "white";
    canvasContext.fillRect(0,0,canvas.width,canvas.height);
    for (var i=0; i < squares.length; i++) {
    var sq = squares[i];
    canvasContext.beginPath();
    canvasContext.rect(sq.X, sq.Y, w, w);
    canvasContext.fillStyle =sq.color;
    canvasContext.fill();
    canvasContext.strokeStyle = "black";
    canvasContext.stroke();
    canvasContext.closePath();
    }
    }

    function update(){
    // a function to update the squares position with every frame.
    for (var i=0; i < squares.length; i++) {
    var sq = squares[i];
    if(sq.Y >= ch - w || sq.Y <= 0){sq.speed *= -1;}
    sq.Y += sq.speed;
    }
    }

    <canvas id="gameCanvas"></canvas>








    share|improve this answer












    In this case you would need to save your squares in an array. You will also need a function to update the squares position. In my answer I'm using a function that updates all the squares and an other one that draws all the squares. A better solution would be to use the a function that draws or updates a single square, and to call that function for each square.






    var cw,ch,w = 20;
    var squares =

    canvas = document.getElementById('gameCanvas');
    // set the canvas width and height
    cw = canvas.width = 800;
    ch = canvas.height = 600;
    canvasContext = canvas.getContext('2d');

    for (var i=0; i <= 50; i++) {
    // make a new square object
    var sq = {}
    var randX = Math.floor(Math.random() * (cw - w));
    sq.X = randX;
    var randY = Math.floor(Math.random() * (ch - w));
    sq.Y = randY;
    sq.speed = 1;
    var colorArray = ['#2185C5', '#7ECEFD', '#FFF6E5', '#FF6666'];
    var randColor = colorArray[Math.floor(Math.random() * colorArray.length)];
    sq.color = randColor;
    // push the new square object into the squares array
    squares.push(sq);

    }

    function Draw() {
    // to animate the squares you will need to use the requestAnimationFrame method
    window.requestAnimationFrame(Draw);
    canvasContext.clearRect(0,0,cw,ch);

    draw();
    update();
    }

    Draw();

    function draw(){
    canvasContext.fillStyle = "white";
    canvasContext.fillRect(0,0,canvas.width,canvas.height);
    for (var i=0; i < squares.length; i++) {
    var sq = squares[i];
    canvasContext.beginPath();
    canvasContext.rect(sq.X, sq.Y, w, w);
    canvasContext.fillStyle =sq.color;
    canvasContext.fill();
    canvasContext.strokeStyle = "black";
    canvasContext.stroke();
    canvasContext.closePath();
    }
    }

    function update(){
    // a function to update the squares position with every frame.
    for (var i=0; i < squares.length; i++) {
    var sq = squares[i];
    if(sq.Y >= ch - w || sq.Y <= 0){sq.speed *= -1;}
    sq.Y += sq.speed;
    }
    }

    <canvas id="gameCanvas"></canvas>








    var cw,ch,w = 20;
    var squares =

    canvas = document.getElementById('gameCanvas');
    // set the canvas width and height
    cw = canvas.width = 800;
    ch = canvas.height = 600;
    canvasContext = canvas.getContext('2d');

    for (var i=0; i <= 50; i++) {
    // make a new square object
    var sq = {}
    var randX = Math.floor(Math.random() * (cw - w));
    sq.X = randX;
    var randY = Math.floor(Math.random() * (ch - w));
    sq.Y = randY;
    sq.speed = 1;
    var colorArray = ['#2185C5', '#7ECEFD', '#FFF6E5', '#FF6666'];
    var randColor = colorArray[Math.floor(Math.random() * colorArray.length)];
    sq.color = randColor;
    // push the new square object into the squares array
    squares.push(sq);

    }

    function Draw() {
    // to animate the squares you will need to use the requestAnimationFrame method
    window.requestAnimationFrame(Draw);
    canvasContext.clearRect(0,0,cw,ch);

    draw();
    update();
    }

    Draw();

    function draw(){
    canvasContext.fillStyle = "white";
    canvasContext.fillRect(0,0,canvas.width,canvas.height);
    for (var i=0; i < squares.length; i++) {
    var sq = squares[i];
    canvasContext.beginPath();
    canvasContext.rect(sq.X, sq.Y, w, w);
    canvasContext.fillStyle =sq.color;
    canvasContext.fill();
    canvasContext.strokeStyle = "black";
    canvasContext.stroke();
    canvasContext.closePath();
    }
    }

    function update(){
    // a function to update the squares position with every frame.
    for (var i=0; i < squares.length; i++) {
    var sq = squares[i];
    if(sq.Y >= ch - w || sq.Y <= 0){sq.speed *= -1;}
    sq.Y += sq.speed;
    }
    }

    <canvas id="gameCanvas"></canvas>





    var cw,ch,w = 20;
    var squares =

    canvas = document.getElementById('gameCanvas');
    // set the canvas width and height
    cw = canvas.width = 800;
    ch = canvas.height = 600;
    canvasContext = canvas.getContext('2d');

    for (var i=0; i <= 50; i++) {
    // make a new square object
    var sq = {}
    var randX = Math.floor(Math.random() * (cw - w));
    sq.X = randX;
    var randY = Math.floor(Math.random() * (ch - w));
    sq.Y = randY;
    sq.speed = 1;
    var colorArray = ['#2185C5', '#7ECEFD', '#FFF6E5', '#FF6666'];
    var randColor = colorArray[Math.floor(Math.random() * colorArray.length)];
    sq.color = randColor;
    // push the new square object into the squares array
    squares.push(sq);

    }

    function Draw() {
    // to animate the squares you will need to use the requestAnimationFrame method
    window.requestAnimationFrame(Draw);
    canvasContext.clearRect(0,0,cw,ch);

    draw();
    update();
    }

    Draw();

    function draw(){
    canvasContext.fillStyle = "white";
    canvasContext.fillRect(0,0,canvas.width,canvas.height);
    for (var i=0; i < squares.length; i++) {
    var sq = squares[i];
    canvasContext.beginPath();
    canvasContext.rect(sq.X, sq.Y, w, w);
    canvasContext.fillStyle =sq.color;
    canvasContext.fill();
    canvasContext.strokeStyle = "black";
    canvasContext.stroke();
    canvasContext.closePath();
    }
    }

    function update(){
    // a function to update the squares position with every frame.
    for (var i=0; i < squares.length; i++) {
    var sq = squares[i];
    if(sq.Y >= ch - w || sq.Y <= 0){sq.speed *= -1;}
    sq.Y += sq.speed;
    }
    }

    <canvas id="gameCanvas"></canvas>






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 7 at 19:48









    enxaneta

    4,0471412




    4,0471412












    • Update all draw all is actually netter than update single draw single, easier for browser to optimize, but you'll want to make it smarter by conposing the less pathes as possible (i.e when consecutive rects share the same color, don't call beginPath, nor fill nor stroke). Also, what is closePath doing here?
      – Kaiido
      Nov 8 at 0:31


















    • Update all draw all is actually netter than update single draw single, easier for browser to optimize, but you'll want to make it smarter by conposing the less pathes as possible (i.e when consecutive rects share the same color, don't call beginPath, nor fill nor stroke). Also, what is closePath doing here?
      – Kaiido
      Nov 8 at 0:31
















    Update all draw all is actually netter than update single draw single, easier for browser to optimize, but you'll want to make it smarter by conposing the less pathes as possible (i.e when consecutive rects share the same color, don't call beginPath, nor fill nor stroke). Also, what is closePath doing here?
    – Kaiido
    Nov 8 at 0:31




    Update all draw all is actually netter than update single draw single, easier for browser to optimize, but you'll want to make it smarter by conposing the less pathes as possible (i.e when consecutive rects share the same color, don't call beginPath, nor fill nor stroke). Also, what is closePath doing here?
    – Kaiido
    Nov 8 at 0:31










    up vote
    0
    down vote













    It doesnt't work on my Computer but on JsFidlle:



    The Problem(Picture)






    share|improve this answer

























      up vote
      0
      down vote













      It doesnt't work on my Computer but on JsFidlle:



      The Problem(Picture)






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        It doesnt't work on my Computer but on JsFidlle:



        The Problem(Picture)






        share|improve this answer












        It doesnt't work on my Computer but on JsFidlle:



        The Problem(Picture)







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 9 at 14:42









        MorpheusCode

        114




        114






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53195444%2fhtml-canvas-random-start-position-to-move-afterwards%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







            這個網誌中的熱門文章

            Academy of Television Arts & Sciences

            L'Équipe

            1995 France bombings