代码之家  ›  专栏  ›  技术社区  ›  user944513

为什么在angularjs中图像不在画布中央?

  •  0
  • user944513  · 技术社区  · 7 年前

    你能告诉我为什么我的图像不居中我用了这个方法吗

    function drawImage() {
                        clear();
                        element.save();
                        element.scale(currentScale, currentScale);
                        element.rotate(currentAngle * Math.PI / 180);
                        element.drawImage(image, canvas.width/2-image.width/2, canvas.height/2-image.height/2);
                        element.restore();
                    }
    

    这是我的密码 http://plnkr.co/edit/Tvika6ygEBGmR9OMLSaT?p=preview

    预期产量

    enter image description here

    1 回复  |  直到 7 年前
        1
  •  0
  •   hossein sedighian    7 年前

    首先你要使用刻度,所以你必须做一些类似的事情

    image.width * currentScale
    

    那么你必须使用

    Math.abs() 为正值

    或者检查图像宽度/2不大于画布宽度/2

    你现在的价值是-1578-2129

     element.drawImage(image, canvas.width/2-image.width/2, canvas.height/2-image.height/2);