记录一下小程序canvas
小程序canvas学习
效果图:
.wxml
<canvas style="width: 100vw; height: 100vh;" canvas-id="firstCanvas"></canvas>
.js
onLoad: function (options) { const ctx = wx.createCanvasContext('firstCanvas')
var canvasWidth = wx.getSystemInfoSync().windowWidth
var canvasHeight = wx.getSystemInfoSync().windowHeight
var numParticles = 50
var bg = [18, 10, 34]
var cols = ['#FF5722', '#FF9800', '#FF9800', '#FF9800', '#FF9800', '#B71C1C', '#00BCD4', '#00BCD4', '#009688']
setup()
function setup() {
ctx.beginPath();
ctx.rect(0, 0, canvasWidth, canvasHeight)
ctx.fillStyle = `rgba(${bg[0]}, ${bg[1]}, ${bg[2]}, ${1})`
ctx.fill()
ctx.draw()
} // window.requestAnimationFrame(animate); setInterval(animate, 60)
function animate() {
fade(0.3)
draw() // window.requestAnimationFrame(function(){animate()})
} function fade(amt) {
ctx.beginPath();
ctx.rect(0, 0, canvasWidth, canvasHeight)
ctx.fillStyle = `rgba(${bg[0]}, ${bg[1]}, ${bg[2]}, ${amt})`
ctx.fill()
ctx.draw()
} function Particle() {
this.pos = {
x: Math.random() * canvasWidth * 0.8 + canvasWidth * 0.1,
y: Math.random() * canvasHeight * 0.8 + canvasHeight * 0.1
}
this.r = 1
this.speed = 6
this.step = Math.random() * 400
this.vx = Math.random() * this.speed / 4 - this.speed / 8
this.vy = Math.random() * this.speed / 4 - this.speed / 8
this.colIndex = Math.floor(Math.random() * cols.length)
this.history = []
this.update = function () {
this.step++
this.step %= 400
if (this.history.length > 5) {
this.history.shift()
}
this.pos.x += this.vx
this.pos.y += this.vy
this.vx = this.vx * 0.98 + (Math.random() * this.speed * 2 - this.speed) * 0.12
this.vy = this.vy * 0.98 + (Math.random() * this.speed * 2 - this.speed) * 0.12
if (this.history.length > 4) {
ctx.beginPath()
ctx.moveTo(this.pos.x, this.pos.y)
for (var i = this.history.length - 1; i >= 0; i--) {
ctx.lineTo(this.history[i].x, this.history[i].y)
}
ctx.fillStyle = cols[this.colIndex]
ctx.strokeStyle = cols[this.colIndex]
ctx.fill()
ctx.lineWidth = 2
ctx.lineJoin = "round"
// ctx.closePath()
ctx.stroke()
}
if (this.pos.x > canvasWidth || this.pos.x < 0 || this.pos.y > canvasHeight || this.pos.y < 0) {
delete this.pos
delete this.history
return false;
}
this.history.push({
x: this.pos.x,
y: this.pos.y
})
return true;
}
} var particles = [new Particle()] function draw() {
if (particles.length < numParticles) {
particles.push(new Particle())
}
particles = particles.filter(function (p) {
return p.update()
}) }
},
总结:目前小程序canvas还很卡 不建议使用
PC端:
效果图
代码:
js
<script type="text/javascript"> var canvas = document.createElement('canvas')
document.getElementsByTagName('body')[0].appendChild(canvas)
var ctx = canvas.getContext('2d')
var numParticles = 50 var bg = [18, 10, 34]
var cols = ['#FF5722', '#FF9800', '#FF9800', '#FF9800', '#FF9800', '#B71C1C', '#00BCD4', '#00BCD4', '#009688'] setup() function setup() {
canvas.width = window.innerWidth
canvas.height = window.innerHeight
ctx.beginPath();
ctx.rect(0, 0, canvas.width, canvas.height)
ctx.fillStyle = `rgba(${bg[0]}, ${bg[1]}, ${bg[2]}, ${1})`
ctx.fill()
} // window.requestAnimationFrame(animate); setInterval(animate, 1000/29.9)
function animate() {
fade(0.3)
draw()
// window.requestAnimationFrame(function(){animate()})
} function fade(amt) {
ctx.beginPath();
ctx.rect(0, 0, canvas.width, canvas.height)
ctx.fillStyle = `rgba(${bg[0]}, ${bg[1]}, ${bg[2]}, ${amt})`
ctx.fill()
} function Particle () {
this.pos = {
x: Math.random() * canvas.width * 0.8 + canvas.width * 0.1,
y: Math.random() * canvas.height * 0.8 + canvas.height * 0.1
}
this.r = 1
this.speed = 6
this.step = Math.random() * 400
this.vx = Math.random() * this.speed/4 - this.speed/8
this.vy = Math.random() * this.speed/4 - this.speed/8
this.colIndex = Math.floor(Math.random()*cols.length)
this.history = []
this.update = function () {
//////////////////////////////////////
this.step ++
this.step %= 400
if (this.history.length > 5){
this.history.shift()
}
this.pos.x += this.vx
this.pos.y += this.vy
this.vx = this.vx * 0.98 + (Math.random() * this.speed * 2 - this.speed) * 0.12
this.vy = this.vy * 0.98 + (Math.random() * this.speed * 2 - this.speed) * 0.12
//////////////////////////////////////
if (this.history.length > 4){
ctx.beginPath()
ctx.moveTo(this.pos.x ,this.pos.y)
for (var i = this.history.length-1; i >= 0; i--){
ctx.lineTo(this.history[i].x ,this.history[i].y)
}
// ctx.fillStyle = `hsla(${Math.sin( this.step / 300) * 70 + 70},${99}%,${50}%,1)`
// ctx.strokeStyle = `hsla(${Math.sin( this.step / 300) * 70 + 70},${99}%,${50}%,0.5)`
ctx.fillStyle = cols[this.colIndex]
ctx.strokeStyle = cols[this.colIndex]
ctx.fill()
ctx.lineWidth = 2
ctx.lineJoin = "round"
// ctx.closePath()
ctx.stroke()
} //////////////////////////////////////
if (this.pos.x > canvas.width || this.pos.x < 0 || this.pos.y > canvas.height || this.pos.y < 0) {
delete this.pos
delete this.history
return false;
}
this.history.push({
x: this.pos.x,
y: this.pos.y
})
return true;
}
} var particles = [new Particle()] function draw() {
if (particles.length < numParticles) {
particles.push(new Particle())
} particles = particles.filter(function (p){
return p.update()
}) }
</script>
记录一下小程序canvas的更多相关文章
- 技术博客--微信小程序canvas实现图片编辑
技术博客--微信小程序canvas实现图片编辑 我们的这个小程序不仅仅是想给用户提供一个保存和查找的平台,还希望能给用户一个展示自己创意的舞台,因此我们实现了图片的编辑部分.我们对对图片的编辑集成了很 ...
- 原创:WeZRender:微信小程序Canvas增强组件
WeZRender是一个微信小程序Canvas增强组件,基于HTML5 Canvas类库ZRender. 使用 WXML: <canvas style="width: 375px; h ...
- 微信小程序-canvas绘制文字实现自动换行
在使用微信小程序canvas绘制文字时,时常会遇到这样的问题:因为canvasContext.fillText参数为 我们只能设置文本的最大宽度,这就产生一定的了问题.如果我们绘制的文本长度不确定或者 ...
- 微信小程序 canvas 字体自动换行(支持换行符)
微信小程序 canvas 自动适配 自动换行,保存图片分享到朋友圈 https://github.com/richard1015/News 微信IDE演示代码https://developers.w ...
- 微信小程序--canvas画布实现图片的编辑
技术:微信小程序 概述 上传图片,编辑图片大小,添加文字,改变文字颜色等 详细 代码下载:http://www.demodashi.com/demo/14789.html 概述 微信小程序--ca ...
- 小程序canvas生成海报保存至手机相册
小程序canvas画图保存至手机相册 (1)可直接展示生成的海报 .因手机分辨率不同可能导致生成的海报会有细微差别,这里隐藏canvas海报,页面正常设置海报样式保存时保存隐藏的canvas海报 (2 ...
- 优化版小程序canvas,增加失败逻辑,及完善文字
wxml <view class="shareBox" style="backgound:{{isShow ? '#000' : '#fff'}}" wx ...
- 微信小程序 | canvas绘图
1.新的尺寸单位 rpx rpx(responsive pixel): 可以根据屏幕宽度进行自适应. 规定屏幕宽为750rpx.如在 iPhone6 上,屏幕宽度为375px,共有750个物理像素,则 ...
- 微信小程序canvas生成并保存图片
---恢复内容开始--- 微信小程序canvas生成并保存图片,具体实现效果如下图 实现效果需要做以下几步工作 一.先获取用户屏幕大小,然后才能根据屏幕大小来定义canvas的大小 二.获取图 ...
随机推荐
- python实现使用词云展示图片
记录瞬间 首先,要安装一些第三方包 pip install scipyCollecting scipy Downloading https://files.pythonhosted.org/packa ...
- ORACLE中 大量数据插入表 SQL
declare g_commit_count number; cursor cu1 is select gl_flexfields_pkg.get_description_sql(gcc.chart_ ...
- 关于Xamarin、Qml、数据绑定、MVC、MVVM 相关的散讲
关于Xamarin.Qml.数据绑定.MVC.MVVM 相关的散讲 SURFSKY 2017.02 最近又在学习Xamarin了?为什么是“又”?有几个利好消息,让我重新拾起它: ()微软去年收购了X ...
- Log4j 2使用教程二 【详解】
配置 Log4j 2的配置可以通过4种方式中的1种完成: 1.通过使用XML,JSON,YAML或属性格式编写的配置文件. 2.以编程方式,通过创建一个ConfigurationFactory和配置实 ...
- 【JavaScript】轮播图
代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <tit ...
- Python-----多线程threading用法
threading模块是Python里面常用的线程模块,多线程处理任务对于提升效率非常重要,先说一下线程和进程的各种区别,如图 概括起来就是 IO密集型(不用CPU) 多线程计算密集型(用CPU) 多 ...
- 2.2JAVA基础复习——JAVA语言的基础组成运算符和语句
JAVA语言的基础组成有: 1.关键字:被赋予特殊含义的单词. 2.标识符:用来标识的符号. 3.注释:用来注释说明程序的文字. 4.常量和变量:内存存储区域的表示. 5.运算符:程序中用来运算的符号 ...
- 为什么做java开发的公司需要那么多程序员?
注:文章转载自知乎 透过现象看本质. Java是企业应用市场的王者,如果一家非互联网公司用Java,那么十有八九是做企业应用的. 所以,这个问题本质上是:为什么做企业应用的公司需要那么多Java程序员 ...
- VMware install MikroTik RouterOS
1 download the vmdk from Mikro Tik official website 2 create a new vmware host with use an exited vm ...
- Python脚本备份
#!/usr/bin/python3 # -*- coding:utf- -*- # 保证源程序可以输入汉字 print bool([]) # 任何为零的数字或空集(空列表.空元组和空字典等)均为Fa ...