How to set a outer geometry mask in d2d so d2d only draws outside that geometry - directx

I am working on API which requires me set up a outer geometry mask on ID2D1Rendertarget such that any draw call after that only draws portion of drawings which lies outside this geometry.
https://msdn.microsoft.com/en-us/library/windows/desktop/dd756675(v=vs.85).aspx explains how can we setup a inner geometry mask on ID2D1Rendertarget such that any draw call after that only draws portion of drawings which lies inside this geometry.I want to implement just opposite of that. Is this possible? Any help is deeply appreciated.

One way to do this is to subtract your geometry from a rectangle that fills the entire render target. Check out the MSDN page on combining geometries. I have a small code example below:
ComPtr<ID2D1PathGeometry> invertedGeometry;
ComPtr<ID2D1RectangleGeometry> rectangleGeometry;
d2dFactory->CreateRectangleGeometry(
{ 0, 0, targetWidth, targetHeight },
&rectangleGeometry
);
ComPtr<ID2D1GeometrySink> geometrySink;
d2dFactory->CreatePathGeometry(&invertedGeometry);
invertedGeometry->Open(&geometrySink);
rectangleGeometry->CombineWithGeometry(
pathGeometry.Get(),
D2D1_COMBINE_MODE_EXCLUDE,
D2D1::Matrix3x2F::Identity(),
geometrySink.Get()
);
geometrySink->Close();
Use the inverted geometry as the geometric mask instead of the original path geometry.
A second way to do this is to rasterize your geometry to a bitmap and use it as an opacity mask. You can flip the colors depending on whether or not you want the inside or outside to mask.

Related

Drawing rounded rectangle with transparency in Skia

I'm trying to draw a rounded rectangle with transparency with the library Skia.
I get a perfect result if i'm using overlapping without transparency.
But when i'm using transparency, i get this:
That's what i get when i do not overlap:
Even if i modify the radius of the inner rectancle, there're still some overlaps/gabs.
My questitons:
What is the correct way to caluclate the radius of the inner rectangle, when i can accept some missplaced pixels?
Is there another way?
You can use saveLayerAlpha before drawing and then restore after drawing. This allows you to draw multiple paths, shapes, etc with a "global" alpha applied to the layer.

Update part of the screen with Direct2D

I have a viewport with some shapes that I draw by using Direct2D. At the moment when I change somehting, for example I set a Rectangle fill from red to green, I first clear the render target and then I draw again all the shapes with the new properties.
Since I know the position and the area of the rectangle I modified, is there a way to clear and re-draw only the area that has been updated insted of re-draw all the thousand shapes I have?
The documentation for:
IDXGISwapChain1::Present1(
UINT SyncInterval,
UINT PresentFlags,
[in] const DXGI_PRESENT_PARAMETERS *pPresentParameters);
states that
An app can use Present1 to optimize presentation by specifying scroll and dirty rectangles.
This information about the modified rectangles is supplied via the *pPresentParameters parameter. For details see:
DXGI_PRESENT_PARAMETERS structure

Delphi draw a closed rectangle with two rounded corners and to rectangular corners

How can I draw a rectangle that has two round corners and the opposite corners are rectangular corners. The shape must be closed so it can be filled with the Brush color. The Polyline method doesn't draw curved lines. Can I add the points of an Arc to the polyline points? I tried to draw a RoundRect using Canvas method and then, overlapping a rectangle over the lower round corners, but I couldn't figure out how to erase the upper line of the rectangle when drawign just the border of the shape without filling it. Note: if you think is relevant, I can add the code I used.
Sample of the desired shape:
Sample of what I got with Delphi:
You don't have to fill the shape at the same time you draw it. You can use a series of TCanvas.LineTo() and TCanvas.ArcTo()/TCanvas.AngleArc() calls first to create the shape, then call TCanvas.FloodFill() afterwards to fill it.
Otherwise, you can overlap TCanvas.Rectangle() on top of TCanvas.RoundRect() with the same fill color, and then use TCanvas.MoveTo()/TCanvas.LineTo() to draw over the dividing line with the same fill color.
Another option would be to forget using TCanvas drawing methods and just use Win32 API calls instead. Use CreateRoundRectRgn(), CreateRectRgn(), and CombineRn() to create a HRGN that has your desired shape, then use FillRgn() and FrameRgn() to draw on your TCanvas using that HRGN.

iOS: How to fill a rectangle except in one area?

I've used the CGContext based routine to fill a rectangle, but this time I actually want to fill all of a rectangle EXCEPT one part of it. I know this is possible in other drawing systems for other platofrm but can it be done with core graphics on iOS?
Both borderWidth and cornerRadius are animatable properties. So you could just animate them directly (using CABasicAnimation). In that rendering, you'd go from no border and no corners to having a border and corners in an animated way.
If you want to animate the rectangular tracing of the border, you'd need to use a CAShapeLayer (because the end of its path is animatable) and provide the illusion that way.

Problem with actionscript 3 erasing drawing

I have a based image and some sprites on top of the basd image movieclip... Some of the sprites can be drawn by the user using graphics api in actionscript 3. I can draw things on the sprites but I can't create an eraser like brush that can remove part of the unwanted drawings. I try using Alpha but no it doesn't work
I have googled about it and come up with the solution:
1) Linebitmapstyle... This solution is not the best one coz I my sprites can be moved so if I use linebitmapstyle, it does draw the pixel from the image to the sprite but if the sprite moved the drawn pixel won't change.
2) Masking may not work for me either....
What is the best way of creating the eraser
You may rather want to use a Bitmap to make such things easier to manipulate (unless you need to do scalable vector graphics of course!). To draw shapes you can still use the graphics API to create the shapes.
To do so, instantiate a "dummy" sprite (or another IBitmapDrawable implementation) to create the graphics and then "copy" them to the BitmapData the bitmapData.draw() function. This way you can for instance draw with the option BlendMode.ERASE in order remove the pixels of the shape.
Example (from the top of my mind) :
// creates a bitmap data canvas
var bitmapData:BitmapData = new BitmapData(500, 500);
// creates a bitmap display object to contain the BitmapData
addChild(new Bitmap(bitmapData));
// creates a dummy object to draw and draws a 10px circle
var brush:Sprite = new Sprite(); // note this is not even added to the stage
brush.graphics.beginFill(0xff0000);
brush.graphics.drawCircle(10, 10, 10);
// the matrix will be used to position the "brush strokes" on the canvas
var matrix:Matrix = new Matrix();
// draws a circle in the middle of the canvas
matrix.translate(250, 250);
bitmapData.draw(brush, matrix
// translates the position 5 pixels to the right to slightly erase the previously
// drawn circle creating a half moon
matrix.translate(5, 0);
bitmapData.draw(brush, matrix,null,BlendMode.ERASE);

Resources