Ejemplo n.º 1
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.º 2
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.º 3
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.º 4
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.º 5
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.º 6
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);
                }
            }
        }