payamprivate Posted October 2 Share Posted October 2 Hi everyone, I'm working on a 2D web-based board game (traditional tabletop grid/board mechanics with checker pieces moving across points). Because it's turn-based rather than an action game, 95% of the time the screen is completely static, with bursts of 60 FPS animation only during piece drags, dice rolls, or checker slide animations. I'm debating between two architectural approaches for the rendering layer on HTML5 Canvas: A classic continuous requestAnimationFrame loop that clears and redraws all layers (background board wood grain, pieces, UI overlays) every frame, but only when an isAnimating or isInteracting dirty flag is true, otherwise halting RAF until the next input/state event. A layered canvas approach: one static background canvas for the heavy textured board surface, and an overlay canvas on top that only re-renders moving pieces, using dirty clipping rectangles (ctx.clearRect(x, y, w, h)) during piece movements. For modern mobile browsers (especially Safari on iOS where canvas memory footprints and battery consumption can be sensitive): Has the layered multi-canvas approach yielded measurable battery / CPU benefits in your experience compared to just a single canvas with conditional RAF? Are dirty rect clearing optimizations still worth the mathematical bookkeeping overhead given how fast modern 2D canvas 2D contexts are on hardware-accelerated mobile devices? Would appreciate any insights or benchmarks from folks who have built turn-based or grid games for mobile web! onomogo and CGTRocketX 2 Quote Link to comment Share on other sites More sharing options...
payamprivate Posted October 15 Author Share Posted October 15 Following up on this thread with our benchmark results and profiling data after implementing and testing both architectures on iOS Safari (A13 / A15) and Android Chromium: 1. Single Canvas + Event-Driven RAF vs. Layered Canvases: Surprisingly, on mobile Safari, having multiple stacked transparent canvases introduced a measurable compositing penalty during scrolling and responsive viewport resizes. Each canvas element maintains its own backing store in GPU memory. On high-DPI displays (window.devicePixelRatio >= 3), a 1000×800 logical canvas allocates ~3000×2400 pixels (~28 MB uncompressed texture memory per layer). Stacking 2 or 3 canvases caused WebKit's compositor to occasionally stutter when switching between gestures and CSS transforms. Instead, a single hardware-accelerated canvas with an event-driven requestAnimationFrame loop won decisively. When no animation is running and no checker piece is being dragged, the loop completely halts (cancelAnimationFrame). Idle CPU utilization sits at 0.0%, and GPU power draw drops to baseline, which eliminated all thermal throttling on older iPhones. 2. Dirty Rectangles vs. Full Redraw: On modern mobile GPUs, computing dirty clipping rectangles and managing bounding box arithmetic for 30 checkers actually introduced more JavaScript bookkeeping overhead than it saved. A clean ctx.clearRect(0, 0, width, height) followed by re-drawing the board and checkers took under 1.8ms on a 2020 iPhone SE and ~0.9ms on an iPhone 14 Pro—well within the 16.6ms frame budget for 60 FPS (or 8.3ms for 120Hz ProMotion). The key bottleneck wasn't rasterization, but GC allocation: pre-allocating point coordinates and reusing vector structs inside the drag handler eliminated frame drops entirely. 3. Live Implementation: We deployed this architecture in production for our web client at Boardgammon (instant guest mode works without login or registration if anyone wants to test touch-drag responsiveness or inspect DevTools). If you open the Safari / Chrome performance profiler while moving pieces, you can see the paint frequency locking cleanly at 60/120 FPS during active drag/slide animations and immediately falling back to zero RAF cycles during idle turns. Hope these profiling findings help anyone else weighing canvas architectures for turn-based board or card game interfaces on mobile web! onomogo 1 Quote Link to comment Share on other sites More sharing options...
CSAF Posted October 17 Share Posted October 17 Useful writeup. Two things we'd add from our own browser games. First, on 120Hz ProMotion screens requestAnimationFrame fires twice as often, so a slide animation that moves a fixed amount per frame runs at double speed. We shipped exactly that bug once: an animation stepped once per frame ran 2.3 times too fast on a 144Hz monitor. Advancing by elapsed time with an accumulator fixed it. Second, on memory: capping the backing store at devicePixelRatio 2 instead of 3 takes a 1000 by 800 canvas from about 28.8 MB to 12.8 MB (4 bytes per pixel). Whether that difference shows on a wood grain board is worth checking on a real phone before deciding. For transparency, we're CSAF and our code and posts are written with AI assistance. Quote Link to comment Share on other sites More sharing options...
CGTRocketX Posted October 22 Share Posted October 22 We've hit the same single-canvas vs stacked-layers choice on a mobile web client. On Safari the extra canvases cost more in compositor / GPU memory than they saved, so one hardware-accelerated canvas with RAF only while something is moving won for us. Dirty-rect bookkeeping for a modest sprite count also lost to a full clear and redraw once we stopped allocating inside the drag path. Idle-at-zero RAF is the battery win that mattered most. Quote Link to comment Share on other sites More sharing options...
SomeStudioJP Posted 16 hours ago Share Posted 16 hours ago Really useful profiling data. One middle ground that pairs well with a single DOM canvas plus idle gating is baking any heavy board wood grain or grid lines once into an off-DOM canvas and blitting it with a single drawImage before drawing the pieces. You avoid Safari's multi-layer compositing tax while skipping procedural redraws during drag frames. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.