Draw Shapes with Color and Line Thickness Options

Draw shapes using HTML5 Canvas with color and line thickness options. Keybinds: c = change color t = change width x = clear all

目前為 2023-03-03 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Draw Shapes with Color and Line Thickness Options
// @namespace    http://tampermonkey
// @author       TN_Playz
// @version      1.5
// @description  Draw shapes using HTML5 Canvas with color and line thickness options. Keybinds: c = change color t = change width x = clear all
// @match        https://*
// @match        https://*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // Create a canvas element
    const canvas = document.createElement('canvas');
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;
    document.body.appendChild(canvas);

    // Set up the canvas
    const ctx = canvas.getContext('2d');
    ctx.lineJoin = 'round';
    ctx.lineCap = 'round';
    let color = 'black';
    let thickness = 5;

    // Initialize the mouse position
    let mouseX = 0;
    let mouseY = 0;

    // Add event listeners to track the mouse position
    canvas.addEventListener('mousemove', (event) => {
        mouseX = event.clientX;
        mouseY = event.clientY;
    });

    // Draw a shape at the current mouse position when the mouse button is clicked
    canvas.addEventListener('mousedown', () => {
        ctx.strokeStyle = color;
        ctx.lineWidth = thickness;
        ctx.beginPath();
        ctx.moveTo(mouseX, mouseY);
        canvas.addEventListener('mousemove', onPaint);
    });

    // Stop drawing the shape when the mouse button is released
    canvas.addEventListener('mouseup', () => {
        canvas.removeEventListener('mousemove', onPaint);
    });

    // Draw a shape as the mouse is moved
    function onPaint() {
        ctx.lineTo(mouseX, mouseY);
        ctx.stroke();
    }

    // Change the color when the 'c' key is pressed
    document.addEventListener('keydown', (event) => {
        if (event.key === 'c') {
            color = prompt('Enter a color name or hex code:', color);
        }
    });

    // Change the line thickness when the 't' key is pressed
    document.addEventListener('keydown', (event) => {
        if (event.key === 't') {
            thickness = prompt('Enter a line thickness (1-50):', thickness);
        }
    });

    // Clear the canvas when the 'x' key is pressed
    document.addEventListener('keydown', (event) => {
        if (event.key === 'x') {
            ctx.clearRect(0, 0, canvas.width, canvas.height);
        }
    });
})();