Image Drop

Drag and drop images onto the canvas to load them

当前为 2020-06-02 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Image Drop
// @version      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';
    document.querySelector("body > div.game > div.gameParent > div.gameHeader").remove();
    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 = "";
    }
})();