Exemple #1
0
        private void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args)
        {
            SKImageInfo info    = args.Info;
            SKSurface   surface = args.Surface;
            SKCanvas    canvas  = surface.Canvas;

            canvas.Clear();

            float xCenter = info.Width / 2;
            float yCenter = info.Height / 2;

            SKMatrix   matrix   = SKMatrix.MakeTranslation(-xCenter, -yCenter);
            SKMatrix44 matrix44 = SKMatrix44.CreateIdentity();

            matrix44.PostConcat(SKMatrix44.CreateRotationDegrees(1, 0, 0, _Deg));

            SKMatrix44 perspectiveMatrix = SKMatrix44.CreateIdentity();

            perspectiveMatrix[3, 2] = -1 / 5000f;
            matrix44.PostConcat(perspectiveMatrix);

            SKMatrix.PostConcat(ref matrix, matrix44.Matrix);
            SKMatrix.PostConcat(ref matrix, SKMatrix.MakeTranslation(xCenter, yCenter));
            canvas.SetMatrix(matrix);
            float xBitmap = xCenter - CurrentBitmap.Width / 2;
            float yBitmap = yCenter - CurrentBitmap.Height / 2;

            canvas.DrawBitmap(CurrentBitmap, xBitmap, yBitmap);
        }
Exemple #2
0
        public void TransformConvertsToMatrix()
        {
            var matrix44 = SKMatrix44.CreateRotationDegrees(0, 0, 1, 45);
            var matrix   = SKMatrix.CreateRotationDegrees(45);

            Assert.Equal(matrix.Values, matrix44.Matrix.Values);
        }
Exemple #3
0
        protected override async Task OnInit()
        {
            // create the base and step 3D rotation matrices (around the y-axis)
            rotationMatrix = SKMatrix44.CreateRotationDegrees(0, 1, 0, 30);
            rotationStep   = SKMatrix44.CreateRotationDegrees(0, 1, 0, 5);

            await base.OnInit();
        }
Exemple #4
0
        public ThreeDRotation()
        {
            Log.Debug("Demo", "Enter");
            InitializeComponent();

            // create the base and step 3D rotation matrices (around the y-axis)
            rotationMatrix = SKMatrix44.CreateRotationDegrees(0, 1, 0, 30);
            rotationStep   = SKMatrix44.CreateRotationDegrees(0, 1, 0, 5);
        }
Exemple #5
0
        public void RotationMapsScalars()
        {
            var matrixRotate = SKMatrix44.CreateRotationDegrees(0, 1, 0, 90);

            var resultRotateZero  = matrixRotate.MapScalars(0, 0, 0, 1);
            var resultRotateValue = matrixRotate.MapScalars(5, 25, 0, 1);

            Assert.Equal(new[] { 0f, 0f, 0f, 1f }, resultRotateZero);
            AssertSimilar(new[] { 0f, 25f, -5f, 1f }, resultRotateValue, PRECISION);
        }
Exemple #6
0
        public void RotationMapsPoints()
        {
            var matrixRotate = SKMatrix44.CreateRotationDegrees(0, 1, 0, 90);

            var resultRotateZero  = matrixRotate.MapPoint(SKPoint.Empty);
            var resultRotateValue = matrixRotate.MapPoint(new SKPoint(5, 25));

            Assert.Equal(new SKPoint(0f, 0f), resultRotateZero);
            Assert.Equal(0, resultRotateValue.X, PRECISION);
            Assert.Equal(25, resultRotateValue.Y, PRECISION);
        }
        public virtual void Update(SKCanvas canvas, long absoluteElapsedMillis)
        {
            var elapsedMillis = absoluteElapsedMillis - _absoluteElapsedMillisPrevious;

            if (_absoluteElapsedMillisPrevious == 0)
            {
                _absoluteElapsedMillisPrevious = absoluteElapsedMillis;
                return;
            }

            _internalAbsoluteMillis       += elapsedMillis;
            _absoluteElapsedMillisPrevious = absoluteElapsedMillis;

            canvas.Save();

            // Traversed distance = speed x time
            var dist = TranslationSpeed * _internalAbsoluteMillis * 0.001;

            // New position
            var deg2radFactor = 0.0174533;
            var angle         = Direction * deg2radFactor;

            Position = InitialPosition + new SKPoint
            {
                X = (float)(dist * Math.Cos(angle)),
                Y = (float)(dist * Math.Sin(angle))
            };

            var matrix = SKMatrix.CreateTranslation(-Position.X, -Position.Y);

            // New Orientation
            Orientation = InitialOrientation + new SKPoint3
            {
                X = _internalAbsoluteMillis * 0.001f * RotationSpeed.X,
                Y = _internalAbsoluteMillis * 0.001f * RotationSpeed.Y,
                Z = _internalAbsoluteMillis * 0.001f * RotationSpeed.Z
            };

            var matrix44 = SKMatrix44.CreateIdentity();

            matrix44.PostConcat(SKMatrix44.CreateRotationDegrees(1, 0, 0, Orientation.X));
            matrix44.PostConcat(SKMatrix44.CreateRotationDegrees(0, 1, 0, Orientation.Y));
            matrix44.PostConcat(SKMatrix44.CreateRotationDegrees(0, 0, 1, Orientation.Z));

            // Apply transforms
            matrix = matrix.PostConcat(matrix44.Matrix);
            matrix = matrix.PostConcat(SKMatrix.CreateTranslation(Position.X, Position.Y));
            canvas.SetMatrix(matrix);

            Draw(canvas);

            canvas.Restore();
        }
        void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args)
        {
            SKImageInfo info    = args.Info;
            SKSurface   surface = args.Surface;
            SKCanvas    canvas  = surface.Canvas;

            canvas.Clear();

            // Find center of canvas
            float xCenter = info.Width / 2;
            float yCenter = info.Height / 2;

            // Translate center to origin
            SKMatrix matrix = SKMatrix.MakeTranslation(-xCenter, -yCenter);

            // Scale so text fits
            float scale = Math.Min(info.Width / textBounds.Width,
                                   info.Height / textBounds.Height);

            SKMatrix.PostConcat(ref matrix, SKMatrix.MakeScale(scale, scale));

            // Calculate composite 3D transforms
            float depth = 0.75f * scale * textBounds.Width;

            SKMatrix44 matrix44 = SKMatrix44.CreateIdentity();

            matrix44.PostConcat(SKMatrix44.CreateRotationDegrees(1, 0, 0, xRotationDegrees));
            matrix44.PostConcat(SKMatrix44.CreateRotationDegrees(0, 1, 0, yRotationDegrees));
            matrix44.PostConcat(SKMatrix44.CreateRotationDegrees(0, 0, 1, zRotationDegrees));

            SKMatrix44 perspectiveMatrix = SKMatrix44.CreateIdentity();

            perspectiveMatrix[3, 2] = -1 / depth;
            matrix44.PostConcat(perspectiveMatrix);

            // Concatenate with 2D matrix
            SKMatrix.PostConcat(ref matrix, matrix44.Matrix);

            // Translate back to center
            SKMatrix.PostConcat(ref matrix,
                                SKMatrix.MakeTranslation(xCenter, yCenter));

            // Set the matrix and display the text
            canvas.SetMatrix(matrix);
            float xText = xCenter - textBounds.MidX;
            float yText = yCenter - textBounds.MidY;

            canvas.DrawText(text, xText, yText, textPaint);
        }
Exemple #9
0
        static SKMatrix44 GetMatrix(VoxelData voxelData, int size)
        {
            var r      = 1.61803398875f;
            var d      = size / (r * voxelData.size.MaxDimension);
            var tran   = SKMatrix44.CreateTranslate(size * 0.5f, size * 0.5f, 0);
            var rotx   = SKMatrix44.CreateRotationDegrees(1, 0, 0, -26f);
            var roty   = SKMatrix44.CreateRotationDegrees(0, 1, 0, 45);
            var matrix = SKMatrix44.CreateIdentity();

            matrix.PreConcat(tran);
            matrix.PreConcat(rotx);
            matrix.PreConcat(roty);
            matrix.PreScale(d, -d, d);
            matrix.PreTranslate(-voxelData.size.X * 0.5f, -voxelData.size.Z * 0.5f, voxelData.size.Y * 0.5f);
            return(matrix);
        }
Exemple #10
0
        public void Matrix44MapsScalars()
        {
            // translate
            var matrixTranslate = SKMatrix44.CreateTranslate(10, 20, 0);

            var resultTranslateZero  = matrixTranslate.MapScalars(0, 0, 0, 1);
            var resultTranslateValue = matrixTranslate.MapScalars(5, 25, 0, 1);

            Assert.Equal(new[] { 10f, 20f, 0f, 1f }, resultTranslateZero);
            Assert.Equal(new[] { 15f, 45f, 0f, 1f }, resultTranslateValue);

            // rotate
            var matrixRotate = SKMatrix44.CreateRotationDegrees(0, 1, 0, 90);

            var resultRotateZero  = matrixRotate.MapScalars(0, 0, 0, 1);
            var resultRotateValue = matrixRotate.MapScalars(5, 25, 0, 1);

            Assert.Equal(new[] { 0f, 0f, 0f, 1f }, resultRotateZero);
            Assert.Equal(new[] { 0f, 25f, -5f, 1f }, resultRotateValue.Select(v => (int)(v / EPSILON) * EPSILON));
        }
Exemple #11
0
        void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args)
        {
            SKImageInfo info    = args.Info;
            SKSurface   surface = args.Surface;
            SKCanvas    canvas  = surface.Canvas;

            canvas.Clear();

            // Find center of canvas
            float xCenter = info.Width / 2;
            float yCenter = info.Height / 2;

            // Translate center to origin
            SKMatrix matrix = SKMatrix.MakeTranslation(-xCenter, -yCenter);

            // Use 3D matrix for 3D rotations and perspective
            SKMatrix44 matrix44 = SKMatrix44.CreateIdentity();

            matrix44.PostConcat(SKMatrix44.CreateRotationDegrees(1, 0, 0, (float)xRotateSlider.Value));
            matrix44.PostConcat(SKMatrix44.CreateRotationDegrees(0, 1, 0, (float)yRotateSlider.Value));
            matrix44.PostConcat(SKMatrix44.CreateRotationDegrees(0, 0, 1, (float)zRotateSlider.Value));

            SKMatrix44 perspectiveMatrix = SKMatrix44.CreateIdentity();

            perspectiveMatrix[3, 2] = -1 / (float)depthSlider.Value;
            matrix44.PostConcat(perspectiveMatrix);

            // Concatenate with 2D matrix
            SKMatrix.PostConcat(ref matrix, matrix44.Matrix);

            // Translate back to center
            SKMatrix.PostConcat(ref matrix,
                                SKMatrix.MakeTranslation(xCenter, yCenter));

            // Set the matrix and display the bitmap
            canvas.SetMatrix(matrix);
            float xBitmap = xCenter - bitmap.Width / 2;
            float yBitmap = yCenter - bitmap.Height / 2;

            canvas.DrawBitmap(bitmap, xBitmap, yBitmap);
        }
Exemple #12
0
        public void Matrix44ConvertsToMatrix()
        {
            var rowMajor44 = new float[] {
                2, 3, 4, 5,
                4, 6, 8, 10,
                6, 9, 12, 15,
                8, 12, 16, 20,
            };
            var rowMajor = new float[] {
                rowMajor44[0], rowMajor44[1], rowMajor44[3],
                rowMajor44[4], rowMajor44[5], rowMajor44[7],
                rowMajor44[12], rowMajor44[13], rowMajor44[15],
            };

            var matrix44 = SKMatrix44.FromRowMajor(rowMajor44);

            Assert.Equal(rowMajor, matrix44.Matrix.Values);

            matrix44 = SKMatrix44.CreateRotationDegrees(0, 0, 1, 45);
            Assert.Equal(SKMatrix.MakeRotationDegrees(45).Values, matrix44.Matrix.Values);
        }
Exemple #13
0
        public void Matrix44MapsPoints()
        {
            // translate
            var matrixTranslate = SKMatrix44.CreateTranslate(10, 20, 0);

            var resultTranslateZero  = matrixTranslate.MapPoint(SKPoint.Empty);
            var resultTranslateValue = matrixTranslate.MapPoint(new SKPoint(5, 25));

            Assert.Equal(new SKPoint(10f, 20f), resultTranslateZero);
            Assert.Equal(new SKPoint(15f, 45f), resultTranslateValue);

            // rotate
            var matrixRotate = SKMatrix44.CreateRotationDegrees(0, 1, 0, 90);

            var resultRotateZero  = matrixRotate.MapPoint(SKPoint.Empty);
            var resultRotateValue = matrixRotate.MapPoint(new SKPoint(5, 25));

            Assert.Equal(new SKPoint(0f, 0f), resultRotateZero);
            Assert.Equal(0, resultRotateValue.X, PRECISION);
            Assert.Equal(25, resultRotateValue.Y, PRECISION);
        }
        void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args)
        {
            SKImageInfo info    = args.Info;
            SKSurface   surface = args.Surface;
            SKCanvas    canvas  = surface.Canvas;

            canvas.Clear();

            string strName   = Apartment.Name;
            string strRating = Apartment.RatesAverage?.ToString();

            // Create an SKPaint object to display the text
            SKPaint textPaint = new SKPaint
            {
                Typeface = SKTypeface.FromFamilyName("Brandon_reg"),
                Color    = SKColors.Black
            };

            // Adjust TextSize property so text is 90% of screen width
            textPaint.TextSize = 0.05f * info.Width;

            // Find the text bounds
            SKRect textBounds = new SKRect();

            textPaint.MeasureText(strName, ref textBounds);

            // Calculate offsets to center the text on the screen
            float xTextName = 0.1f * info.Width;
            float yTextName = info.Height / 2 - textBounds.MidY;

            float yTextRating = yTextName + 1.2f * textBounds.Height;


            // And draw the text


            float xCenter = info.Width / 2;
            float yCenter = info.Height / 2;

            // Translate center to origin
            SKMatrix matrix = SKMatrix.MakeTranslation(-xCenter, -yCenter);

            // Use 3D matrix for 3D rotations and perspective
            SKMatrix44 matrix44 = SKMatrix44.CreateIdentity();

            matrix44.PostConcat(SKMatrix44.CreateRotationDegrees(1, 0, 0, angle));


            SKMatrix44 perspectiveMatrix = SKMatrix44.CreateIdentity();

            perspectiveMatrix[3, 2] = -1 / (float)2000;
            matrix44.PostConcat(perspectiveMatrix);

            // Concatenate with 2D matrix
            SKMatrix.PostConcat(ref matrix, matrix44.Matrix);

            // Translate back to center
            SKMatrix.PostConcat(ref matrix,
                                SKMatrix.MakeTranslation(xCenter, yCenter));

            // Set the matrix and display the bitmap
            canvas.SetMatrix(matrix);

            // Create a new SKRect object for the frame around the text
            SKRect frameRect = SKRect.Create(0.05f * info.Width, 0, 0.9f * info.Width, info.Height);

            // Create an SKPaint object to display the frame
            SKPaint framePaint = new SKPaint
            {
                Style       = SKPaintStyle.Stroke,
                StrokeWidth = 5,
                Color       = SKColors.Blue
            };

            SKPaint rectanglePaint = new SKPaint
            {
                Color = SKColors.Beige
            };

            // Draw one frame
            canvas.DrawRoundRect(frameRect, 20, 20, rectanglePaint);

            canvas.DrawText(strName, xTextName, yTextName, textPaint);
            canvas.DrawText($"{strRating}", xTextName, yTextRating, textPaint);


            if (bitmap != null)
            {
                var pictureFrame = SKRect.Create(info.Width * (float)0.7, 0, info.Width * (float)0.25, info.Height);

                float xBitmap = (float)0.8 * info.Width;
                float yBitmap = 0;

                canvas.DrawBitmap(bitmap, pictureFrame);
            }
        }
        private void FolderCanvas_PaintSurface(object sender, SkiaSharp.Views.Forms.SKPaintSurfaceEventArgs e)
        {
            SKImageInfo info    = e.Info;
            SKSurface   surface = e.Surface;
            SKCanvas    canvas  = surface.Canvas;

            canvas.Clear();


            float scaleFactor = 0.67f;
            float density     = scaleFactor * info.Size.Height / (float)this.Height;


            canvas.Save();

            using (SKPath backPath = SKPath.ParseSvgPathData(
                       "M87.909,9.917V4.02A4.032,4.032,0,0,0,83.889,0H23.72A4.032,4.032,0,0,0,19.7,4.02v5.9H13.338a4.529,4.529,0,0,0-3.822,4.02l.27,112.7a3.954,3.954,0,0,0,3.951,4.024H167.344a3.963,3.963,0,0,0,3.97-4.011l-.6-112.71a4.092,4.092,0,0,0-4.07-4.02Z"))
            {
                backPath.Transform(SKMatrix.MakeScale(density, density));
                backPath.GetTightBounds(out var backPathTightBounds);
                var translateXBackPath = info.Width * 0.5f - backPathTightBounds.MidX;
                var translateYbackPath = info.Height - backPathTightBounds.Bottom - 20f;
                canvas.Translate(translateXBackPath, translateYbackPath);
                fillPaint.Shader = SKShader.CreateLinearGradient(
                    new SKPoint(info.Width * 0.5f, 0),
                    new SKPoint(info.Width * 0.5f, info.Height),
                    new SKColor[] { SKColor.Parse(Color1), SKColor.Parse(Color2), SKColor.Parse(Color2) },
                    new float[] { 0, 0.37f, 1 },
                    SKShaderTileMode.Clamp
                    );
                canvas.DrawPath(backPath, fillPaint);
            }

            canvas.Restore();

            canvas.Save();
            using (SKPath backFilePath = SKPath.ParseSvgPathData("M174.079,150.908H29.155l-.4-114.144H174.481Z"))
            {
                backFilePath.Transform(SKMatrix.MakeScale(density, density));
                backFilePath.GetTightBounds(out var backFilePathTightBounds);
                var translateXBackFilePath = info.Width * 0.5f - backFilePathTightBounds.MidX;
                var translateYbackFilePath = info.Height - backFilePathTightBounds.Bottom - 20f;
                canvas.Translate(translateXBackFilePath, translateYbackFilePath);
                fillPaint.Shader = SKShader.CreateLinearGradient(
                    new SKPoint(info.Width * 0.5f, 0),
                    new SKPoint(info.Width * 0.5f, info.Height),
                    new SKColor[] { SKColor.Parse("#e6e6e6"), SKColor.Parse("#e8e8e8"), SKColor.Parse("#f0f0f0"), SKColor.Parse("#f2f2f2") },
                    new float[] { 0, 0.633f, 0.949f, 1 },
                    SKShaderTileMode.Clamp
                    );
                canvas.DrawPath(backFilePath, fillPaint);
            }
            canvas.Restore();

            canvas.Save();
            using (SKPath frontFilePath = SKPath.ParseSvgPathData("M170.491,158.681H25.567L22.753,49.764H173.3Z"))
            {
                frontFilePath.Transform(SKMatrix.MakeScale(density, density));
                frontFilePath.GetTightBounds(out var frontFilePathTightBounds);
                var translateXFrontFilePath = info.Width * 0.5f - frontFilePathTightBounds.MidX;
                var translateYFrontFilePath = info.Height - frontFilePathTightBounds.Bottom - 20f;
                canvas.Translate(translateXFrontFilePath, translateYFrontFilePath);
                fillPaint.Shader = SKShader.CreateLinearGradient(
                    new SKPoint(info.Width * 0.5f, 0),
                    new SKPoint(info.Width * 0.5f, info.Height),
                    new SKColor[] { SKColor.Parse("#cccccc"), SKColor.Parse("#cecece"), SKColor.Parse("#d6d6d6"), SKColor.Parse("#e3e3e3"), SKColor.Parse("#f6f6f6"), SKColor.Parse("#ffffff") },
                    new float[] { 0, 0.427f, 0.64f, 0.806f, 0.947f, 1 },
                    SKShaderTileMode.Clamp
                    );
                canvas.DrawPath(frontFilePath, fillPaint);
            }
            canvas.Restore();

            canvas.Save();
            using (SKPath frontPath = SKPath.ParseSvgPathData("M165.433,164.917a4.106,4.106,0,0,1-4.089,4.011H7.735a4.578,4.578,0,0,1-4.325-4.292L0,68.019A3.948,3.948,0,0,1,3.95,64H164.895a3.948,3.948,0,0,1,3.95,4.02Z"))
            {
                frontPath.Transform(SKMatrix.MakeScale(density, density));
                frontPath.GetTightBounds(out var frontPathTightBounds);

                fillPaint.Shader = SKShader.CreateLinearGradient(
                    new SKPoint(info.Width * 0.5f, 0),
                    new SKPoint(info.Width * 0.5f, info.Height),
                    new SKColor[] { SKColor.Parse(Color3), SKColor.Parse(Color4), SKColor.Parse(Color5), SKColor.Parse(Color6) },
                    new float[] { 0, 0.595f, 0.957f, 1 },
                    SKShaderTileMode.Clamp
                    );

                SKMatrix   matrix   = SKMatrix.MakeTranslation(-frontPathTightBounds.Right, -frontPathTightBounds.Bottom);
                SKMatrix44 matrix44 = SKMatrix44.CreateIdentity();
                matrix44.PostConcat(SKMatrix44.CreateRotationDegrees(1, 0, 0, -FrontPathDegree));
                matrix44.PostConcat(SKMatrix44.CreateRotationDegrees(0, 1, 0, -0.5f * FrontPathDegree));
                matrix44.PostConcat(SKMatrix44.CreateRotationDegrees(0, 0, 1, 0));
                SKMatrix.PostConcat(ref matrix, matrix44.Matrix);
                SKMatrix.PostConcat(ref matrix, SKMatrix.MakeTranslation(frontPathTightBounds.Right, frontPathTightBounds.Bottom));
                canvas.SetMatrix(matrix);

                var translateXFrontPath = info.Width * 0.5f - frontPathTightBounds.MidX;
                var translateYFrontPath = info.Height - frontPathTightBounds.Bottom - 20f;
                canvas.Translate(translateXFrontPath, translateYFrontPath);
                canvas.DrawPath(frontPath, fillPaint);
            }
            canvas.Restore();
        }