<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Zico</title>
    <description>The latest articles on DEV Community by Zico (@zico_33df1fa5f7c3cccf27f5).</description>
    <link>https://dev.to/zico_33df1fa5f7c3cccf27f5</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4215207%2F1e02de3d-9fe4-4e9d-80dc-47bebac126ec.png</url>
      <title>DEV Community: Zico</title>
      <link>https://dev.to/zico_33df1fa5f7c3cccf27f5</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/zico_33df1fa5f7c3cccf27f5"/>
    <language>en</language>
    <item>
      <title>Building Trick or Take: Open-Source Multiplayer Web Game with Vanilla Canvas 2D, Cloudflare Workers &amp; Agora RTC</title>
      <dc:creator>Zico</dc:creator>
      <pubDate>Thu, 29 Oct 2026 13:02:31 +0000</pubDate>
      <link>https://dev.to/zico_33df1fa5f7c3cccf27f5/building-trick-or-take-open-source-multiplayer-web-game-with-vanilla-canvas-2d-cloudflare-workers-2k2p</link>
      <guid>https://dev.to/zico_33df1fa5f7c3cccf27f5/building-trick-or-take-open-source-multiplayer-web-game-with-vanilla-canvas-2d-cloudflare-workers-2k2p</guid>
      <description>&lt;p&gt;In the spirit of Halloween, I set out to build an edge-native, zero-framework multiplayer browser game called &lt;strong&gt;&lt;a href="https://trickortake.com" rel="noopener noreferrer"&gt;Trick or Take&lt;/a&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The game is completely open source under the MIT license on GitHub:&lt;br&gt;
👉 &lt;strong&gt;GitHub Repository&lt;/strong&gt;: &lt;a href="https://github.com/zicojiao/trick-or-take" rel="noopener noreferrer"&gt;https://github.com/zicojiao/trick-or-take&lt;/a&gt;&lt;br&gt;
🎮 &lt;strong&gt;Play Live&lt;/strong&gt;: &lt;a href="https://trickortake.com" rel="noopener noreferrer"&gt;https://trickortake.com&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is a deep-dive breakdown into the architectural decisions, client-side rendering pipeline, edge-based serverless state synchronization, and low-latency voice integration.&lt;/p&gt;


&lt;h2&gt;
  
  
  1. The Game Loop: "Take the Candy. Don't Get Caught."
&lt;/h2&gt;

&lt;p&gt;The core gameplay loop is deceptively simple and high-tension:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Lily is busy at the kitchen counter, and a candy bowl sits right behind her.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hold&lt;/strong&gt; (via Spacebar, mouse click, or mobile touch) to snatch candy treats. The longer you maintain your hold, the faster your snatch rate accelerates.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Release&lt;/strong&gt; to instantly assume an innocent posture.&lt;/li&gt;
&lt;li&gt;Lily turns around unpredictably every 0.90 to 1.35 seconds, with a brief 0.55s–1.90s warning jitter.&lt;/li&gt;
&lt;li&gt;If caught holding while Lily looks back: you get a broom bonk, a stun penalty, and your unbanked candy drops straight to zero!&lt;/li&gt;
&lt;li&gt;Rounds last 60 seconds. Playable solo or in multiplayer rooms (2–6 players) with optional live voice chat.&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  2. Zero-Framework Vanilla Canvas 2D Engine
&lt;/h2&gt;

&lt;p&gt;Modern frontend stacks often default to heavy component libraries or monolithic game engines. For &lt;strong&gt;Trick or Take&lt;/strong&gt;, the objective was maximum responsiveness and sub-second cold starts across low-end mobile devices and desktops alike.&lt;/p&gt;
&lt;h3&gt;
  
  
  Why Vanilla Canvas 2D?
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Ultra-Lightweight Footprint&lt;/strong&gt;: The entire client codebase compiles to less than &lt;strong&gt;45 KB&lt;/strong&gt; of gzipped JavaScript.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Instant Cold Start&lt;/strong&gt;: Loads and renders within &lt;strong&gt;50 ms&lt;/strong&gt; without hydration lag or virtual DOM overhead.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rock-Solid 60 FPS&lt;/strong&gt;: Pure &lt;code&gt;requestAnimationFrame&lt;/code&gt; loop driving standard HTML5 2D context drawing operations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Asset-Free Audio Synthesizer&lt;/strong&gt;: Instead of loading megabytes of MP3/OGG sound files, all background tunes and broom-bonk SFX are procedurally synthesized using the Web Audio API (&lt;code&gt;AudioContext&lt;/code&gt;, &lt;code&gt;OscillatorNode&lt;/code&gt;, and &lt;code&gt;GainNode&lt;/code&gt;).&lt;/li&gt;
&lt;/ol&gt;


&lt;h2&gt;
  
  
  3. Serverless Edge Architecture (Cloudflare Workers + D1)
&lt;/h2&gt;

&lt;p&gt;Rather than maintaining dedicated GameLift or containerized WebSocket servers, the backend runs entirely on Cloudflare's serverless edge infrastructure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[Browser: Canvas + Web Audio] 
       │ 
       ▼ HTTPS / JSON
[Cloudflare Workers Edge API] 
       │ 
       ├─► [Cloudflare D1: SQLite Room State]
       ├─► [HTMLRewriter: 12-Language SSR Localization]
       └─► [Agora RTC &amp;amp; RTM Token Minting]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Optimistic Concurrency with Monotonic Versioning
&lt;/h3&gt;

&lt;p&gt;Multiplayer rooms (2 to 6 players) share state via Cloudflare D1. To avoid race conditions when multiple players press "Ready" or report state updates simultaneously without the cost of distributed locks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each room record maintains a monotonic &lt;code&gt;version&lt;/code&gt; counter.&lt;/li&gt;
&lt;li&gt;State updates use conditional SQL queries:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;rooms&lt;/span&gt; 
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="k"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;version&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; 
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="k"&gt;version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;If a write conflict occurs, the Worker applies bounded exponential backoff with jitter and retries.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  4. Ephemeral Live Voice Chat (Agora RTC)
&lt;/h2&gt;

&lt;p&gt;Multiplayer parties are ten times more hilarious when players can yell warnings to each other before Lily turns!&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Edge Token Minting&lt;/strong&gt;: Cloudflare Workers generate short-lived Agora RTC authentication tokens on demand for authorized room members. The primary Agora App Certificate is securely isolated in Workers secrets and never touches browser bundles.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Microphone Handling&lt;/strong&gt;: When players join voice, the browser establishes peer audio streams via the Agora Web SDK, with automated reconnection and graceful degradations if a player has unstable network connectivity.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  5. Anti-Cheat &amp;amp; Verified Leaderboard Runs
&lt;/h2&gt;

&lt;p&gt;In casual browser games, client-side score manipulation is notoriously rampant. Trick or Take implements a dual-layer verification model:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Deterministic PRNG Seed&lt;/strong&gt;: Each round generates a cryptographic seed governing Lily's turn timestamps and warning durations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Input-Trace Replay&lt;/strong&gt;: Solo runs do not submit arbitrary integer scores. Instead, the browser logs a micro-trace of timestamps representing user press and release actions. The Cloudflare Worker re-simulates the run against the round's seed. If a player was holding during an active turn or exceeded physical limits, the score is rejected.&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  6. Open Source &amp;amp; Contributions
&lt;/h2&gt;

&lt;p&gt;The entire project is licensed under the &lt;strong&gt;MIT License&lt;/strong&gt; and welcoming contributions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;💻 &lt;strong&gt;GitHub Repository&lt;/strong&gt;: &lt;a href="https://github.com/zicojiao/trick-or-take" rel="noopener noreferrer"&gt;https://github.com/zicojiao/trick-or-take&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;🎮 &lt;strong&gt;Live Game&lt;/strong&gt;: &lt;a href="https://trickortake.com" rel="noopener noreferrer"&gt;https://trickortake.com&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Whether you're interested in browser gamedev, edge-native architectures, or just want to play a fun Halloween heist with friends, feel free to try it out, clone the repo, or open a pull request!&lt;/p&gt;

</description>
      <category>gamedev</category>
      <category>webdev</category>
      <category>javascript</category>
      <category>opensource</category>
    </item>
    <item>
      <title>Building Trick or Take: An Open-Source Multiplayer Halloween Web Game on Cloudflare Workers &amp; Agora RTC</title>
      <dc:creator>Zico</dc:creator>
      <pubDate>Thu, 29 Oct 2026 11:48:26 +0000</pubDate>
      <link>https://dev.to/zico_33df1fa5f7c3cccf27f5/building-trick-or-take-an-open-source-multiplayer-halloween-web-game-on-cloudflare-workers-agora-41ca</link>
      <guid>https://dev.to/zico_33df1fa5f7c3cccf27f5/building-trick-or-take-an-open-source-multiplayer-halloween-web-game-on-cloudflare-workers-agora-41ca</guid>
      <description>&lt;h1&gt;
  
  
  Building Trick or Take: An Open-Source Multiplayer Halloween Web Game on Cloudflare Workers &amp;amp; Agora RTC
&lt;/h1&gt;

&lt;p&gt;Halloween is the perfect season for fast-paced, spooky multiplayer fun. To celebrate, we built and open-sourced &lt;strong&gt;&lt;a href="https://trickortake.com" rel="noopener noreferrer"&gt;Trick or Take&lt;/a&gt;&lt;/strong&gt;—a real-time multiplayer browser party game where up to 6 players enter a haunted mansion, collect candy corn, cast spectral curses, and steal candy from rival players in frantic 60-second rounds.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;🎮 &lt;strong&gt;Play Live&lt;/strong&gt;: &lt;a href="https://trickortake.com" rel="noopener noreferrer"&gt;https://trickortake.com&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;🐙 &lt;strong&gt;GitHub Repository&lt;/strong&gt;: &lt;a href="https://github.com/zicojiao/trick-or-take" rel="noopener noreferrer"&gt;https://github.com/zicojiao/trick-or-take&lt;/a&gt; (MIT License)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this post, I want to break down the architectural choices, edge-native backend design, real-time voice integration, and client-side rendering pipeline that power Trick or Take.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. Why Zero-Framework Vanilla Canvas 2D?
&lt;/h2&gt;

&lt;p&gt;Modern web game development often jumps directly into heavyweight engines like PixiJS, Phaser, Three.js, or Babylon.js. While these frameworks are exceptional for complex scenes, they introduce hefty bundle sizes (often 500KB - 2MB+) and significant runtime overhead.&lt;/p&gt;

&lt;p&gt;For &lt;strong&gt;&lt;a href="https://trickortake.com" rel="noopener noreferrer"&gt;Trick or Take&lt;/a&gt;&lt;/strong&gt;, our core design goal was &lt;strong&gt;instantaneous cold-start playability (&amp;lt;50ms initial render)&lt;/strong&gt; directly in any mobile or desktop browser without splash screens or asset-loading bars.&lt;/p&gt;

&lt;h3&gt;
  
  
  Pure HTML5 Canvas 2D Pipeline
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Fixed-Resolution Scaling&lt;/strong&gt;: A virtual resolution of 800x600 dynamically scaled with pixel-ratio awareness via &lt;code&gt;devicePixelRatio&lt;/code&gt; to prevent blurriness on Retina displays.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Particle &amp;amp; Floating Score Engine&lt;/strong&gt;: Lightweight object-pooled particle emitter handling dust motes, pumpkin lantern glows, candy burst animations, and floating score popups (+100, -50).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Smooth Spatial Interpolation&lt;/strong&gt;: Client-side linear interpolation (&lt;code&gt;lerp&lt;/code&gt;) across tick states ensures silky 60fps animations even across fluctuating network latencies.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Micro-Bundle Size&lt;/strong&gt;: The entire front-end client—including sprites, UI overlays, audio synthesizers, and multiplayer networking—weighs &lt;strong&gt;under 45KB uncompressed&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  2. Serverless Multiplayer at the Edge: Cloudflare Workers + D1
&lt;/h2&gt;

&lt;p&gt;Traditional multiplayer game backends rely on long-running stateful Node.js/Go WebSockets servers hosted on centralized VPS instances. This architecture introduces geographic latency penalties and idle hosting costs.&lt;/p&gt;

&lt;p&gt;Instead, &lt;strong&gt;&lt;a href="https://trickortake.com" rel="noopener noreferrer"&gt;Trick or Take&lt;/a&gt;&lt;/strong&gt; is engineered natively on &lt;strong&gt;Cloudflare Workers&lt;/strong&gt; and &lt;strong&gt;Cloudflare D1 (Serverless SQLite)&lt;/strong&gt; deployed across 300+ global edge locations.&lt;/p&gt;

&lt;h3&gt;
  
  
  Optimistic Concurrency &amp;amp; Versioning
&lt;/h3&gt;

&lt;p&gt;When multiple players simultaneously move, pick up candy, or activate abilities like the &lt;em&gt;Ghost Walk&lt;/em&gt; or &lt;em&gt;Jack-o'-Lantern Shield&lt;/em&gt;, race conditions can arise. &lt;/p&gt;

&lt;p&gt;Trick or Take solves this with:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Room State Versioning&lt;/strong&gt;: Every room snapshot maintains a monotonic &lt;code&gt;version&lt;/code&gt; counter.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Atomic SQLite Mutations&lt;/strong&gt;: State transitions (e.g. claiming a candy pile) execute as atomic D1 transactions verifying the current state version before applying updates.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Optimistic Client Prediction&lt;/strong&gt;: The client locally applies movement and interactions immediately, reconciling against edge state confirmations on the next tick.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Edge room update transaction logic&lt;/span&gt;
&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;prepare&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`
  UPDATE rooms 
  SET state = ?1, version = version + 1, updated_at = ?2
  WHERE room_id = ?3 AND version = ?4
`&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;serializedState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nx"&gt;roomId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;clientVersion&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  3. In-Browser Live Spatial Voice Chat via Agora RTC
&lt;/h2&gt;

&lt;p&gt;Multiplayer party games are infinitely more entertaining with voice chatter. Rather than forcing players to join external Discord calls, Trick or Take embeds &lt;strong&gt;real-time, low-latency in-browser voice rooms powered by Agora RTC Web SDK&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Architecture
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Ephemeral Token Minting&lt;/strong&gt;: When a player joins a lobby, the Cloudflare Worker issues a short-lived Agora RTC token scoped to the specific &lt;code&gt;roomId&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Audio-Only Profiles&lt;/strong&gt;: Optimized for low-bandwidth voice streaming (&lt;code&gt;music_standard&lt;/code&gt; scenario, 32kbps mono/stereo) ensuring stutter-free voice alongside game packets.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Agora RTM Presence &amp;amp; Signaling&lt;/strong&gt;: Lobby state refreshes, ready checks, and voice mute indicators are broadcast across the room via lightweight real-time messaging.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  4. Internationalization &amp;amp; Accessibility (12 Locales)
&lt;/h2&gt;

&lt;p&gt;Trick or Take was designed for global accessibility from day one:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;12 Built-in Languages&lt;/strong&gt;: English, Simplified Chinese, Traditional Chinese, Japanese, Korean, German, French, Spanish, Portuguese, Italian, Russian, and Arabic.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Right-to-Left (RTL) Support&lt;/strong&gt;: Full layout mirroring for Arabic players.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Touch &amp;amp; Keyboard Parity&lt;/strong&gt;: Virtual dual-zone joystick for mobile devices and responsive WASD/Arrow key controls on desktop.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  5. Completely Open Source
&lt;/h2&gt;

&lt;p&gt;The entire game engine, Cloudflare Workers backend, Agora voice integration, and localization catalogs are 100% open-source under the permissive &lt;strong&gt;MIT License&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;🕹️ &lt;strong&gt;Play Now&lt;/strong&gt;: &lt;a href="https://trickortake.com" rel="noopener noreferrer"&gt;https://trickortake.com&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;⭐️ &lt;strong&gt;Star &amp;amp; Fork on GitHub&lt;/strong&gt;: &lt;a href="https://github.com/zicojiao/trick-or-take" rel="noopener noreferrer"&gt;https://github.com/zicojiao/trick-or-take&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Feel free to check out the code, run it locally via &lt;code&gt;wrangler dev&lt;/code&gt;, or deploy your own custom themed party game! What do you think of building serverless multiplayer games on Cloudflare Workers? Let's discuss in the comments below!&lt;/p&gt;

</description>
      <category>gamedev</category>
      <category>cloud</category>
      <category>webdev</category>
      <category>opensource</category>
    </item>
  </channel>
</rss>
