小程序canvas学习

效果图:

.wxml

  1. <canvas style="width: 100vw; height: 100vh;" canvas-id="firstCanvas"></canvas>

.js

  1. onLoad: function (options) {
  2.  
  3. const ctx = wx.createCanvasContext('firstCanvas')
  4. var canvasWidth = wx.getSystemInfoSync().windowWidth
  5. var canvasHeight = wx.getSystemInfoSync().windowHeight
  6. var numParticles = 50
  7. var bg = [18, 10, 34]
  8. var cols = ['#FF5722', '#FF9800', '#FF9800', '#FF9800', '#FF9800', '#B71C1C', '#00BCD4', '#00BCD4', '#009688']
  9. setup()
  10. function setup() {
  11. ctx.beginPath();
  12. ctx.rect(0, 0, canvasWidth, canvasHeight)
  13. ctx.fillStyle = `rgba(${bg[0]}, ${bg[1]}, ${bg[2]}, ${1})`
  14. ctx.fill()
  15. ctx.draw()
  16. }
  17.  
  18. // window.requestAnimationFrame(animate);
  19.  
  20. setInterval(animate, 60)
  21. function animate() {
  22. fade(0.3)
  23. draw()
  24.  
  25. // window.requestAnimationFrame(function(){animate()})
  26. }
  27.  
  28. function fade(amt) {
  29. ctx.beginPath();
  30. ctx.rect(0, 0, canvasWidth, canvasHeight)
  31. ctx.fillStyle = `rgba(${bg[0]}, ${bg[1]}, ${bg[2]}, ${amt})`
  32. ctx.fill()
  33. ctx.draw()
  34. }
  35.  
  36. function Particle() {
  37. this.pos = {
  38. x: Math.random() * canvasWidth * 0.8 + canvasWidth * 0.1,
  39. y: Math.random() * canvasHeight * 0.8 + canvasHeight * 0.1
  40. }
  41. this.r = 1
  42. this.speed = 6
  43. this.step = Math.random() * 400
  44. this.vx = Math.random() * this.speed / 4 - this.speed / 8
  45. this.vy = Math.random() * this.speed / 4 - this.speed / 8
  46. this.colIndex = Math.floor(Math.random() * cols.length)
  47. this.history = []
  48. this.update = function () {
  49. this.step++
  50. this.step %= 400
  51. if (this.history.length > 5) {
  52. this.history.shift()
  53. }
  54. this.pos.x += this.vx
  55. this.pos.y += this.vy
  56. this.vx = this.vx * 0.98 + (Math.random() * this.speed * 2 - this.speed) * 0.12
  57. this.vy = this.vy * 0.98 + (Math.random() * this.speed * 2 - this.speed) * 0.12
  58. if (this.history.length > 4) {
  59. ctx.beginPath()
  60. ctx.moveTo(this.pos.x, this.pos.y)
  61. for (var i = this.history.length - 1; i >= 0; i--) {
  62. ctx.lineTo(this.history[i].x, this.history[i].y)
  63. }
  64. ctx.fillStyle = cols[this.colIndex]
  65. ctx.strokeStyle = cols[this.colIndex]
  66. ctx.fill()
  67. ctx.lineWidth = 2
  68. ctx.lineJoin = "round"
  69. // ctx.closePath()
  70. ctx.stroke()
  71. }
  72. if (this.pos.x > canvasWidth || this.pos.x < 0 || this.pos.y > canvasHeight || this.pos.y < 0) {
  73. delete this.pos
  74. delete this.history
  75. return false;
  76. }
  77. this.history.push({
  78. x: this.pos.x,
  79. y: this.pos.y
  80. })
  81. return true;
  82. }
  83. }
  84.  
  85. var particles = [new Particle()]
  86.  
  87. function draw() {
  88. if (particles.length < numParticles) {
  89. particles.push(new Particle())
  90. }
  91. particles = particles.filter(function (p) {
  92. return p.update()
  93. })
  94.  
  95. }
  96. },

总结:目前小程序canvas还很卡 不建议使用

PC端:

效果图

代码:

js

  1. <script type="text/javascript">
  2.  
  3. var canvas = document.createElement('canvas')
  4. document.getElementsByTagName('body')[0].appendChild(canvas)
  5. var ctx = canvas.getContext('2d')
  6. var numParticles = 50
  7.  
  8. var bg = [18, 10, 34]
  9. var cols = ['#FF5722', '#FF9800', '#FF9800', '#FF9800', '#FF9800', '#B71C1C', '#00BCD4', '#00BCD4', '#009688']
  10.  
  11. setup()
  12.  
  13. function setup() {
  14. canvas.width = window.innerWidth
  15. canvas.height = window.innerHeight
  16. ctx.beginPath();
  17. ctx.rect(0, 0, canvas.width, canvas.height)
  18. ctx.fillStyle = `rgba(${bg[0]}, ${bg[1]}, ${bg[2]}, ${1})`
  19. ctx.fill()
  20. }
  21.  
  22. // window.requestAnimationFrame(animate);
  23.  
  24. setInterval(animate, 1000/29.9)
  25. function animate() {
  26. fade(0.3)
  27. draw()
  28. // window.requestAnimationFrame(function(){animate()})
  29. }
  30.  
  31. function fade(amt) {
  32. ctx.beginPath();
  33. ctx.rect(0, 0, canvas.width, canvas.height)
  34. ctx.fillStyle = `rgba(${bg[0]}, ${bg[1]}, ${bg[2]}, ${amt})`
  35. ctx.fill()
  36. }
  37.  
  38. function Particle () {
  39. this.pos = {
  40. x: Math.random() * canvas.width * 0.8 + canvas.width * 0.1,
  41. y: Math.random() * canvas.height * 0.8 + canvas.height * 0.1
  42. }
  43. this.r = 1
  44. this.speed = 6
  45. this.step = Math.random() * 400
  46. this.vx = Math.random() * this.speed/4 - this.speed/8
  47. this.vy = Math.random() * this.speed/4 - this.speed/8
  48. this.colIndex = Math.floor(Math.random()*cols.length)
  49. this.history = []
  50. this.update = function () {
  51. //////////////////////////////////////
  52. this.step ++
  53. this.step %= 400
  54. if (this.history.length > 5){
  55. this.history.shift()
  56. }
  57. this.pos.x += this.vx
  58. this.pos.y += this.vy
  59. this.vx = this.vx * 0.98 + (Math.random() * this.speed * 2 - this.speed) * 0.12
  60. this.vy = this.vy * 0.98 + (Math.random() * this.speed * 2 - this.speed) * 0.12
  61. //////////////////////////////////////
  62. if (this.history.length > 4){
  63. ctx.beginPath()
  64. ctx.moveTo(this.pos.x ,this.pos.y)
  65. for (var i = this.history.length-1; i >= 0; i--){
  66. ctx.lineTo(this.history[i].x ,this.history[i].y)
  67. }
  68. // ctx.fillStyle = `hsla(${Math.sin( this.step / 300) * 70 + 70},${99}%,${50}%,1)`
  69. // ctx.strokeStyle = `hsla(${Math.sin( this.step / 300) * 70 + 70},${99}%,${50}%,0.5)`
  70. ctx.fillStyle = cols[this.colIndex]
  71. ctx.strokeStyle = cols[this.colIndex]
  72. ctx.fill()
  73. ctx.lineWidth = 2
  74. ctx.lineJoin = "round"
  75. // ctx.closePath()
  76. ctx.stroke()
  77. }
  78.  
  79. //////////////////////////////////////
  80. if (this.pos.x > canvas.width || this.pos.x < 0 || this.pos.y > canvas.height || this.pos.y < 0) {
  81. delete this.pos
  82. delete this.history
  83. return false;
  84. }
  85. this.history.push({
  86. x: this.pos.x,
  87. y: this.pos.y
  88. })
  89. return true;
  90. }
  91. }
  92.  
  93. var particles = [new Particle()]
  94.  
  95. function draw() {
  96. if (particles.length < numParticles) {
  97. particles.push(new Particle())
  98. }
  99.  
  100. particles = particles.filter(function (p){
  101. return p.update()
  102. })
  103.  
  104. }
  105. </script>

记录一下小程序canvas的更多相关文章

  1. 技术博客--微信小程序canvas实现图片编辑

    技术博客--微信小程序canvas实现图片编辑 我们的这个小程序不仅仅是想给用户提供一个保存和查找的平台,还希望能给用户一个展示自己创意的舞台,因此我们实现了图片的编辑部分.我们对对图片的编辑集成了很 ...

  2. 原创:WeZRender:微信小程序Canvas增强组件

    WeZRender是一个微信小程序Canvas增强组件,基于HTML5 Canvas类库ZRender. 使用 WXML: <canvas style="width: 375px; h ...

  3. 微信小程序-canvas绘制文字实现自动换行

    在使用微信小程序canvas绘制文字时,时常会遇到这样的问题:因为canvasContext.fillText参数为 我们只能设置文本的最大宽度,这就产生一定的了问题.如果我们绘制的文本长度不确定或者 ...

  4. 微信小程序 canvas 字体自动换行(支持换行符)

    微信小程序 canvas 自动适配 自动换行,保存图片分享到朋友圈  https://github.com/richard1015/News 微信IDE演示代码https://developers.w ...

  5. 微信小程序--canvas画布实现图片的编辑

    技术:微信小程序   概述 上传图片,编辑图片大小,添加文字,改变文字颜色等 详细 代码下载:http://www.demodashi.com/demo/14789.html 概述 微信小程序--ca ...

  6. 小程序canvas生成海报保存至手机相册

    小程序canvas画图保存至手机相册 (1)可直接展示生成的海报 .因手机分辨率不同可能导致生成的海报会有细微差别,这里隐藏canvas海报,页面正常设置海报样式保存时保存隐藏的canvas海报 (2 ...

  7. 优化版小程序canvas,增加失败逻辑,及完善文字

    wxml <view class="shareBox" style="backgound:{{isShow ? '#000' : '#fff'}}" wx ...

  8. 微信小程序 | canvas绘图

    1.新的尺寸单位 rpx rpx(responsive pixel): 可以根据屏幕宽度进行自适应. 规定屏幕宽为750rpx.如在 iPhone6 上,屏幕宽度为375px,共有750个物理像素,则 ...

  9. 微信小程序canvas生成并保存图片

    ---恢复内容开始--- 微信小程序canvas生成并保存图片,具体实现效果如下图     实现效果需要做以下几步工作 一.先获取用户屏幕大小,然后才能根据屏幕大小来定义canvas的大小 二.获取图 ...

随机推荐

  1. Android Glide 加载图片

    0.借鉴文章地址:http://blog.csdn.net/zivensonice/article/details/51835802 和 http://www.cnblogs.com/zhaoyanj ...

  2. springboot引入AOP

    AOP是Aspect Oriented Programming的缩写,意为面向切面编程.通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是spring框架的一个重要内容,她通过对 ...

  3. 手把手教你使用 Clion 开发 Linux C++ 项目

    手把手教你使用 Clion 开发 Linux C++ 项目 关于CLion CLion是一款专为开发C及C++所设计的跨平台IDE.它是以IntelliJ为基础设计的,包含了许多智能功能来提高开发人员 ...

  4. java中String字符串的==解析

    今天不知道怎么看了下string的==的问题,本身我觉得我这个水平去判断几个字符串相等还能出问题?呵呵,真的出了大问题,根本原因在于对java字节码的不了解. 首先,==运算符比较的是两个变量所指向的 ...

  5. Centos7上安装、破解bamboo6.0.3

    1.下载bamboo安装包,地址:https://www.atlassian.com/software/bamboo/download?_ga=2.65378349.245489969.1512876 ...

  6. restful 跨域

    同源策略(Same origin policy)是一种约定,它是浏览器最核心也最基本的安全功能. 就浏览器而言的, http://127.0.0.1:8000  协议 域名 端口 跨域 问题// 简单 ...

  7. 宝塔安装swoole

    新建文件夹 mkdir swoole 切入到文件夹中,进行下载安装包 wget http://pecl.php.net/get/swoole-4.3.2.tgz 解压 tar -zxvf swoole ...

  8. Java8-对map过滤

    1.对map按值过滤返回值 public class TestMapFilter { public static void main(String[] args) { Map<Integer, ...

  9. ADO.Net的发展史

    1.演变历史: 它们是按照这个时间先后的顺序逐步出现的,史前->ODBC->OLEDB->ADO->ADO.Net. 2.下面分别介绍一下这几个. a. 史前的数据访问是什么样 ...

  10. maven 安装本地jar

    mvn install:install-file -Dfile=D:/open-api-sdk-2.0.jar -DgroupId=com.jd.open -DartifactId=jd-api-sd ...