Image Drop

Drag and drop images onto the canvas to load them

目前為 2020-06-25 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Image Drop
// @version      1.1
// @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==

(function() {
    'use strict';
    
    // Uncomment the lines below to remove the sketchful logo above the canvas
    // document.querySelector("body > div.game > div.gameParent > div.gameHeader").style.display = "none";
    // Add spacing on the top
    // document.querySelector("body > div.game > div.gameParent").style.marginTop = "20px";

    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);

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

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

    function handleFiles(files) {
        files = [...files];
        files.forEach(previewFile);
        files.forEach(previewFile); // Doesn't work unless I do this terribleness
    }

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

    function loadImage(img) {
        if (img.height && img !== null && sketchCanvas.height) {
            let 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
            let startX = Math.floor(sketchCanvas.width/2 - img.width/2);

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

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

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