洛谷绘画增强工具 v2.0 介绍
这是一款专为洛谷刷题场景设计的 Tampermonkey 轻量增强脚本,完全嵌入洛谷原生页面,无需跳转第三方工具,给你的刷题体验加一层实用buff。
核心实用功能
全场景全屏草稿板 按 Alt+D 一键唤起半透明全屏画布,不用再切出页面打开画图软件。做数学推导、画算法流程、模拟递归栈、标记样例路径都能随手画,底层洛谷页面内容完全可见,推导思路和题目内容随时对照,再也不用在草稿纸上反复翻找。
极简专业绘画工具栏
顶部悬浮工具栏零学习成本,6种高辨识度常用颜色(黑/红/蓝/绿/橙/紫),4档笔刷粗细(细/中/粗/极粗),搭配独立橡皮擦按钮,画重点、写公式、擦除修改都能一步到位。所有工具状态自动本地保存,刷新页面也不会重置你习惯的画笔设置。
趣味弹幕氛围加持
内置“今日宜AK IOI”“恭喜AC!”“RP++”等编程圈专属梗,开启后屏幕随机飘过彩色弹幕,刷题刷累了点一下,瞬间拉满刷题氛围感,写代码的枯燥感直接减半。
注:介绍为AI生成
记得看更新!
废话不多上代码
// ==UserScript==
// @name 洛谷绘画增强工具 (Luogu Draw Plus)
// @namespace http://tampermonkey.net/
// @version 2.0.0
// @description 为洛谷页面添加高级绘画板,支持4种粗细、6种颜色及橡皮擦功能,附带趣味弹幕
// @author You
// @match https://www.luogu.com.cn/*
// @match https://www.luogu.com/*
// @icon https://www.luogu.com.cn/favicon.ico
// @grant GM_addStyle
// @grant GM_setValue
// @grant GM_getValue
// @run-at document-idle
// ==/UserScript==
(function() {
'use strict';
// --- 配置与状态管理 ---
const CONFIG_KEY = 'luogu_draw_config_v2';
const defaultConfig = {
brushColor: '#000000',
brushSize: 3, // 默认中等粗细
isEraser: false,
danmakuEnabled: false
};
let config = GM_getValue(CONFIG_KEY, defaultConfig);
let isCanvasOpen = false;
let isDrawing = false;
let lastX = 0;
let lastY = 0;
let danmakuInterval = null;
// 预设颜色 (6种)
const COLORS = [
{ name: '黑', value: '#000000' },
{ name: '红', value: '#ef4444' },
{ name: '蓝', value: '#3b82f6' },
{ name: '绿', value: '#22c55e' },
{ name: '橙', value: '#f97316' },
{ name: '紫', value: '#a855f7' }
];
// 预设粗细 (4种)
const SIZES = [
{ name: '细', value: 2 },
{ name: '中', value: 5 },
{ name: '粗', value: 10 },
{ name: '极粗', value: 20 }
];
// --- 样式注入 ---
GM_addStyle(`
/* 控制面板 */
#ldp-panel {
position: fixed;
bottom: 20px;
right: 20px;
background: rgba(255, 255, 255, 0.95);
backdrop-filter: blur(8px);
border: 1px solid #e5e7eb;
border-radius: 12px;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
padding: 12px;
z-index: 2147483647;
display: flex;
flex-direction: column;
gap: 8px;
font-family: system-ui, -apple-system, sans-serif;
transition: transform 0.3s ease;
min-width: 140px;
}
#ldp-panel.minimized {
transform: translateY(calc(100% - 40px));
}
.ldp-btn {
display: flex;
align-items: center;
justify-content: center;
gap: 8px;
padding: 8px 12px;
border: none;
border-radius: 6px;
cursor: pointer;
font-size: 13px;
font-weight: 500;
transition: all 0.2s;
background: #f3f4f6;
color: #374151;
}
.ldp-btn:hover { background: #e5e7eb; }
.ldp-btn.active { background: #3b82f6; color: white; }
.ldp-btn.active:hover { background: #2563eb; }
.ldp-toggle {
position: absolute;
top: -10px;
right: 10px;
background: #fff;
border: 1px solid #e5e7eb;
border-radius: 50%;
width: 20px;
height: 20px;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
font-size: 10px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
z-index: 10;
}
/* 画布 */
#ldp-canvas {
position: fixed;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
z-index: 2147483646;
cursor: crosshair;
background-color: rgba(255, 255, 255, 0.85);
display: none;
touch-action: none;
}
/* 画布工具栏 */
#ldp-toolbar {
position: fixed;
top: 20px;
left: 50%;
transform: translateX(-50%);
background: white;
padding: 10px 20px;
border-radius: 50px;
box-shadow: 0 4px 15px rgba(0,0,0,0.15);
z-index: 2147483647;
display: none;
gap: 15px;
align-items: center;
border: 1px solid #e5e7eb;
flex-wrap: wrap;
justify-content: center;
max-width: 90vw;
}
.ldp-tool-group {
display: flex;
align-items: center;
gap: 5px;
padding: 0 5px;
border-right: 1px solid #eee;
}
.ldp-tool-group:last-child { border-right: none; }
.ldp-color-btn {
width: 20px;
height: 20px;
border-radius: 50%;
border: 2px solid transparent;
cursor: pointer;
transition: transform 0.2s;
}
.ldp-color-btn:hover { transform: scale(1.2); }
.ldp-color-btn.selected { border-color: #333; transform: scale(1.1); }
.ldp-size-btn {
padding: 2px 8px;
border-radius: 4px;
border: 1px solid #ddd;
background: white;
cursor: pointer;
font-size: 12px;
}
.ldp-size-btn.selected { background: #3b82f6; color: white; border-color: #3b82f6; }
.ldp-eraser-btn {
padding: 4px 8px;
border-radius: 4px;
border: 1px solid #ddd;
background: white;
cursor: pointer;
font-size: 12px;
display: flex;
align-items: center;
gap: 4px;
}
.ldp-eraser-btn.active { background: #fca5a5; border-color: #ef4444; color: #991b1b; }
/* 弹幕 */
.ldp-danmaku {
position: fixed;
white-space: nowrap;
font-weight: bold;
text-shadow: 1px 1px 2px rgba(0,0,0,0.6);
z-index: 2147483645;
pointer-events: none;
animation: ldp-slide 8s linear forwards;
font-family: "Microsoft YaHei", sans-serif;
}
@keyframes ldp-slide {
from { transform: translateX(100vw); }
to { transform: translateX(-100%); }
}
`);
// --- 核心逻辑 ---
// 1. 初始化 UI
function initUI() {
// 控制面板
const panel = document.createElement('div');
panel.id = 'ldp-panel';
panel.innerHTML = `
<div class="ldp-toggle" id="ldp-minimize">−</div>
<button class="ldp-btn" id="btn-canvas"><i class="fa-solid fa-pen"></i> 绘画板 (Alt+D)</button>
<button class="ldp-btn" id="btn-danmaku"><i class="fa-solid fa-comment-dots"></i> 弹幕护体</button>
<div style="font-size:10px; color:#9ca3af; text-align:center; margin-top:4px;">Luogu Draw v2.0</div>
`;
document.body.appendChild(panel);
// 画布
const canvas = document.createElement('canvas');
canvas.id = 'ldp-canvas';
document.body.appendChild(canvas);
// 工具栏
const toolbar = document.createElement('div');
toolbar.id = 'ldp-toolbar';
// 构建颜色选择器
let colorHtml = '<div class="ldp-tool-group">';
COLORS.forEach(c => {
colorHtml += `<div class="ldp-color-btn ${config.brushColor === c.value && !config.isEraser ? 'selected' : ''}"
style="background:${c.value}" data-color="${c.value}" title="${c.name}"></div>`;
});
colorHtml += '</div>';
// 构建粗细选择器
let sizeHtml = '<div class="ldp-tool-group">';
SIZES.forEach(s => {
sizeHtml += `<button class="ldp-size-btn ${config.brushSize === s.value && !config.isEraser ? 'selected' : ''}"
data-size="${s.value}" title="${s.name}">${s.name}</button>`;
});
sizeHtml += '</div>';
// 橡皮擦和清空
const eraserHtml = `
<div class="ldp-tool-group">
<button class="ldp-eraser-btn ${config.isEraser ? 'active' : ''}" id="btn-eraser">
<i class="fa-solid fa-eraser"></i> 橡皮擦
</button>
<button class="ldp-btn" id="btn-clear" style="padding:4px 8px; font-size:12px; background:#fee2e2; color:#991b1b;">
<i class="fa-solid fa-trash"></i> 清空
</button>
<button class="ldp-btn" id="btn-close-canvas" style="padding:4px 8px; font-size:12px; background:#ef4444; color:white;">
<i class="fa-solid fa-times"></i> 关闭
</button>
</div>
`;
toolbar.innerHTML = colorHtml + sizeHtml + eraserHtml;
document.body.appendChild(toolbar);
bindEvents();
updateDanmakuState();
}
// 2. 事件绑定
function bindEvents() {
const panel = document.getElementById('ldp-panel');
const canvas = document.getElementById('ldp-canvas');
const ctx = canvas.getContext('2d');
const toolbar = document.getElementById('ldp-toolbar');
// 最小化面板
document.getElementById('ldp-minimize').onclick = () => {
panel.classList.toggle('minimized');
document.getElementById('ldp-minimize').innerText =
panel.classList.contains('minimized') ? '+' : '−';
};
// 开启/关闭画布
document.getElementById('btn-canvas').onclick = toggleCanvas;
document.getElementById('btn-close-canvas').onclick = toggleCanvas;
// 弹幕开关
document.getElementById('btn-danmaku').onclick = () => {
config.danmakuEnabled = !config.danmakuEnabled;
saveConfig();
updateDanmakuState();
};
// 画布绘图逻辑
canvas.addEventListener('mousedown', startDrawing);
canvas.addEventListener('mousemove', draw);
canvas.addEventListener('mouseup', stopDrawing);
canvas.addEventListener('mouseout', stopDrawing);
// 颜色选择
toolbar.querySelectorAll('.ldp-color-btn').forEach(btn => {
btn.onclick = () => {
config.isEraser = false;
config.brushColor = btn.dataset.color;
saveConfig();
updateToolbarUI();
ctx.globalCompositeOperation = 'source-over';
ctx.strokeStyle = config.brushColor;
};
});
// 粗细选择
toolbar.querySelectorAll('.ldp-size-btn').forEach(btn => {
btn.onclick = () => {
config.isEraser = false;
config.brushSize = parseInt(btn.dataset.size);
saveConfig();
updateToolbarUI();
ctx.lineWidth = config.brushSize;
};
});
// 橡皮擦
document.getElementById('btn-eraser').onclick = () => {
config.isEraser = !config.isEraser;
saveConfig();
updateToolbarUI();
if (config.isEraser) {
ctx.globalCompositeOperation = 'destination-out';
} else {
ctx.globalCompositeOperation = 'source-over';
ctx.strokeStyle = config.brushColor;
}
};
// 清空画布
document.getElementById('btn-clear').onclick = () => {
ctx.clearRect(0, 0, canvas.width, canvas.height);
};
// 键盘快捷键
document.addEventListener('keydown', (e) => {
if (e.altKey && e.key === 'd') {
e.preventDefault();
toggleCanvas();
}
if (e.key === 'Escape' && isCanvasOpen) {
toggleCanvas();
}
});
// 窗口大小改变时重置画布尺寸
window.addEventListener('resize', () => {
if (isCanvasOpen) {
resizeCanvas();
}
});
}
// 3. 画布功能实现
function toggleCanvas() {
const canvas = document.getElementById('ldp-canvas');
const toolbar = document.getElementById('ldp-toolbar');
const btn = document.getElementById('btn-canvas');
isCanvasOpen = !isCanvasOpen;
if (isCanvasOpen) {
canvas.style.display = 'block';
toolbar.style.display = 'flex';
btn.classList.add('active');
resizeCanvas();
// 初始化画笔状态
const ctx = canvas.getContext('2d');
ctx.lineJoin = 'round';
ctx.lineCap = 'round';
if (config.isEraser) {
ctx.globalCompositeOperation = 'destination-out';
} else {
ctx.globalCompositeOperation = 'source-over';
ctx.strokeStyle = config.brushColor;
ctx.lineWidth = config.brushSize;
}
} else {
canvas.style.display = 'none';
toolbar.style.display = 'none';
btn.classList.remove('active');
}
}
function resizeCanvas() {
const canvas = document.getElementById('ldp-canvas');
// 保存当前画面内容
const tempCanvas = document.createElement('canvas');
const tempCtx = tempCanvas.getContext('2d');
tempCanvas.width = canvas.width;
tempCanvas.height = canvas.height;
if (canvas.width > 0 && canvas.height > 0) {
tempCtx.drawImage(canvas, 0, 0);
}
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// 恢复画面
const ctx = canvas.getContext('2d');
ctx.drawImage(tempCanvas, 0, 0);
ctx.lineJoin = 'round';
ctx.lineCap = 'round';
// 重新应用当前工具状态
if (config.isEraser) {
ctx.globalCompositeOperation = 'destination-out';
} else {
ctx.globalCompositeOperation = 'source-over';
ctx.strokeStyle = config.brushColor;
ctx.lineWidth = config.brushSize;
}
}
function startDrawing(e) {
isDrawing = true;
[lastX, lastY] = [e.offsetX, e.offsetY];
}
function draw(e) {
if (!isDrawing) return;
const canvas = document.getElementById('ldp-canvas');
const ctx = canvas.getContext('2d');
ctx.beginPath();
ctx.moveTo(lastX, lastY);
ctx.lineTo(e.offsetX, e.offsetY);
ctx.stroke();
[lastX, lastY] = [e.offsetX, e.offsetY];
}
function stopDrawing() {
isDrawing = false;
}
// 4. UI 更新辅助函数
function updateToolbarUI() {
// 更新颜色选中状态
document.querySelectorAll('.ldp-color-btn').forEach(btn => {
if (btn.dataset.color === config.brushColor && !config.isEraser) {
btn.classList.add('selected');
} else {
btn.classList.remove('selected');
}
});
// 更新粗细选中状态
document.querySelectorAll('.ldp-size-btn').forEach(btn => {
if (parseInt(btn.dataset.size) === config.brushSize && !config.isEraser) {
btn.classList.add('selected');
} else {
btn.classList.remove('selected');
}
});
// 更新橡皮擦状态
const eraserBtn = document.getElementById('btn-eraser');
if (config.isEraser) {
eraserBtn.classList.add('active');
} else {
eraserBtn.classList.remove('active');
}
}
// 5. 弹幕功能实现
const danmakuTexts = [
"今日宜AK IOI", "恭喜AC!", "RP++", "这题水过", "注意long long",
"从入门到放弃", "Orz 大佬", "一发入魂", "TLE警告", "RE快乐",
"代码一遍过", "样例即答案", "提交即AC", "Python大法好", "C++永不为奴",
"动态规划不会写", "搜索剪枝很重要", "记得开O2", "栈溢出警告"
];
const danmakuColors = [
"#ef4444", "#f97316", "#eab308", "#22c55e", "#06b6d4", "#3b82f6", "#a855f7", "#ec4899"
];
function updateDanmakuState() {
const btn = document.getElementById('btn-danmaku');
if (config.danmakuEnabled) {
btn.classList.add('active');
startDanmaku();
} else {
btn.classList.remove('active');
stopDanmaku();
}
}
function startDanmaku() {
if (danmakuInterval) clearInterval(danmakuInterval);
createDanmaku(); // 立即发一条
danmakuInterval = setInterval(createDanmaku, 1800);
}
function stopDanmaku() {
if (danmakuInterval) clearInterval(danmakuInterval);
document.querySelectorAll('.ldp-danmaku').forEach(el => el.remove());
}
function createDanmaku() {
const text = danmakuTexts[Math.floor(Math.random() * danmakuTexts.length)];
const color = danmakuColors[Math.floor(Math.random() * danmakuColors.length)];
const el = document.createElement('div');
el.className = 'ldp-danmaku';
el.innerText = text;
el.style.top = Math.random() * 80 + 10 + '%'; // 10% - 90% 高度
el.style.color = color;
el.style.fontSize = (14 + Math.random() * 8) + 'px';
document.body.appendChild(el);
// 动画结束后移除
setTimeout(() => {
if (el.parentNode) el.parentNode.removeChild(el);
}, 8000);
}
// 6. 工具函数
function saveConfig() {
GM_setValue(CONFIG_KEY, config);
}
// 7. 启动
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', initUI);
} else {
initUI();
}
})();