Canvas 精灵遮罩后只剩白块?检查 destination-in
用同一张彩色精灵和白色圆形遮罩验证 source-in 与 destination-in 保留哪一层。


想用圆形遮罩留下彩色精灵,结果画布只有白色圆形。先检查绘制顺序与合成操作。Canvas 里的 source 是本次绘制,destination 是这次绘制前已有的像素。
用同一精灵画两份
做两个透明 64×64 Canvas,分别先画同一张不透明彩色方形。准备一张同尺寸遮罩位图,中间是实心白圆,周围透明。A 在第二次绘制前设 destination-in,再把白色遮罩整张绘入。B 保持顺序,只换成 source-in。
在圆内和圆外采样
A 的圆内保留原精灵颜色,B 的圆内是本次绘制进来的白色。圆外应透明。对半透明边缘,输出 alpha 由两层 alpha 相乘。可用 getImageData 读圆心和四角,避开边缘抗锯齿干扰。
之后设回 source-over,或用 save 和 restore 限定画布状态。若遮罩把背景也裁掉,先把精灵画进独立的透明画布,再合成到场景。可从精灵切图工具准备一张实际精灵,使用自己的 Canvas 绘制流程完成遮罩与验收。
推荐使用本站工具

Automatic Sprite Slicing and ZIP Export: A Faster Way to Prepare 2D Game Assets
Learn how automatic sprite slicing and one-click ZIP export can help indie game developers turn character sheets, icon collections, and AI-generated 2D artwork into organized PNG assets faster.

From AI Sprite Cutter to Sprite Sheet Maker: A Complete 2D Asset Workflow
A practical workflow guide for turning raw 2D images into sliced PNG files, downloading them as a ZIP package, and using those files to create sprite sheets for Unity, Cocos, Godot, and custom game engines.

AI Sprite Cutter: How Indie Game Developers Can Split 2D Game Assets Faster
A practical guide for indie game developers, 2D artists, and prototype teams. Learn how an AI sprite cutter can split character sheets, UI icons, white-background assets, and AI-generated artwork into clean PNG files for Unity, Cocos, Godot, and sprite sheet workflows.
