代码地址如下:
http://www.demodashi.com/demo/14461.html

一、前期准备工作

软件环境:微信开发者工具

官方下载地址:https://mp.weixin.qq.com/debug/wxadoc/dev/devtools/download.html

1、基本需求。
  • 实现用户自定画笔大小
  • 实现用户自定色彩
  • 实现用户动画撤回之前的操作
  • 实现生成分享海报
  • 实现用户预览画作,清除画布
2、案例目录结构

二、程序实现具体步骤

1.index.wxml代码
<view class="option-panel">
<view class="option-row" id="firstRow">
<view class="width-container">
<text>笔触大小</text>
<block wx:for="{{lineWidthArr.length}}" wx:key="index">
<brush-point class="brush-point" radius="{{lineWidthArr[index]}}" data-index="{{index}}" selected="{{index === curWidthIndex}}" bind:select="clickChangeWidth" color="{{currentColor}}"></brush-point>
</block>
</view>
</view>
<view class="option-row" id="secondRow">
<view class="color-slecotr-left"></view>
<scroll-view scroll-x="true">
<block wx:for="{{avaliableColors}}" wx:key="index">
<color-box class="color-box" data-color="{{avaliableColors[index]}}" selected="{{avaliableColors[index]===currentColor}}" bind:select="clickChangeColor"></color-box>
</block>
</scroll-view>
<view class="color-slecotr-right"></view>
</view> <view class="option-row" id="thirdRow">
<view class="tool-container">
<custom-button class="icon-text"
imgUrl="/images/games/common/btn_back.png"
bind:clickEvent="clickFallback"
text="撤销"
width="100%">
</custom-button> <custom-toggle class="icon-text"
imgUrl="/images/games/common/btn_erase.png"
selected="{{bgColor===currentColor}}"
bind:clickEvent="clickErase"
text="橡皮"
width="100%">
</custom-toggle> <custom-button class="icon-text"
imgUrl="/images/games/common/btn_tranCan.png"
bind:clickEvent="clickClearAll"
text="清除"
width="100%">
</custom-button> <custom-button class="icon-text"
imgUrl="/images/games/common/btn_pageview.png"
bind:clickEvent="pageView"
text="预览"
width="100%">
</custom-button>
</view>
</view>
<view class="option-row" id="forthRow">
<button type="primary" class="share-btn" bindtap='goRelease'>发布佳作</button>
<button type="primary" class="share-btn" bindtap='clickShare'>发起猜猜</button>
</view>
</view>
</view>
2.index.wxss代码
page{
height: 100%;
width:100%;
} .container {
height: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-between;
box-sizing: border-box;
} /* 显示的题目 */ .container .question {
width: 100%;
height: 10%;
background: #f0efef;
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
color: #fb21a1;
box-shadow: 2rpx 5rpx 2rpx silver;
} /* 刷新按钮 */ .container .question .userinfo-avatar {
height: 80rpx;
width: 80rpx;
border-radius: 50%;
overflow: hidden;
} .container .question text {
margin: auto 10rpx auto 20rpx;
} .container .question .refresh-btn {
width: 50rpx;
height: 50rpx;
transform: scaleX(-1);
} /* 中间画板 */ .container .palette {
width: 100%;
height: 56%;
display: flex;
justify-content: center;
align-items: center;
box-shadow: 2rpx 5rpx 2rpx silver;
}
3.index.js逻辑代码

a.UI事件动画部分的功能实现

/*--------------------- UI事件 --------------------------------------------------- */
// 绘制开始 手指开始按到屏幕上
touchStart: function (e) {
this.lineBegin(e.touches[0].x, e.touches[0].y)
curDrawArr.push({
x: e.touches[0].x,
y: e.touches[0].y
});
}, // 绘制中 手指在屏幕上移动
touchMove: function (e) {
if (begin) {
this.lineAddPoint(e.touches[0].x, e.touches[0].y);
this.draw(true);
curDrawArr.push({
x: e.touches[0].x,
y: e.touches[0].y
});
}
}, // 绘制结束 手指抬起
touchEnd: function () {
drawInfos.push({
drawArr: curDrawArr,
color: this.data.currentColor,
lineWidth: this.data.lineWidthArr[this.data.curWidthIndex],
});
curDrawArr = [];
this.lineEnd();
},

b.设置线条颜色,设置线条宽度,开始绘制线条,绘制线条中间添加点,等操作...

 // 设置线条颜色
setCurrentColor: function (color) {
this.data.currentColor = color;
this.context.strokeStyle = color;
this.setData({
currentColor: color
});
}, // 设置线条宽度
setLineWidthByIndex: function (index) {
let width = this.data.lineWidthArr[index];
this.context.setLineWidth(width);
this.setData({
curWidthIndex: index
});
}, // 开始绘制线条
lineBegin: function (x, y) {
begin = true;
this.context.beginPath()
startX = x;
startY = y;
this.context.moveTo(startX, startY)
this.lineAddPoint(x, y);
}, // 绘制线条中间添加点
lineAddPoint: function (x, y) {
this.context.moveTo(startX, startY)
this.context.lineTo(x, y)
this.context.stroke();
startX = x;
startY = y;
}, // 绘制线条结束
lineEnd: function () {
this.context.closePath();
begin = false;
}, // 根据保存的绘制信息重新绘制
reDraw: function () {
this.init();
this.fillBackground(this.context);
// this.draw(false);
for (var i = 0; i < drawInfos.length; i++) {
this.context.strokeStyle = drawInfos[i].color;
this.context.setLineWidth(drawInfos[i].lineWidth);
let drawArr = drawInfos[i].drawArr;
this.lineBegin(drawArr[0].x, drawArr[0].y)
for (var j = 1; j < drawArr.length; j++) {
this.lineAddPoint(drawArr[j].x, drawArr[j].y);
// this.draw(true);
} this.lineEnd();
} this.draw();
}, // 将canvas导出为临时图像文件
// canvasId: 要导出的canvas的id
// cb: 回调函数
store: function (canvasId, cb) {
wx.canvasToTempFilePath({
destWidth: 400,
destHeight: 300,
canvasId: canvasId,
success: function (res) {
typeof (cb) == 'function' && cb(res.tempFilePath);
},
fail: function (res) {
console.log("store fail");
console.log(res);
}
})
},

三、案例运行效果图

四、总结与备注

暂无微信小程序-基于canvas画画涂鸦

代码地址如下:
http://www.demodashi.com/demo/14461.html

注:本文著作权归作者,由demo大师代发,拒绝转载,转载需要作者授权

微信小程序-基于canvas画画涂鸦的更多相关文章

  1. 微信小程序 在canvas画布上划动,页面禁止滑动

    要实现微信小程序 在canvas画布上划动,页面禁止滑动,不仅要设置disable-scroll="true",还要要给canvas绑定一个触摸事件才能生效. <canvas ...

  2. 微信小程序基于swiper组件的tab切换

    代码地址如下:http://www.demodashi.com/demo/14010.html 一.前期准备工作 软件环境:微信开发者工具 官方下载地址:https://mp.weixin.qq.co ...

  3. 微信小程序基于scroll-view实现锚点定位

    代码地址如下:http://www.demodashi.com/demo/14009.html 一.前期准备工作 软件环境:微信开发者工具 官方下载地址:https://mp.weixin.qq.co ...

  4. 关于微信小程序前端Canvas组件教程

    关于微信小程序前端Canvas组件教程 微信小程序Canvas接口函数 ​ 上述为微信小程序Canvas的内部接口,通过熟练使用Canvas,即可画出较为美观的前端页面.下面是使用微信小程序画图的一些 ...

  5. 微信小程序-基于高德地图API实现天气组件(动态效果)

    微信小程序-基于高德地图API实现天气组件(动态效果) ​ 在社区翻腾了许久,没有找到合适的天气插件.迫不得已,只好借鉴互联网上的web项目,手动迁移到小程序中使用.现在分享到互联网社区中,帮助后续有 ...

  6. 微信小程序--基于ColorUI构建皮皮虾短视频去水印组件(仅供学习使用)

    微信小程序--基于ColorUI构建皮皮虾短视频去水印组件(仅供学习使用) 没错,我是皮友,我想学习舞蹈(/doge)和瑜伽 ,要无水印的那种有助于我加深学习. 1.组件效果展示 2.组件引入准备 h ...

  7. 微信小程序 -- 基于 movable-view 实现拖拽排序

    微信小程序 -- 基于 movable-view 实现拖拽排序 项目基于colorui样式组件 ColorUI组件库 (color-ui.com) 1.实现效果 2. 设计思路 movable-vie ...

  8. 关于微信小程序使用canvas生成图片,内容图片跨域的问题

    最近有个项目是保存为名片(图片),让用户发送给朋友或朋友圈,找了很多方案都不适用,绞尽脑汁之后还是选了使用canvas,但是用这玩意儿生成图片最大的缺点就是,如果你的内容中有图片,并且这个图片是通过外 ...

  9. 微信小程序使用canvas绘制图片的注意事项

    1.单位换算问题,canvas的尺寸单位是px,所以单位需要做个换算,可以通过wx.getSystemInfo获取屏幕宽高(单位是px),微信小程序无论什么机型,屏幕宽度都是750rpx,因此可以做个 ...

随机推荐

  1. Nginx HTTP负载均衡/反向代理的相关参数测试

    原文地址:http://www.cnblogs.com/xiaochaohuashengmi/archive/2011/03/15/1984976.html 测试目的 (1)弄清楚HTTP Upstr ...

  2. Java 内存释放

     问题一什么叫垃圾回收机制 垃圾回收是一种动态存储管理技术它自动地释放不再被程序引用的对象按照特定的垃圾收集算法来实现资源自动回收的功能.当一个对象不再被引用的时候内存回收它占领的空间 ...

  3. 文件操作篇 close creat dup dup2 fcntl flock fsync lseek mkstemp open read sync write

    文件操作篇 close creat dup dup2 fcntl flock fsync lseek mkstemp open read sync write close(关闭文件) 相关函数 ope ...

  4. Android 项目的代码混淆,Android proguard 使用说明

    简单介绍 Java代码是非常easy反编译的. 为了非常好的保护Java源码,我们往往会对编译好的class文件进行混淆处理. ProGuard是一个混淆代码的开源项目.它的主要作用就是混淆,当然它还 ...

  5. C语言:字符串读取流读取文件中的数据

    #include<stdio.h> int main() { //定义文件指针 FILE *f = NULL; //打开文件 f = fopen("1.txt",&qu ...

  6. Linux监听进程是否存在,并加入定时任务

    前言 我们在linux主机上可能需要一直运行某一服务,如果关机后或者误杀,使得服务停止,从而影响日常的任务.比如一BI项目数据库的抽取,使用Taskctl调度,在每天固定时间进行数据的抽取,如果主机上 ...

  7. Reorder List leetcode java

    题目: Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must d ...

  8. Gradle for Android 翻译 -1

    英文版电子书下载 参考:Gradle for Android  一.从 Gradle 和 AS 开始 [Getting Started with Gradle and Android Studio] ...

  9. Debug时检测到Loaderlock的解决办法

    昨天遇到了Loaderlock的问题. 出错信息为:检测到LoaderLock,正试图在OS加载程序锁内执行托管代码,不要尝试在DllMain或映像初始化函数内运行托管代码,这样会导致应用程序挂起. ...

  10. Android -- Camera源码简析,启动流程

    com.android.camera.Camera.java,主要的实现Activity,继承于ActivityBase. ActivityBase 在ActivityBase中执行流程: onCre ...