Help with Javascript - Boxes and Text


#1

Hello! I'm creating a project game on a different platform(JS), but I'd like some information from other websites. How would one be able to post text in a rectangle and keep that text in a rectangle?
For example:
function start(x, y, x1, y1, t){
var rect = new Rectangle(x, y);
rect.setPosition(x1, y1);
rect.setColor(Color.gray)
add(rect);
var text = new Text(t, "10pt Arial");
txt.setPosition(x1, y1);
txt.setColor(Color.white);
add(txt);
}
In this case, the text would move out of the rectangle. Is there any possible way to ensure that the text will stay in the box? Some sort of getY sorcery? The text, of course, would be allowed to move vertically until it reaches the border of the box and would move beyond, but would NOT traverse across it's horizontal borders.

*Sorry about my previous introduction, wasn't sure how chatting would work here.


#3

We are given a scant lot to go on. Where are the definitions of these classes?

Rectangle
Text

And what of the DOM insertion? Where is that?


#4

First please specify the language for your code above.
My first guess would be Java since there is a static Color class and Rectangle class.
Secondly that add() can be used in a class that extends Jframe or Canvas, not so sure.

The thing that questions my guess would be the method declaration which starts with "function" which doesn't exist in Java.

If you want to make a game in javascript, you don't have to change much on your script.
First notice that you will have to make a <canvas id="c" width="500" height="500"></canvas> in your html file. Than you can add the following in your javascript to get it:

var c = document.getElementById("c").getContext("2d");

After that you can directly start drawing things. Here is how: https://www.w3schools.com/html/html5_canvas.asp