Giter Club home page Giter Club logo

paintbrush's Introduction

canvas实现的画板

最近一直负责公司云课堂的ppt部分,里面包括画板。这部分的功能我觉得值得分享出来。 github地址

引入

直接引用lib目录下的index.js,引入Paintbrush类。

import Paintbrush from '../lib/index';

初始化

const canvasCtr = new Paintbrush('#canvasDiv', {
    canEdit: true,
    width: 600,
    height: 600
});

直接new一下,第一个参数是画板元素,可以是选择器,也可以是dom元素,是必填的。第二个参数是相关设置,参数说明如下:

字段 说明 类型 是否必填 默认值
type 画板绘制时的类型,line表示画笔,arrowLine表示箭头,straightLine表示直线,rect表示方形,circle表示圆形,ellipse表示椭圆 String line
width 画板宽度,单位默认为px Number 500
height 画板高度,单位默认为px Number 500
lineWidth 线条宽度 Number 3
strokeStyle 画笔颜色 String red
canEdit 是否支持绘制,true表示支持绘制 Boolean false

绘制

在绘制过程中,主要是控制绘制图形的类型,就是你要绘制圆还是直线,以及线条的粗细、颜色等。比如改变颜色:

canvasCtr.setStyle({ strokeStyle: '#abcdef' });

具体方法说明如下:

方法名 功能 参数
setStyle 设置颜色和线条粗细 为一个对象,strokeStyle表示颜色,lineWidth表示线条的粗细
setType 设置绘制的类型 为一个字符串,line表示画笔,arrowLine表示箭头,straightLine表示直线,rect表示方形,circle表示圆形,ellipse表示椭圆
clear 清空画板 是否清空历史数据,默认为false
switchToDlete 开启删除模式,这时不能画,点击指定已画线条进行删除

获取已绘制数据

可以直接读取地段way来获取数据,如

console.log(canvasCtr.way);
// { line: {}, straightLine: {}, rect: {}, circle: {}, ellipse: {}, arrowLine: {} }

输出的数据格式为一个对象,字段分别为图形的类型。

paintbrush's People

Contributors

myyang2016 avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.