Putting rectangle object on border of canvas












2















I'm making a program that regards bouncing balls, when they are the same color and touch each other they spawn a new ball. Now I want to add a rectangle object on part of the border that if touched by the balls will destroy the ball object. I'm having issues setting rectangles on the border of the left and right side of the canvas. Here is my code where i draw the rectangles on the border of the canvas.



function Wall(x,y,width,height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;

ctx.strokeStyle = 'red';
ctx.lineWidth = '6';
ctx.strokeRect(canvas.width/2, 0, canvas.width, 0); //top, right half
ctx.strokeRect(-canvas.width/2, canvas.height , canvas.width,0); //bottom,left half
ctx.strokeRect(-canvas.width/2, 0, canvas.height/2, 0); //Where i want to border left side, top half, DOESNT WORK

}


I feel like it's something really simple that I'm missing. Or is there a better way to go about this concept?










share|improve this question



























    2















    I'm making a program that regards bouncing balls, when they are the same color and touch each other they spawn a new ball. Now I want to add a rectangle object on part of the border that if touched by the balls will destroy the ball object. I'm having issues setting rectangles on the border of the left and right side of the canvas. Here is my code where i draw the rectangles on the border of the canvas.



    function Wall(x,y,width,height) {
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;

    ctx.strokeStyle = 'red';
    ctx.lineWidth = '6';
    ctx.strokeRect(canvas.width/2, 0, canvas.width, 0); //top, right half
    ctx.strokeRect(-canvas.width/2, canvas.height , canvas.width,0); //bottom,left half
    ctx.strokeRect(-canvas.width/2, 0, canvas.height/2, 0); //Where i want to border left side, top half, DOESNT WORK

    }


    I feel like it's something really simple that I'm missing. Or is there a better way to go about this concept?










    share|improve this question

























      2












      2








      2








      I'm making a program that regards bouncing balls, when they are the same color and touch each other they spawn a new ball. Now I want to add a rectangle object on part of the border that if touched by the balls will destroy the ball object. I'm having issues setting rectangles on the border of the left and right side of the canvas. Here is my code where i draw the rectangles on the border of the canvas.



      function Wall(x,y,width,height) {
      this.x = x;
      this.y = y;
      this.width = width;
      this.height = height;

      ctx.strokeStyle = 'red';
      ctx.lineWidth = '6';
      ctx.strokeRect(canvas.width/2, 0, canvas.width, 0); //top, right half
      ctx.strokeRect(-canvas.width/2, canvas.height , canvas.width,0); //bottom,left half
      ctx.strokeRect(-canvas.width/2, 0, canvas.height/2, 0); //Where i want to border left side, top half, DOESNT WORK

      }


      I feel like it's something really simple that I'm missing. Or is there a better way to go about this concept?










      share|improve this question














      I'm making a program that regards bouncing balls, when they are the same color and touch each other they spawn a new ball. Now I want to add a rectangle object on part of the border that if touched by the balls will destroy the ball object. I'm having issues setting rectangles on the border of the left and right side of the canvas. Here is my code where i draw the rectangles on the border of the canvas.



      function Wall(x,y,width,height) {
      this.x = x;
      this.y = y;
      this.width = width;
      this.height = height;

      ctx.strokeStyle = 'red';
      ctx.lineWidth = '6';
      ctx.strokeRect(canvas.width/2, 0, canvas.width, 0); //top, right half
      ctx.strokeRect(-canvas.width/2, canvas.height , canvas.width,0); //bottom,left half
      ctx.strokeRect(-canvas.width/2, 0, canvas.height/2, 0); //Where i want to border left side, top half, DOESNT WORK

      }


      I feel like it's something really simple that I'm missing. Or is there a better way to go about this concept?







      javascript canvas border






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 25 '18 at 22:31









      TheManCTheManC

      204




      204
























          2 Answers
          2






          active

          oldest

          votes


















          1














          You need to use the right coordinates — the top corner is simply 0, 0.






          function Wall() {
          let canvas = document.getElementById('myCanvas')
          ctx = canvas.getContext('2d')
          ctx.strokeStyle = 'red';
          ctx.lineWidth = '6';
          ctx.strokeRect(canvas.width/2, 0, canvas.width, 0); //top, right half
          ctx.strokeRect(0, canvas.height , canvas.width/2,0); //bottom,left half
          ctx.strokeRect(0, 0, 0, canvas.height/2);
          ctx.strokeRect(canvas.width, canvas.height/2, canvas.width, canvas.height);

          }
          Wall()

          <canvas id="myCanvas"></canvas>








          share|improve this answer

































            0














            Not sure I've all understood (especially why you use negative coordinates) but it should do the job:



            ctx.strokeRect(-canvas.width/2, 0, canvas.width, 0)





            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',
              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%2f53472677%2fputting-rectangle-object-on-border-of-canvas%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              You need to use the right coordinates — the top corner is simply 0, 0.






              function Wall() {
              let canvas = document.getElementById('myCanvas')
              ctx = canvas.getContext('2d')
              ctx.strokeStyle = 'red';
              ctx.lineWidth = '6';
              ctx.strokeRect(canvas.width/2, 0, canvas.width, 0); //top, right half
              ctx.strokeRect(0, canvas.height , canvas.width/2,0); //bottom,left half
              ctx.strokeRect(0, 0, 0, canvas.height/2);
              ctx.strokeRect(canvas.width, canvas.height/2, canvas.width, canvas.height);

              }
              Wall()

              <canvas id="myCanvas"></canvas>








              share|improve this answer






























                1














                You need to use the right coordinates — the top corner is simply 0, 0.






                function Wall() {
                let canvas = document.getElementById('myCanvas')
                ctx = canvas.getContext('2d')
                ctx.strokeStyle = 'red';
                ctx.lineWidth = '6';
                ctx.strokeRect(canvas.width/2, 0, canvas.width, 0); //top, right half
                ctx.strokeRect(0, canvas.height , canvas.width/2,0); //bottom,left half
                ctx.strokeRect(0, 0, 0, canvas.height/2);
                ctx.strokeRect(canvas.width, canvas.height/2, canvas.width, canvas.height);

                }
                Wall()

                <canvas id="myCanvas"></canvas>








                share|improve this answer




























                  1












                  1








                  1







                  You need to use the right coordinates — the top corner is simply 0, 0.






                  function Wall() {
                  let canvas = document.getElementById('myCanvas')
                  ctx = canvas.getContext('2d')
                  ctx.strokeStyle = 'red';
                  ctx.lineWidth = '6';
                  ctx.strokeRect(canvas.width/2, 0, canvas.width, 0); //top, right half
                  ctx.strokeRect(0, canvas.height , canvas.width/2,0); //bottom,left half
                  ctx.strokeRect(0, 0, 0, canvas.height/2);
                  ctx.strokeRect(canvas.width, canvas.height/2, canvas.width, canvas.height);

                  }
                  Wall()

                  <canvas id="myCanvas"></canvas>








                  share|improve this answer















                  You need to use the right coordinates — the top corner is simply 0, 0.






                  function Wall() {
                  let canvas = document.getElementById('myCanvas')
                  ctx = canvas.getContext('2d')
                  ctx.strokeStyle = 'red';
                  ctx.lineWidth = '6';
                  ctx.strokeRect(canvas.width/2, 0, canvas.width, 0); //top, right half
                  ctx.strokeRect(0, canvas.height , canvas.width/2,0); //bottom,left half
                  ctx.strokeRect(0, 0, 0, canvas.height/2);
                  ctx.strokeRect(canvas.width, canvas.height/2, canvas.width, canvas.height);

                  }
                  Wall()

                  <canvas id="myCanvas"></canvas>








                  function Wall() {
                  let canvas = document.getElementById('myCanvas')
                  ctx = canvas.getContext('2d')
                  ctx.strokeStyle = 'red';
                  ctx.lineWidth = '6';
                  ctx.strokeRect(canvas.width/2, 0, canvas.width, 0); //top, right half
                  ctx.strokeRect(0, canvas.height , canvas.width/2,0); //bottom,left half
                  ctx.strokeRect(0, 0, 0, canvas.height/2);
                  ctx.strokeRect(canvas.width, canvas.height/2, canvas.width, canvas.height);

                  }
                  Wall()

                  <canvas id="myCanvas"></canvas>





                  function Wall() {
                  let canvas = document.getElementById('myCanvas')
                  ctx = canvas.getContext('2d')
                  ctx.strokeStyle = 'red';
                  ctx.lineWidth = '6';
                  ctx.strokeRect(canvas.width/2, 0, canvas.width, 0); //top, right half
                  ctx.strokeRect(0, canvas.height , canvas.width/2,0); //bottom,left half
                  ctx.strokeRect(0, 0, 0, canvas.height/2);
                  ctx.strokeRect(canvas.width, canvas.height/2, canvas.width, canvas.height);

                  }
                  Wall()

                  <canvas id="myCanvas"></canvas>






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 25 '18 at 22:41

























                  answered Nov 25 '18 at 22:38









                  Mark MeyerMark Meyer

                  38.9k33160




                  38.9k33160

























                      0














                      Not sure I've all understood (especially why you use negative coordinates) but it should do the job:



                      ctx.strokeRect(-canvas.width/2, 0, canvas.width, 0)





                      share|improve this answer




























                        0














                        Not sure I've all understood (especially why you use negative coordinates) but it should do the job:



                        ctx.strokeRect(-canvas.width/2, 0, canvas.width, 0)





                        share|improve this answer


























                          0












                          0








                          0







                          Not sure I've all understood (especially why you use negative coordinates) but it should do the job:



                          ctx.strokeRect(-canvas.width/2, 0, canvas.width, 0)





                          share|improve this answer













                          Not sure I've all understood (especially why you use negative coordinates) but it should do the job:



                          ctx.strokeRect(-canvas.width/2, 0, canvas.width, 0)






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 25 '18 at 22:37









                          PragmateekPragmateek

                          9,26685488




                          9,26685488






























                              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%2f53472677%2fputting-rectangle-object-on-border-of-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

                              404 Error Contact Form 7 ajax form submitting

                              How to know if a Active Directory user can login interactively

                              Refactoring coordinates for Minecraft Pi buildings written in Python