Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript How to paint an element in screen using javascript?

shivajikobardan

Legendary Coder
Code:
<div class="body">
    <div id="board">
      <div id="bat" class="bat"></div>
      <div id="ball" class="ball"></div>
    </div>
  </div>

This is my HTML.
I've done CSS For all of them and generated this:
hnrN3k8CBV2NYuAMWZW5YVh7aNbkwa_Oj5P5ZmEHWO3rkwv7NetQ0-UL4rotwlG2DCpiou4oxv1To1z8HS9oe2orZQ1D9Adv3VuZhKF2wilLhITlWxjekfS1cFkObOjC6tX_doWv37QRpFgfWPrgsTpelep9BMMcp24Uw8Qw0vuKJbvqH_5RBTCLwd7khQ

Now, I want the bat to move left and right when I press arrow keys.
Code:
window.addEventListener("keydown", function (e) {

  switch (e.key) {
    case "ArrowLeft":
      batDir.x = -1;
      batDir.y = 0;
      paintBat(batDir.x, batDir.y);
      break;
    case "ArrowRight":
      batDir.x = 1;
      batDir.y = 0;
      paintBat(batDir.x, batDir.y);
      break;

  }
})

My goal is to paint the bat at new position. I'm wondering how to do it.

The logic should be

newBatPosition.x=oldBatPosition.x+batDirection.x
newBatPosition.y=oldBatPosition.y+batDirection.y

But what will do the job of painting newBatPosition.x and newBatPosition.y is what I'm not clear of. I'm not using canvas.

Plus, what'll be the oldBatPosition? I've used CSS to paint them. So, I'm wondering how do I get oldBatPosition coordinates as well.

I just made a similar project using tutorial and it's just disheartening that I can't make this project.

 
Back
Top Bottom