Knovajs / HTML5 Canvas - Rotation origin

被刻印的时光 ゝ 提交于 2020-01-07 05:51:12

问题


I have successfully created a button which rotates an image either clockwise or C-Clockwise. However, this button can only be used once. i.e. if i press CW i cannot then use CCW to revert the image back.

Any ideas?

$rw = $('#rotate_right');

$rw.on('click', function(event) { event.preventDefault ? event.preventDefault() : event.returnValue = false;

    darthVaderImg.offsetX(img_width / 2); 
    darthVaderImg.offsetY(img_height / 2);
    // when we are setting {x,y} properties we are setting position of top left corner of darthVaderImg.
    // but after applying offset when we are setting {x,y}
    // properties we are setting position of central point of darthVaderImg.
    // so we also need to move the image to see previous result
    darthVaderImg.x(darthVaderImg.x() + img_width / 2);
    darthVaderImg.y(darthVaderImg.y() + img_height / 2);

    darthVaderImg.rotation(90);

    stage.batchDraw();
    export_changes();
});

$rl = $('#rotate_left');
$rl.on('click', function(event) {
    event.preventDefault ? event.preventDefault() : event.returnValue = false;
    //darthVaderImg.rotate(90);

    darthVaderImg.offsetX(img_width / 2); 
    darthVaderImg.offsetY(img_height / 2);
    // when we are setting {x,y} properties we are setting position of top left corner of darthVaderImg.
    // but after applying offset when we are setting {x,y}
    // properties we are setting position of central point of darthVaderImg.
    // so we also need to move the image to see previous result
    darthVaderImg.x(darthVaderImg.x() + img_width / 2);
    darthVaderImg.y(darthVaderImg.y() + img_height / 2);

    darthVaderImg.rotation(-90);

    stage.batchDraw();
    export_changes();
});`

回答1:


rotation method will set the current angle of a shape. if you need to rotate more you can use this:

var oldRotation = node.rotation();
node.rotation(oldRotation + 90);

or just:

node.rotate(90);



回答2:


Based on @lavrton's answer, here is a working snippet. I guess you need to pay attention to the rotation point - it was easy for me as I used a wedge. See the brief code in the button click events for illustration on what to do.

// add a stage
var s = new Konva.Stage({
  container: 'container',
  width: 800,
  height: 200
});

// add a layer        
var l = new Konva.Layer();
s.add(l);

    
// Add a green rect to the LAYER just to show boundary of the stage.
var green = new Konva.Rect({stroke: 'lime', width: 799, height: 199, x: 0, y: 0});
l.add(green);

// add a circle to contain the wedge
var circle = new Konva.Circle({
      x: s.getWidth() / 2,
      y: s.getHeight() / 2,
      radius: 70,
      fill: 'red',
      stroke: 'black',
      strokeWidth: 4
    });
l.add(circle);

// add the wedge - used as our rotation example
var w = new Konva.Wedge({
      x: s.getWidth() / 2,
      y: s.getHeight() / 2,
      radius: 70,
      angle: 60,
      fill: 'lime',
      stroke: 'black',
      strokeWidth: 4,
      rotation: -120
    });
l.add(w);

l.draw(); // redraw the layer it all sits on.

// wire up the buttons
var oldAngle = 0;
$( document ).ready(function() {
$('#plus90').on('click', function(e){
  oldAngle = w.rotation();
  w.rotate(90);
  $('#info').html('Rotation is ' + oldAngle + ' + 90 = ' + w.rotation())
  l.draw();
})

$('#minus90').on('click', function(e){
  oldAngle = w.rotation();
  w.rotate(-90);
  $('#info').html('Rotation is ' + oldAngle + ' - 90 = ' + w.rotation())
  l.draw();
})

  $('#info').html('Initial rotation = ' + w.rotation())

});
#container {
  border: 1px solid #ccc;
}
#info {
height: 20px;
border-bottom: 1px solid #ccc;
}
#hint {
font-style: italic;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.rawgit.com/konvajs/konva/1.6.3/konva.min.js"></script>


<body>
<div id='hint'>Hint: green is stage, red circle is static, lime wedge rotates.Click the buttons!<br/>
Note the initial rotation set on the creation of the wedge.
</div>

<div id='info'>Info:</div>
<div id='ctrls'>
<button id='minus90'>Rotate -90 degrees</button>
<button id='plus90'>Rotate +90 degrees</button>
</div>


  <div id="container"></div>
</body>


来源:https://stackoverflow.com/questions/44591179/knovajs-html5-canvas-rotation-origin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!