Telegram Web K with changes to work inside I2P https://web.telegram.i2p/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

254 lines
7.3 KiB

3 years ago
/*
* https://github.com/morethanwords/tweb
* Copyright (C) 2019-2021 Eduard Kuzmenko
* https://github.com/morethanwords/tweb/blob/master/LICENSE
*/
function resizeableImage(originalImage: HTMLImageElement, canvas?: HTMLCanvasElement) {
2 years ago
let cropComponent: HTMLDivElement,
container: HTMLDivElement,
cropImage: HTMLImageElement,
event_state: Partial<{
mouse_x: number,
mouse_y: number,
container_width: number,
container_height: number,
container_left: number,
3 years ago
container_top: number
2 years ago
}> = {},
keyZoomValue = 4.0,
MINWIDTH = 50,
MINHEIGHT = 50,
CROPWIDTH = 200,
CROPHEIGHT = 200,
cropLeft = 0,
cropTop = 0,
cropWidth = 0,
3 years ago
cropHeight = 0,
scaledRatio = 0;
2 years ago
3 years ago
if(originalImage.complete) init();
else originalImage.onload = init;
2 years ago
3 years ago
function removeHandlers() {
container.removeEventListener('mousedown', startMoving);
container.removeEventListener('touchstart', startMoving);
container.removeEventListener('wheel', resizing);
2 years ago
3 years ago
document.removeEventListener('mouseup', endMoving);
document.removeEventListener('touchend', endMoving);
document.removeEventListener('mousemove', moving);
document.removeEventListener('touchmove', moving);
document.removeEventListener('keypress', keyHandler);
cropComponent.remove();
container.remove();
cropImage.remove();
}
2 years ago
3 years ago
function addHandlers() {
container.addEventListener('mousedown', startMoving, false);
container.addEventListener('touchstart', startMoving, false);
container.addEventListener('wheel', resizing, false);
2 years ago
3 years ago
document.addEventListener('keypress', keyHandler, false);
2 years ago
// document.querySelector('.btn-crop').addEventListener('click', openCropCanvasImg);
3 years ago
}
2 years ago
3 years ago
function init() {
originalImage.classList.add('crop-blur');
originalImage.draggable = false;
2 years ago
3 years ago
cropImage = new Image();
cropImage.src = originalImage.src;
cropImage.draggable = false;
cropImage.classList.add('crop-overlay-image');
2 years ago
3 years ago
if(!canvas) {
canvas = document.createElement('canvas');
}
2 years ago
3 years ago
cropComponent = document.createElement('div');
cropComponent.classList.add('crop-component');
2 years ago
3 years ago
container = document.createElement('div');
container.classList.add('crop-overlay');
2 years ago
3 years ago
const overlayColor = document.createElement('div');
overlayColor.classList.add('crop-overlay-color');
2 years ago
3 years ago
cropComponent.appendChild(container);
const wrapper = originalImage.parentNode as HTMLElement;
wrapper.appendChild(cropComponent);
cropComponent.appendChild(cropImage);
cropComponent.appendChild(originalImage);
cropComponent.appendChild(overlayColor);
container.appendChild(cropImage);
cropImage.style.maxWidth = originalImage.width + 'px';
scaledRatio = originalImage.naturalWidth / originalImage.offsetWidth;
2 years ago
3 years ago
const left = originalImage.offsetWidth / 2 - CROPWIDTH / 2;
const top = originalImage.offsetHeight / 2 - CROPHEIGHT / 2;
2 years ago
3 years ago
updateCropSize(CROPWIDTH, CROPHEIGHT);
updateCropImage(left, top);
updateContainer(left, top);
addHandlers();
2 years ago
// crop();
3 years ago
}
2 years ago
3 years ago
function updateCropSize(width: number, height: number) {
cropWidth = width * scaledRatio;
cropHeight = height * scaledRatio;
container.style.width = width + 'px';
container.style.height = height + 'px';
}
2 years ago
3 years ago
function updateCropImage(left: number, top: number) {
cropTop = top * scaledRatio;
cropLeft = left * scaledRatio;
cropImage.style.top = -top + 'px';
cropImage.style.left = -left + 'px';
}
2 years ago
3 years ago
function updateContainer(left: number, top: number) {
container.style.top = top + 'px';
container.style.left = left + 'px';
}
2 years ago
3 years ago
// Save the initial event details and container state
function saveEventState(e: any) {
event_state.container_width = container.offsetWidth;
event_state.container_height = container.offsetHeight;
2 years ago
3 years ago
event_state.container_left = container.offsetLeft;
event_state.container_top = container.offsetTop;
2 years ago
3 years ago
event_state.mouse_x = (e.clientX || e.pageX || e.touches && e.touches[0].clientX) + window.scrollX;
event_state.mouse_y = (e.clientY || e.pageY || e.touches && e.touches[0].clientY) + window.scrollY;
}
2 years ago
3 years ago
function imgZoom(zoom: number) {
zoom = zoom * Math.PI * 2
2 years ago
let newWidth = Math.floor(container.clientWidth + zoom),
newHeight = Math.floor(container.clientHeight + zoom),
w = cropImage.clientWidth,
h = cropImage.clientHeight,
left: number,
top: number,
right: number,
3 years ago
bottom: number;
2 years ago
3 years ago
if(newWidth < MINWIDTH) {
return;
} else if(newWidth > w) {
return;
}
2 years ago
3 years ago
left = container.offsetLeft - (zoom / 2);
top = container.offsetTop - (zoom / 2);
right = left + newWidth;
bottom = top + newHeight;
2 years ago
3 years ago
if(left < 0) left = 0;
if(top < 0) top = 0;
if(right > w) return;
if(bottom > h) return;
updateCropSize(newWidth, newWidth);
updateCropImage(left, top);
updateContainer(left, top);
2 years ago
// crop();
3 years ago
}
2 years ago
3 years ago
function keyHandler(e: KeyboardEvent) {
e.preventDefault();
2 years ago
switch(String.fromCharCode(e.charCode)) {
case '+':
imgZoom(keyZoomValue);
break;
case '-':
imgZoom(-keyZoomValue);
break;
3 years ago
}
}
2 years ago
3 years ago
function resizing(e: any) {
e.preventDefault();
imgZoom(e.deltaY > 0 ? 1 : -1);
}
2 years ago
3 years ago
function startMoving(e: MouseEvent | TouchEvent) {
e.preventDefault();
e.stopPropagation();
2 years ago
3 years ago
saveEventState(e);
2 years ago
3 years ago
document.addEventListener('mousemove', moving);
document.addEventListener('touchmove', moving);
document.addEventListener('mouseup', endMoving);
document.addEventListener('touchend', endMoving);
}
2 years ago
3 years ago
function endMoving(e: MouseEvent | TouchEvent) {
e.preventDefault();
2 years ago
3 years ago
document.removeEventListener('mouseup', endMoving);
document.removeEventListener('touchend', endMoving);
document.removeEventListener('mousemove', moving);
document.removeEventListener('touchmove', moving);
}
2 years ago
3 years ago
function moving(e: any) {
2 years ago
let currentTouch = {x: 0, y: 0},
left: number,
top: number,
w: number,
3 years ago
h: number;
2 years ago
3 years ago
e.preventDefault();
e.stopPropagation();
2 years ago
3 years ago
currentTouch.x = e.pageX || e.touches && e.touches[0].pageX;
currentTouch.y = e.pageY || e.touches && e.touches[0].pageY;
2 years ago
3 years ago
left = currentTouch.x - (event_state.mouse_x - event_state.container_left);
top = currentTouch.y - (event_state.mouse_y - event_state.container_top);
w = container.offsetWidth;
h = container.offsetHeight;
2 years ago
3 years ago
if(left < 0) left = 0;
else if(left > cropImage.offsetWidth - w) left = cropImage.offsetWidth - w;
if(top < 0) top = 0;
else if(top > cropImage.offsetHeight - h) top = cropImage.offsetHeight - h;
2 years ago
3 years ago
updateCropImage(left, top);
updateContainer(left, top);
2 years ago
// crop();
3 years ago
}
function crop() {
canvas.width = cropWidth;
canvas.height = cropHeight;
2 years ago
3 years ago
const ctx = canvas.getContext('2d');
ctx.drawImage(originalImage,
cropLeft, cropTop,
cropWidth, cropHeight,
0, 0,
cropWidth, cropHeight
);
}
2 years ago
3 years ago
return {crop, removeHandlers};
}
export default resizeableImage;