You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a series of canvas translations and a rotation that when applied individually to the canvas results in the correct placement of the image, but when I create the SKMatrix with the same data the result is not the same. I believe it is to do with the way the rotation is created by the matrix, but could be wrong. Anyway so I set up some variables, for this example I'll use hard coded variables.
int locationX = 0;
int locationY = 256;
int centerX = 256;
int centerY = 128;
int drawX = -128;
int drawY = 0;
var radians = MapTester.ToRadians(90);
When I use canvas directly with these values, as in the following code example the result is a blue rectangle in the lower left corner:
// draw a blue rectangle in the lower left corner of the image
canvas.Translate(locationX, locationY);
canvas.Translate(centerX,centerY);
canvas.RotateRadians(radians);
canvas.Translate(drawX, drawY);
canvas.DrawPicture(RectangleTile.Picture);
canvas.Restore();
// Draw a black square in top left corner to show canvas was restored
paint.Color = SKColors.Black;
canvas.DrawRect(0, 0, 7, 7, paint);
canvas.Save();
When I try to create a Matrix with the same values and pass a reference to the DrawPicture method it doesn't work:
// Attempt adding a red rectangle on top of the above blue rectangle
SKMatrix matrix = SKMatrix.CreateIdentity();
matrix = matrix.PostConcat(SKMatrix.CreateTranslation(locationX,locationY));
matrix = matrix.PostConcat(SKMatrix.CreateTranslation(centerX, centerY));
matrix = matrix.PostConcat(SKMatrix.CreateRotation(radians));
matrix = matrix.PostConcat(SKMatrix.CreateTranslation(drawX, drawY));
canvas.DrawPicture(TestTile.Picture, ref matrix);
canvas.Save();
The resulting image only has the blue rectangle and not the one drawn using the matrix.
In case it makes a difference here is the method in it's entirety:
public void DrawMatrixFailExample()
{
SKImageInfo imageInfo = new SKImageInfo(512, 768);
SKSurface surface = SKSurface.Create(imageInfo);
SKCanvas canvas = surface.Canvas;
canvas.Clear(SKColors.Azure);
SKPaint paint = new SKPaint();
paint.Color = SKColors.Blue;
paint.Style = SKPaintStyle.Fill;
SquareTile.Draw(canvas);
canvas.Save();
int locationX = 0;
int locationY = 256;
int centerX = 256;
int centerY = 128;
int drawX = -128;
int drawY = 0;
var radians = MapTester.ToRadians(90);
// draw a blue rectangle in the lower left corner of the image
canvas.Translate(locationX, locationY);
canvas.Translate(centerX,centerY);
canvas.RotateRadians(radians);
canvas.Translate(drawX, drawY);
canvas.DrawPicture(RectangleTile.Picture);
canvas.Restore();
// Draw a black square in top left corner to show canvas was restored
paint.Color = SKColors.Black;
canvas.DrawRect(0, 0, 7, 7, paint);
canvas.Save();
// Attempt adding a red rectangle on top of the above blue rectangle
SKMatrix matrix = SKMatrix.CreateIdentity();
matrix = matrix.PostConcat(SKMatrix.CreateTranslation(locationX,locationY));
matrix = matrix.PostConcat(SKMatrix.CreateTranslation(centerX, centerY));
matrix = matrix.PostConcat(SKMatrix.CreateRotation(radians));
matrix = matrix.PostConcat(SKMatrix.CreateTranslation(drawX, drawY));
canvas.DrawPicture(TestTile.Picture, ref matrix);
canvas.Save();
using (var data = surface.Snapshot())
using (var pngImage = data.Encode(SKEncodedImageFormat.Png, 100))
{
string output_file = output_folder + Path.DirectorySeparatorChar + "matrix-rotation-fail-example.png";
using (var stream = File.OpenWrite(output_file))
{
pngImage.SaveTo(stream);
}
}
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
I have a series of canvas translations and a rotation that when applied individually to the canvas results in the correct placement of the image, but when I create the SKMatrix with the same data the result is not the same. I believe it is to do with the way the rotation is created by the matrix, but could be wrong. Anyway so I set up some variables, for this example I'll use hard coded variables.
When I use canvas directly with these values, as in the following code example the result is a blue rectangle in the lower left corner:
When I try to create a Matrix with the same values and pass a reference to the DrawPicture method it doesn't work:
The resulting image only has the blue rectangle and not the one drawn using the matrix.
In case it makes a difference here is the method in it's entirety:
Beta Was this translation helpful? Give feedback.
All reactions