How can I keep image and lens rotation synchronized with BlowupJS?

Viewed 261

I used the blowup.js plugin as a base, and I am trying to rotate the image, and the lens to follow the rotation. But it is not working.

When I put the rotate(180deg) for example, the lens and the image are mismatched, if I remove the rotate(180deg) they are aligned.

Anybody know how to help me?

sample in JSFiddle

var $element = $('#target');
$element.css({
  'transform': 'rotate(180deg)'
}); // rotate imagem in html

// Constants
var $IMAGE_URL = $element.attr("src");
var NATIVE_IMG = new Image();
NATIVE_IMG.src = $element.attr("src");

var lens = document.createElement("div");
lens.id = "BlowupLens";
$("body").append(lens);
$blowupLens = $("#BlowupLens");

$blowupLens.css({
  "position": "absolute",
  "display": "none",
  "pointer-events": "none",
  "zIndex": 999999,
  "width": 200,
  "height": 200,
  "border": "6px solid #FFF",
  "background": "#FFF",
  "border-radius": "50%",
  "box-shadow": "0 8px 17px 0 rgba(0, 0, 0, 0.2)",
  "background-repeat": "no-repeat",
});

// Show magnification lens
$element.mouseenter(function() {
  $blowupLens.css("display", "block");
});


// Mouse motion on image
$element.mousemove(function(e) {

  // Lens position coordinates
  var lensX = e.pageX - (200 / 2);
  var lensY = e.pageY - (200 / 2);

  var width = $element.width();
  var height = $element.height();

  // Relative coordinates of image
  var relX = e.pageX - $('#target').offset().left;
  var relY = e.pageY - $('#target').offset().top;

  // Zoomed image coordinates 
  var zoomX = -Math.floor(relX / width * (NATIVE_IMG.width) - 200 / 2);
  var zoomY = -Math.floor(relY / height * (NATIVE_IMG.height) - 200 / 2);

  var backPos = zoomX + "px " + zoomY + "px";
  var backgroundSize = NATIVE_IMG.width + "px " + NATIVE_IMG.height + "px";

  // Apply styles to lens
  $blowupLens.css({
    left: lensX,
    top: lensY,
    "background-image": "url(" + encodeURI($IMAGE_URL) + ")",
    "background-size": backgroundSize,
    "background-position": backPos,
    "transform": "rotate(180deg)" //rotate the image original
  });
})

// Hide magnification lens
$element.mouseleave(function() {
  $blowupLens.css("display", "none");
});
#target {
  margin-left: 160px;
  width: 700;
  height: 500px;
}
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>

<body>
  <img id="target" src="https://iili.io/0hL7ou.png">
</body>

1 Answers

You might rotate the lens in the opposite direction (-180deg) and inverse background position too:

Side note: you don't want to apply background-image on every mousemove, move it to the lens init.

var $element = $('#target');
$element.css({
  'transform': 'rotate(180deg)'
}); // rotate imagem in html

// Constants
var $IMAGE_URL = $element.attr("src");
var NATIVE_IMG = new Image();
NATIVE_IMG.src = $element.attr("src");

var lens = document.createElement("div");
lens.id = "BlowupLens";
$("body").append(lens);
$blowupLens = $("#BlowupLens");

$blowupLens.css({
  "position": "absolute",
  "display": "none",
  "pointer-events": "none",
  "zIndex": 999999,
  "width": 200,
  "height": 200,
  "border": "6px solid #FFF",
  "background": "#FFF",
  "border-radius": "50%",
  "box-shadow": "0 8px 17px 0 rgba(0, 0, 0, 0.2)",
  "background-repeat": "no-repeat",
});

// Show magnification lens
$element.mouseenter(function() {
  $blowupLens.css("display", "block");
});


// Mouse motion on image
$element.mousemove(function(e) {

  // Lens position coordinates
  var lensX = e.pageX - (200 / 2);
  var lensY = e.pageY - (200 / 2);

  var width = $element.width();
  var height = $element.height();

  // Relative coordinates of image
  var relX = e.pageX - $('#target').offset().left;
  var relY = e.pageY - $('#target').offset().top;

  // Zoomed image coordinates 
  var zoomX = -Math.floor(relX / width * (NATIVE_IMG.width) - 200 / 2);
  var zoomY = -Math.floor(relY / height * (NATIVE_IMG.height) - 200 / 2);

  var backPos = "calc(100% - " + zoomX + "px) calc(100% - " + zoomY + "px)";
  var backgroundSize = NATIVE_IMG.width + "px " + NATIVE_IMG.height + "px";

  // Apply styles to lens
  $blowupLens.css({
    left: lensX,
    top: lensY,
    "background-image": "url(" + encodeURI($IMAGE_URL) + ")",
    "background-size": backgroundSize,
    "background-position": backPos,
    "transform": "rotate(-180deg)" //rotate the image original
  });
})

// Hide magnification lens
$element.mouseleave(function() {
  $blowupLens.css("display", "none");
});
#target {
  margin-left: 160px;
  width: 400;
  height: 250px;
}
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>

<body>
  <img id="target" src="https://iili.io/0hL7ou.png">
</body>

Update: For more general case of image rotation, I'd use an additional absolutely positioned image within the lens instead of background-image. It's way easier to control:

var $pic = $('#target');

// Constants
const lensD = 70, // lens diameter
  src = $pic.attr("src"),
  tоp = $pic.offset().top,
  lеft = $pic.offset().left;

let angle, scaleX, scaleY;

const $lens = $('<div id="lens"/>')
  .css({
    width: lensD,
    height: lensD
  })
  .appendTo('body');
  
const $lensImage = $(`<img src="${src}">`)
  .on('load', function() {
    scaleX = this.width / ($pic.width() || 1);
    scaleY = this.height / ($pic.height() || 1);
  })
  .appendTo($lens);

// Mouse motion on image
$pic.mousemove(function(e) {

  // Lens position
  $lens.css({
    transform: (`
      translateX(${e.pageX - lensD/2}px)
      translateY(${e.pageY - lensD/2}px)
    `)
  });

  // Zoomed image position
  $lensImage.css({
    transform: (`
      translateX(${(lеft - e.pageX) * scaleX + lensD/2}px)
      translateY(${(tоp - e.pageY) * scaleY + lensD/2}px)
      rotateZ(${angle}deg)
    `)
  });
});

// Show magnification lens
$pic.mouseenter(function() {
  $lens.css("display", "block");
});

// Hide magnification lens
$pic.mouseleave(function() {
  $lens.css("display", "none");
});

// Rotation (aux)
$('#angle')
  .on('input', function() {
    $('#a').val((angle = $(this).val()) + 'deg');
    $pic.css({
      transform: `rotateZ(${angle}deg)`
    });
    $lensImage.css({
      transform: `rotateZ(${angle}deg)`
    });
  })
  .trigger('input')
  .val();
#a {
  border: 0
}

#target,
#lens img {
  width: 200px;
  height: 100px;
  margin: 40px;
}

#lens {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 1;
  display: none;
  margin: 0;
  padding: 0;
  border: solid 3px #0003;
  border-radius: 50%;
  overflow: hidden;
  pointer-events: none;
}

#lens img {
  position: absolute;
  width: auto;
  height: auto;
  margin: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

<fieldset>
  Rotate: <input id="a" readonly><br>
  -180 <input type="range" min="-180" max="180" step="1" value="-9" id="angle"> 180
</fieldset>

<img id="target" src="https://picsum.photos/id/111/400/200">

Related