0

I want to draw on a rotated image with a circle. The circle is moved with drag and drop. I created a JSFiddle here. When I use degrees = 0 it works fine:

enter image description here

The position which is drawn is correct. When I change degrees to something different than zero, e.g., degrees = 45 I get the following result:

enter image description here

The position of the orange circle which should point to the draw position is different.

Here is the code I use:

console.clear();

var history = new Array();

var imageObj = new Image();
var img = null;
var offsetX = 0;
var offsetY = 0;
var degrees = 45;

stage = new Konva.Stage({
    container: 'container',
    width: 600,
    height: 400
});
layer = new Konva.Layer();
stage.add(layer);

var rect = new Konva.Rect({
    x: 50,
    y: 50,
    width: 438,
    height: 300,
    fill: 'yellow'
});
layer.add(rect);

var canvas = document.createElement('canvas');
var context = canvas.getContext('2d');


var circle = new Konva.Circle({
    x: 0,
    y: 0,
    radius: 40,
    opacity: 0.7,
    fill: '#ff5e0a',
    stroke: '#d95009',
    strokeWidth: 1,
    draggable: true
});
layer.add(circle);


circle.on('dragmove touchmove', function (e) {
    draw(e.evt.clientX, e.evt.clientY, circle.radius(), img.x(), img.y());
});


imageObj.onload = function () {
    canvas.width = imageObj.width;
    canvas.height = imageObj.height;
    context.drawImage(imageObj, 0, 0, imageObj.width, imageObj.height);


    img = new Konva.Image({
        x: 50,
        y: 50,
        image: canvas
    });

    img.setRotation(degrees);

    offsetX = imageObj.width / 2;
    offsetY = imageObj.height / 2;

    img.setOffsetX(offsetX);
    img.setOffsetY(offsetY);
    img.x(img.x() + offsetX);
    img.y(img.y() + offsetY);


        // translate context to center of canvas
  //  context.translate(canvas.width / 2, canvas.height / 2);

  //  context.rotate(degrees * Math.PI/180);


    layer.add(img);

    circle.moveToTop();
    layer.draw();
};

imageObj.crossOrigin = "anonymous";
imageObj.src = "https://dl.dropboxusercontent.com/u/47067729/darth-vader.jpg";




function draw(absX, absY, radius, imageX, imageY) {
    var x = 0;
    var y = 0;

    // set pointer
    circle.x(absX);
    circle.y(absY);

    imageX = imageX - (offsetX);
    imageY = imageY - (offsetY);

    x = absX - imageX;
    y = absY - imageY;

    // translate context to center of canvas
  //  context.translate(canvas.width / 2, canvas.height / 2);

 //   context.rotate(degrees * Math.PI/180);

    context.beginPath();
    context.arc(x, y, radius, 0, 2 * Math.PI);
    context.fill();

    layer.draw();
}

(Full example is in my JSFiddle here.)

When I drag the orange circle over the rotated image the drawing has a different position than the orange circle is at.

What I tried was in my draw function:

context.translate(canvas.width / 2, canvas.height / 2);
context.rotate(degrees * Math.PI/180);

but this does not work. It does not show the correct draw position.

How do I get the correct draw position on the rotated image?

Michael
  • 32,527
  • 49
  • 210
  • 370

1 Answers1

1

Here is the solution: http://jsfiddle.net/confile/8cxp8pa4/

console.clear();

var history = new Array();

var imageObj = new Image();
var img = null;
var offsetX = 0;
var offsetY = 0;
var degrees = 45;

stage = new Konva.Stage({
    container: 'container',
    width: 600,
    height: 400
});
layer = new Konva.Layer();
stage.add(layer);

var rect = new Konva.Rect({
    x: 50,
    y: 50,
    width: 438,
    height: 300,
    fill: 'yellow'
});
layer.add(rect);

var canvas = document.createElement('canvas');
var context = canvas.getContext('2d');


var circle = new Konva.Circle({
    x: 0,
    y: 0,
    radius: 40,
    opacity: 0.7,
    fill: '#ff5e0a',
    stroke: '#d95009',
    strokeWidth: 1,
    draggable: true
});
layer.add(circle);


circle.on('dragmove touchmove', function (e) {
    draw(e.evt.clientX, e.evt.clientY, circle.radius(), img.x(), img.y());
});


imageObj.onload = function () {
    canvas.width = imageObj.width;
    canvas.height = imageObj.height;
    context.drawImage(imageObj, 0, 0, imageObj.width, imageObj.height);

    var imgX = 50;
    var imgY = 50;

    img = new Konva.Image({
        x: imgX,
        y: imgY,
        image: canvas
    });

    img.setRotation(degrees);

    offsetX = imageObj.width / 2;
    offsetY = imageObj.height / 2;

    var rotationX = imgX + offsetX;
    var rotationY = imgY + offsetY;

    img.setOffsetX(offsetX);
    img.setOffsetY(offsetY);
    img.x(rotationX);
    img.y(rotationY);

    context.translate(canvas.width/2, canvas.height/2);

    context.rotate(-degrees * Math.PI/180);


    layer.add(img);

    circle.moveToTop();
    layer.draw();
};

imageObj.crossOrigin = "anonymous";
imageObj.src = "https://dl.dropboxusercontent.com/u/47067729/darth-vader.jpg";




function draw(absX, absY, radius, imageX, imageY) {
    var x = 0;
    var y = 0;

    var c2 = $("#container2")[0];
    var ctx2 = c2.getContext('2d');
    c2.width = canvas.width;
    c2.height = canvas.height;
    ctx2.clearRect( 0 , 0 , canvas.width, canvas.height);
    ctx2.drawImage(canvas, 0,0, canvas.width, canvas.height);

    // set pointer
    circle.x(absX);
    circle.y(absY);

   // imageX = imageX - (offsetX);
   // imageY = imageY - (offsetY);

    x = absX - imageX;
    y = absY - imageY;

    // translate context to center of canvas
  //  context.translate(canvas.width / 2, canvas.height / 2);

 //   context.rotate(degrees * Math.PI/180);

    context.beginPath();
    context.arc(x, y, radius, 0, 2 * Math.PI);
    context.fill();

    layer.draw();
}
Michael
  • 32,527
  • 49
  • 210
  • 370
  • @CJ. Which one is to slow yours or mine? – Michael Feb 15 '15 at 23:45
  • Haha, mine! - plus you figured it out without having to get the trig involved! Was annoying me as well, just the ordering and get the correct sign on the rotation – C.. Feb 15 '15 at 23:47
  • @CJ. This was a little bit confusing what you wrote. So the one which I posted is the fastest right? – Michael Feb 15 '15 at 23:49
  • No, just meant when I had last looked, there wasn't an answer, then when came back to post you had got there first- sorry just ignore me, never meant to confuse! – C.. Feb 15 '15 at 23:51
  • In that case I'm way too slow. – JSdc Feb 19 '15 at 03:07