针对一个游戏项目,我使用它们的属性(如文件名、位置、比例和旋转)来绘制图像。
这是进行绘制的部分:
this.context.save();
this.context.translate(item.position.x, item.position.y);
if (item.rotation > 0) {
this.context.rotate(item.rotation * (Math.PI / 180));
}
if (item.scale.x !== 1 || item.scale.y !== 1) {
this.context.scale(item.scale.x, item.scale.y);
}
var width = item.imageSize.width * item.scale.x;
var height = item.imageSize.height * item.scale.y;
this.context.drawImage(this.assets.image[item.fileName], -(width / 2), -(height / 2), width, height);
this.context.restore();
(不要在意奇怪的位置,这不重要)
这个方法很好用,但是有一件事情我不太明白:
旋转和缩放可以通过两种不同的方式实现:先缩放再旋转,或者先旋转再缩放。从逻辑上讲,人们会认为先缩放再旋转是正确的,但是出于某些原因,只有先旋转再缩放才能得到正确的结果。
那么,哪种方法才是正确的呢?