如何在Processing中获取屏幕坐标

时间:2011-03-29 16:53:20

标签: text coordinates positioning processing

我正在使用Processing来做项目 我使用draw函数在草图板上有一个草图(实际上是文本) 我想得到文本每个单词的屏幕坐标,以做一些其他有趣的事情。

我不知道我可以用什么功能来检索,取回屏幕坐标。

任何人都可以提供帮助。我很感激你的帮助。

由于

2 个答案:

答案 0 :(得分:1)

希望我能正确解决您的问题,下面的草图解释了如何实现解决方案。它非常快速,基本上依赖于手动字定位。首先,我将整个文本分成单个单词;存储在myWords数组中 在draw - 循环中,我使用两个变量 - xPosyPos - 来表示在屏幕上移动的虚拟光标。 if子句检查当前单词是否会跳出草图区域(包括填充):

        float xPosEnd = xPos + textWidth (myWords[i]);

如果是这样,光标将跳转到下一行的开头。

        if (xPosEnd > width - PADDING_X) {
            ...

现在间距依赖于鼠标,线高度是固定的;但也很容易动态。您可以使用xPosyPos变量来处理单词的位置。此外,xPosEnd表示单词end-position。正如我所说,这种方法非常快速,也可以应用于角色级别。

手动文本定位脚本

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);
}

答案 1 :(得分:0)

  

我可以用来检索的功能,   取回屏幕坐标

您可能正在寻找screen系统变量

  

描述   系统变量哪个   存储计算机的尺寸   屏幕。例如,如果是当前的   屏幕分辨率为1024x768,    screen.width 为1024, screen.height   是768.这些尺寸很有用   导出全屏时   应用


示例程序

println ("Width:" + screen.width);
println ("Height:" + screen.height);

如需更多参考,请参阅Language API


修改

  

但我正在寻找找到的方法   任何对象的坐标   画在素描板上。喜欢说如果   我有一些文字,我想知道   每个单词的起点和终点坐标   在文中

您使用什么方法绘制文字?您将使用什么方法为文本制作动画?这取决于您用于查找坐标的方法。

如果您使用text方法,则提供坐标作为参数
例如:text(data, x, y)

如果您为文本设置动画以向右移动5个坐标,那么如果初始x坐标为10,则x坐标现在为15。

相关问题