Rotating around a point - offset function moves the shape?












0















When you first start to use offset() to set the rotation point of a shape or group, you will be confused because the position of the group is changed too.



This is the function I have produced to wrap the correct maths. Does anyone have a better solution?



Longer story: I found the need to recalculate the (x,y) for a shape when setting the offset to be non-intuitive. IMHO when imagining the process of rotation most people will conceptualise the canvas based on early maths lessons. If I imagine the chalkboard and my maths teacher talking about rotating a triangle, she would point to the centre of rotation and I could imagine the triangle spinning around her finger. I get that in Konvajs the offset is related to drawing operations and it has a relationship to the x,y pos of the shape for deeper reasons - it is just not intuitive for me to think about rotating as moving the drawing position!



Note that in the function in the below snippet, the x,y position of the rotation point being passed in is in relation to the top left of the stage.



/*
Set the offset for rotation to the given location and re-position the shape
*/
function RotatePoint(shape, pos){ // where pos = {x: xpos, y: yPos}
var initialPos = shape.getAbsolutePosition();
var moveBy = {x: pos.x - initialPos.x, y: pos.y - initialPos.y};

// offset is relative to initial x,y of shape, so deduct x,y.
shape.offsetX(moveBy.x);
shape.offsetY(moveBy.y);

// reposition x,y because changing offset moves it.
shape.x(initialPos.x + moveBy.x);
shape.y(initialPos.y + moveBy.y);
}


And in action...






/*
Set the offset for rotation to the given location and re-position the shape
*/
function RotatePoint(shape, pos){ // where pos = {x: xpos, y: yPos}
var initialPos = shape.getAbsolutePosition();
var moveBy = {x: pos.x - initialPos.x, y: pos.y - initialPos.y};

// offset is relative to initial x,y of shape, so deduct x,y.
shape.offsetX(moveBy.x);
shape.offsetY(moveBy.y);

// reposition x,y because changing offset moves it.
shape.x(initialPos.x + moveBy.x);
shape.y(initialPos.y + moveBy.y);
}


// from here on was just a part-complete project I grabbed to illustrate the above function

// kpi chart is one or more circles.
var outputKpiChart = function(opts){

// Set up the canvas / stage
var stage = new Konva.Stage({container: opts.ele, width: opts.size.width, height: opts.size.height});
var layer = new Konva.Layer({draggable: false});
stage.add(layer);

var theCfg = getFormat('one');

for (var i = 0; i < theCfg.length; i = i + 1){
var cfg = theCfg[i];

var group = new Konva.Group({});

var w = opts.size.width;
var h = opts.size.height;
var r = opts.size.width/4;
var ctr = {x: opts.size.width/2, y: opts.size.height/2};
var circle = new Konva.Circle({
x: w * cfg.pos.x,
y: h * cfg.pos.y,
radius: w * cfg.r,
stroke: '#D4B22B',
strokeWidth: 4,
fill: '#7D8DF9'
});
group.add(circle);

var text = new Konva.Text({
x: w * cfg.pos.x,
y: h * cfg.pos.y,
text: '42',
fill: 'blue',
fontSize: 140,
fontFamily: 'Roboto'
});

text.offset({x: text.getWidth()/2, y: text.getHeight()/2});
group.add(text);

var text1 = new Konva.Text({
x: w * cfg.pos.x,
y: (h * cfg.pos.y) - (w * cfg.r * .25),
text: 'Meaning of Life',
fill: 'blue',
fontSize: 20,
fontFamily: 'Roboto'
});

text1.x(text1.x() + (text.width() - text1.width())/2); // centre over text 1

text1.offset({x: text.getWidth()/2, y: text.getHeight()/2});
group.add(text1);

layer.add(group)

var pos = group.getClientRect();

// set the group rotate point. Note - x,y is relative to top-left of stage !
RotatePoint(group, {x: pos.x + pos.width/2, y: pos.y + pos.height/2});

var c1 = new Konva.Circle({x: group.offsetX(), y: group.offsetY(), radius: 4, fill: 'red'});
layer.add(c1)

// the tween has to be created after the node has been added to the layer
var tween = new Konva.Tween({
node: group,
duration: 4,
rotation: 360,
easing: Konva.Easings.BackEaseOut
});
tween.play();
}


stage.draw();

}

// this is a dashboard project, this func is intended to return positioning
// metrics for complex kpi displays
var getFormat = function(formatName){

switch (formatName){

case 'one': // display a single circle kpi display
config = [{name: 'c1', r: .25, pos: {x: .5, y: .5}, size: {x: .5, y: .5}}]
break;
}
return config;
}

outputKpiChart({ele: '#container', size: {width: 600, height: 300} })

<script src="https://cdnjs.cloudflare.com/ajax/libs/konva/2.5.1/konva.js"></script>
<p>A function to move the rotaton point of a group.</p>
<div id='container' style="display: inline-block; width: 300px, height: 200px; background-color: silver; overflow: hidden; position: relative;"></div>












share|improve this question





























    0















    When you first start to use offset() to set the rotation point of a shape or group, you will be confused because the position of the group is changed too.



    This is the function I have produced to wrap the correct maths. Does anyone have a better solution?



    Longer story: I found the need to recalculate the (x,y) for a shape when setting the offset to be non-intuitive. IMHO when imagining the process of rotation most people will conceptualise the canvas based on early maths lessons. If I imagine the chalkboard and my maths teacher talking about rotating a triangle, she would point to the centre of rotation and I could imagine the triangle spinning around her finger. I get that in Konvajs the offset is related to drawing operations and it has a relationship to the x,y pos of the shape for deeper reasons - it is just not intuitive for me to think about rotating as moving the drawing position!



    Note that in the function in the below snippet, the x,y position of the rotation point being passed in is in relation to the top left of the stage.



    /*
    Set the offset for rotation to the given location and re-position the shape
    */
    function RotatePoint(shape, pos){ // where pos = {x: xpos, y: yPos}
    var initialPos = shape.getAbsolutePosition();
    var moveBy = {x: pos.x - initialPos.x, y: pos.y - initialPos.y};

    // offset is relative to initial x,y of shape, so deduct x,y.
    shape.offsetX(moveBy.x);
    shape.offsetY(moveBy.y);

    // reposition x,y because changing offset moves it.
    shape.x(initialPos.x + moveBy.x);
    shape.y(initialPos.y + moveBy.y);
    }


    And in action...






    /*
    Set the offset for rotation to the given location and re-position the shape
    */
    function RotatePoint(shape, pos){ // where pos = {x: xpos, y: yPos}
    var initialPos = shape.getAbsolutePosition();
    var moveBy = {x: pos.x - initialPos.x, y: pos.y - initialPos.y};

    // offset is relative to initial x,y of shape, so deduct x,y.
    shape.offsetX(moveBy.x);
    shape.offsetY(moveBy.y);

    // reposition x,y because changing offset moves it.
    shape.x(initialPos.x + moveBy.x);
    shape.y(initialPos.y + moveBy.y);
    }


    // from here on was just a part-complete project I grabbed to illustrate the above function

    // kpi chart is one or more circles.
    var outputKpiChart = function(opts){

    // Set up the canvas / stage
    var stage = new Konva.Stage({container: opts.ele, width: opts.size.width, height: opts.size.height});
    var layer = new Konva.Layer({draggable: false});
    stage.add(layer);

    var theCfg = getFormat('one');

    for (var i = 0; i < theCfg.length; i = i + 1){
    var cfg = theCfg[i];

    var group = new Konva.Group({});

    var w = opts.size.width;
    var h = opts.size.height;
    var r = opts.size.width/4;
    var ctr = {x: opts.size.width/2, y: opts.size.height/2};
    var circle = new Konva.Circle({
    x: w * cfg.pos.x,
    y: h * cfg.pos.y,
    radius: w * cfg.r,
    stroke: '#D4B22B',
    strokeWidth: 4,
    fill: '#7D8DF9'
    });
    group.add(circle);

    var text = new Konva.Text({
    x: w * cfg.pos.x,
    y: h * cfg.pos.y,
    text: '42',
    fill: 'blue',
    fontSize: 140,
    fontFamily: 'Roboto'
    });

    text.offset({x: text.getWidth()/2, y: text.getHeight()/2});
    group.add(text);

    var text1 = new Konva.Text({
    x: w * cfg.pos.x,
    y: (h * cfg.pos.y) - (w * cfg.r * .25),
    text: 'Meaning of Life',
    fill: 'blue',
    fontSize: 20,
    fontFamily: 'Roboto'
    });

    text1.x(text1.x() + (text.width() - text1.width())/2); // centre over text 1

    text1.offset({x: text.getWidth()/2, y: text.getHeight()/2});
    group.add(text1);

    layer.add(group)

    var pos = group.getClientRect();

    // set the group rotate point. Note - x,y is relative to top-left of stage !
    RotatePoint(group, {x: pos.x + pos.width/2, y: pos.y + pos.height/2});

    var c1 = new Konva.Circle({x: group.offsetX(), y: group.offsetY(), radius: 4, fill: 'red'});
    layer.add(c1)

    // the tween has to be created after the node has been added to the layer
    var tween = new Konva.Tween({
    node: group,
    duration: 4,
    rotation: 360,
    easing: Konva.Easings.BackEaseOut
    });
    tween.play();
    }


    stage.draw();

    }

    // this is a dashboard project, this func is intended to return positioning
    // metrics for complex kpi displays
    var getFormat = function(formatName){

    switch (formatName){

    case 'one': // display a single circle kpi display
    config = [{name: 'c1', r: .25, pos: {x: .5, y: .5}, size: {x: .5, y: .5}}]
    break;
    }
    return config;
    }

    outputKpiChart({ele: '#container', size: {width: 600, height: 300} })

    <script src="https://cdnjs.cloudflare.com/ajax/libs/konva/2.5.1/konva.js"></script>
    <p>A function to move the rotaton point of a group.</p>
    <div id='container' style="display: inline-block; width: 300px, height: 200px; background-color: silver; overflow: hidden; position: relative;"></div>












    share|improve this question



























      0












      0








      0








      When you first start to use offset() to set the rotation point of a shape or group, you will be confused because the position of the group is changed too.



      This is the function I have produced to wrap the correct maths. Does anyone have a better solution?



      Longer story: I found the need to recalculate the (x,y) for a shape when setting the offset to be non-intuitive. IMHO when imagining the process of rotation most people will conceptualise the canvas based on early maths lessons. If I imagine the chalkboard and my maths teacher talking about rotating a triangle, she would point to the centre of rotation and I could imagine the triangle spinning around her finger. I get that in Konvajs the offset is related to drawing operations and it has a relationship to the x,y pos of the shape for deeper reasons - it is just not intuitive for me to think about rotating as moving the drawing position!



      Note that in the function in the below snippet, the x,y position of the rotation point being passed in is in relation to the top left of the stage.



      /*
      Set the offset for rotation to the given location and re-position the shape
      */
      function RotatePoint(shape, pos){ // where pos = {x: xpos, y: yPos}
      var initialPos = shape.getAbsolutePosition();
      var moveBy = {x: pos.x - initialPos.x, y: pos.y - initialPos.y};

      // offset is relative to initial x,y of shape, so deduct x,y.
      shape.offsetX(moveBy.x);
      shape.offsetY(moveBy.y);

      // reposition x,y because changing offset moves it.
      shape.x(initialPos.x + moveBy.x);
      shape.y(initialPos.y + moveBy.y);
      }


      And in action...






      /*
      Set the offset for rotation to the given location and re-position the shape
      */
      function RotatePoint(shape, pos){ // where pos = {x: xpos, y: yPos}
      var initialPos = shape.getAbsolutePosition();
      var moveBy = {x: pos.x - initialPos.x, y: pos.y - initialPos.y};

      // offset is relative to initial x,y of shape, so deduct x,y.
      shape.offsetX(moveBy.x);
      shape.offsetY(moveBy.y);

      // reposition x,y because changing offset moves it.
      shape.x(initialPos.x + moveBy.x);
      shape.y(initialPos.y + moveBy.y);
      }


      // from here on was just a part-complete project I grabbed to illustrate the above function

      // kpi chart is one or more circles.
      var outputKpiChart = function(opts){

      // Set up the canvas / stage
      var stage = new Konva.Stage({container: opts.ele, width: opts.size.width, height: opts.size.height});
      var layer = new Konva.Layer({draggable: false});
      stage.add(layer);

      var theCfg = getFormat('one');

      for (var i = 0; i < theCfg.length; i = i + 1){
      var cfg = theCfg[i];

      var group = new Konva.Group({});

      var w = opts.size.width;
      var h = opts.size.height;
      var r = opts.size.width/4;
      var ctr = {x: opts.size.width/2, y: opts.size.height/2};
      var circle = new Konva.Circle({
      x: w * cfg.pos.x,
      y: h * cfg.pos.y,
      radius: w * cfg.r,
      stroke: '#D4B22B',
      strokeWidth: 4,
      fill: '#7D8DF9'
      });
      group.add(circle);

      var text = new Konva.Text({
      x: w * cfg.pos.x,
      y: h * cfg.pos.y,
      text: '42',
      fill: 'blue',
      fontSize: 140,
      fontFamily: 'Roboto'
      });

      text.offset({x: text.getWidth()/2, y: text.getHeight()/2});
      group.add(text);

      var text1 = new Konva.Text({
      x: w * cfg.pos.x,
      y: (h * cfg.pos.y) - (w * cfg.r * .25),
      text: 'Meaning of Life',
      fill: 'blue',
      fontSize: 20,
      fontFamily: 'Roboto'
      });

      text1.x(text1.x() + (text.width() - text1.width())/2); // centre over text 1

      text1.offset({x: text.getWidth()/2, y: text.getHeight()/2});
      group.add(text1);

      layer.add(group)

      var pos = group.getClientRect();

      // set the group rotate point. Note - x,y is relative to top-left of stage !
      RotatePoint(group, {x: pos.x + pos.width/2, y: pos.y + pos.height/2});

      var c1 = new Konva.Circle({x: group.offsetX(), y: group.offsetY(), radius: 4, fill: 'red'});
      layer.add(c1)

      // the tween has to be created after the node has been added to the layer
      var tween = new Konva.Tween({
      node: group,
      duration: 4,
      rotation: 360,
      easing: Konva.Easings.BackEaseOut
      });
      tween.play();
      }


      stage.draw();

      }

      // this is a dashboard project, this func is intended to return positioning
      // metrics for complex kpi displays
      var getFormat = function(formatName){

      switch (formatName){

      case 'one': // display a single circle kpi display
      config = [{name: 'c1', r: .25, pos: {x: .5, y: .5}, size: {x: .5, y: .5}}]
      break;
      }
      return config;
      }

      outputKpiChart({ele: '#container', size: {width: 600, height: 300} })

      <script src="https://cdnjs.cloudflare.com/ajax/libs/konva/2.5.1/konva.js"></script>
      <p>A function to move the rotaton point of a group.</p>
      <div id='container' style="display: inline-block; width: 300px, height: 200px; background-color: silver; overflow: hidden; position: relative;"></div>












      share|improve this question
















      When you first start to use offset() to set the rotation point of a shape or group, you will be confused because the position of the group is changed too.



      This is the function I have produced to wrap the correct maths. Does anyone have a better solution?



      Longer story: I found the need to recalculate the (x,y) for a shape when setting the offset to be non-intuitive. IMHO when imagining the process of rotation most people will conceptualise the canvas based on early maths lessons. If I imagine the chalkboard and my maths teacher talking about rotating a triangle, she would point to the centre of rotation and I could imagine the triangle spinning around her finger. I get that in Konvajs the offset is related to drawing operations and it has a relationship to the x,y pos of the shape for deeper reasons - it is just not intuitive for me to think about rotating as moving the drawing position!



      Note that in the function in the below snippet, the x,y position of the rotation point being passed in is in relation to the top left of the stage.



      /*
      Set the offset for rotation to the given location and re-position the shape
      */
      function RotatePoint(shape, pos){ // where pos = {x: xpos, y: yPos}
      var initialPos = shape.getAbsolutePosition();
      var moveBy = {x: pos.x - initialPos.x, y: pos.y - initialPos.y};

      // offset is relative to initial x,y of shape, so deduct x,y.
      shape.offsetX(moveBy.x);
      shape.offsetY(moveBy.y);

      // reposition x,y because changing offset moves it.
      shape.x(initialPos.x + moveBy.x);
      shape.y(initialPos.y + moveBy.y);
      }


      And in action...






      /*
      Set the offset for rotation to the given location and re-position the shape
      */
      function RotatePoint(shape, pos){ // where pos = {x: xpos, y: yPos}
      var initialPos = shape.getAbsolutePosition();
      var moveBy = {x: pos.x - initialPos.x, y: pos.y - initialPos.y};

      // offset is relative to initial x,y of shape, so deduct x,y.
      shape.offsetX(moveBy.x);
      shape.offsetY(moveBy.y);

      // reposition x,y because changing offset moves it.
      shape.x(initialPos.x + moveBy.x);
      shape.y(initialPos.y + moveBy.y);
      }


      // from here on was just a part-complete project I grabbed to illustrate the above function

      // kpi chart is one or more circles.
      var outputKpiChart = function(opts){

      // Set up the canvas / stage
      var stage = new Konva.Stage({container: opts.ele, width: opts.size.width, height: opts.size.height});
      var layer = new Konva.Layer({draggable: false});
      stage.add(layer);

      var theCfg = getFormat('one');

      for (var i = 0; i < theCfg.length; i = i + 1){
      var cfg = theCfg[i];

      var group = new Konva.Group({});

      var w = opts.size.width;
      var h = opts.size.height;
      var r = opts.size.width/4;
      var ctr = {x: opts.size.width/2, y: opts.size.height/2};
      var circle = new Konva.Circle({
      x: w * cfg.pos.x,
      y: h * cfg.pos.y,
      radius: w * cfg.r,
      stroke: '#D4B22B',
      strokeWidth: 4,
      fill: '#7D8DF9'
      });
      group.add(circle);

      var text = new Konva.Text({
      x: w * cfg.pos.x,
      y: h * cfg.pos.y,
      text: '42',
      fill: 'blue',
      fontSize: 140,
      fontFamily: 'Roboto'
      });

      text.offset({x: text.getWidth()/2, y: text.getHeight()/2});
      group.add(text);

      var text1 = new Konva.Text({
      x: w * cfg.pos.x,
      y: (h * cfg.pos.y) - (w * cfg.r * .25),
      text: 'Meaning of Life',
      fill: 'blue',
      fontSize: 20,
      fontFamily: 'Roboto'
      });

      text1.x(text1.x() + (text.width() - text1.width())/2); // centre over text 1

      text1.offset({x: text.getWidth()/2, y: text.getHeight()/2});
      group.add(text1);

      layer.add(group)

      var pos = group.getClientRect();

      // set the group rotate point. Note - x,y is relative to top-left of stage !
      RotatePoint(group, {x: pos.x + pos.width/2, y: pos.y + pos.height/2});

      var c1 = new Konva.Circle({x: group.offsetX(), y: group.offsetY(), radius: 4, fill: 'red'});
      layer.add(c1)

      // the tween has to be created after the node has been added to the layer
      var tween = new Konva.Tween({
      node: group,
      duration: 4,
      rotation: 360,
      easing: Konva.Easings.BackEaseOut
      });
      tween.play();
      }


      stage.draw();

      }

      // this is a dashboard project, this func is intended to return positioning
      // metrics for complex kpi displays
      var getFormat = function(formatName){

      switch (formatName){

      case 'one': // display a single circle kpi display
      config = [{name: 'c1', r: .25, pos: {x: .5, y: .5}, size: {x: .5, y: .5}}]
      break;
      }
      return config;
      }

      outputKpiChart({ele: '#container', size: {width: 600, height: 300} })

      <script src="https://cdnjs.cloudflare.com/ajax/libs/konva/2.5.1/konva.js"></script>
      <p>A function to move the rotaton point of a group.</p>
      <div id='container' style="display: inline-block; width: 300px, height: 200px; background-color: silver; overflow: hidden; position: relative;"></div>








      /*
      Set the offset for rotation to the given location and re-position the shape
      */
      function RotatePoint(shape, pos){ // where pos = {x: xpos, y: yPos}
      var initialPos = shape.getAbsolutePosition();
      var moveBy = {x: pos.x - initialPos.x, y: pos.y - initialPos.y};

      // offset is relative to initial x,y of shape, so deduct x,y.
      shape.offsetX(moveBy.x);
      shape.offsetY(moveBy.y);

      // reposition x,y because changing offset moves it.
      shape.x(initialPos.x + moveBy.x);
      shape.y(initialPos.y + moveBy.y);
      }


      // from here on was just a part-complete project I grabbed to illustrate the above function

      // kpi chart is one or more circles.
      var outputKpiChart = function(opts){

      // Set up the canvas / stage
      var stage = new Konva.Stage({container: opts.ele, width: opts.size.width, height: opts.size.height});
      var layer = new Konva.Layer({draggable: false});
      stage.add(layer);

      var theCfg = getFormat('one');

      for (var i = 0; i < theCfg.length; i = i + 1){
      var cfg = theCfg[i];

      var group = new Konva.Group({});

      var w = opts.size.width;
      var h = opts.size.height;
      var r = opts.size.width/4;
      var ctr = {x: opts.size.width/2, y: opts.size.height/2};
      var circle = new Konva.Circle({
      x: w * cfg.pos.x,
      y: h * cfg.pos.y,
      radius: w * cfg.r,
      stroke: '#D4B22B',
      strokeWidth: 4,
      fill: '#7D8DF9'
      });
      group.add(circle);

      var text = new Konva.Text({
      x: w * cfg.pos.x,
      y: h * cfg.pos.y,
      text: '42',
      fill: 'blue',
      fontSize: 140,
      fontFamily: 'Roboto'
      });

      text.offset({x: text.getWidth()/2, y: text.getHeight()/2});
      group.add(text);

      var text1 = new Konva.Text({
      x: w * cfg.pos.x,
      y: (h * cfg.pos.y) - (w * cfg.r * .25),
      text: 'Meaning of Life',
      fill: 'blue',
      fontSize: 20,
      fontFamily: 'Roboto'
      });

      text1.x(text1.x() + (text.width() - text1.width())/2); // centre over text 1

      text1.offset({x: text.getWidth()/2, y: text.getHeight()/2});
      group.add(text1);

      layer.add(group)

      var pos = group.getClientRect();

      // set the group rotate point. Note - x,y is relative to top-left of stage !
      RotatePoint(group, {x: pos.x + pos.width/2, y: pos.y + pos.height/2});

      var c1 = new Konva.Circle({x: group.offsetX(), y: group.offsetY(), radius: 4, fill: 'red'});
      layer.add(c1)

      // the tween has to be created after the node has been added to the layer
      var tween = new Konva.Tween({
      node: group,
      duration: 4,
      rotation: 360,
      easing: Konva.Easings.BackEaseOut
      });
      tween.play();
      }


      stage.draw();

      }

      // this is a dashboard project, this func is intended to return positioning
      // metrics for complex kpi displays
      var getFormat = function(formatName){

      switch (formatName){

      case 'one': // display a single circle kpi display
      config = [{name: 'c1', r: .25, pos: {x: .5, y: .5}, size: {x: .5, y: .5}}]
      break;
      }
      return config;
      }

      outputKpiChart({ele: '#container', size: {width: 600, height: 300} })

      <script src="https://cdnjs.cloudflare.com/ajax/libs/konva/2.5.1/konva.js"></script>
      <p>A function to move the rotaton point of a group.</p>
      <div id='container' style="display: inline-block; width: 300px, height: 200px; background-color: silver; overflow: hidden; position: relative;"></div>





      /*
      Set the offset for rotation to the given location and re-position the shape
      */
      function RotatePoint(shape, pos){ // where pos = {x: xpos, y: yPos}
      var initialPos = shape.getAbsolutePosition();
      var moveBy = {x: pos.x - initialPos.x, y: pos.y - initialPos.y};

      // offset is relative to initial x,y of shape, so deduct x,y.
      shape.offsetX(moveBy.x);
      shape.offsetY(moveBy.y);

      // reposition x,y because changing offset moves it.
      shape.x(initialPos.x + moveBy.x);
      shape.y(initialPos.y + moveBy.y);
      }


      // from here on was just a part-complete project I grabbed to illustrate the above function

      // kpi chart is one or more circles.
      var outputKpiChart = function(opts){

      // Set up the canvas / stage
      var stage = new Konva.Stage({container: opts.ele, width: opts.size.width, height: opts.size.height});
      var layer = new Konva.Layer({draggable: false});
      stage.add(layer);

      var theCfg = getFormat('one');

      for (var i = 0; i < theCfg.length; i = i + 1){
      var cfg = theCfg[i];

      var group = new Konva.Group({});

      var w = opts.size.width;
      var h = opts.size.height;
      var r = opts.size.width/4;
      var ctr = {x: opts.size.width/2, y: opts.size.height/2};
      var circle = new Konva.Circle({
      x: w * cfg.pos.x,
      y: h * cfg.pos.y,
      radius: w * cfg.r,
      stroke: '#D4B22B',
      strokeWidth: 4,
      fill: '#7D8DF9'
      });
      group.add(circle);

      var text = new Konva.Text({
      x: w * cfg.pos.x,
      y: h * cfg.pos.y,
      text: '42',
      fill: 'blue',
      fontSize: 140,
      fontFamily: 'Roboto'
      });

      text.offset({x: text.getWidth()/2, y: text.getHeight()/2});
      group.add(text);

      var text1 = new Konva.Text({
      x: w * cfg.pos.x,
      y: (h * cfg.pos.y) - (w * cfg.r * .25),
      text: 'Meaning of Life',
      fill: 'blue',
      fontSize: 20,
      fontFamily: 'Roboto'
      });

      text1.x(text1.x() + (text.width() - text1.width())/2); // centre over text 1

      text1.offset({x: text.getWidth()/2, y: text.getHeight()/2});
      group.add(text1);

      layer.add(group)

      var pos = group.getClientRect();

      // set the group rotate point. Note - x,y is relative to top-left of stage !
      RotatePoint(group, {x: pos.x + pos.width/2, y: pos.y + pos.height/2});

      var c1 = new Konva.Circle({x: group.offsetX(), y: group.offsetY(), radius: 4, fill: 'red'});
      layer.add(c1)

      // the tween has to be created after the node has been added to the layer
      var tween = new Konva.Tween({
      node: group,
      duration: 4,
      rotation: 360,
      easing: Konva.Easings.BackEaseOut
      });
      tween.play();
      }


      stage.draw();

      }

      // this is a dashboard project, this func is intended to return positioning
      // metrics for complex kpi displays
      var getFormat = function(formatName){

      switch (formatName){

      case 'one': // display a single circle kpi display
      config = [{name: 'c1', r: .25, pos: {x: .5, y: .5}, size: {x: .5, y: .5}}]
      break;
      }
      return config;
      }

      outputKpiChart({ele: '#container', size: {width: 600, height: 300} })

      <script src="https://cdnjs.cloudflare.com/ajax/libs/konva/2.5.1/konva.js"></script>
      <p>A function to move the rotaton point of a group.</p>
      <div id='container' style="display: inline-block; width: 300px, height: 200px; background-color: silver; overflow: hidden; position: relative;"></div>






      html5-canvas konvajs






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 15 '18 at 7:33







      Vanquished Wombat

















      asked Nov 13 '18 at 9:01









      Vanquished WombatVanquished Wombat

      3,87311338




      3,87311338
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53277253%2frotating-around-a-point-offset-function-moves-the-shape%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53277253%2frotating-around-a-point-offset-function-moves-the-shape%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Coverage of Google Street View

          Full-time equivalent

          Surfing