HTML Canvas:如何绘制翻转/镜像图像?
当我在 HTML 画布上绘制图像时,我尝试翻转/镜像图像;我发现一个游戏教程显示了角色必须面对的每个方向的精灵表,但这对我来说似乎不太正确。特别是因为每个框架都有不同的尺寸。
实现这一目标的最佳技术是什么?
我尝试在画布上调用 setScale(-1, 1);
但没有成功。也许这不是为了这个。
谢谢
I'm trying to flip/mirror an image as I paint it on an HTML canvas; I found a game tutorial showing a sprite sheet per direction a character has to face, but this doesn't seem quite right to me. Especially since each frame has a different size.
What would be the best technique to reach this goal?
I tried to call the setScale(-1, 1);
on my canvas with no success. Maybe that isn't meant for this.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以通过使用
myContext.scale(-1 转换上下文来实现此目的,1)
在绘制图像之前,但是这会减慢你的游戏速度。最好有一个单独的、反向的精灵。
You can do this by transforming the context with
myContext.scale(-1,1)
before drawing your image, howeverThis is going to slow down your game. It's a better idea to have a separate, reversed sprite.
您需要设置画布的比例并反转宽度。
这可能存在一些性能问题,但对我来说这不是问题。
You need to set the scale of the canvas as well as inverting the width.
There are probably some performance issues with this but for me that was not an issue.
如果您只是使用
ctx.scale(-1, 1)
水平翻转它,它就会越界...所以使用translate
来调整其位置:对于更短的时间代码中,您可以删除
translate
并使用图像大小作为drawImage
的第二个参数(x 坐标)中的负偏移量:如果您想稍后恢复上下文,在其前后添加
save/restore
全部:If you just flip it horizontally using
ctx.scale(-1, 1)
it will get off of bounds... so usetranslate
to adjust its position:For a shorter code you can remove the
translate
and use the image size as negative offset in the second parameter of thedrawImage
(x coordinate) instead:If you want to restore the context later, add
save/restore
before and after it all:创建反射时不需要重新绘制整个图像。原始反射仅显示图像的底部部分。通过这种方式,您可以重新绘制图像的较小部分,从而提供更好的性能,并且您不需要创建线性渐变来隐藏图像的下部部分(因为您从不绘制它)。
You don't need to redraw the entire image when creating a reflection. An original reflection simply shows the bottom part of the image. This way you are redrawing a smaller part of the image which provides better performance and also you don't need to create linear gradient to hide the lower part of the image (since you never draw it).