예제 #1
0
        public async Task Draw(int width, int height, Canvas2DContext canvas)
        {
            var index = 0;

            for (var y = 0; y < _data.Height; ++y)
            {
                for (var x = 0; x < _data.Width; ++x, ++index)
                {
                    var val = _data.Density[index];
                    if (val == 0)
                    {
                        continue;
                    }

                    //todo: highlight the visible rectangle.
                    bool inside = true;

                    var clr = ChooseColor(val);
                    if (!inside)
                    {
                        DarkenColor(clr);
                    }
                    await canvas.SetFillStyleAsync(ColorToString(clr));

                    await canvas.RectAsync(x, y, 1, 1);
                }
            }
        }
        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();
        }