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;
 

FAQ: How to paint an element in screen using javascript?

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

To paint an element on the screen using JavaScript, you can create a new element using the document.createElement() method, set its properties such as width, height, background color, and position using the style property, and then append it to the document body or another parent element using the appendChild() method.

2. Can I use the canvas element to paint elements on the screen with JavaScript?

Yes, you can use the HTML canvas element to paint elements on the screen using JavaScript. The canvas element provides a drawing surface that you can use to draw shapes, text, images, and more. You can use the canvas API methods like fillRect(), strokeRect(), and drawImage() to paint elements on the canvas.

3. How can I animate elements painted on the screen using JavaScript?

To animate elements painted on the screen using JavaScript, you can use the requestAnimationFrame() method to create smooth animations. Inside the animation loop, you can update the properties of the painted elements, such as position, size, or color, to create the desired animation effect.

4. Is it possible to paint SVG elements on the screen using JavaScript?

Yes, you can paint SVG elements on the screen using JavaScript. You can create SVG elements using the document.createElementNS() method with the namespace URI for SVG elements, set their attributes like width, height, and fill color, and then append them to an SVG container element in the document.

5. How can I handle user interactions with painted elements on the screen using JavaScript?

To handle user interactions with painted elements on the screen using JavaScript, you can add event listeners to the painted elements for events like click, mouseover, or keypress. Inside the event handler functions, you can update the properties of the elements or trigger other actions based on the user's interaction.

Similar threads

Replies
11
Views
1K
Replies
7
Views
1K
Replies
5
Views
524
Replies
3
Views
2K
Replies
21
Views
5K
Replies
13
Views
1K
Replies
5
Views
1K
Replies
5
Views
2K
Replies
10
Views
3K
Replies
9
Views
2K
Back
Top