{"id":26280066,"url":"https://github.com/kristofferstrube/distributeddrawing","last_synced_at":"2025-05-07T03:04:20.178Z","repository":{"id":109456377,"uuid":"273249603","full_name":"KristofferStrube/DistributedDrawing","owner":"KristofferStrube","description":"A distributed drawing tool written in Blazor Wasm that uses an external SignalR Hub to sync drawings between clients.","archived":false,"fork":false,"pushed_at":"2023-12-27T03:23:45.000Z","size":10470,"stargazers_count":27,"open_issues_count":0,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-07T03:04:09.248Z","etag":null,"topics":["blazor","blazor-webassembly","collaboration","creative","draw","github-actions","github-pages","signalr","svg","wasm","webassembly"],"latest_commit_sha":null,"homepage":"","language":"HTML","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/KristofferStrube.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2020-06-18T13:44:12.000Z","updated_at":"2025-02-22T11:45:04.000Z","dependencies_parsed_at":"2023-03-13T14:11:26.891Z","dependency_job_id":null,"html_url":"https://github.com/KristofferStrube/DistributedDrawing","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KristofferStrube%2FDistributedDrawing","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KristofferStrube%2FDistributedDrawing/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KristofferStrube%2FDistributedDrawing/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KristofferStrube%2FDistributedDrawing/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/KristofferStrube","download_url":"https://codeload.github.com/KristofferStrube/DistributedDrawing/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252804206,"owners_count":21806769,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["blazor","blazor-webassembly","collaboration","creative","draw","github-actions","github-pages","signalr","svg","wasm","webassembly"],"created_at":"2025-03-14T14:18:29.999Z","updated_at":"2025-05-07T03:04:20.151Z","avatar_url":"https://github.com/KristofferStrube.png","language":"HTML","funding_links":[],"categories":[],"sub_categories":[],"readme":"# DistributedDrawing\nA  distributed drawing tool that uses an external SignalR Hub to sync drawings between clients. It also saves the drawings automatically.\n\n## Demo\nIt can be demoed at [kristofferstrube.github.io/DistributedDrawing](https://kristofferstrube.github.io/DistributedDrawing/)\n\n## Frontend\nI have experimented with a couple of different ways to draw the lines.\n\nThe first approach was to add a `@foreach` loop that renders `\u003cLine /\u003e` tags in the SVG using razor syntax, but that caused a big delay as all previous lines had to be drawn each time a single line was added\n\nNext I looked at using the [Canvas](https://github.com/BlazorExtensions/Canvas) package by the people behind [BlazorExtensions](https://github.com/BlazorExtensions). This used a canvas instead to render lines which is very effective compared to SVG elements. I had a working version of this, but I did not continue with this as I wanted to be able to potentially manipulate the drawn elements in the future without having to learn how to use graphics libraries.\n\nNext I looked at using JSInterop to add/clear `\u003cLine /\u003e` tags from JavaScript, so that I knew that Blazor would not handle when these are rendered.\n\nI have later found that you can do this smarter by creating a component for a SVG `line` like I helped doing in [PetaBridge](https://github.com/petabridge)'s project [DrawTogether.NET](https://github.com/petabridge/DrawTogether.NET/blob/dev/src/DrawTogether.UI/Server/Components/Curve.razor#L16).\n\n## Backend\nThe backend is not part of this repo, but it's not the most complex if you are familiar with SignalR.\n\n```csharp\npublic class StaticStorage\n{\n    public static IList\u003cLine\u003e draws = new List\u003cLine\u003e();\n    public static IDictionary\u003cstring, string\u003e users = new Dictionary\u003cstring, string\u003e();\n}\n    \npublic class DrawHub : Hub\n{\n    private readonly string drawingPath = \"drawing.draw\";\n\n    public override async Task OnConnectedAsync()\n    {\n        if (StaticStorage.draws.Count() == 0)\n        {\n            using (FileStream fs = File.OpenRead(drawingPath))\n            {\n                StaticStorage.draws = await JsonSerializer.DeserializeAsync\u003cIList\u003cLine\u003e\u003e(fs);\n            }\n        }\n        await base.OnConnectedAsync();\n    }\n\n    public async Task\u003cint\u003e CountLines()\n    {\n        return StaticStorage.draws.Count();\n    }\n\n    public async IAsyncEnumerable\u003cLine\u003e StartLines([EnumeratorCancellation]\n    CancellationToken cancellationToken)\n    {\n        foreach (Line line in StaticStorage.draws)\n        {\n            cancellationToken.ThrowIfCancellationRequested();\n\n            yield return line;\n        }\n    }\n\n    public async Task\u003cList\u003cstring\u003e\u003e StartUsers(string user)\n    {\n        StaticStorage.users.Add(Context.ConnectionId, user);\n        await Clients.Others.SendAsync(\"ReceiveUsers\", StaticStorage.users.Values.ToList());\n        return StaticStorage.users.Values.ToList();\n    }\n\n    public override async Task OnDisconnectedAsync(Exception exception)\n    {\n        StaticStorage.users.Remove(Context.ConnectionId);\n        await Clients.Others.SendAsync(\"ReceiveUsers\", StaticStorage.users.Values.ToList());\n        using (FileStream fs = File.Create(drawingPath))\n        {\n            await JsonSerializer.SerializeAsync(fs, StaticStorage.draws);\n        }\n        await base.OnDisconnectedAsync(exception);\n    }\n\n    public async Task Draw(double prevX, double prevY, double currX, double currY, string color, float lineWidth)\n    {\n        await Clients.Others.SendAsync(\"ReceiveDraw\", prevX, prevY, currX, currY, color, lineWidth);\n        StaticStorage.draws.Add(new Line(prevX, prevY, currX, currY, color, lineWidth));\n    }\n\n    public async Task Clear()\n    {\n        await Clients.All.SendAsync(\"ReceiveClear\");\n        StaticStorage.draws.Clear();\n    }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkristofferstrube%2Fdistributeddrawing","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkristofferstrube%2Fdistributeddrawing","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkristofferstrube%2Fdistributeddrawing/lists"}