sketchpad
Use
npm
npm i c-sketchpad --save const Sketchpad=;
src
init
const 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 = options; sketchpad; //active recall sketchpad; //return the base64 format for drawing the image sketchpad; //empty drawn content and all recording sketchpad; //receive a custom tool constructor
Custom tool
{ thisfrontCanvasCtx = frontCanvasCtx; thismainCanvasCtx = mainCanvasCtx; //tool button node (Required) thisbtnEl = document; //option content node (Required) thisoptionEl = document; } //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. { const x=eventcanvasX;//Encapsulate canvasX as the click coordinate x-axis in the event const y=eventcanvasY;//Encapsulate canvasY as the click coordinate y axis in the event } { const x=eventcanvasX;//Encapsulate canvasX as the click coordinate x-axis in the event const y=eventcanvasY;//Encapsulate canvasY as the click coordinate y axis in the event } { //Returns a function that provides the undo function logger to the sketchpad const render={//Provide a canvas rendering context parameter ctx; ctx; ctx; ctx; ctx; } renderneedRender=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. { const x=eventcanvasX;//Encapsulate canvasX as the click coordinate x-axis in the event const y=eventcanvasY;//Encapsulate canvasY as the click coordinate y axis in the event } const sketchpad = options; sketchpad;//use registerTool function