绕中心旋转步骤
1平移坐标位置加一半的宽度
2旋转
3平移负的坐标位置加一半的宽度(平移回来)
4正常画图(x,y坐标)
或者
1平移坐标位置加一半的宽度
2旋转
3画图时坐标为负的1/2宽度(平移回来)
注:
1ctx.scale()缩放不仅缩放大小 还把移动的距离以及x,y的位置也缩放了
2ctx.save()和ctx.restore():
ctx.save()是开始保存在此之前的状态,ctx.restore()是恢复save()之前的状态。在save和restore之间的scale,translate,rotate变化都不会影响到之后的操作(即不管怎么变化,restore()之后画布都被还原为save()之前的状态了!)
js部分
ctx.beginPath();
ctx.save();
ctx.translate(arr[i].x+arr[i].r/2,arr[i].y+arr[i].r/2)
ctx.rotate(Math.PI*2*(Math.random()))
// ctx.scale(0.9,0.9)
// ctx.translate(-(arr[i].x+arr[i].r/2),-(arr[i].y+arr[i].r/2))
var img=new Image();
img.src='img/雪花.png';
ctx.drawImage(img,-arr[i].r/2,-arr[i].r/2,arr[i].r,arr[i].r)
ctx.restore();
ctx.closePath()