ref: 优化取色和截屏

This commit is contained in:
muwoo 2021-07-16 17:09:25 +08:00
parent d824f503e6
commit 7783d9520d
2 changed files with 50 additions and 16 deletions

View File

@ -3,6 +3,7 @@ import {exec, spawn} from "child_process";
import robot from "robotjs";
import Api from "./api";
import ioHook from 'iohook';
import {throttle} from './utils';
const browsers = require("../browsers")();
const {picker, separator, superPanel} = browsers;
@ -72,6 +73,20 @@ class Listener {
}
init(mainWindow) {
this.fn = throttle(({x, y}, picker) => {
const img = robot.screen.capture(parseInt(x) - 5, parseInt(y) - 5, 9, 9);
const colors = {}
for(let i = 0; i< 9; i++) {
colors[i] = {};
for (let j = 0; j < 9; j++) {
colors[i][j] = img.colorAt(j, i);
}
}
picker.getWindow().webContents.send("updatePicker", colors);
}, 100);
this.colorPicker();
this.initPlugin();
this.lockScreen();
@ -123,27 +138,12 @@ class Listener {
this.closePicker();
});
});
ioHook.on('mousemove', e => {
let x = e.x
let y = e.y
if (!picker.getWindow()) return;
let color = "#" + robot.getPixelColor(parseInt(x), parseInt(y));
picker.getWindow().setPosition(parseInt(x) + 10, parseInt(y) + 10);
const img = robot.screen.capture(parseInt(x) - 5, parseInt(y) - 5, 9, 9);
const colors = {}
for(let i = 0; i< 9; i++) {
colors[i] = {};
for (let j = 0; j < 9; j++) {
colors[i][j] = img.colorAt(j, i);
}
}
picker.getWindow().webContents.send("updatePicker", colors);
this.fn(e, picker);
})
ioHook.on('mouseup', e => {

View File

@ -19,3 +19,37 @@ export function getData(path, defaultValue) {
return defaultValue || undefined;
}
}
export function throttle (func, wait, options) {
let context, args, result;
let timeout = null;
let previous = 0;
if (!options) options = {};
let later = function() {
previous = options.leading === false ? 0 : Date.now();
timeout = null;
result = func.apply(context, args);
if (!timeout) context = args = null;
};
return function() {
let now = Date.now();
if (!previous && options.leading === false) previous = now;
// 计算剩余时间
let remaining = wait - (now - previous);
context = this;
args = arguments;
if (remaining <= 0 || remaining > wait) {
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
previous = now;
result = func.apply(context, args);
if (!timeout) context = args = null;
} else if (!timeout && options.trailing !== false) {
timeout = setTimeout(later, remaining);
}
return result;
};
}