draw rectangle under stackview - layout

I find myself in the situation of having to manually draw a rectangle with rounded edges as a background for a horizontal stack view. To create the classic ios "bubble" effect, such as whatsapp chats, where the text is surrounded by a green shape.
I am attaching the code:
var BackgroundStack = new UIView();
BackgroundStack.Frame = new CGRect(0, -20, TopSchedaStack.Bounds.Width,
TopSchedaStack.Layer.Bounds.Height+40);
BackgroundStack.BackgroundColor = UIColor.TertiarySystemGroupedBackgroundColor;
BackgroundStack.Layer.CornerRadius = 20;
TopSchedaStack.InsertSubview(BackgroundStack, 0);
the perplexity lies in "BackgroundStack.Frame ...", I need to wrap the generated element in the "TopSchedaStack" storyboard of the UIHorizontalStackView type, with a gray rectangle. So I create it with the CGRect function but when I have to position it precisely under the object it is horizontally and vertically constrained to the view and centered. I am struggling, I cannot get the width I expect from the stack. In the storyboard file it is positioned one way but when I go to recover its width with:
TopSchedaStack.Bounds.Width or TopSchedaStack.Frame.Width
The value obtained is not what I expect. I expect the stack to be long from the left edge to the right edge of the view, taking away the guidelines. therefore it is an object constrained to the right and left respectively at point 0.
I would like the gray rectangle to cover the portion of the blue rectangle depicted in the image containing the storyaboard project. I'm pretty much going randomly changing values until it comes back, I'm sure I'll succeed sooner or later but I'm convinced it's not the right way to do this. Can you help me?

Related

ggez resize window without distording drawn elements

I use ggez and would like to resize the window. The only way I found is
graphics::set_drawable_size(&mut ctx, width, height);
However, when I use it, the white circle I drawn turns into a white ellipse, and a seemingly grey circle appears below. So is there another way of resizing window, one that doesn't distort what is drawn ?
use
graphics::set_screen_coordinates(ctx,rect)
right after declaring changing size with graphics::set_mode(ctx,windowmode) turns the ellipse back into a circle. The gray shape vanishes after reducing the window (this is a pretty obscure thing).

How to clip vega-lite text inside a rect?

In a webpage created with node/webpack, vega-lite, and vegaEmbed, I have a layer with rect marks with short annotations inside them using text marks. I'd like to clip the text to its surrounding rect but haven't figured out a way to do this and hope someone can point me in the right direction.
I realize text has a limit property in pixel units. If I could determine the pixel units of my rect marks (I don't know how to do this), using limit seems like a reasonable approach.
Also, if I knew the pixel extents of my rectangle, I can then write code to align the text within the rect which would be desirable. Currently I just use the same x as the rect, with a dx offset.
I've read about background for text which is a similar problem, but not the same.

How to get edges of a polygon using pixijs?

I'm using pixi.js to create some editable polygons. So, what I want to achieve is this:
I have one polygon
Then, when I hit the edge a small circle should appear
And next I can drag and drop that part of the edge to creating a new point for the polygon
For now, what I know is the polygon vertices and I'm thinking to use the line function (y=mx+b) to check if the point where the mouse is belongs to the edge. My problem here, is that I have no idea how to obtain that edges. Any Suggestion? Of course, if you have any other idea to do this feel free to share =).
For now, what I know is the polygon vertices
You probably draw your polygon using https://pixijs.download/dev/docs/PIXI.Graphics.html#drawPolygon method by passing to it a list of points - similar as last shape in this example: https://pixijs.io/examples/#/graphics/simple.js
// draw polygon
const path = [600, 370, 700, 460, 780, 420, 730, 570, 590, 520];
graphics.lineStyle(0);
graphics.beginFill(0x3500FA, 1);
graphics.drawPolygon(path);
graphics.endFill();
^ In that example we have 5 points: P (600, 370), Q (700, 460), R (780, 420), S (730, 570), T (590, 520).
It also means that we have 5 edges: PQ, QR, RS, ST, TP
Now, we should have some way to tell if mouse pointer "is hovered over some some edge". By "is hovered" i mean: it lies in some distance from edge - lets say said distance is 10 pixels. So we want to know if mouse pointer is 10 pixels away from some edge.
To know that we can use formula explained in Line defined by two points part in: https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line#Line_defined_by_two_points
P1=(x1,y1) and P2=(x2,y2) - are the beginning and end vertices of some edge (for example PQ)
(x0,y0) is our "mouse point"
You can iterate over all edges and perform above calculation - if the distance is less that 10 pixel for some edge then you have the answer. If there is more than one edge which meets this requirement then you should pick one with smallest distance (it can happen if for example mouse is placed near some vertice).
Now you have the selected edge. Now lets do following point from your question:
2. Then, when I hit the edge a small circle should appear
To calculate position of this circle we can use equation from same Wikipedia page: https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line#Line_defined_by_an_equation - the part The point on this line which is closest to (x0,y0) has coordinates:.
Here you need to convert coordinates of vertices from your selected edge to line function.
Then we can proceed to last point from your question:
3. And next I can drag and drop that part of the edge to creating a new point for the polygon
You can do it by adding new vertice to your polygon.
Lets assume that selected edge is PQ - then this new vertice should be added between vertices P and Q in the vertices list which you pass to drawPolygon method. Lets name this new vertice X. Coordinates of vertice X should be equal to current mouse coordinates.
Then you will have following edges: PX, XQ, QR, RS, ST, TP.
You probably want to activate this "mode" after mouse is clicked and when mouse button is down etc - but that is separate issue related to interactivity / GUI etc - not graphics :) .
Note: is good to separate your presentation part of application (graphics / pixi.js related things) from mechanics and interactivity / GUI etc. So for example: do your calculations in separate place (other class, method etc) from where you do your actual drawing (calling pixi.js methods, update canvas etc). Store results of calculations in some place (from above example it would be: list of vertices, position of circle, colors etc), and then when time comes to draw you take those results and just draw polygons using them. Dont mix everything in one place ;)

CGPoint x on SKSpriteNode not working correctly(Half of Node out of screen)

I'm actually developing a game in Swift SpriteKit.
I set the position of a SKSpriteNode:
skPart1.position = CGPointMake(0, 100)
So the node should start at the very left window-edge.
|-------------------------|
|-------------------------|
|-------------------------|
|-------------------------|
|==========|--------------|
|==========|--------------|
|==========|--------------|
But in reality half of my SKSpriteNode is outside of the screen:
|-------------------------|
|-------------------------|
|-------------------------|
|-------------------------|
==========|--------------------|
==========|--------------------|
==========|--------------------|
I've read the same problem on stackoverflow, but the only solution provided there was, to set the scaleMode to AspectFit.
I've figured out, that it works with SKShapeNode. But why not in SKSpriteNode?
And I've alreay tried that.
How can I fix that?
SKSpriteNode has an anchorPoint property that defaults to the center of the image, which is why half of the sprite is off the left side of the screen.
You can adjust the anchorPoint so that it behaves like your SKShapeNode with the anchor in the bottom left. Try this :
skPart1.anchorPoint = CGPointMake(0,0);
I know you have accepted an answer, however this is the ideal way to handle the situation and it's why the property exists.
I think this actually depends on what you're using as your SKShapeNode. If you're using a rectangle, then the point you give it will be the lower left corner of the rectangle. But if you use a SKShapeNode circle, it'll drop the circle centered on that point you give it, and you'll see very similar behavior to the SKSpriteNode.
The SKSpriteNode is using the center of the image as the point it places your sprite at, and so when you're placing your node at (0, 100), exactly half of it is being draw to the left of the screen.
If you want your sprite to be drawn as far left as possible, but completely on the screen, you should be able to accomplish this by offsetting for one half of the sprite's width.
skPart1.position = CGPoint(x: skPart1.size.width / 2, y: 100)

How to cut a hole in an SVG rectangle [duplicate]

This question already has answers here:
How can I cut one shape inside another?
(4 answers)
Closed 6 years ago.
So basically as my title says, I want to "cut a hole" in a rect element.
I have two rect elements, one on top of the other. The one on the bottom has a fill colour of white, and the one on the top has a fill colour of grey.
What I want to do is cut a triangle out of the top rect element so that the rect element below shows through.
This svg element is going to be used as an audio button for a media player on a page. In other words, you'll be able to click (or drag) your mouse left/right and the change in audio level will be represented by a change in the width of the rect element on the bottom, which shows through the triangle cut out of the top rect element.
I hope that's not too confusing. :P
Here is a quick mockup of what it should look like: http://forboden.com/coding/s1.png
Here is my code: http://forboden.com/coding/svgClipTest.html
Where am I going wrong here?
You should be able to use the fill-rule: evenodd(default) property to achieve this effect, if you want to prevent the fill of the rectangle from painting where the circle is. See this example from the SVG specification:
The key point is draw outer shape and inner shapes(holes) in different direction (clockwise vs anti-clockwise).
Draw the outer shape clockwise and draw the inner(holes) shapes anti-clockwise.
Or conversely, draw the outer shape(holes) anti-clockwise and draw the inner shapes clockwise.
Concat the path datas of outer shape and inner shapes(holes).
You can cut more holes by concat more hole path data.
This image explain how to cut a hole:
I see that you have it solved already, just wanted to add that if you want something more advanced then it's often quite easy to use a <mask>, see http://dev.w3.org/SVG/profiles/1.1F2/test/svg/masking-path-11-b.svg for example.
However, if you can avoid masking and clipping (e.g by just drawing things on top) that usually leads to better performance/user-experience.
Easiest way is to use <path> with the hole, and set pointer-events to none so events can pass through to the <rect> under. Of course there are many other ways to handle events such as wrapping them with a <g> and handling events on it.
You don't need to limit yourself to the basic shapes and use complicated clipping. Make things felxible enough so you can copy&paste path data generated by tools like inkscape.

Resources