npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

c-sketchpad

v2.5.0

Published

canvas画板

Downloads

132

Readme

sketchpad

DEMO

changelog

中文文档

Use

npm

npm i c-sketchpad --save

const Sketchpad=require('c-sketchpad');

src

<script src="/docs/sketchpad.min.js"></script>

init

const sketchpad = new Sketchpad({
            el: '#demo',
            toolBtnSize: 40,
            height: 400,
            toolList: ['Brush','Line','Polygon','Eraser'],
            saveBtn: true,
            maxRecall: 20
        });

Option

el: '#demo',                    //dom node or string that can be recognized by document.querySelector

toolBtnSize: 40                 //tool button size

height: 400                     //Canvas drawing area height

toolList: ['Brush','Eraser']    //tool array: Brush Eraser Line Polygon

saveBtn: true                   //display the save picture button

maxRecall: 20                   //number of recall

Api

const sketchpad = new Sketchpad({options});


sketchpad.recall();             //active recall

sketchpad.save();               //return the base64 format for drawing the image

sketchpad.clean();              //empty drawn content and all recording

sketchpad.toolRegister();       //receive a custom tool constructor

Custom tool

Custom tool demo

class CustomTool {
    constructor({//The constructor receives 4 parameters. It depends on the tool to decide which canvas to draw on.
        //Pre-canvas for drawing interactive layers Reference Line tool
        frontCanvasEl,//Pre-canvas layer dom node
        frontCanvasCtx,//Pre-canvas getContext('2d') rendering context
        
        //The confirmed drawing element will be drawn in the main canvas layer. The undo function is based on this layer.
        mainCnavasEl,//Main canvcas layer dom node
        mainCanvasCtx //Main canvas rendering context
    }) {

        this.frontCanvasCtx = frontCanvasCtx;
        this.mainCanvasCtx = mainCanvasCtx;


        //tool button node (Required)
        this.btnEl = document.createElement('button');

        //option content node (Required)
        this.optionEl = document.createElement('div');
    }


    //Unified touch and mouse event triggering
    //drawStartFn to touchstart mousedown event
    //drawMoveFn to touchmove mousemove  tip: only respond when drawStartFn triggered
    //drawEndFn to touchend mouseup  tip: only respond when drawStartFn triggered
    //since canvas is HD compatible according to different device DPR, please use canvasX and canvasY as click coordinates.
    drawStartFn(event) {
        const x=event.canvasX;//Encapsulate canvasX as the click coordinate x-axis in the event
        const y=event.canvasY;//Encapsulate canvasY as the click coordinate y axis in the event
    }

    drawMoveFn(event) {
        const x=event.canvasX;//Encapsulate canvasX as the click coordinate x-axis in the event
        const y=event.canvasY;//Encapsulate canvasY as the click coordinate y axis in the event
    }

    drawEndFn(event) {
        //Returns a function that provides the undo function logger to the sketchpad
        const render=(ctx)=>{//Provide a canvas rendering context parameter
            ctx.save();
            ctx.moveTo(0,0);
            ctx.lineTo(30,30);
            ctx.stroke();
            ctx.restore();
        }
        render.needRender=true;//Whether the sketchpad draws on the mainCanvas immediately after receiving the function ()

        return render;
        
        //Return false if there is no drawing operation
        return false;
    }
    
    //Trigger only when the mouse moves over the canvas. Compared to drawMoveFn, drawMoveFn only fires after drawStartFn trigger. When drawStartFn triggers, triggers mouseMoveFn and triggers drawMoveFn.
    //When the mouse function is detected, the mouse will not display the cursor when it is detected. Please draw the interaction in the frontCanvas layer for this tool.
    mousemoveFn(e){
        const x=event.canvasX;//Encapsulate canvasX as the click coordinate x-axis in the event
        const y=event.canvasY;//Encapsulate canvasY as the click coordinate y axis in the event
    }
}



const sketchpad = new Sketchpad({options});


sketchpad.toolRegister(CustomTool);//use registerTool function