How to get screen coordinates in Processing
I am using Processing to do a project.
I have a sketch (actually text) in the sketch board by using thedraw
function.
I want to get the screen coordinates of each word of the text to do some other interesting things.
I do not know what function I can use to retrieve, get back the screen coordinates.
Can anybody help on this. I'll appreciate you开发者_StackOverflow中文版r help.
Thanks
Hopefully I got your problem correctly and the following sketch explains how you can achieve a solution. Its pretty fast forward and basically relies on manual word positioning. First of all I split the whole text into single words; stored in the myWords
array.
Within the draw
-loop I use two variables – xPos
and yPos
– to represent an imaginary cursor that moves over the screen. An if clause checks if the current word would jump out of the sketch area (including the padding):
float xPosEnd = xPos + textWidth (myWords[i]);
If so, the cursor will jump to the beginning of the next line.
if (xPosEnd > width - PADDING_X) {
...
Right now the spacing relies on the mouse and the line height is fixed; but could also easily be dynamic. You can use the xPos
and yPos
variables to play around with the positions of the words. Furthermore does xPosEnd
indicate the word end-position. As I said, this approach is pretty fast forward and can be also applied on a character level.
Manual text positioning script
public static final int FONT_SIZE = 20;
public static final float LINE_HEIGHT = FONT_SIZE * 1.3f;
public static final float PADDING_X = 25;
public static final float PADDING_Y = 15;
PFont font;
String myText;
String[] myWords;
float spacing = 5;
void setup () {
size (480, 320);
smooth ();
font = createFont ("Arial", FONT_SIZE);
textFont (font);
myText = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, ";
myText += "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ";
myText += "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris ";
myText += "nisi ut aliquip ex ea commodo consequat.";
myWords = myText.split (" ");
}
void draw () {
background (0);
float xPos = PADDING_X;
float yPos = PADDING_Y + FONT_SIZE;
// For every word in the text
for (int i=0; i < myWords.length; i++) {
// Calculate the expected end position of
// the current word in this line
float xPosEnd = xPos + textWidth (myWords[i]);
// Check if word is not to long
// for current screen bounds. If...
if (xPosEnd > width - PADDING_X) {
// Set the cursor to the beginning
// of the next line
xPos = PADDING_X;
yPos += LINE_HEIGHT;
}
// Display word at xPos-yPos
text (myWords[i], xPos, yPos);
// Move the cursor to the right for the
// next word in list
xPos += textWidth (myWords[i]) + spacing;
}
}
void mouseMoved () {
spacing = map (mouseX, 0, width, 0, 40);
}
what function I can use to retrieve, get back the screen coordinates
May be you are looking for the screen
System variable
Description System variable which stores the dimensions of the computer screen. For example, if the current screen resolution is 1024x768, screen.width is 1024 and screen.height is 768. These dimensions are useful when exporting full-screen applications.
Sample program
println ("Width:" + screen.width);
println ("Height:" + screen.height);
For more reference always refer the Language API
Edit
But I am looking for ways to find coordinates of any object that is drawn on the sketch board. Like say if i have some text, i want to know the start and end coordinates of each word in the text
What method are you using to draw the text? What method will you be using for animating the text? It depends on the methods you use to find the coordinates.
If you used the text
method then you provided the coordinates as parameters
eg: text(data, x, y)
If you animated the text to move 5 coordinates to the right then if the initial x coordinate was 10 then the x coordinate now will be 15.
精彩评论