How to paint an element in screen using javascript?

In summary, you are trying to move a bat element left and right using arrow keys and change its position using CSS styles. However, this method will not work for smooth motion and positioning as it relies on absolute positioning instead of CSS grid based rendering. To achieve the desired result, you will need to use the logic of calculating new bat position based on the old position and bat direction, and then use the bat element's CSS styles to update its position.
  • #1
shivajikobardan
674
54
TL;DR Summary
paint element in javascript
JavaScript:
<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:
pFgfWPrgsTpelep9BMMcp24Uw8Qw0vuKJbvqH_5RBTCLwd7khQ.png

Now, I want the bat to move left and right when I press arrow keys.

JavaScript:
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.

 
Technology news on Phys.org
  • #2
Normally we would do this by changing the bottom and left CSS styles on the bat element: this relies on the bat and ball being absolutely positioned children of a (relatively positioned) parent element rather than the CSS grid based rendering you used for Snake (which won't work for the smooth motion you need for bat and ball games).
JavaScript:
batElement.style.bottom = batPosition.y;
batElement.style.left = batPosition.x;
 

1. How do I paint an element on the screen using javascript?

To paint an element on the screen using javascript, you can use the document.createElement() method to create a new element, and then use the appendChild() method to add it to the desired location on the page.

2. Can I paint multiple elements on the screen using javascript?

Yes, you can paint multiple elements on the screen using javascript by repeating the process of creating and appending elements as many times as needed.

3. How do I change the color or style of the painted element?

To change the color or style of a painted element, you can use the style property of the element and specify the desired background color, font, size, etc.

4. Can I use javascript to animate the painted element?

Yes, you can use javascript to animate the painted element by using the requestAnimationFrame() method, which allows you to create smooth and efficient animations.

5. Is there a specific library or framework for painting elements on the screen with javascript?

There are several libraries and frameworks available for painting elements on the screen with javascript, such as Canvas, SVG, and Three.js. You can choose the one that best suits your needs and skill level.

Similar threads

  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
21
Views
5K
  • Computing and Technology
Replies
3
Views
2K
  • Programming and Computer Science
Replies
13
Views
1K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
10
Views
3K
Replies
40
Views
2K
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
Back
Top