添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement . We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

There are two ways.

Offset

Use the offset property to change the origin of the shape to its center. You may need to adjust x and y props to keep the image on the same place

For more info read https://konvajs.org/docs/posts/Position_vs_Offset.html#page-title

Good old math

Use power of math to calculate new x and y when shape is rotated around the center. These functions may help:

export function degToRad(angle) {
  return angle / 180 * Math.PI;
export function getCenter(shape) {
  const angleRad = degToRad(shape.rotation || 0);
  return {
      shape.x +
      shape.width / 2 * Math.cos(angleRad) +
      shape.height / 2 * Math.sin(-angleRad),
      shape.y +
      shape.height / 2 * Math.cos(angleRad) +
      shape.width / 2 * Math.sin(angleRad)
export function rotateAroundPoint(shape, deltaDeg, point) {
  const angleRad = degToRad(deltaDeg);
  const x = Math.round(
    point.x +
      (shape.x - point.x) * Math.cos(angleRad) -
      (shape.y - point.y) * Math.sin(angleRad)
  const y = Math.round(
    point.y +
      (shape.x - point.x) * Math.sin(angleRad) +
      (shape.y - point.y) * Math.cos(angleRad)
  return {
    ...shape,
    rotation: Math.round((shape.rotation + deltaDeg)),
export function rotateAroundCenter(shape, deltaDeg) {
  const center = getCenter(shape);
  return rotateAroundPoint(shape, deltaDeg, center);
// usage
const attrs = {
  x: image.x(),
  y: image.y(),
  width: image.width(),
  height: image.height(),
  rotation: image.rotation();
const rotatedAttrs = rotateAroundCenter(attrs, 45);
image.setAttrs(rotatedAttrs);