Exemplo n.º 1
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";
        }
Exemplo n.º 2
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";
        }
Exemplo n.º 3
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
                }
            }
        }