简体   繁体   English

如何获得画布的中心点

[英]How to get the center point of canvas

How to get The center point of the canvas object/rectangle. 如何获取画布对象/矩形的中心点。 I use the Konvajs library for my small project. 我将Konvajs库用于我的小型项目。 in the konva documentation said that you need to center the point to get good rotation. 在konva文档中说,您需要将点居中以获得良好的旋转。 http://konvajs.github.io/docs/animations/Rotation.html http://konvajs.github.io/docs/animations/Rotation.html

Ex 防爆

 var yellowRect = new Konva.Rect({
    x: 220,
    y: 75,
    width: 100,
    height: 50,
    fill: 'yellow',
    stroke: 'black',
    strokeWidth: 4,
    offset: {
        x: 50 // how to solve this using formula so it will dynamic,
        y: 25 // how to solve this using formula so it will dynamic
    }
});

Rotating rectangular objects around their centers 围绕其中心旋转矩形对象

By default, KonvaJS sets the rotation point of a rectangle at its top-left corner. 默认情况下,KonvaJS将矩形的旋转点设置在其左上角。 Therefore to rotate from the center of the rectangle you must push the rotation point to the center of the rectangle. 因此,要从矩形的中心旋转,必须将旋转点推到矩形的中心。

You can do this by setting the offsetX=rectangleWidth/2 and offsetY=rectangleHeight/2 您可以通过设置offsetX=rectangleWidth/2offsetY=rectangleHeight/2

var yellowRectWidth=100;
var yellowRectHeight=50;
var yellowRect = new Konva.Rect({
    x: 220,
    y: 75,
    width: yellowRectWidth,
    height: yellowRectHeight,
    fill: 'yellow',
    stroke: 'black',
    strokeWidth: 4,
    offset: {
        x: yellowRectWidth/2,
        y: yellowRectHeight/2
    }
});

Rotating circular objects around their centers 围绕它们的中心旋转圆形对象

By default, KonvaJS sets the rotation point of circular shapes at their centerpoints. 默认情况下,KonvaJS将圆形的旋转点设置在其中心点。 Therefore to rotate from the center of circular shapes you won't have to set any offsets. 因此,要从圆形的中心旋转,您无需设置任何偏移。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM