Is it possible to change the position of a p5 text element after it is drawn? - p5.js

Can you change the position of a p5 text element after it is draw without redrawing the entire canvas? If so, how would I be able to?
I'm trying to make text appear under players set to their current position, but when they move I want it to change its position to the player's position. I have the events with that setup, I just don't know how to change the position of the element.

Unlike some libraries, such as fabric.js, graphical elements in p5.js are not persistent objects that can be moved or modified once they are drawn. Instead, everything in p5.js is drawn in immediate mode. So when you are drawing text you specify it's attributes at the time you draw it, using the Typography functions, and the text(). In order to "move" text you would clear the canvas, or re-paint the background (using clear() or background() respectively, and then re-drawing the text with different settings or parameters.

Related

UI Elements not affected by light

I'm trying to make a menu screen in which all the UI elements (buttons, text...) are completely dark and by touching the screen you create a fire (or just an area light) that makes the UI elements visible.
Sort of like this
I read that the default shader for the UI elements isn't affected by light, but i can't seem to change it.
How do I go about doing this?
The UI elements by default use a unlit shader and are also rendered directly to clip space. so you'll need to do two things, first put a lit shader onto the elements, the unity standard shader should do fine, then you should change the canvas render mode to world space. with the canvas in world space you can move it around like it was a sprite. i would also recommend creating a second higher priority camera for the UI with culling turned off. with the canvas in view of the UI camera, you should be able to place a light source near it and see the resulting lighting on the UI.

Disable rectangle movement behavior in Report Designer

In SSRS, it is a good practice to put your report elements inside rectangles.
This makes it possible to move several items at once by moving the rectangle around. When moving the rectangle around with a mouse, this works like a charm: the Tablix position inside the Rectangle remains the same, so the Rectangle moves with all contents in-place to another position:
However, when you manually (via the Top / Left properties in the properties pane) alter the position of these rectangles in Design Mode, the rectangle moves, but the contents remain on the same place. So whenever I do the same movement via the properties pane (which is more precise IMHO), the contents are not moved along with the rectangle, and oftentimes even become invisible:
Whenever you alter the position of the Rectangle directly inside the XML of the report (in 'Code Mode'), the contents are moved along correctly - but this is a rather time-consuming way.
Is there a way to make SSRS move the contents of rectangles along in Design Mode, even when the position of the rectangles is altered via the properties pane?

User interaction in Processing

I have a general question (I know I should present specific code with a problem, but in my case the problem is of a more general nature).
In Processing, let's say I make an ellipse:
ellipse(30, 30, 10, 10);
Now, is there a way to get the pixels where this ellipse is on the canvas? The reason would be to have a way of creating user interaction with the mouse (for instance). So when someone clicks the mouse over the ellipse, something happens.
I thought of turning everything into objects and use a constructor to somehow store the position of the shape, but this is easier said than done, particularly for more complex shapes. And that is what I am interested in. It's one thing to calculate the position of an ellipse, but what about more complex shapes? Are there any libraries?
Check out the geomerative library. It has a way to check whether the mouse is inside any SVG shape. I can't remember off the top of my head but it works something like you make a shape:
myShape = RG.loadShape("shape.svg");
and a point:
RPoint p = new RPoint(mouseX, mouseY);
and the boolean function contains() will tell you if the point is inside the shape:
myShape.contains(p);
It's better to use a mathematical formula than pixel-by-pixel checking of the mouse position (it's much faster, and involves less code).
For a perfect circle, you can calculate the Euclidean distance using Pythagoras' theorem. Assume your circle is centred at position (circleX,circleY), and has a radius (not diameter) of circleR. You can check if the mouse is over the circle like this:
if(sq(mouseX-circleX)+sq(mouseY-circleY) <= sq(circleR)) {
// mouse is over circle
} else {
// mouse is not over circle
}
This approach basically imagines a right-angled triangle, where the hypotenuse (the longest side) runs from the centre of the circle to the mouse position. It uses Pythagoras' theorem to calculate the length of that hypotenuse, and if it's less than the circle's radius then the mouse is inside the circle. (It includes a slight optimisation though -- it's comparing squares to avoid doing a square root, as that can be comparatively slow.)
An alternative to my original mathematical answer also occurred to me. If you can afford the memory and processing power of drawing all your UI elements twice then you can get good results by using a secondary buffer.
The principle involves having an off-screen graphics buffer (e.g. using PGraphics). It must be exactly the same size as the main display, and have anti-aliasing disabled. Draw all your interactive UI elements (buttons etc.) to this buffer. However, instead of drawing them the normal way, give each one a unique colour which it uses for fill and stroke (don't add any text or images... just solid colours). For example, one button might be entirely red, and another entirely green. Any other RGB value works, as long as each item has a unique colour. Make sure the background has a unique colour too.
The user never sees that buffer, so don't draw it to the screen (unless you're debugging or something). When you want to detect what item the mouse is over, just lookup the mouse position on that off-screen buffer. Get the pixel colour at that location, and match it to the UI element.
After you've done all that, just go ahead and draw everything to the main display as normal.
It's worth noting that you can cut-down the processing time of this approach a lot if your UI elements never (or rarely) move. You only need to redraw the secondary buffer when something appears/disappears, animates, or changes size/position.

How to control TeeChart's Series marks position?

I want to draw marks in custom positions, but what is the best event or procedure for it?
Because if I process them in OnAfterDraw event my chart will be rendered twice and etc.
Is it possible to set the position before drawing the chart?
You need the series to have been drawn at least once (so its marks' positions have been populated) to be able to modify these positions.

How to draw a selected text in win32 using only gdi call?

I tried to draw a selected text using two TextOut call and TA_UPDATECP flag but when I move the selected area, the characters are moving a bit.
Does someone know the correct way to do that ?
According to the MSDN documentation, when you have set the TA_UPDATECP flag using SetTextAlign(), TextOut() ignores its position parameters in favor of the device context's current position. Without seeing your code, I suspect that your drawing algorithm calls TextOut() with a current position that differs slightly based on the selection.
As a debugging strategy, you can call GetCurrentPositionEx() just before you call TextOut() to obtain the current position and make sure it matches your expectation for where the text should be drawn.
I believe that antialiasing and ClearType can draw text at fractional pixel positions. If you draw a string of text, and then try to redraw a portion of the middle, there might not be a way for you to draw text starting at exactly the same position as those characters in the middle. The trick seems to be to redraw the entire string, but with a clipping region for the selected text.
Chapter 4 in this tutorial on writing a Win32 text editor goes into quite some detail on how to draw selected text.

Resources