Skip to content

Commit fa5cdd9

Browse files
committed
1.3.0 - css transform support
1 parent b4a599d commit fa5cdd9

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

projects/ng-magnizoom/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ng-magnizoom",
3-
"version": "1.2.0",
3+
"version": "1.3.0",
44
"description": "Angular image magnifier (based on HTML canvas)",
55
"repository": {
66
"type": "git",

projects/ng-magnizoom/src/lib/magnizoom.component.ts

+31-4
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,37 @@ export class NgMagnizoomComponent implements OnInit, OnChanges {
225225

226226
calculateMousePosition(clientX: number, clientY: number) {
227227
const boundingRect = this.canvas.getBoundingClientRect();
228-
const viewToModelX = this.canvasWidth / boundingRect.width;
229-
const viewToModelY = this.canvasHeight / boundingRect.height;
230-
const x = (clientX - boundingRect.left) * viewToModelX;
231-
const y = (clientY - boundingRect.top) * viewToModelY;
228+
229+
const boundingRectX = (clientX - boundingRect.left);
230+
const boundingRectY = (clientY - boundingRect.top);
231+
232+
let elementX = boundingRectX, elementY = boundingRectY;
233+
let elementWidth = boundingRect.width, elementHeight = boundingRect.height;
234+
235+
const computedStyle = window.getComputedStyle(this.canvas, null);
236+
const domMatrix = new DOMMatrix(computedStyle.transform);
237+
if (!domMatrix.isIdentity) {
238+
const { a, b } = domMatrix;
239+
const transformRot = Math.atan2(b, a);
240+
const transformScale = Math.round( Math.sqrt(a * a + b * b) * 100 ) / 100;
241+
if (transformRot) {
242+
const boundingRectCenterX = boundingRect.width / 2;
243+
const boundingRectCenterY = boundingRect.height / 2;
244+
const dx = (boundingRectX - boundingRectCenterX);
245+
const dy = (boundingRectY - boundingRectCenterY);
246+
const d = Math.sqrt(dx * dx + dy * dy);
247+
const r = Math.atan2(dy, dx);
248+
elementWidth = this.canvas.clientWidth * transformScale;
249+
elementHeight = this.canvas.clientHeight * transformScale;
250+
elementX = (elementWidth / 2) + (d * Math.cos(r - transformRot));
251+
elementY = (elementHeight / 2) + (d * Math.sin(r - transformRot));
252+
}
253+
}
254+
255+
const viewToModelX = this.canvasWidth / elementWidth;
256+
const viewToModelY = this.canvasHeight / elementHeight;
257+
const x = elementX * viewToModelX;
258+
const y = elementY * viewToModelY;
232259
this._centerPosition = { x, y };
233260
}
234261

src/app/app.component.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ <h1>Ng Magnizoom</h1>
44

55
<h2>Mode</h2>
66
<h3>Lens</h3>
7-
<ng-magnizoom zoomMode="LENS" imageSrc="{{imageSrc}}"></ng-magnizoom>
7+
<ng-magnizoom zoomMode="LENS" imageSrc="{{imageSrc}}" [imageStyle]="{ 'transform-origin':'800px 200px', 'transform': 'scale(0.5) translate(100px, 200px) rotate(40deg)' }"></ng-magnizoom>
88
<h3>Cover</h3>
99
<ng-magnizoom zoomMode="COVER" imageSrc="{{imageSrc}}"></ng-magnizoom>
1010

0 commit comments

Comments
 (0)