精灵裁切框越界后,为什么 Canvas 输出会留空?
用一张 32×16 的双色图,验证越界裁切如何按比例保留输出画布的空白。


切出来的精灵右边多了一段透明区,但导出尺寸仍然正确。使用 Canvas 的 drawImage 时,源裁框超出图片边界会按比例裁掉目标区域的一部分。先用不透明色块确认空白从哪一步产生。
用两份相同目标画布复现
准备一张 32×16 图,左半红、右半蓝。创建两个 32×16 的透明输出画布,并各自关闭图像平滑。A 使用源矩形 (16, 0, 16, 16),把它绘制到目标矩形 (0, 0, 32, 16)。B 只把源宽从 16 改成 32。B 的源请求伸出右边 16 个像素。
从像素读回判断
A 应整块为蓝色。B 的目标左半应为蓝色,右半仍为透明。用 getImageData 对比 x=8 和 x=24 处的 alpha。如果右半非透明,先确认输出画布绘制前为空,再检查是否有背景填充。若颜色不对,还要核对读取的源图尺寸和选区坐标。
选定交付策略
游戏帧要求统一画布时,可保留受控留白并记录精灵原点。要求内容铺满时,先将源裁框约束到边界内,再明确决定如何缩放有效区域。使用精灵切图工具准备真实样图,在自己的 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.
