如何在Processing中获取屏幕坐标

发布于 2024-10-27 23:07:03 字数 181 浏览 6 评论 0原文

我正在使用处理来做一个项目。
我使用 draw 函数在素描板上画了一个草图(实际上是文本)。
我想获取文本每个单词的屏幕坐标来做一些其他有趣的事情。

我不知道我可以使用什么函数来检索、取回屏幕坐标。

任何人都可以帮忙解决这个问题吗?我会很感激你的帮助。

谢谢

I am using Processing to do a project.
I have a sketch (actually text) in the sketch board by using the draw 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 your help.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感情洁癖 2024-11-03 23:07:03

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

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

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

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

现在间距依赖于鼠标,行高是固定的;但也很容易是动态的。您可以使用 xPosyPos 变量来调整单词的位置。此外,xPosEnd 指示单词结束位置。正如我所说,这种方法速度相当快,也可以应用于角色级别。

手动文本定位脚本

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

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);
}
白云不回头 2024-11-03 23:07:03

我可以使用什么函数来检索,
获取屏幕坐标

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

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


示例程序

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

如需更多参考,请参阅语言 API


编辑

但我正在寻找方法
任何物体的坐标
画在素描板上。就像说如果
我有一些文字,我想知道
每个单词的起始和结束坐标
在正文中

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

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

如果您对文本进行动画处理以向右移动 5 个坐标,那么如果初始 x 坐标为 10,那么现在的 x 坐标将为 15。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文