PPIL

Pixel Place Image Loader

目前为 2022-05-09 提交的版本,查看 最新版本

此脚本不应直接安装,它是供其他脚本使用的外部库。如果你需要使用该库,请在脚本元属性加入:// @require https://update.gf.qytechs.cn/scripts/443894/1048547/PPIL.js

// ==UserScript==
// @name         PPIL
// @description  Pixel Place Image Loader
// @version      1.5.1
// @author       0vC4
// @namespace    https://gf.qytechs.cn/users/670183
// @match        https://pixelplace.io/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=pixelplace.io
// @license      MIT
// @grant        none
// @run-at       document-start
// ==/UserScript==





const PPIL = (() => {
    if (window.PPIL) return window.PPIL;
    const log = console.log;
  

  
    const worker = function(func) {
        const worker = new Worker(URL.createObjectURL(new Blob(["onmessage=async({data})=>self.postMessage(await(" + func.toString() + ")(data))"], { type: "text/javascript" })));
        const root = {};
        const post = data => (worker.postMessage(data), root);
        const then = callback => (worker.onmessage=({data})=>callback(data), root);
        return Object.assign(root, {post, then});
    };
  

  
    const PPIL = {
        config({colors, exclude, zero}) {
            Object.assign(PPIL, {
                colors,
                exclude,
                zero
            });
        }
    };



    PPIL.loadImage = (w, h) => callback => {
        const dropArea = document.createElement('div');
        top.document.body.appendChild(dropArea);
        dropArea.style = "width: calc(100% - 2em);height: calc(100% - 2em);position: fixed;left: 0px;top: 0px;background-color: rgba(0, 0, 0, 0.533);z-index: 9999;display: flex;color: white;font-size: 48pt;justify-content: center;align-items: center;border: 3px white dashed;border-radius: 18px;margin: 1em;";
        dropArea.textContent = "Drop Image";
        dropArea.onclick = e => dropArea.remove();
  
        ['dragenter','dragover','dragleave','drop'].forEach(eName =>
            dropArea.addEventListener(eName, e => {
                e.preventDefault();
                e.stopPropagation();
            }, false)
        );
  
        dropArea.addEventListener('drop', e => {
  
            const reader = new FileReader();
            reader.readAsDataURL(e.dataTransfer.files[0]);
            reader.onload = e => {
  
                const img = new Image;
                img.src = reader.result;
                img.onload = e => {
                    let {width, height} = img;
                    if (w != null) width = w;
                    if (h != null) height = h;
                    const canvas = document.createElement('canvas');
                    canvas.width = width;
                    canvas.height = height;
                    const ctx = canvas.getContext('2d');
                    ctx.drawImage(img, 0, 0, width, height);
                    const rgba = ctx.getImageData(0, 0, width, height).data;
                    const {colors, exclude, zero} = PPIL;
  
                    worker(async ({rgba, width, height, colors, exclude, zero}) => {
                        const palette = [...colors.map(p => exclude.includes(p) ? zero : p)]
                        .filter(p => p != zero)
                        .map(clr => [
                            (clr>>16)&0xFF,
                            (clr>>8)&0xFF,
                            clr&0xFF
                        ]);
  
                        const toPixel = (r, g, b) => colors.indexOf(
                            palette
                            .map(([r2, g2, b2]) => [
                                ((r2-r)**2 + (g2-g)**2 + (b2-b)**2)*0x1000000 + (r2<<16) + (g2<<8) + b2
                            ])
                            .sort((a,b) => a-b)[0] & 0xFFFFFF
                        );
  
                        const pixels = new Uint8Array(rgba.length>>2);
                        for (let i = 0; i < rgba.length; i += 4)
                            pixels[i>>2] = rgba[i+3] >= 0xAA ? toPixel(rgba[i], rgba[i+1], rgba[i+2]) : -1;
  
                        return [pixels, width, height];
  
                    })
                    .then(callback)
                    .post({rgba,width,height,colors,exclude,zero});
                };
            };
  
            dropArea.remove();
        },false);
    };
  

  
    window.PPIL = PPIL;
    return PPIL;
  })();
  // 0vC4#7152

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址