0.0.7-release • Published 1 month ago

paper-pen v0.0.7-release

Weekly downloads
-
License
MIT
Repository
github
Last release
1 month ago

paper-pen.js

canvas、 annotation、annotate、Rect、Polygon、Line、Dot、Text、Matrix.

  • Quick start

npm i paper-pen --save

<script src='https://unpkg.com/paper-pen@0.0.5/dist/paper-pen.esm.min.js'></script>

online example

  • features (待实现)

    • mask 超像素标注
    • group 编组
    • text editor
  • demo

    //demo.html
    <canvas id='c'></canvas>
    // demo.js
    import { Canvas } from 'paper-pen'
    let paperPen = new Canvas(
        '#c', // id 、class string (#c, .c)| HTMLCanvasElement
        { // options
            width: 1600, // canvas width
            height: 700, // canvas height
            preventDefault: true, // prevent right click
            selection: true, // can or not renderTop
            skipFindTarget: false // skip find target, default false
        }
    )
    paperPen.setBackground('Your Image Url') // set background
    paperPen.on('img:load', () => {
        // do anything
    })

Now you hava a 1600 * 700 canvas and set a background image for it.

  • 是否开启滚轮缩放 (Whether to enable wheel scaling)

wheel: {scale: true}

    import { Canvas } from 'paper-pen'
    let canvas = new Canvas({
        '#c',
        {
            wheel: {
                scale: true, // 开启此项,会使滚轮支持画布缩放
                scroll: true, // 开启此项,会使鼠标滚轮在画布中禁用滚动条
            }
        }
    })
  • 是否开启鼠标右键拖动画布(Do you want to enable the right mouse button to drag the canvas)

rightMove: true

import { Canvas } from 'paper-pen'
    let canvas = new Canvas({
        '#c',
        {
            rightMove: true
        }
    })
  • How to draw other shape(Rect、Polygon、Line、Point、and so on.)

    • For example --- Rect
      import { Rect, Polygon } from 'paper-pen'
      let rect = new Rect({
          left: 300,
          top: 300,
          width: 100,
          height: 100,
          stroke: '#000',
          opacity: 0.5
      })
      paperPen.add(rect)
    
      let poly = new Polygon({
          points: [{x: 100, y: 100}, {x: 200, y:100}, {x:150, y: 350}],
          stroke: '#0f0',
          opacity: .5,
          fill: '#00f',
          lineWidth: 5
      })
      paperPen.add(poly)

    Now you get a Rect

    this Library provide (Rect、Polygon、Line、Point、CrossLine、Text、Ruler、Arrow), Of course you can custom other Shapes by this Library.

API

  • class

    need to call with 'new' new Rect() new Line()...

namedescription
CanvasCanvas Instance
Rectdraw Rectangle, Instance
Polygondraw Polygon, Instance
Pointdraw Point, Instance
Linedraw Line, Instance
CrossLinedraw CrossLine, Instance
Textdraw Text, Instance
Rulerdraw Ruler, Instance
Arrowdraw Arrow, Instance
MatrixMatrix transform
  • Canvas properties
nametypedescription
widthnumbercanvas width
heightnumbercanvas height
skipFindTargetbooleanskip find element
preventDefaultbooleanprevent right click
selectionbooleancan or not renderTop
transformMatrixobjectmatrix {a,b,c,d,e,f}
_objectsarrayshape, shape
imageFillModestring'contain'、'cover',针对backgroundImage
wheelobject鼠标滚轮相关
wheel.scaleboolean是否开启滚轮缩放,default false
wheel.scrollboolean是否缩放时禁用浏览器滚动条 default false
wheel.maxnumber缩放最大范围
wheel.minnumber缩放最小范围
rightMoveboolean右键移动画布 default false
  • shape common properties
nametypedescription
strokestringcanvas strokeStyle
fillstringcanvas fillStyle
lineWidthnumberlineWidth
opacitynumber0-1 globalAlpha
cornerSizenumber
cornerStylestring'squarecircle'
cornerBorderColorstringcolor
cornerColorstring
cornerOpacitynumber0 - 1
lockMoveboolean
displayGraphboolean用来决定是否显示当前图形default true
needControlboolean是否显示控制点 default true
  • shape common methods
nametypedescription
getBoundingBox获取包围盒
getCommonConfig获取控制点默认配置
  • rect properties
nametypedescription
leftnumber
topnumber
widthnumberwidth
heightnumberheight

-polygon properties

nametypedescription
pointsArray<{x: number, y: number}>
needArrowboolean是否需要起始箭头
needCenterControlboolean是否需要线段中点
centerPointsStylestring'square'| 'circle' 中点样式
centerPointsSizenumber中点大小
centerPointsStrokestring中点描边颜色
centerPointsFillstring中点填充颜色
  • instance methods
namedescriptionparameterreturn
setBackgroundset canvas backgroundimgUrl|Image|Video|Canvas|bitmap
getPointerObtain the coordinates of the mouse on the canvasnull{x number, y:number}
addAdd elements to object listDrawObject
removeremove elements from object listDrawObject
setActiveObjectset the current object to activeDrawObject
setCursorset the cursor of canvascss cursor
resetActiveDeactivate all objectsnull
toJSONget all elements on the canvasnull{},{}
requestRenderAllclear all Objects on the canvas, draw all objects on the canvasnull
toDataUrl将画布转换为图片(Convert Canvas to Pictures)null{imgData, imgUrl}
toBitMap将画布转换为单通道位图(Convert the canvas to a single channel bitmap){r:number,g: number,b:number,a?:number},...|nullnew Promise (ImageBitmap) 不传参数,默认转为二值图(黑白)
off卸载指定事件(事件名称,回调函数)
offAll卸载监听事件, 不传参,卸载所有实例监听事件事件名称,如:'mouse:down',将会卸载所有的mouse:down事件
  • eventListener
namedescriptioncallback
mouse:down鼠标按下事件
mouse:move鼠标移动事件
mouse:wheel鼠标滚轮事件
mouse:up鼠标抬起事件
img:load当背景图是在线地址时,等图片加载完成后的回调
render:before画布元素渲染前
render:after画布元素渲染后
obj:add元素被添加到画布后
obj:remove元素从画布中删除后
obj:edit元素被编辑后
obj:move元素被移动后
    // paper-pen为开发者提供了mouse:down 、mouse:move、mouse:up、img:load、after:render事件回调。
    paperPen.on('mouse:down', opt => {
        let pos = paperPen.getPointer(opt) // 获取鼠标按下的点映射到当前画布的真实坐标位置。
        console.log(pos)
        // 您能在此处做想要做的业务。
    })
    paperPen.off('mouse:down') // 此方法会去掉画布上所有的mouse:down事件, 最好指定第二个参数来删除对应的事件。
    paperPen.on('mouse:down', handleMouseDown)
    paperPen.off('mouse:down', handleMouseDown)
  • Matrix, Support chain operation(支持链式操作)
namedecriptionparamstype
translatetranslate matrixx , ynumber
scaleMultiples corresponding to x-axis and y-axis scalingx, ynumber
scaleUscale Matrixscalernumber
rotaterotate matrixradiannumber
transformtransform matrixa,b,c,d,e,fnumber
applyToPointApply the current matrix to pointx, ynumber
    import { Matrix } from 'paper-pen'
    let mat = new Matrix() /*
        默认生成单位矩阵
        1, 0, 0,
        0, 1, 0,
        0, 0, 1
    */
    mat.translate(10, 10) // (1, 0, 0, 1, 10, 10) 返回新的矩阵
    mat.translate(100, 100).scale(1.5, 2) // 这里的换算都是用矩阵的叉乘。
0.0.7-release

1 month ago

0.0.7-alpha

5 months ago

0.0.6

9 months ago

0.0.5

10 months ago

0.0.4

10 months ago

0.0.3

10 months ago

0.0.2

10 months ago

0.0.1

10 months ago