【发布时间】:2015-10-29 07:07:41
【问题描述】:
我有一个画布,我可以在其中使用变换函数以一定角度绘制圆。
但我也使用缩放和平移来进行相机缩放和定位。问题是变换函数导致缩放基于它的左上角,我不知道如何解决这个问题。
这是我的代码:
function draw(){
ctx.clearRect(0,0,element.width,element.height);
ctx.save();
ctx.setTransform(scale, 0.01, 0.01, 0.3*scale, scale, element.height/2*0.7);
ctx.translate(0-(camera.x - element.width/2),0-(camera.y-element.height/2));
for(var i = 0; i < obj.length; i++){
ctx.beginPath();
ctx.arc(0,0,obj[i].radius,0,Math.PI*2);
ctx.lineWidth = 4.5;
ctx.strokeStyle = "white";
ctx.stroke();
}
ctx.restore();
requestAnimationFrame(draw);
}
如果您放大和缩小,您会看到问题在于它没有停留在正确的位置,相机正在漂移,我需要对此进行纠正:http://jsfiddle.net/ub97gdgj/
如何解决这个锚点问题?
【问题讨论】:
标签: javascript math transformation