Image Drop

Drag and drop images onto the canvas to load them

이 스크립트를 설치하려면 Tampermonkey, Greasemonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

You will need to install an extension such as Tampermonkey to install this script.

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Userscripts와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 유저 스크립트 관리자 확장 프로그램이 필요합니다.

(이미 유저 스크립트 관리자가 설치되어 있습니다. 설치를 진행합니다!)

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

(이미 유저 스타일 관리자가 설치되어 있습니다. 설치를 진행합니다!)

// ==UserScript==
// @name         Image Drop
// @version      1.2
// @description  Drag and drop images onto the canvas to load them
// @author       Bell
// @namespace    https://greasyfork.org/users/281093
// @match        https://sketchful.io/
// @grant        none
// jshint esversion: 6
// ==/UserScript==

document.querySelector('#private').style.display = 'inline';
const sketchCanvas = document.querySelector('#canvas');
const sketchCtx = sketchCanvas.getContext('2d');

sketchCanvas.addEventListener('dragenter', highlight, false);
sketchCanvas.addEventListener('dragleave', unhighlight, false);
sketchCanvas.addEventListener('drop', handleDrop, false);
sketchCanvas.addEventListener('dragover', function(event) {
	event.preventDefault();
}, false);

sketchCanvas.save = () => {
	canvas.dispatchEvent(new MouseEvent('pointerup', {
		bubbles: true,
		clientX: 0,
		clientY: 0,
		button: 0
	}));
};

function handleDrop(e) {
	e.preventDefault();
	sketchCanvas.style.filter = '';
	const dt = e.dataTransfer;
	const files = dt.files;

	if (files.length && files !== null) {
		handleFiles(files);
	}
}

function handleFiles(files) {
	([...files]).forEach(previewFile);
}

function previewFile(file) {
	const reader = new FileReader();
	reader.readAsDataURL(file);
	reader.onloadend = () => {
		const img = document.createElement('img');
		img.src = reader.result;
		img.onload = () => { loadImage(img); };
	};
}

function loadImage(img) {
	if (img.height && img !== null && sketchCanvas.height) {
		const heightRatio = img.height / sketchCanvas.height;

		// Scale the image to fit the canvas
		if (heightRatio && heightRatio != 1) {
			img.height /= heightRatio;
			img.width /= heightRatio;
		}

		// Center the image
		const startX = Math.floor(sketchCanvas.width / 2 - img.width / 2);

		sketchCtx.drawImage(img, startX, 0, img.width, img.height);
		sketchCanvas.save();
	}
}

function highlight(e) {
	e.preventDefault();
	sketchCanvas.style.filter = 'brightness(0.5)';
}

function unhighlight(e) {
	e.preventDefault();
	sketchCanvas.style.filter = '';
}