Fixed mouse position and cleanup code
This commit is contained in:
parent
67c5d36be7
commit
bfc6671788
@ -1,16 +1,9 @@
|
|||||||
class PhotoCalendar {
|
class PhotoCalendar {
|
||||||
dragOptions = {
|
dragOptions = {
|
||||||
isDragging: false,
|
isDragging: false,
|
||||||
lastZoom: 1,
|
|
||||||
MAX_ZOOM: 5,
|
|
||||||
MIN_ZOOM: 0.1,
|
|
||||||
SCROLL_SENSITIVITY: 0.0005,
|
|
||||||
startX: null,
|
|
||||||
startY: null,
|
|
||||||
x: null,
|
x: null,
|
||||||
y: null,
|
y: null,
|
||||||
dragging: false,
|
dragging: false,
|
||||||
mouseOver: false,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
options = {
|
options = {
|
||||||
@ -30,9 +23,9 @@ class PhotoCalendar {
|
|||||||
}
|
}
|
||||||
|
|
||||||
overPhotoImg(x, y) {
|
overPhotoImg(x, y) {
|
||||||
this.mouseOver = y >= 0 && y <= this.options.calendarStartY;
|
return y >= 0 && y <= this.options.calendarStartY;
|
||||||
return this.mouseOver;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getEventLocation(e) {
|
getEventLocation(e) {
|
||||||
if (e.touches && e.touches.length == 1) {
|
if (e.touches && e.touches.length == 1) {
|
||||||
return { x: e.touches[0].clientX, y: e.touches[0].clientY };
|
return { x: e.touches[0].clientX, y: e.touches[0].clientY };
|
||||||
@ -40,27 +33,34 @@ class PhotoCalendar {
|
|||||||
return { x: e.clientX, y: e.clientY };
|
return { x: e.clientX, y: e.clientY };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getMousePositionInCanvas(x,y) {
|
||||||
|
const canvas = this.getCanvas();
|
||||||
|
const rect = canvas.getBoundingClientRect();
|
||||||
|
const scaleX = canvas.width / rect.width;
|
||||||
|
const scaleY = canvas.height / rect.height;
|
||||||
|
const mouseX = (x - rect.left) * scaleX;
|
||||||
|
const mouseY = (y - rect.top) * scaleY;
|
||||||
|
return { x: mouseX, y: mouseY };
|
||||||
|
}
|
||||||
|
|
||||||
onPointerDown(e) {
|
onPointerDown(e) {
|
||||||
this.dragOptions.isDragging = true;
|
|
||||||
let x = this.getEventLocation(e).x;
|
let x = this.getEventLocation(e).x;
|
||||||
let y = this.getEventLocation(e).y;
|
let y = this.getEventLocation(e).y;
|
||||||
|
|
||||||
if (this.overPhotoImg(x, y)) {
|
const mousePosition = this.getMousePositionInCanvas(x,y);
|
||||||
|
if (this.overPhotoImg(mousePosition.x, mousePosition.y)) {
|
||||||
|
this.dragOptions.isDragging = true;
|
||||||
this.dragOptions.x = x - this.options.photoOffsetX;
|
this.dragOptions.x = x - this.options.photoOffsetX;
|
||||||
this.dragOptions.y = y - this.options.photoOffsetY;
|
this.dragOptions.y = y - this.options.photoOffsetY;
|
||||||
this.renderPhoto();
|
this.renderPhoto();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.dragStop();
|
||||||
this.dragOptions.x = x;
|
this.dragOptions.x = x;
|
||||||
this.dragOptions.y = y;
|
this.dragOptions.y = y;
|
||||||
}
|
}
|
||||||
dragStop() {
|
|
||||||
this.dragOptions.isDragging = false;
|
|
||||||
}
|
|
||||||
onPointerUp(e) {
|
|
||||||
this.dragStop();
|
|
||||||
}
|
|
||||||
|
|
||||||
onPointerMove(e) {
|
onPointerMove(e) {
|
||||||
if (this.dragOptions.isDragging) {
|
if (this.dragOptions.isDragging) {
|
||||||
@ -74,19 +74,27 @@ class PhotoCalendar {
|
|||||||
let x = this.getEventLocation(e).x;
|
let x = this.getEventLocation(e).x;
|
||||||
let y = this.getEventLocation(e).y;
|
let y = this.getEventLocation(e).y;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
onPointerUp(e) {
|
||||||
|
this.dragStop();
|
||||||
|
}
|
||||||
|
|
||||||
|
dragStop() {
|
||||||
|
this.dragOptions.isDragging = false;
|
||||||
|
}
|
||||||
|
|
||||||
handleTouch(e, singleTouchHandler) {
|
handleTouch(e, singleTouchHandler) {
|
||||||
if (e.touches.length <= 1) {
|
if (e.touches.length <= 1) {
|
||||||
singleTouchHandler(e);
|
singleTouchHandler(e);
|
||||||
|
|
||||||
if (e.type == "touchend") {
|
if (e.type == "touchend") {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
this.dragOptions.mouseOver = false;
|
this.dragStop();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (e.type == "touchmove" && e.touches.length == 2) {
|
||||||
if (e.type == "touchmove" && e.touches.length == 2)
|
|
||||||
{
|
|
||||||
this.dragStop();
|
this.dragStop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -108,6 +116,7 @@ class PhotoCalendar {
|
|||||||
document.getElementById("imageInput").addEventListener("change", (e) => self.handleImageUpload(e));
|
document.getElementById("imageInput").addEventListener("change", (e) => self.handleImageUpload(e));
|
||||||
document.getElementById("downloadButton").addEventListener("click", (e) => self.downloadCanvas());
|
document.getElementById("downloadButton").addEventListener("click", (e) => self.downloadCanvas());
|
||||||
|
|
||||||
|
// Handle photo position
|
||||||
const canvas = this.getCanvas();
|
const canvas = this.getCanvas();
|
||||||
canvas.addEventListener("mousedown", (e) => self.onPointerDown(e));
|
canvas.addEventListener("mousedown", (e) => self.onPointerDown(e));
|
||||||
canvas.addEventListener("touchstart", (e) => self.handleTouch(e, (e) => self.onPointerDown(e)));
|
canvas.addEventListener("touchstart", (e) => self.handleTouch(e, (e) => self.onPointerDown(e)));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user