Ejemplo n.º 1
0
        //dynamic _innerDiv; //we use this one so that de "display:table" do not keep us from having the borders.

        ///// <summary>
        ///// Initializes a new instance of the Rectangle class.
        ///// </summary>
        //public Rectangle() : base() { }

        //protected internal override void INTERNAL_OnAttachedToVisualTree()
        //{
        //    ManageStrokeChanged();
        //    ManageStrokeThicknessChanged();
        //}

        //public override object CreateDomElement(object parentRef, out object domElementWhereToPlaceChildren)
        //{
        //    object div1;

        //    dynamic outerStyle = INTERNAL_HtmlDomManager.CreateDomElementAppendItAndGetStyle("div", parentRef, this, out div1);
        //    outerStyle.boxSizing = "border-box";
        //    outerStyle.borderCollapse = "separate";

        //    dynamic innerStyle = INTERNAL_HtmlDomManager.CreateDomElementAppendItAndGetStyle("div", div1, this, out _innerDiv);
        //    innerStyle.width = "100%";
        //    innerStyle.height = "100%";
        //    innerStyle.borderStyle = "solid";
        //    innerStyle.borderWidth = "0px";

        //    domElementWhereToPlaceChildren = null;
        //    return div1;
        //}

        //protected internal override void ManageStrokeChanged()
        //{
        //    if (INTERNAL_VisualTreeManager.IsElementInVisualTree(this))
        //    {
        //        var outerStyle = INTERNAL_HtmlDomManager.GetFrameworkElementOuterStyleForModification(this);

        //        if (Stroke is SolidColorBrush)
        //        {
        //            INTERNAL_HtmlDomManager.GetDomElementStyleForModification(_innerDiv).borderColor = ((SolidColorBrush)Stroke).INTERNAL_ToHtmlString();
        //            var strokeThickness = this.StrokeThickness;
        //            outerStyle.paddingBottom = strokeThickness * 2 + "px";
        //            outerStyle.paddingRight = strokeThickness * 2 + "px";
        //        }
        //        else if (Stroke == null)
        //        {
        //            INTERNAL_HtmlDomManager.GetDomElementStyleForModification(_innerDiv).borderColor = "";
        //            outerStyle.paddingBottom = "0px";
        //            outerStyle.paddingRight = "0px";
        //        }
        //        else
        //        {
        //            throw new NotSupportedException("The specified brush is not supported.");
        //        }
        //    }
        //}

        //protected internal override void ManageStrokeThicknessChanged()
        //{
        //    if (Stroke != null)
        //    {
        //        if (INTERNAL_VisualTreeManager.IsElementInVisualTree(this))
        //        {
        //            dynamic innerStyle = INTERNAL_HtmlDomManager.GetDomElementStyleForModification(_innerDiv);
        //            innerStyle.borderWidth = StrokeThickness + "px";

        //            dynamic outerStyle = INTERNAL_HtmlDomManager.GetFrameworkElementOuterStyleForModification(this);
        //            outerStyle.paddingBottom = StrokeThickness * 2 + "px";
        //            outerStyle.paddingRight = StrokeThickness * 2 + "px";
        //        }
        //    }
        //}

        //internal override void ManageFill_Changed()
        //{
        //    if (INTERNAL_VisualTreeManager.IsElementInVisualTree(this))
        //        if (Fill is SolidColorBrush)
        //            INTERNAL_HtmlDomManager.GetDomElementStyleForModification(_innerDiv).backgroundColor = ((SolidColorBrush)Fill).INTERNAL_ToHtmlString();
        //}

        #endregion

        public override object CreateDomElement(object parentRef, out object domElementWhereToPlaceChildren)
        {
            return(INTERNAL_ShapesDrawHelpers.CreateDomElementForPathAndSimilar(this, parentRef, out _canvasDomElement, out domElementWhereToPlaceChildren));

            //domElementWhereToPlaceChildren = null;
            //var canvas = INTERNAL_HtmlDomManager.CreateDomElementAndAppendIt("canvas", parentRef, this);
            //return canvas;
        }
Ejemplo n.º 2
0
        internal static void GetShapeInfos(Shape shape,
                                           out double xOffsetToApplyBeforeMultiplication,
                                           out double yOffsetToApplyBeforeMultiplication,
                                           out double xOffsetToApplyAfterMultiplication,
                                           out double yOffsetToApplyAfterMultiplication,
                                           out double sizeX,
                                           out double sizeY,
                                           out double horizontalMultiplicator,
                                           out double verticalMultiplicator,
                                           out Size shapeActualSize)
        {
            double  width           = shape.Width;
            double  height          = shape.Height;
            double  strokeThickness = shape.StrokeThickness;
            Stretch strech          = shape.Stretch;

            double minX = 0;
            double maxX = double.IsNaN(width) ? 0 : width;
            double minY = 0;
            double maxY = double.IsNaN(height) ? 0 : height;

            INTERNAL_ShapesDrawHelpers.PrepareStretch(shape, shape._canvasDomElement, minX, maxX, minY, maxY, strech, out shapeActualSize);

            if (shape.Stroke == null)
            {
                strokeThickness = 0;
            }

            INTERNAL_ShapesDrawHelpers.GetMultiplicatorsAndOffsetForStretch(shape,
                                                                            strokeThickness,
                                                                            minX,
                                                                            maxX,
                                                                            minY,
                                                                            maxY,
                                                                            strech,
                                                                            shapeActualSize,
                                                                            out horizontalMultiplicator,
                                                                            out verticalMultiplicator,
                                                                            out xOffsetToApplyBeforeMultiplication,
                                                                            out yOffsetToApplyBeforeMultiplication,
                                                                            out xOffsetToApplyAfterMultiplication,
                                                                            out yOffsetToApplyAfterMultiplication,
                                                                            out shape._marginOffsets);

            if (strech == Stretch.None)
            {
                Thickness margin = shape.Margin;
                Margin_MethodToUpdateDom(shape, new Thickness(margin.Left + shape._marginOffsets.X,
                                                              margin.Top + shape._marginOffsets.Y,
                                                              margin.Right,
                                                              margin.Bottom));
            }
            sizeX = Math.Max(1, maxX - minX) * horizontalMultiplicator;
            sizeY = Math.Max(1, maxY - minY) * verticalMultiplicator;
        }
Ejemplo n.º 3
0
        internal static void GetShapeInfos(Shape shape, out double xOffsetToApplyBeforeMultiplication, out double yOffsetToApplyBeforeMultiplication, out double xOffsetToApplyAfterMultiplication, out double yOffsetToApplyAfterMultiplication, out double sizeX, out double sizeY, out double horizontalMultiplicator, out double verticalMultiplicator, out Size shapeActualSize)
        {
            double minX = 1;
            double maxX = 1;
            double minY = 1;
            double maxY = 1;

            if (shape.Width > 0)
            {
                minX = 0;
                maxX = shape.Width;
            }
            if (shape.Height > 0)
            {
                minY = 0;
                maxY = shape.Height;
            }

            INTERNAL_ShapesDrawHelpers.PrepareStretch(shape, shape._canvasDomElement, minX, maxX, minY, maxY, shape.Stretch, out shapeActualSize);

            double strokeThickness = shape.StrokeThickness; //Note: if Stroke is null, the StrokeThickness should not have an influence on the multiplicators.

            if (shape.Stroke == null)
            {
                strokeThickness = 0;
            }

            INTERNAL_ShapesDrawHelpers.GetMultiplicatorsAndOffsetForStretch(shape, strokeThickness, minX, maxX, minY, maxY, shape.Stretch, shapeActualSize, out horizontalMultiplicator, out verticalMultiplicator, out xOffsetToApplyBeforeMultiplication, out yOffsetToApplyBeforeMultiplication, out xOffsetToApplyAfterMultiplication, out yOffsetToApplyAfterMultiplication, out shape._marginOffsets);

            if (shape.Stretch == Stretch.None)
            {
                Margin_MethodToUpdateDom(shape, new Thickness(shape.Margin.Left + shape._marginOffsets.X,
                                                              shape.Margin.Top + shape._marginOffsets.Y,
                                                              shape.Margin.Right, shape.Margin.Bottom));
            }


            //dynamic context = INTERNAL_HtmlDomManager.Get2dCanvasContext(shape._canvasDomElement);


            sizeX = maxX - minX;
            if (sizeX == 0)
            {
                sizeX = 1;
            }
            sizeX *= horizontalMultiplicator;

            sizeY = maxY - minY;
            if (sizeY == 0)
            {
                sizeY = 1;
            }
            sizeY *= verticalMultiplicator;
        }
Ejemplo n.º 4
0
        //protected internal override void INTERNAL_OnAttachedToVisualTree()
        //{
        //    ScheduleRedraw();
        //}

        protected internal override void Redraw()
        {
            if (INTERNAL_VisualTreeManager.IsElementInVisualTree(this))
            {
                if (Data != null)
                {
                    double minX = double.MaxValue;
                    double minY = double.MaxValue;
                    double maxX = double.MinValue;
                    double maxY = double.MinValue;
                    if (Data is PathGeometry)
                    {
                        ((PathGeometry)Data).UpdateStrokeThickness(StrokeThickness);
                    }
                    Data.GetMinMaxXY(ref minX, ref maxX, ref minY, ref maxY);

                    Size shapeActualSize;
                    INTERNAL_ShapesDrawHelpers.PrepareStretch(this, _canvasDomElement, minX, maxX, minY, maxY, Stretch, out shapeActualSize);
                    double horizontalMultiplicator;
                    double verticalMultiplicator;
                    double xOffsetToApplyBeforeMultiplication;
                    double yOffsetToApplyBeforeMultiplication;
                    double xOffsetToApplyAfterMultiplication;
                    double yOffsetToApplyAfterMultiplication;
                    double strokeThickness = StrokeThickness;
                    if (Stroke == null)
                    {
                        strokeThickness = 0;
                    }

                    INTERNAL_ShapesDrawHelpers.GetMultiplicatorsAndOffsetForStretch(this, strokeThickness, minX, maxX, minY, maxY, Stretch, shapeActualSize, out horizontalMultiplicator, out verticalMultiplicator, out xOffsetToApplyBeforeMultiplication, out yOffsetToApplyBeforeMultiplication, out xOffsetToApplyAfterMultiplication, out yOffsetToApplyAfterMultiplication, out _marginOffsets);

                    ApplyMarginToFixNegativeCoordinates(new Point());
                    if (Stretch == Stretch.None)
                    {
                        ApplyMarginToFixNegativeCoordinates(_marginOffsets);
                    }

                    // A call to "context.beginPath" is required on IE and Edge for the figures to be drawn properly (cf. ZenDesk #971):
                    CSHTML5.Interop.ExecuteJavaScriptAsync(@"$0.getContext('2d').beginPath()", _canvasDomElement);

                    //problem here: the shape seems to be overall smaller than intended due to the edges of the path not being sharp?
                    Data.DefineInCanvas(this, _canvasDomElement, horizontalMultiplicator, verticalMultiplicator, xOffsetToApplyBeforeMultiplication, yOffsetToApplyBeforeMultiplication, xOffsetToApplyAfterMultiplication, yOffsetToApplyAfterMultiplication, shapeActualSize); //puts the lines and stuff in the context
                    Shape.DrawFillAndStroke(this, Data.GetFillRuleAsString(), minX, minY, maxX, maxY, horizontalMultiplicator, verticalMultiplicator, xOffsetToApplyBeforeMultiplication, yOffsetToApplyBeforeMultiplication, shapeActualSize);
                }
            }
        }
Ejemplo n.º 5
0
        protected internal override void Redraw()
        {
            if (!INTERNAL_VisualTreeManager.IsElementInVisualTree(this))
            {
                return;
            }

            if (Points?.Count < 2)
            {
                return;
            }

            double minX, minY, maxX, maxY;

            GetMinMaxXY(out minX, out maxX, out minY, out maxY);

            Size shapeActualSize;

            INTERNAL_ShapesDrawHelpers.PrepareStretch(this, _canvasDomElement, 0, maxX, 0, maxY, Stretch, out shapeActualSize);

            double horizontalMultiplicator;
            double verticalMultiplicator;
            double xOffsetToApplyBeforeMultiplication;
            double yOffsetToApplyBeforeMultiplication;
            double xOffsetToApplyAfterMultiplication;
            double yOffsetToApplyAfterMultiplication;

            INTERNAL_ShapesDrawHelpers.GetMultiplicatorsAndOffsetForStretch(this, StrokeThickness, 0, maxX, 0, maxY,
                                                                            Stretch, shapeActualSize, out horizontalMultiplicator, out verticalMultiplicator, out xOffsetToApplyBeforeMultiplication,
                                                                            out yOffsetToApplyBeforeMultiplication, out xOffsetToApplyAfterMultiplication, out yOffsetToApplyAfterMultiplication, out _marginOffsets);

            ApplyMarginToFixNegativeCoordinates(new Point());

            if (Stretch == Stretch.None)
            {
                ApplyMarginToFixNegativeCoordinates(_marginOffsets);
            }

            INTERNAL_ShapesDrawHelpers.PrepareLines(_canvasDomElement, Points, StrokeThickness, false);
            //todo: make sure the parameters below are correct.
            Shape.DrawFillAndStroke(this, FillRule == FillRule.Nonzero ? "nonzero" : "evenodd", xOffsetToApplyAfterMultiplication,
                                    yOffsetToApplyAfterMultiplication, xOffsetToApplyAfterMultiplication + maxX, yOffsetToApplyAfterMultiplication + maxY,
                                    horizontalMultiplicator, verticalMultiplicator, xOffsetToApplyBeforeMultiplication, yOffsetToApplyBeforeMultiplication, shapeActualSize);
        }
Ejemplo n.º 6
0
        // note: we only define the line. Erasing the previous one (if any) and actually drawing the
        // new one should be made directly by the container.
        internal protected override void DefineInCanvas(Path path,
                                                        object canvasDomElement,
                                                        double horizontalMultiplicator,
                                                        double verticalMultiplicator,
                                                        double xOffsetToApplyBeforeMultiplication,
                                                        double yOffsetToApplyBeforeMultiplication,
                                                        double xOffsetToApplyAfterMultiplication,
                                                        double yOffsetToApplyAfterMultiplication,
                                                        Size shapeActualSize)
        {
            string strokeAsString = string.Empty;

            INTERNAL_ShapesDrawHelpers.PrepareLine(path._canvasDomElement, StartPoint, EndPoint);

            dynamic context = INTERNAL_HtmlDomManager.Get2dCanvasContext(canvasDomElement);

            context.strokeStyle = strokeAsString; //set the shape's lines color
            context.lineWidth   = path.StrokeThickness + "px";
        }
Ejemplo n.º 7
0
        internal override void DefineInCanvas(Shapes.Path path, object canvasDomElement, double horizontalMultiplicator, double verticalMultiplicator, double xOffsetToApplyBeforeMultiplication, double yOffsetToApplyBeforeMultiplication, double xOffsetToApplyAfterMultiplication, double yOffsetToApplyAfterMultiplication, Size shapeActualSize)
        {
            string strokeAsString = string.Empty;

            if (path.Stroke == null || path.Stroke is SolidColorBrush) //todo: make sure we want the same behaviour when it is null and when it is a SolidColorBrush (basically, check if null means default value)
            {
                if (path.Stroke != null)                               //if stroke is null, we want to set it as an empty string, otherwise, it is a SolidColorBrush and we want to get its color.
                {
                    strokeAsString = ((SolidColorBrush)path.Stroke).INTERNAL_ToHtmlString();
                }
            }
            else
            {
                throw new NotSupportedException("The specified brush is not supported.");
            }
            dynamic context      = INTERNAL_HtmlDomManager.Get2dCanvasContext(canvasDomElement);
            double  actualWidth  = RadiusX * 2; //it's a radius and we want the whole width (basically a "diameter")
            double  actualHeight = RadiusY * 2; //it's a radius and we want the whole height

            INTERNAL_ShapesDrawHelpers.PrepareEllipse(canvasDomElement, actualWidth, actualHeight, Center.X + xOffsetToApplyBeforeMultiplication + xOffsetToApplyAfterMultiplication, Center.Y + yOffsetToApplyBeforeMultiplication + yOffsetToApplyAfterMultiplication);
            context.strokeStyle = strokeAsString;
        }
Ejemplo n.º 8
0
        //protected internal override void INTERNAL_OnAttachedToVisualTree()
        //{
        //    ScheduleRedraw();
        //}

        override internal protected void Redraw()
        {
            if (INTERNAL_VisualTreeManager.IsElementInVisualTree(this))
            {
                double xOffsetToApplyBeforeMultiplication;
                double yOffsetToApplyBeforeMultiplication;
                double xOffsetToApplyAfterMultiplication;
                double yOffsetToApplyAfterMultiplication;
                double sizeX;
                double sizeY;
                double horizontalMultiplicator;
                double verticalMultiplicator;
                Size   shapeActualSize;
                Shape.GetShapeInfos(this, out xOffsetToApplyBeforeMultiplication, out yOffsetToApplyBeforeMultiplication, out xOffsetToApplyAfterMultiplication, out yOffsetToApplyAfterMultiplication, out sizeX, out sizeY, out horizontalMultiplicator, out verticalMultiplicator, out shapeActualSize);

                ApplyMarginToFixNegativeCoordinates(new Point());
                if (Stretch == Media.Stretch.None)
                {
                    ApplyMarginToFixNegativeCoordinates(_marginOffsets);
                }

                //todo: size might not perfectly match that of she designer. (so it might be very different from it in big ellipses?)
                INTERNAL_ShapesDrawHelpers.PrepareEllipse(_canvasDomElement, sizeX, sizeY, sizeX / 2 + xOffsetToApplyBeforeMultiplication + xOffsetToApplyAfterMultiplication, sizeY / 2 + yOffsetToApplyBeforeMultiplication + yOffsetToApplyAfterMultiplication);

                //todo: make sure the parameters below are correct.
                Shape.DrawFillAndStroke(this, "evenodd", xOffsetToApplyAfterMultiplication, yOffsetToApplyAfterMultiplication, xOffsetToApplyAfterMultiplication + sizeX, yOffsetToApplyAfterMultiplication + sizeY, horizontalMultiplicator, verticalMultiplicator, xOffsetToApplyBeforeMultiplication, yOffsetToApplyBeforeMultiplication, shapeActualSize);


                //dynamic context = INTERNAL_HtmlDomManager.Get2dCanvasContext(_canvasDomElement);
                //if (fillValue != null || (fillValue is string && !string.IsNullOrWhiteSpace((string)fillValue)))
                //{
                //    context.fill("evenodd"); //note: remember: always fill before stroke, otherwise the filling will hide the stroke.
                //}
                //if (StrokeThickness > 0 && Stroke != null)
                //{
                //    context.stroke();
                //}
            }
        }
Ejemplo n.º 9
0
        internal override void DefineInCanvas(Path path, object canvasDomElement, double horizontalMultiplicator, double verticalMultiplicator, double xOffsetToApplyBeforeMultiplication, double yOffsetToApplyBeforeMultiplication, double xOffsetToApplyAfterMultiplication, double yOffsetToApplyAfterMultiplication, Size shapeActualSize) //note: we only define the line. Erasing the previous one (if any) and actually drawing the new one should be made directly by the container.
        {
            string strokeAsString = string.Empty;

            //if (path.Stroke == null || path.Stroke is SolidColorBrush) //todo: make sure we want the same behaviour when it is null and when it is a SolidColorBrush (basically, check if null means default value)
            //{
            //    if (path.Stroke != null) //if stroke is null, we want to set it as an empty string, otherwise, it is a SolidColorBrush and we want to get its color.
            //    {
            //        strokeAsString = ((SolidColorBrush)path.Stroke).Color.INTERNAL_ToHtmlString();
            //    }
            //}
            //else
            //{
            //    throw new NotSupportedException("The specified brush is not supported.");
            //}


            INTERNAL_ShapesDrawHelpers.PrepareLine(path._canvasDomElement, StartPoint, EndPoint);

            dynamic context = INTERNAL_HtmlDomManager.Get2dCanvasContext(canvasDomElement);

            context.strokeStyle = strokeAsString; //set the shape's lines color
            context.lineWidth   = path.StrokeThickness + "px";
        }
Ejemplo n.º 10
0
 public override object CreateDomElement(object parentRef, out object domElementWhereToPlaceChildren)
 {
     return(INTERNAL_ShapesDrawHelpers.CreateDomElementForPathAndSimilar(this, parentRef, out _canvasDomElement, out domElementWhereToPlaceChildren));
 }
Ejemplo n.º 11
0
        override internal protected void Redraw()
        {
            if (INTERNAL_VisualTreeManager.IsElementInVisualTree(this))
            {
                double minX = X1;
                double minY = Y1;
                double maxX = X2;
                double maxY = Y2;
                if (X1 > X2)
                {
                    minX = X2;
                    maxX = X1;
                }
                if (Y1 > Y2)
                {
                    minY = Y2;
                    maxY = Y1;
                }

                Size shapeActualSize;
                INTERNAL_ShapesDrawHelpers.PrepareStretch(this, _canvasDomElement, minX, maxX, minY, maxY, Stretch, out shapeActualSize);

                double horizontalMultiplicator;
                double verticalMultiplicator;
                double xOffsetToApplyBeforeMultiplication;
                double yOffsetToApplyBeforeMultiplication;
                double xOffsetToApplyAfterMultiplication;
                double yOffsetToApplyAfterMultiplication;
                INTERNAL_ShapesDrawHelpers.GetMultiplicatorsAndOffsetForStretch(this, StrokeThickness, minX, maxX, minY, maxY, Stretch, shapeActualSize, out horizontalMultiplicator, out verticalMultiplicator, out xOffsetToApplyBeforeMultiplication, out yOffsetToApplyBeforeMultiplication, out xOffsetToApplyAfterMultiplication, out yOffsetToApplyAfterMultiplication, out _marginOffsets);

                ApplyMarginToFixNegativeCoordinates(new Point());

                if (Stretch == Stretch.None)
                {
                    ApplyMarginToFixNegativeCoordinates(_marginOffsets);
                }

                object context = CSHTML5.Interop.ExecuteJavaScriptAsync(@"$0.getContext('2d')", _canvasDomElement); //Note: we do not use INTERNAL_HtmlDomManager.Get2dCanvasContext here because we need to use the result in ExecuteJavaScript, which requires the value to come from a call of ExecuteJavaScript.

                //we remove the previous drawing:
                CSHTML5.Interop.ExecuteJavaScriptAsync("$0.clearRect(0,0, $1, $2)", context, shapeActualSize.Width, shapeActualSize.Height);


                double preparedX1 = (X1 + xOffsetToApplyBeforeMultiplication) * horizontalMultiplicator + xOffsetToApplyAfterMultiplication;
                double preparedX2 = (X2 + xOffsetToApplyBeforeMultiplication) * horizontalMultiplicator + xOffsetToApplyAfterMultiplication;
                double preparedY1 = (Y1 + yOffsetToApplyBeforeMultiplication) * verticalMultiplicator + yOffsetToApplyAfterMultiplication;
                double preparedY2 = (Y2 + yOffsetToApplyBeforeMultiplication) * verticalMultiplicator + yOffsetToApplyAfterMultiplication;

                //todo: if possible, manage strokeStyle and lineWidth in their respective methods (Stroke_Changed and StrokeThickness_Changed) then use context.save() and context.restore() (can't get it to work yet).
                double opacity     = Stroke == null ? 1 : Stroke.Opacity;
                object strokeValue = GetHtmlBrush(this, Stroke, opacity, minX, minY, maxX, maxY, horizontalMultiplicator, verticalMultiplicator, xOffsetToApplyBeforeMultiplication, yOffsetToApplyBeforeMultiplication, shapeActualSize);

                //we set the StrokeDashArray:
                if (strokeValue != null && StrokeThickness > 0)
                {
                    double thickness = StrokeThickness;
                    CSHTML5.Interop.ExecuteJavaScriptAsync(@"
$0.strokeStyle = $1", context, strokeValue);
                    CSHTML5.Interop.ExecuteJavaScriptAsync(@"
$0.lineWidth = $1", context, StrokeThickness);
                    if (StrokeDashArray != null)
                    {
                        if (CSHTML5.Interop.IsRunningInTheSimulator)
                        {
                            //todo: put a message saying that it doesn't work in certain browsers (maybe use a static boolean to put that message only once)
                        }
                        else
                        {
                            object options = CSHTML5.Interop.ExecuteJavaScript(@"new Array()");
                            for (int i = 0; i < StrokeDashArray.Count; ++i)
                            {
                                CSHTML5.Interop.ExecuteJavaScriptAsync(@"
$0[$1] = $2;
", options, i, StrokeDashArray[i] * thickness);
                            }

                            CSHTML5.Interop.ExecuteJavaScriptAsync(@"
if ($0.setLineDash)
    $0.setLineDash($1)", context, options);
                            //context.setLineDash(str + "]");
                        }
                    }
                }


                INTERNAL_ShapesDrawHelpers.PrepareLine(_canvasDomElement, new Point(preparedX1, preparedY1), new Point(preparedX2, preparedY2));

                if (strokeValue != null)
                {
                    CSHTML5.Interop.ExecuteJavaScriptAsync(@"$0.strokeStyle = $1", context, strokeValue);
                }

                //context.strokeStyle = strokeAsString; //set the shape's lines color
                CSHTML5.Interop.ExecuteJavaScriptAsync("$0.lineWidth= $1", context, StrokeThickness.ToString());
                //context.lineWidth = StrokeThickness.ToString();
                if (Stroke != null && StrokeThickness > 0)
                {
                    CSHTML5.Interop.ExecuteJavaScriptAsync("$0.stroke()", context); //draw the line
                    //context.stroke(); //draw the line
                }
            }
        }
Ejemplo n.º 12
0
        override internal protected void Redraw()
        {
            if (Points.Count < 2)
            {
                // It is fine to have 0 or 1 points but nothing to draw in that case.
                return;
            }

            double minX = Points[0].X;
            double minY = Points[0].Y;
            double maxX = Points[0].X;
            double maxY = Points[0].Y;

            foreach (var p in Points)
            {
                if (p.X < minX)
                {
                    minX = p.X;
                }
                if (p.Y < minY)
                {
                    minY = p.Y;
                }
                if (p.X > maxX)
                {
                    maxX = p.X;
                }
                if (p.Y > maxY)
                {
                    maxY = p.Y;
                }
            }

            Size shapeActualSize;

            INTERNAL_ShapesDrawHelpers.PrepareStretch(this, _canvasDomElement, minX, maxX, minY, maxY, Stretch, out shapeActualSize);

            double horizontalMultiplicator;
            double verticalMultiplicator;
            double xOffsetToApplyBeforeMultiplication;
            double yOffsetToApplyBeforeMultiplication;
            double xOffsetToApplyAfterMultiplication;
            double yOffsetToApplyAfterMultiplication;

            INTERNAL_ShapesDrawHelpers.GetMultiplicatorsAndOffsetForStretch(this, StrokeThickness, minX, maxX, minY, maxY, Stretch, shapeActualSize, out horizontalMultiplicator, out verticalMultiplicator, out xOffsetToApplyBeforeMultiplication, out yOffsetToApplyBeforeMultiplication, out xOffsetToApplyAfterMultiplication, out yOffsetToApplyAfterMultiplication, out _marginOffsets);

            ApplyMarginToFixNegativeCoordinates(new Point());

            if (Stretch == Stretch.None)
            {
                ApplyMarginToFixNegativeCoordinates(_marginOffsets);
            }

            object context = CSHTML5.Interop.ExecuteJavaScriptAsync(@"$0.getContext('2d')", _canvasDomElement);

            //we remove the previous drawing:
            CSHTML5.Interop.ExecuteJavaScriptAsync("$0.clearRect(0,0, $1, $2)", context, shapeActualSize.Width, shapeActualSize.Height);

            double opacity     = Stroke == null ? 1 : Stroke.Opacity;
            object strokeValue = GetHtmlBrush(this, context, Stroke, opacity, minX, minY, maxX, maxY, horizontalMultiplicator, verticalMultiplicator, xOffsetToApplyBeforeMultiplication, yOffsetToApplyBeforeMultiplication, shapeActualSize);
            object fillValue   = GetHtmlBrush(this, context, Fill, opacity, minX, minY, maxX, maxY, horizontalMultiplicator, verticalMultiplicator, xOffsetToApplyBeforeMultiplication, yOffsetToApplyBeforeMultiplication, shapeActualSize);

            if (fillValue != null)
            {
                CSHTML5.Interop.ExecuteJavaScriptAsync(@"$0.fillStyle = $1", context, fillValue);
            }
            else
            {
                // If fillValue is not set it will be black.
                CSHTML5.Interop.ExecuteJavaScriptAsync(@"$0.fillStyle = 'transparent'", context);
            }

            INTERNAL_ShapesDrawHelpers.PrepareLines(_canvasDomElement, Points, StrokeThickness, true);

            if (strokeValue != null)
            {
                CSHTML5.Interop.ExecuteJavaScriptAsync(@"$0.strokeStyle = $1", context, strokeValue);
            }

            CSHTML5.Interop.ExecuteJavaScriptAsync("$0.lineWidth = $1", context, StrokeThickness.ToString());
            if (Stroke != null && StrokeThickness > 0)
            {
                CSHTML5.Interop.ExecuteJavaScriptAsync("$0.stroke()", context);
            }
        }
Ejemplo n.º 13
0
        protected internal override void Redraw()
        {
            if (INTERNAL_VisualTreeManager.IsElementInVisualTree(this))
            {
                if (Data != null)
                {
                    double minX = double.MaxValue;
                    double minY = double.MaxValue;
                    double maxX = double.MinValue;
                    double maxY = double.MinValue;
                    if (Data is PathGeometry pathGeometry)
                    {
                        if (pathGeometry.Figures == null || pathGeometry.Figures.Count == 0)
                        {
                            return;
                        }
                    }
                    Data.GetMinMaxXY(ref minX, ref maxX, ref minY, ref maxY);


                    // If the rare case that the shape is so big (in terms of coordinates of the points, prior to any transform) that it exceeds the size of Int32 (for an example, refer to the charts in "Client_LD"), we need to make it smaller so that the HTML <canvas> control is able to draw it. We compensate by applying a ScaleTransform to the Data.Tranform:
                    Transform originalTransform = Data.Transform;
                    Transform actualTransform   = originalTransform;
                    double    int32FactorX      = 1d;
                    double    int32FactorY      = 1d;
                    INTERNAL_ShapesDrawHelpers.FixCoordinatesGreaterThanInt32MaxValue(minX,
                                                                                      minY,
                                                                                      maxX,
                                                                                      maxY,
                                                                                      ref actualTransform,
                                                                                      ref int32FactorX,
                                                                                      ref int32FactorY);

                    // Apply the transform to the min/max:
                    if (originalTransform != null)
                    {
                        // todo: appying the transform to the min/max is accurate only for Scale and
                        // translate transforms. For other transforms such as rotate, the min/max is incorrect.
                        // For a correct result, we should apply the transform to each point of the figure.

                        Point tmp1 = originalTransform.Transform(new Point(minX, minY));
                        Point tmp2 = originalTransform.Transform(new Point(maxX, maxY));
                        minX = tmp1._x;
                        minY = tmp1._y;
                        maxX = tmp2._x;
                        maxY = tmp2._y;
                    }

                    Size shapeActualSize;
                    INTERNAL_ShapesDrawHelpers.PrepareStretch(this, _canvasDomElement, minX, maxX, minY, maxY, Stretch, out shapeActualSize);

                    double horizontalMultiplicator;
                    double verticalMultiplicator;
                    double xOffsetToApplyBeforeMultiplication;
                    double yOffsetToApplyBeforeMultiplication;
                    double xOffsetToApplyAfterMultiplication;
                    double yOffsetToApplyAfterMultiplication;
                    double strokeThickness = Stroke == null ? 0d : StrokeThickness;
                    INTERNAL_ShapesDrawHelpers.GetMultiplicatorsAndOffsetForStretch(this,
                                                                                    strokeThickness,
                                                                                    minX,
                                                                                    maxX,
                                                                                    minY,
                                                                                    maxY,
                                                                                    Stretch,
                                                                                    shapeActualSize,
                                                                                    out horizontalMultiplicator,
                                                                                    out verticalMultiplicator,
                                                                                    out xOffsetToApplyBeforeMultiplication,
                                                                                    out yOffsetToApplyBeforeMultiplication,
                                                                                    out xOffsetToApplyAfterMultiplication,
                                                                                    out yOffsetToApplyAfterMultiplication,
                                                                                    out _marginOffsets);

                    ApplyMarginToFixNegativeCoordinates(new Point());
                    if (Stretch == Stretch.None)
                    {
                        ApplyMarginToFixNegativeCoordinates(_marginOffsets);
                    }

                    // A call to "context.beginPath" is required on IE and Edge for the figures to be drawn properly (cf. ZenDesk #971):
                    CSHTML5.Interop.ExecuteJavaScriptAsync(@"$0.getContext('2d').beginPath()", _canvasDomElement);

                    var context = INTERNAL_HtmlDomManager.Get2dCanvasContext(_canvasDomElement);

                    // We want the Transform to be applied only while drawing with the "DefineInCanvas" method, not when applying the stroke and fill, so that the Stroke Thickness does not get affected by the transform (like in Silverlight). To do so, we save the current canvas context, then apply the transform, then draw, and then restore to the original state before applying the stroke:
                    context.save();

                    // Apply the transform if any:
                    INTERNAL_ShapesDrawHelpers.ApplyTransformToCanvas(actualTransform, _canvasDomElement);

                    //problem here: the shape seems to be overall smaller than intended due to the edges of the path not being sharp?
                    Data.DefineInCanvas(this,
                                        _canvasDomElement,
                                        horizontalMultiplicator * int32FactorX,
                                        verticalMultiplicator * int32FactorY,
                                        xOffsetToApplyBeforeMultiplication,
                                        yOffsetToApplyBeforeMultiplication,
                                        xOffsetToApplyAfterMultiplication,
                                        yOffsetToApplyAfterMultiplication,
                                        shapeActualSize);

                    // Read the comment near the "save()" above to know what this "restore" is here for.
                    context.restore();

                    Shape.DrawFillAndStroke(this,
                                            Data.GetFillRuleAsString(),
                                            minX,
                                            minY,
                                            maxX,
                                            maxY,
                                            horizontalMultiplicator, // Note: here we do not multiply by "int32Factor" because we do not want to affect the tickness
                                            verticalMultiplicator,   // Note: here we do not multiply by "int32Factor" because we do not want to affect the tickness
                                            xOffsetToApplyBeforeMultiplication,
                                            yOffsetToApplyBeforeMultiplication,
                                            shapeActualSize);
                }
            }
        }