private async Task TraceLineAsync(double x, double y) { if (_lastX is not null && _lastY is not null) { await _canvas2DContext.SetLineWidthAsync(LineWidth); await _canvas2DContext.BeginPathAsync(); await _canvas2DContext.MoveToAsync(_lastX.Value, _lastY.Value); await _canvas2DContext.LineToAsync(x, y); await _canvas2DContext.StrokeAsync(); } await _canvas2DContext.BeginPathAsync(); await _canvas2DContext.ArcAsync(x, y, PointSize, 0, Radius, anticlockwise : true); await _canvas2DContext.ClosePathAsync(); await _canvas2DContext.FillAsync(); _lastX = x; _lastY = y; }
internal async Task SetLineWidth(float width) { if (!this.actualLineWidth.Equals(width)) { await ctx.SetLineWidthAsync(width); this.actualLineWidth = width; } }
public async ValueTask Render(GameContext game, Canvas2DContext context) { var tmpW = context.LineWidth; var tmpS = context.StrokeStyle; await context.BeginPathAsync(); await context.SetStrokeStyleAsync("rgb(255,255,0)"); await context.SetLineWidthAsync(3); await context.StrokeRectAsync(_bounds.X, _bounds.Y, _bounds.Width, _bounds.Height); await context.SetStrokeStyleAsync(tmpS); await context.SetLineWidthAsync(tmpW); }
public async Task Draw(bool vertical) { await ResetCanvas(); if (vertical) { HexGridController.CanvasHeight = Height; HexGridController.CanvasWidth = Width; } else { HexGridController.CanvasHeight = Width; HexGridController.CanvasWidth = Height; } HexGridController.Diameter = Diameter; await Canvas2DContext.SetLineWidthAsync(LineThinkness); await Canvas2DContext.SetStrokeStyleAsync(LineColor); for (var row = 0; row < HexGridController.Rows; row++) { for (var col = 0; col < HexGridController.Columns; col++) { await Canvas2DContext.BeginPathAsync(); var point = HexGridController.GetCoordinates(row, col)[6]; if (vertical) { await Canvas2DContext.MoveToAsync(point.X, point.Y); } else { await Canvas2DContext.MoveToAsync(point.Y, point.X); } for (var pos = 1; pos <= 6; pos++) { point = HexGridController.GetCoordinates(row, col)[pos]; if (vertical) { await Canvas2DContext.LineToAsync(point.X, point.Y); } else { await Canvas2DContext.LineToAsync(point.Y, point.X); } } await Canvas2DContext.StrokeAsync(); } } }
protected override async Task OnAfterRenderAsync(bool firstRender) { _stopwatch.Start(); if (firstRender) { _ctx = await _canvas.CreateCanvas2DAsync(); await _ctx.SetFillStyleAsync("green"); await _ctx.SetStrokeStyleAsync("#777777"); await _ctx.SetLineWidthAsync(1); } await _ctx.ClearRectAsync(0, 0, GridSize *Scale, GridSize *Scale); await _ctx.BeginPathAsync(); for (int i = 0; i <= Scale; ++i) { await _ctx.MoveToAsync(GridSize *i, 0); await _ctx.LineToAsync(GridSize *i, GridSize *Scale); await _ctx.MoveToAsync(0, GridSize *i); await _ctx.LineToAsync(GridSize *Scale, GridSize *i); } for (int y = 0; y < Scale; ++y) { for (int x = 0; x < Scale; ++x) { if (_game.GetState(x, y)) { await _ctx.RectAsync(x *GridSize + 1, y *GridSize + 1, GridSize - 2, GridSize - 2); } } } await _ctx.StrokeAsync(); await _ctx.FillAsync(); _stopwatch.Stop(); RenderCostInMilliSecond = _stopwatch.ElapsedMilliseconds; _stopwatch.Reset(); }
public async ValueTask Render(Canvas2DContext context) { await context.DrawImageAsync(Sprite.SpriteSheet, _transform.Position.X, _transform.Position.Y, Sprite.Size.Width, Sprite.Size.Height); if (DrawBoundingBox) { await context.BeginPathAsync(); await context.SetStrokeStyleAsync($"rgb(255,255,0)"); await context.SetLineWidthAsync(3); await context.StrokeRectAsync(_transform.BoundingBox.X, _transform.BoundingBox.Y, _transform.BoundingBox.Width, _transform.BoundingBox.Height); } }
public async ValueTask DrawLine(Vector2 from, Vector2 to, Color color) { var f = from + _origin.ToVector2(); var t = to + _origin.ToVector2(); await _canvas2DContext.BeginPathAsync(); await _canvas2DContext.SetStrokeStyleAsync($"rgb({color.R},{color.G},{color.B})"); await _canvas2DContext.SetLineWidthAsync(3); await _canvas2DContext.SetLineCapAsync(LineCap.Round); await _canvas2DContext.MoveToAsync(f.X, f.Y); await _canvas2DContext.LineToAsync(t.X, t.Y); await _canvas2DContext.StrokeAsync(); }
/// <summary> /// 画一条圆内线 /// </summary> /// <param name="count">个数</param> /// <param name="index">索引</param> /// <param name="length">长度</param> /// <param name="width">宽度</param> /// <param name="color">颜色</param> /// <param name="radius">半径</param> /// <returns></returns> private async Task DrawOneLineByCicleAsync(int count, int index, int length, int width, string color, double radius) { int interval = 360 / count; await _context.SaveAsync(); var angle = index * interval * Math.PI / 180; await _context.TranslateAsync(radius, radius); await _context.BeginPathAsync(); await _context.SetStrokeStyleAsync(color); await _context.SetLineWidthAsync(width); await _context.MoveToAsync(radius *Math.Cos(angle), radius *Math.Sin(angle)); await _context.LineToAsync((radius - length) *Math.Cos(angle), (radius - length) *Math.Sin(angle)); await _context.StrokeAsync(); await _context.RestoreAsync(); }
public async ValueTask InitAsync(Canvas2DContext canvas) { await canvas.SetLineWidthAsync(1.0f); _canvas = new Canvas(canvas); }