コード例 #1
0
        static Matrix3x2 GetDisplayTransform(ICanvasAnimatedControl canvas)
        {
            var outputSize = canvas.Size.ToVector2();
            var sourceSize = new Vector2(canvas.ConvertPixelsToDips(simulationW), canvas.ConvertPixelsToDips(simulationH));

            return(Utils.GetDisplayTransform(outputSize, sourceSize));
        }
コード例 #2
0
ファイル: AppIconGenerator.cs プロジェクト: markmnl/Win2D
        async Task GenerateIcon(AppInfo appInfo, IconInfo iconInfo, StorageFolder folder)
        {
            // Draw the icon image into a command list.
            var iconImage = new CanvasCommandList(device);

            using (var ds = iconImage.CreateDrawingSession())
            {
                appInfo.DrawIconImage(ds, iconInfo);
            }

            // Rasterize into a rendertarget.
            var renderTarget = new CanvasRenderTarget(device, iconInfo.Width, iconInfo.Height, 96);

            using (var ds = renderTarget.CreateDrawingSession())
            {
                // Initialize with the appropriate background color.
                ds.Clear(iconInfo.TransparentBackground ? Colors.Transparent : appInfo.BackgroundColor);

                // Work out where to position the icon image.
                var imageBounds = iconImage.GetBounds(ds);

                imageBounds.Height *= 1 + iconInfo.BottomPadding;

                float scaleUpTheSmallerIcons = Math.Max(1, 1 + (60f - iconInfo.Width) / 50f);

                float imageScale = appInfo.ImageScale * scaleUpTheSmallerIcons;

                ds.Transform = Matrix3x2.CreateTranslation((float)-imageBounds.X, (float)-imageBounds.Y) *
                               Utils.GetDisplayTransform(renderTarget.Size.ToVector2(), new Vector2((float)imageBounds.Width, (float)imageBounds.Height)) *
                               Matrix3x2.CreateScale(imageScale, renderTarget.Size.ToVector2() / 2);

                // Optional shadow effet.
                if (appInfo.AddShadow)
                {
                    var shadow = new ShadowEffect
                    {
                        Source     = iconImage,
                        BlurAmount = 12,
                    };

                    ds.DrawImage(shadow);
                }

                // draw the main icon image.
                ds.DrawImage(iconImage);
            }

            // Save to a file.
            using (var stream = await folder.OpenStreamForWriteAsync(iconInfo.Filename, CreationCollisionOption.ReplaceExisting))
            {
                await renderTarget.SaveAsync(stream.AsRandomAccessStream(), CanvasBitmapFileFormat.Png);
            }
        }
コード例 #3
0
ファイル: GameOfLife.xaml.cs プロジェクト: gkrishnaa/Win2D
        // Toggles the color of cells when they are clicked on.
        private void ProcessPointerInput(PointerRoutedEventArgs e)
        {
            if (!isPointerDown)
            {
                return;
            }

            // Invert the display transform, to convert pointer positions into simulation rendertarget space.
            Matrix3x2 transform;

            Matrix3x2.Invert(Utils.GetDisplayTransform(canvas.Size, canvas, simulationW, simulationH), out transform);

            foreach (var point in e.GetIntermediatePoints(canvas))
            {
                if (!point.IsInContact)
                {
                    continue;
                }

                var pos = Vector2.Transform(point.Position.ToVector2(), transform);

                var x = canvas.ConvertDipsToPixels(pos.X);
                var y = canvas.ConvertDipsToPixels(pos.Y);

                // If the point is within the bounds of the rendertarget, and not the same as the last point...
                if (x >= 0 &&
                    y >= 0 &&
                    x < simulationW &&
                    y < simulationH &&
                    ((x != lastPointerX || y != lastPointerY)))
                {
                    // Read the current color.
                    var cellColor = currentSurface.GetPixelColors(x, y, 1, 1);

                    // Toggle the value.
                    cellColor[0] = cellColor[0].R > 0 ? Colors.Black : Colors.White;

                    // Set the new color.
                    currentSurface.SetPixelColors(cellColor, x, y, 1, 1);

                    lastPointerX = x;
                    lastPointerY = y;
                }
            }
        }
コード例 #4
0
ファイル: GameOfLife.xaml.cs プロジェクト: gkrishnaa/Win2D
        void Canvas_Draw(CanvasControl sender, CanvasDrawEventArgs args)
        {
            // Update the simulation state.
            if (!Paused)
            {
                var currentTime = timer.Elapsed;
                var elapsed     = (currentTime - lastTime).TotalSeconds;

                if (elapsed >= (Slow ? 0.25 : 1f / 30))
                {
                    UpdateSimulation();
                    lastTime = currentTime;
                }
            }

            // Display the current surface.
            invertEffect.Source             = currentSurface;
            transformEffect.TransformMatrix = Utils.GetDisplayTransform(canvas.Size, canvas, simulationW, simulationH);
            args.DrawingSession.DrawImage(transformEffect);

            sender.Invalidate();
        }
コード例 #5
0
ファイル: VectorArt.xaml.cs プロジェクト: N500/Win2D
        void Canvas_Draw(CanvasControl sender, CanvasDrawEventArgs args)
        {
            // Transform and clip so the scene will fit whatever size display we are running on.
            Vector2 sceneSize = sceneSizes[whichScene];

            args.DrawingSession.Transform = Utils.GetDisplayTransform(sender.Size.ToVector2(), sceneSize);

            using (args.DrawingSession.CreateLayer(1f, new Rect(0, 0, sceneSize.X, sceneSize.Y)))
            {
                // Draw the vector art.
                currentDrawingSession = args.DrawingSession;

                switch (whichScene)
                {
                case 0:
                    DrawScene0();
                    break;

                case 1:
                    DrawScene1(randomSeed);
                    break;
                }
            }
        }
コード例 #6
0
        private void Canvas_Draw(ICanvasAnimatedControl sender, CanvasAnimatedDrawEventArgs args)
        {
            Matrix3x2 displayTransform = Utils.GetDisplayTransform(sender.Size.ToVector2(), new Vector2(1000, 1000));

            args.DrawingSession.Transform = displayTransform;

            args.DrawingSession.FillGeometry(combinedGeometry, Colors.Blue);

            if (showSourceGeometry)
            {
                args.DrawingSession.DrawGeometry(leftGeometry, Colors.Red, 5.0f);

                args.DrawingSession.Transform = interGeometryTransform * displayTransform;
                args.DrawingSession.DrawGeometry(rightGeometry, Colors.Red, 5.0f);
                args.DrawingSession.Transform = displayTransform;
            }

            if (showTessellation)
            {
                foreach (var triangle in tessellation)
                {
                    args.DrawingSession.DrawLine(triangle.Vertex1, triangle.Vertex2, Colors.Gray);
                    args.DrawingSession.DrawLine(triangle.Vertex2, triangle.Vertex3, Colors.Gray);
                    args.DrawingSession.DrawLine(triangle.Vertex3, triangle.Vertex1, Colors.Gray);
                }
            }

            if (CurrentContourTracingAnimation != ContourTracingAnimationOption.None)
            {
                args.DrawingSession.FillCircle(pointOnContourPath, 2, Colors.White);

                const float arrowSize = 10.0f;

                Vector2 tangentLeft = new Vector2(
                    -tangentOnContourPath.Y,
                    tangentOnContourPath.X);

                Vector2 tangentRight = new Vector2(
                    tangentOnContourPath.Y,
                    -tangentOnContourPath.X);

                Vector2 bisectorLeft = new Vector2(
                    tangentOnContourPath.X + tangentLeft.X,
                    tangentOnContourPath.Y + tangentLeft.Y);

                Vector2 bisectorRight = new Vector2(
                    tangentOnContourPath.X + tangentRight.X,
                    tangentOnContourPath.Y + tangentRight.Y);

                Vector2 arrowheadFront = new Vector2(
                    pointOnContourPath.X + (tangentOnContourPath.X * arrowSize * 2),
                    pointOnContourPath.Y + (tangentOnContourPath.Y * arrowSize * 2));

                Vector2 arrowheadLeft = new Vector2(
                    arrowheadFront.X - (bisectorLeft.X * arrowSize),
                    arrowheadFront.Y - (bisectorLeft.Y * arrowSize));

                Vector2 arrowheadRight = new Vector2(
                    arrowheadFront.X - (bisectorRight.X * arrowSize),
                    arrowheadFront.Y - (bisectorRight.Y * arrowSize));

                const float strokeWidth = arrowSize / 4.0f;
                args.DrawingSession.DrawLine(pointOnContourPath, arrowheadFront, Colors.White, strokeWidth);
                args.DrawingSession.DrawLine(arrowheadFront, arrowheadLeft, Colors.White, strokeWidth);
                args.DrawingSession.DrawLine(arrowheadFront, arrowheadRight, Colors.White, strokeWidth);
            }
        }
コード例 #7
0
            static CanvasBitmap MakeThumbnailPretty(CanvasBitmap capturedBitmap, float thumbnailWidth, float thumbnailHeight, Rect targetRect)
            {
                var pixelColors = capturedBitmap.GetPixelColors();

                // Remove any unused space around the edge of the bitmap, so it will fill the thumbnail.
                Rect cropRect = CropCapturedBitmap(capturedBitmap, pixelColors);

                // Choose a (hopefully) aesthetically pleasing background color.
                Color backgroundColor = ChooseBackgroundColor(pixelColors);

                // Apply letterbox scaling to fit the image into the target thumbnail.
                Vector2 outputSize = new Vector2((float)targetRect.Width, (float)targetRect.Height);
                var     sourceSize = new Vector2((float)cropRect.Width, (float)cropRect.Height);
                var     letterbox  = Utils.GetDisplayTransform(outputSize, sourceSize);
                var     translate  = Matrix3x2.CreateTranslation((float)targetRect.X, (float)targetRect.Y);

                // Position the image where we want it.
                var scaledImage = new Transform2DEffect
                {
                    Source = new AtlasEffect
                    {
                        Source          = capturedBitmap,
                        SourceRectangle = cropRect,
                    },
                    InterpolationMode = CanvasImageInterpolation.HighQualityCubic,
                    TransformMatrix   = letterbox * translate,
                };

                // Create the final thumbnail image.
                var finalImage = new CompositeEffect
                {
                    Inputs =
                    {
                        // Blurred shadow.
                        new ShadowEffect
                        {
                            Source = new MorphologyEffect
                            {
                                Source = scaledImage,
                                Mode   = MorphologyEffectMode.Dilate,
                                Width  = dilateAmount,
                                Height = dilateAmount,
                            },
                            BlurAmount = blurAmount,
                        },

                        // Overlay the image itself.
                        scaledImage
                    }
                };

                // Rasterize the effect into a rendertarget.
                CanvasRenderTarget output = new CanvasRenderTarget(capturedBitmap.Device, thumbnailWidth, thumbnailHeight, 96);

                using (var ds = output.CreateDrawingSession())
                {
                    ds.Clear(backgroundColor);
                    ds.DrawImage(finalImage);
                }

                return(output);
            }