コード例 #1
0
ファイル: CanvasAndLayers.cs プロジェクト: weacw/RunTogether
        void drawLine()
        {
            var canvas = new CommandBufferCanvas(this._renderTexture, Window.instance.devicePixelRatio,
                                                 this._meshPool);

            var paint = new Paint {
                color       = new Color(0xFFFF0000),
                style       = PaintingStyle.stroke,
                strokeWidth = 10,
                shader      = Gradient.linear(new Offset(10, 10), new Offset(180, 180), new List <Color>()
                {
                    Colors.red, Colors.green, Colors.yellow
                }, null, TileMode.clamp)
            };

            canvas.drawLine(
                new Offset(10, 20),
                new Offset(50, 20),
                paint);

            canvas.drawLine(
                new Offset(10, 10),
                new Offset(100, 100),
                paint);

            canvas.drawLine(
                new Offset(10, 10),
                new Offset(10, 50),
                paint);

            canvas.drawLine(
                new Offset(40, 10),
                new Offset(90, 10),
                paint);


            canvas.drawArc(Unity.UIWidgets.ui.Rect.fromLTWH(200, 200, 100, 100), Mathf.PI / 4,
                           -Mathf.PI / 2 + Mathf.PI * 4 - 1, true, paint);

            paint.maskFilter  = MaskFilter.blur(BlurStyle.normal, 1);
            paint.strokeWidth = 4;

            canvas.drawLine(
                new Offset(40, 20),
                new Offset(120, 190),
                paint);

            canvas.scale(3);
            TextBlobBuilder builder = new TextBlobBuilder();
            string          text    = "This is a text blob";

            builder.allocRunPos(new TextStyle(), text, 0, text.Length);
            builder.setBounds(Unity.UIWidgets.ui.Rect.fromLTWH(-10, -20, 200, 50));
            builder.positions = new Vector2d[] {
                new Vector2d(10, 0),
                new Vector2d(20, 0),
                new Vector2d(30, 0),
                new Vector2d(40, 0),
                new Vector2d(50, 0),
                new Vector2d(60, 0),
                new Vector2d(70, 0),
                new Vector2d(80, 0),
                new Vector2d(90, 0),
                new Vector2d(100, 0),
                new Vector2d(110, 0),
                new Vector2d(120, 0),
                new Vector2d(130, 0),
                new Vector2d(140, 0),
                new Vector2d(150, 0),
                new Vector2d(160, 0),
                new Vector2d(170, 0),
                new Vector2d(180, 0),
                new Vector2d(190, 0),
            };

            var textBlob = builder.make();

            canvas.drawTextBlob(textBlob, new Offset(100, 100), new Paint {
                color      = Colors.black,
                maskFilter = MaskFilter.blur(BlurStyle.normal, 5),
            });
            canvas.drawTextBlob(textBlob, new Offset(100, 100), paint);

            canvas.drawLine(
                new Offset(10, 30),
                new Offset(10, 60),
                new Paint()
            {
                style = PaintingStyle.stroke, strokeWidth = 0.1f
            });

            canvas.drawLine(
                new Offset(20, 30),
                new Offset(20, 60),
                new Paint()
            {
                style = PaintingStyle.stroke, strokeWidth = 0.333f
            });

            canvas.flush();
        }
コード例 #2
0
        private void paintImageSpan(PaintingContext context, Offset offset)
        {
            var canvas = context.canvas;
            var bounds = offset & size;

            canvas.save();
            var textOffset = 0;

            foreach (var span in text.children)
            {
                if (span is ImageSpan imageSpan)
                {
                    var offsetForCaret = getOffsetForCaret(
                        new TextPosition(offset: textOffset),
                        bounds
                        );
                    if (textOffset != 0 &&
                        offsetForCaret.dx == 0F &&
                        offsetForCaret.dy == 0F)
                    {
                        return;
                    }

                    var topLeftOffset = new Offset(
                        offset.dx + offsetForCaret.dx - (textOffset == 0 ? 0 : 0),
                        offset.dy + offsetForCaret.dy
                        );
                    if (imageSpan.imageResolver.image == null)
                    {
                        canvas.drawRect(
                            rect: Rect.fromLTWH(
                                topLeftOffset.dx + imageSpan.margin.left,
                                topLeftOffset.dy + imageSpan.margin.top,
                                imageSpan.innerWidth,
                                imageSpan.innerHeight
                                ),
                            paint: ImagePlaceholderBackgroundPaint);
                        var textBlobBuilder = new TextBlobBuilder();
                        var text            = $"{(char) Icons.MaterialImage.codePoint}";
                        var iconSize        = imageSpan.innerWidth > imageSpan.innerHeight * 3
                            ? imageSpan.innerHeight * 3 / 4
                            : imageSpan.innerWidth / 4;
                        textBlobBuilder.allocRunPos(
                            style: new TextStyle(
                                fontFamily: Icons.MaterialImage.fontFamily,
                                fontSize: iconSize
                                ),
                            text: text,
                            offset: 0,
                            size: text.Length);
                        var rect = topLeftOffset & new Size(imageSpan.innerWidth, imageSpan.innerHeight);
                        textBlobBuilder.setBounds(rect.toRect());
                        canvas.drawTextBlob(
                            textBlobBuilder.make(),
                            new Offset(
                                rect.left + (rect.width - iconSize) / 2,
                                rect.top + (rect.height + iconSize) / 2),
                            ImagePlaceholderPaint);

                        imageSpan.imageResolver.Resolve((imageInfo, synchronousCall) =>
                        {
                            if (synchronousCall)
                            {
                                ImageUtils.paintImage(
                                    canvas: canvas,
                                    rect: topLeftOffset & new Size(imageSpan.width, imageSpan.height),
                                    image: imageSpan.imageResolver.image,
                                    fit: BoxFit.scaleDown,
                                    alignment: Alignment.center
                                    );
                            }
                            else
                            {
                                if (owner == null || !owner.debugDoingPaint)
                                {
                                    markNeedsPaint();
                                }
                            }
                        });
                        textOffset += imageSpan.toPlainText().Length;

                        continue;
                    }

                    ImageUtils.paintImage(
                        canvas: canvas,
                        rect: Rect.fromLTWH(
                            topLeftOffset.dx + imageSpan.margin.left,
                            topLeftOffset.dy + imageSpan.margin.top,
                            imageSpan.innerWidth,
                            imageSpan.innerHeight
                            ),
                        image: imageSpan.imageResolver.image,
                        fit: BoxFit.scaleDown,
                        alignment: Alignment.center
                        );
                }

                textOffset += span.toPlainText().Length;
            }

            canvas.restore();
        }