Last active
June 21, 2024 11:50
-
-
Save viniciusfbb/6d04b02188ca938dc9b6ad65c611b9b3 to your computer and use it in GitHub Desktop.
Draw semi-transparent rectangle + circle clipping one minor circle on the center, so that you create a hole in the drawing, and can see what it behind the control
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. Enable Skia on your project (right click on project > Enable Skia); | |
// 2. Add one TImage as the background; | |
// 3. Add one TSkPaintBox in front of the TImage and add the follow code on the TSkPaintBox.OnDraw event: | |
uses | |
System.Math; | |
procedure TForm1.SkPaintBox1Draw(ASender: TObject; const ACanvas: ISkCanvas; | |
const ADest: TRectF; const AOpacity: Single); | |
var | |
LPaint: ISkPaint; | |
begin | |
// Save point of the canvas, to be able to rollback the clips to avoid affect other controls or form draw | |
ACanvas.Save; | |
try | |
// The clip operation is a operation to add or remove an area from the subsequent draws. | |
// Avoid draw outside the control area, that is, will draw only inside the ADest rect | |
ACanvas.ClipRect(ADest); | |
// Avoid draw outside the control area, that is, will draw only inside the ADest rect | |
var LRadius := (Min(ADest.Width, ADest.Height) / 2) * 0.9; // I'm multiplying to 0.9 to remaing 0.1 for a "border of circle" that we will draw in white | |
var LRoundRect := TRectF.Create(ADest.CenterPoint - PointF(LRadius, LRadius), ADest.CenterPoint + PointF(LRadius, LRadius)); | |
ACanvas.ClipRoundRect(TSkRoundRect.Create(LRoundRect, LRadius, LRadius), TSkClipOp.Difference, True); // TSkClipOp.Difference mean remove the area from draws | |
// Draw semi-transparent rect | |
LPaint := TSkPaint.Create; | |
LPaint.Color := TAlphaColors.Black; | |
LPaint.AlphaF := 0.7; | |
ACanvas.DrawPaint(LPaint); | |
// Draw circle | |
LPaint := TSkPaint.Create; | |
LPaint.AntiAlias := True; | |
LPaint.Color := TAlphaColors.White; | |
ACanvas.DrawCircle(ADest.CenterPoint, Min(ADest.Width, ADest.Height) / 2, LPaint); | |
finally | |
// Rollback the clips that we made after the save point | |
ACanvas.Restore; | |
end; | |
end; |
Author
viniciusfbb
commented
May 31, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment