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

canvas-tablet

v2.0.6

Published

Tablet是一个轻量级的基于canvas的在线画板,无其他依赖,`传统网站`或`vue`、`react`、`angular`等单页面应用皆可使用!兼容各种移动设备!

Downloads

19

Readme

Tablet

Tablet是一个轻量级的基于canvas的在线画板,无其他依赖,传统网站vuereactangular等单页面应用皆可使用!兼容各种移动设备!

Tablet is a lightweight canvas-based online drawing board with no other dependencies. It can be used by traditional websites or single-page applications such as vue, react, and angular! Compatible with all kinds of mobile devices!

canvas签名板文档

这是Tablet2.0文档,Tablet1.0文档请前往

Tablet1.0文档

安装(Install)

  1. npm安装: npm install canvas-tablet -S
  2. script标签引入: 将/dist/Tablet-2.0.1.min.js下载下来即可

演示地址(Demo)

https://941477276.github.io/Tablet/index2.html

基本使用(Usage)

<div class="tool-bar">
  <div class="tablet-btns" style="padding: 10px;">
    <input class="-color-picker color-color" type="text" value="字体颜色" id="colpick" readonly/>
    <input class="-color-picker bg-color" type="text" value="背景颜色" id="colpick2" readonly/>
    <div class="clear-canvas">清屏</div>
    <div class="save-canvas-to-img">保存图片</div>
    <span>
         画笔粗细
         <select>
             <option value="1">1</option>
             <option value="3">3</option>
             <option value="5">5</option>
             <option value="8">8</option>
             <option value="10" selected>10</option>
             <option value="15">15</option>
             <option value="20">20</option>
         </select>
         <!--<select>
             <option value="0">正常</option>
             <option value="90">顺时针旋转90度</option>
             <option value="-90">逆时针旋转90度</option>
             <option value="180">旋转180度</option>
         </select>-->
         <button type="button" class="get_blob">获取blob对象</button>
         <button type="button" class="set-img">设置背景图片</button>
         <button type="button" class="backOneStep">撤回</button>
    </span>
  </div>
</div>

<div class="container" id="container"></div>
<script src="./dist/Table.2.0.1.min.js"></script>  
<script>
  var tablet;
  $(function () {
    tablet = new Tablet("#container", {
      // 画笔默认颜色
      defaultColor: "#2e76da",
      // 默认背景色
      defaultBackgroundColor: '#f60',
      // canvas画布是否响应式
      // response: true,
      // canvas的宽度,宽高都可以传递函数
      // width: 0,
      // height: 0,
      // 前面板的额外class
      // extraClass: "",
      // 设置获取到的图片的类型,可选值png、jpg,默认png
      // imgType: "png",
      // 清除画布前执行的函数,如果该函数返回false,则不会进行清除
      // onBeforeClear: function () {},
      // 清除画布后执行的函数
      // onClear: function () {},
      autoResize: true, // 浏览器窗口改变时是否重新绘制
      // 画布初始化后的回调
      onInit: function () {
        var that = this,
          container = this.container;
        $(".-color-picker").each(function () {
          var self = this,
            $self = $(this);
          if ($self.hasClass('color-color')) {

            $self.colpick({
              color: that.config.defaultColor,
              layout: 'hex',
              submitText: "确定",
              onSubmit: function (hsb, hex, rgb, el, bySetColor) {
                var color = "#" + hex;
                $(el).css({
                  //color: color,
                  "border-color": color
                }).colpickHide();
                el.selectedColor = color;
                that.color = color;

                that.setColor(color);
              }
            });
            $self.css({
              "border-color": that.config.defaultColor
            });
          } else if ($self.hasClass('bg-color')) {
            $self.colpick({
              color: that.config.defaultBackgroundColor,
              layout: 'hex',
              submitText: "确定",
              onSubmit: function (hsb, hex, rgb, el, bySetColor) {
                var color = "#" + hex;
                $(el).css({
                  //color: color,
                  "border-color": color
                }).colpickHide();
                el.selectedColor = color;
                //that.bgColor = color;
                that.setBackgroundColor(color);
                // 重绘背景图后必须刷新一下,否则偶尔会出现最后一个线条被重复绘制一遍问题
                var timer = setTimeout(function (){
                  clearTimeout(timer);
                  that.refresh();
                }, 0);
              }
            });
            $self.css({
              "border-color": that.config.defaultBackgroundColor
            });
          }
        });
        $('.clear-canvas').on('click', function () {
          that.clear();
        })
        $(".tablet-btns select").eq(0).on("change", function () {
          that.setLineWidth($(this).val());
        });
        $(".save-canvas-to-img").on("click", function () {
          console.log(that.getBase64());
          if (!that.isMobile) {
            alert("请按f12打开控制台查看base64图片数据!");
          }
        });
        $(".get_blob").on("click", function () {
          console.log(that.getBlob());
        });
        $(".set-img").on("click", function () {
          that.setBackgroundImage(document.getElementById('bg_img'));
          // 重绘背景图后必须刷新一下,否则偶尔会出现最后一个线条被重复绘制一遍问题
          var timer = setTimeout(function (){
            clearTimeout(timer);
            that.refresh();
          }, 0);
        });
        $(".backOneStep").on('click', function () {
          that.revoke();
        });
      }
    });
    console.log(tablet);
  });
</script>

Table画板效果

实例方法(Methods)

refresh(refreshWH)

刷新画布,线条及背景将会重绘,返回当前Tablet实例;如果refreshWH为true,则会重新计算画布宽高

hasCanUseLine

判断是否有可用的线条,可以用来判断用户是否有绘制内容,返回值类型:boolean

getRect

获取画布位置及宽高;返回值类型:{x: number, width: number, y: number, height: number}

getBase64(type, angle)

将画布内容转换成base64数据,并返回;返回值类型:string

  • type:图片类型,只有jpgpng两个选项,默认png
  • angle:旋转角度,默认为0,角度支持90的整数倍

getBlob(type, angle)

将画布内容转换成blob数据,并返回;返回值类型:blob

  • type:图片类型,只有jpgpng两个选项,默认png
  • angle:旋转角度,默认为0,角度支持90的整数倍

getMax(xPoints, yPoints)

获取x、y轴的最大、最小值;xPoints, yPoints分别为x轴、y轴所有坐标;返回值类型:{top: number, left: number, bottom: number, right: number}

setColor(color)

设置画笔颜色;color为颜色值,可以是任何css的颜色表达式;返回当前Tablet实例

setLineWidth(width)

设置画笔粗细;width为粗细值,number类型;返回当前Tablet实例

setBackgroundColor(bgColor, x, y, width, height, addToOperationRecord)

设置画布背景颜色;返回当前Tablet实例

  1. bgColor:颜色值,可以是任何css的颜色表达式
  2. x:绘制起始x点,默认为0
  3. y:绘制起始y点,默认为0
  4. width:绘制宽度,默认为整个画布宽度
  5. height:绘制高度,默认为整个画布高度
  6. addToOperationRecord:是否将此操作添加到操作历史中,默认为true

setBackgroundImage(img, x, y, width, height, onImgLoading, onFail, addToOperationRecord)

设置画布背景颜色;返回当前Tablet实例

  1. img:背景图片,值可以为图片url或者Image对象
  2. x:绘制起始x点,默认为0
  3. y:绘制起始y点,默认为0
  4. width:绘制宽度,默认为整个画布宽度
  5. height:绘制高度,默认为整个画布高度
  6. onImgLoading:图片加载中回调
  7. onFail:图片加载失败回调
  8. addToOperationRecord:是否将此操作添加到操作历史中,默认为true

setCanvasWH(width, height)

设置画布的宽高;width, height分别为画布宽高,如果不传宽高则会自动计算;返回当前Tablet实例

canvasReset

重置canvas画笔属性,使用最后一次的属性进行重置,返回当前Tablet实例;

revoke

回退一步,返回当前Tablet实例;

clear(clearPoints)

clear方法用于清空画布,返回值当前Tablet实例;如果clearPoints为true,则会将之前绘制的线条及操作步骤清空

hasContent(singleColorCountMin, totalColorCountMin) v2.0.5新增

判断画布是否有内容,画布中有可用线条或画布中颜色数量大于等于totalColorCountMin都认为有内容

  1. singleColorCountMin:单个颜色最少数量,默认5
  2. totalColorCountMin:总颜色数量,默认为2

getTabletImageColors v2.0.5新增

获取画布中内容的颜色(16进制)