Git Product home page Git Product logo

wxdraw's Introduction

wxDraw

「已经开始支持图形创建」

轻量的小程序canvas动画库

是什么

canvas 是HTML5的一个重要元素,它能够高效的绘制图形,但是过于底层,且粗糙的Api,导致开发者很难使用它来做较为复杂的图形, 而且它的即时绘制无记忆特性,使得它内部的图形并不支持动画更不支持一切交互事件。

这样的问题出现在所有支持canvas的客户端上同样出现在 微信小程序中的canvas中, 由于小程序由jsCore支持,并没有window对象,并且canvas的Api与标准的canvas的Api有所出入,所以市面上绝大部分canvas库与它无缘

而wxDraw也就应运而生,专门用于处理小程序上canvas的图形创建图形动画,以及交互问题的。

特性

  • 简单 不需要你会canvas,会用jQuery就会使用wxDraw。
  • 灵活 所有图形,随时随地都可以进行属性修改、图形添加以及图形销毁。
  • 事件支持 小程序支持的事件只要是合理的都支持。
  • 缓动动画支持 wxDraw支持链式调用动画『就像jQuery的animate一样』,并且支持几乎所有的缓动函数

支持图形

这里只做演示 详细文档 点这里

这些图形都可以在演示文件里看到 点这里

rect

 var rect = new Shape('rect', {x: 60, y: 60, w: 40, h: 40, fillStyle: "#2FB8AC", rotate: Math.PI/2 },'mix', true);

rect

circle

 let circle = new Shape('circle', { x: 100, y: 100, r: 40, sA: Math.PI/4, 
                             fillStyle: "#C0D860",
                            strokeStyle: "#CC333F", rotate: 20, lineWidth: 0, needGra: 'line', 
                              gra: [[0, '#00A0B0'], [0.2, '#6A4A3C'], [0.4, '#CC333F'], [0.6, '#EB6841'], [1, '#EDC951']]}, 
                           'fill', true)

polygon

let polygon = new Shape('polygon', { x: 200, y: 200, r: 40, sides: 9, //9边形
                          fillStyle: "#FC354C", rotate: Math.PI / 4 }, 'mix', true)

ellipse

    let ellipse = new Shape('ellipse', { x: 200, y: 200, a: 40, b: 100, 
                             fillStyle: "#00A0B0", rotate: Math.PI / 7 }, 'mix', true)

image

图形对象

示例

let img = new Shape('image', { x: 100, y: 300,w:100,h:100, file:"./1.png"}, 'fill', true)
  

cshape 「自定义图形」

  let cshape = new Shape('cshape', {
      rotate: Math.PI / 2,
      points: [[70, 85], [40, 20], [24, 46], [2, 4], [14, 6], [4, 46]],
      lineWidth: 5,
      fillStyle: "#00A0B0",
      rotate: Math.PI / 7,
      needGra: 'circle',
      smooth:false,
      gra: [[0, '#00A0B0'], [0.2, '#6A4A3C'], [0.4, '#CC333F'], [0.6, '#EB6841'], [1, '#EDC951']]
    }, 'fill', true)

line

let line = new Shape('line', { points:[[240,373],[11,32],[28,423],[12,105],[203,41],[128,0.06]], 
               strokeStyle: "#2FB8AC",lineWidth:1, rotate: 0, needShadow: true,smooth:false },  
               'fill', true)

text

  let text = new Shape('text', { x: 200, y: 200, text: "我是测试文字", 
                      fillStyle: "#E6781E", rotate: Math.PI / 3}
                      'fill', true)

wxDraw创作的动画演示

这些动画都可以在演示文件里看到 点这里

Google

贪吃蛇

logo演绎

rect舞蹈

吃豆人

wxDraw事件

这些事件都可以在演示文件里看到 点这里

支持

  • tap
  • touchStart
  • touchEnd
  • touchMove
  • longPress
  • drag「自定义的事件」

事件演示

旁边UI小妹妹亲自示范 😜

第一视角

是不是特别简单,特别方便!!! 来,老铁们,star走一波!!!

TODO

  • 图形创建

    • rect
    • arc
      • 扇形
    • polygon
      • 正n边形
      • 用户自己加的形状
      • 光滑版用户自己加的形状
    • image
    • 线条
      • 贝塞尔曲线『用的较少,绘制出来后 没法事件检测』
      • 普通线条
      • 光滑曲线
    • svg path
    • 椭圆
      • 事件
      • 动画
      • 样式
    • 字体
      • 事件
      • 动画
      • 样式
    • 粒子动画元素『我觉得这个算另外一种系统 不属于形状』
  • 样式

    • 阴影
    • 渐变
    • 线宽
    • 线连接
    • 透明度
  • 选中检测

    • rect
    • arc
      • 整个圆形
      • 扇形区域
    • polygon 区域
      • 正多边形
      • 非规则多边形
    • 线条点击
    • 点 点击
    • 椭圆
    • 层级区分
  • 动画

    • requestAnimationFrame垫片
    • 时间扭曲函数
    • 运动
      • 移动
        • x
        • y
      • 放大缩小 使用矩阵暂时搁浅
      • 旋转
        • [x]原点旋转
        • [x]自身中心点旋转
      • 变色
      • 循环动画
      • 连续动画
      • 暂停动画
    • 精灵图『小程序 所支持的drawImage功能太低级 所以取消』
  • 矩阵变换

  • 层级修改

  • 事件

    • touchstart
    • touchmove
    • touchend
    • 拖拽事件
    • tap事件
    • longpress事件
    • 划入划出「划入划出是鼠标的事件吧」
  • 取消事件绑定

  • clone

wxdraw's People

Contributors

bobiscool avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

wxdraw's Issues

渐变不支持线条么?

比如我想画一条渐变色的弧
let epointYes=new Shape('circle',{x:60,y:250,r:35,strokeStyle:"#d9f912",lineWidth:5,sA:0,eA:Math.PI*2*(that.data.reportData.totalScore/100),lineCap:"round",needGra:'line',gra:[[0,"#d9f912"],[0.2,'#bbf524'],[0.4,'#93ef42'],[0.6,"#7aeb51"],[1,'#4ae474']]},'stroke',false);
不启作用,是否有办法支持?

关于事件问题

非常感谢作者开源这个框架。
我有一个问题,Canvas 上的事件你是怎么处理了? 是把绘制出来的图形的宽高记录下来,自己判断的吧?那就有另外一个问题,假如我往 Canvas 上绘制图片,我怎么确定我点击到的是在图片上。 图片是不规则带,一部分是带透明的。

tap事件没响应

android和ios下都没有响应tap事件,ios下touchstart和touchend事件点很多次偶尔会有一次响应

动画

文档内的动画好像都是以中心为原点,能修改吗

图形颜色怎么支持透明度

图形颜色怎么支持透明度,文档说只支持16进制颜色值,但是小程序官方文档是可以设置RGBA 颜色的

lineDash安卓手机不生效

设置line的linedash在安卓上不生效

  let line = new Shape('line', {
      points: [[x / 2 - 130, 115 + (detail.titleCount - 1) * 30], [x / 2 + 130, 115 + (detail.titleCount - 1) * 30]],
      strokeStyle: "#757575", lineWidth: 1, rotate: 0, smooth: false, isLineDash: true, lineDash: [[5, 5], 5]
    })

这是一个 canvas2wxcanvas 的封装吗?

您好,请问 小程序canvas api 和 canvas 原生api 有多大差异呢?这是一个对小程序canvas的封装吗?是不是使用了可以对其他canvas库进行使用吖?如果不是的话,做这种转换的可能性和难度从您专业的角度来看请问有多大呢?(有一些业务需求)

图集功能有在roadmap里面吗?

准备什么时候支持图集呢?我看小程序里面的drawImage里面是有9个参数的,可以支持显示部分图片区域,可以用图集功能。

老哥,animate start之后能不能在start一次

老哥,animate start之后能不能在start一次类似这样
img.animate({x: '+=100'}, { duration: 1000 })
// .animate({ w: 100 }, { duration: 1000 })
.start()

  setTimeout(function () {
    console.log(123);

    // img.updateOption({ x: 200})
    // img.stop();


      img.animate({x: '-=100'}, { duration: 1000 })
      // .animate({ w: 100 }, { duration: 1000 })
          .start()

  }, 2000);

xy坐标以中心点作为基础导致难以通用

在使用wxDraw过程中,我注意到所有的x, y坐标是基于图形中心点为0点,我在开发中涉及到touch事件处理、与其他网页元素交互等需求,与常见事件的以左上为0点不同,导致需要二次统一坐标,请问能否提供offsetTop, offsetLeft来反馈左上位置?

Shape对象的fillStyle会有默认颜色

let circle = new Shape('circle', { x: 100, y: 100, r: 50, sA: -Math.PI / 1.5, eA: Math.PI / 1.5,strokeStyle: "#000000"});
像这种情况, 我只想描边, 并不想把整个圆都填充, 但默认是有黑色, 无法去除

Shape的公共参数needShadow有bug

Shape的公共参数needShadow,无论设置true还是false,都存在shadow。

目前我的临时解决方式是设置shadow: { offsetX: 0, offsetY: 0, blur: 0, color: "#000000" }

另外文档中有一个错误:

var wxDraw = require("../../utils/wxdraw.min.js").wxDraw;

在项目中报错,
看了下代码,应改为首字母大写

var WxDraw = require("../../utils/wxdraw.min.js").WxDraw;

哪里没清干净事件?

Cannot read property 'getChoosed' of undefined;at pages/img/index page bindtouchstart function
TypeError: Cannot read property 'getChoosed' of undefined
at WxDraw.getDetectedLayers (http://127.0.0.1:61190/appservice/util/wxdraw.js:4398:71)
at http://127.0.0.1:61190/appservice/util/wxdraw.js:3617:35
at Array.forEach ()
at http://127.0.0.1:61190/appservice/util/wxdraw.js:3613:32
at Array.forEach ()
at eventBus.dispatch (http://127.0.0.1:61190/appservice/util/wxdraw.js:3610:24)
at Shape.detect (http://127.0.0.1:61190/appservice/util/wxdraw.js:3830:22)
at WxDraw. (http://127.0.0.1:61190/appservice/util/wxdraw.js:4319:18)
at Array.forEach ()
at WxDraw.touchstartDetect (http://127.0.0.1:61190/appservice/util/wxdraw.js:4318:26)

文档没写清楚

Shape 支持哪些形状,不同形状都有哪些参数,参数是什么意思都没有写清楚

Shape如果是text类型没法填充0

var text = new Shape('text', { x: i * that.data.minWidth, y: j * that.data.minWidth, text: txt, fontSize: initFontSize });
txt=0的时候没法填充

destory 方法只能清除一个图形,再次调用清除另一图形无效

微信开发工具版本:1.9.91
wxDraw.js 版本:latest
测试手机:iphone7
测试微信版本:6.6.5

问题描述:当我希望清除掉 canvas 上所有的图形时,我只能使用 destory() 方法清除掉第一个图形,第二个图形调用 destory() 无效。

问题录频:

代码:
`
let img1 = new Shape("image", { x: 40, y: 80, w: 100, h: 100, file: "../../images/[email protected]" }, "fill", true);
let img2 = new Shape("image", { x: 100, y: 300, w: 100, h: 100, file: "../../images/[email protected]" }, "fill", true);
this.wxCanvas.add(img1);//添加到canvas上面
this.wxCanvas.add(img2);

  this.imgArr = [];
  this.imgArr.push(img1);
  this.imgArr.push(img2);

  img1.destroy();
  img2.destroy();

`

动画

矩形动画宽度由40增加到100,设置什么参数能让其不从中心增加

当为多个图形元素设置动画时,第二次设置的动画不动

微信开发工具版本:1.9.91
wxDraw.js 版本:latest
测试手机:iphone7
测试微信版本:6.6.5
问题描述:我在 canvas 上添加多个图形元素(测试时使用的是 image),并同时为每个图形元素设置了两段动画。然而,第一段动画结束以后,第二段动画就不再执行。当我只为其中一个图形元素设置两段动画的时候,两段动画都可以执行。
录制视频:
wxDraw.js issue-01
代码如下:
`var context = wx.createCanvasContext("first"); //还记得 在wxml里面canvas的id叫first吗
this.wxCanvas = new wxDraw(context, 0, 0, 400, 500);

  let img = new Shape("image", { x: 100, y: 300, w: 100, h: 100, file: "../../images/[email protected]" }, "fill", true);
  let img2 = new Shape("image", { x: 10, y: 30, w: 100, h: 100, file: "../../images/[email protected]" }, "fill", true);
  this.wxCanvas.add(img);//添加到canvas上面
  this.wxCanvas.add(img2);

  this.imgArr = [];
  this.imgArr.push(img);
  this.imgArr.push(img2);

  this.imgArr.forEach(img => {
      img
        .animate({ x: "+=100", y: "+=100" }, { duration: 1000 })
        .start(1);

    setTimeout(() => {
        img
            .animate({x: "-=100", y: "-=100"}, {duration: 1000}).start(1);
    }, 3000);
      
  });`

在WePY框架下使用wxDraw报错

WePY框架下使用wxDraw时,报错,Hello world都做不到

使用大致如下:

<canvas
    class="effect_canvas"
    canvas-id="effectCanvas"
    disable-scroll
></canvas>
import wepy from 'wepy'
import { wxDraw as WxDraw, Shape } from '../plugins/wxdraw'

export default class Index extends wepy.page {
    data = {
        wxCanvas: null
    }

    onLoad() {
        const context = wx.createCanvasContext('effectCanvas')
        this.wxCanvas = new WxDraw(context, 0, 0, 400, 500)
    }
}

报错如下:

wxdraw

画圆的时候多了一条半径长度的边框

image
let epotinNo=new Shape('circle',{x:60,y:300,r:35,strokeStyle:"#cecece",lineWidth:3},'stroke',false);
我想要的就是一个圆,多了个半径,不知道怎么解决呢。
画一个圆弧页会遇到同样的问题
难道需要再画一层盖住么 o(╯□╰)o

Image Shape不继承公共参数

    const avatar = new Shape('image', {
      x: 100,
      y: 100,
      w: 180,
      h: 100,
      file: './avatar.png',

      needShadow: true,
      shadow: {offsetX: 5, offsetY: 5, blur: 5, color: '#000000'}

    }, 'fill', true)

并无阴影效果,其他虚线、渐变等测试均无继承,是API不支持还是Bug?

canvas绘制的多边形图形追加事件之后没有任何反应?

// 绘制多边形
let polygonNo1 = new Shape('polygon', {
x: 50, y: 500, r: 20, sides: 6, //9边形
fillStyle: "#CD661D", rotate: Math.PI
}, 'mix', true)
//追加元素
this.wxCanvas.add(polygonNo1)
//添加事件 没有任何效果?
polygonNo1.bind('tap',function(){
console.log(1)
})
麻烦说下为什么 看你api也没有什么特殊的需要注意点啊

如何实现text 的自动换行?

#如题,

let text = new Shape('text', {
      x:20,
      y: 40,
      text: "内容过长",
      fontSize: 20,
      align: 'center',
      fillStyle: "#2d2d2d"
    },

当内容过长时候怎么实现自动换行,不胜感激!

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.