Skip to content

Commit

Permalink
✨ add canvas animation
Browse files Browse the repository at this point in the history
  • Loading branch information
nicejade committed Sep 16, 2018
1 parent 9c24e70 commit 7581863
Show file tree
Hide file tree
Showing 8 changed files with 317 additions and 35 deletions.
19 changes: 5 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
"name": "nicelinks-quick-app",
"version": "1.0.0",
"subversion": {
"toolkit": "0.0.34"
"toolkit": "0.0.35-beta.2"
},
"description": "",
"scripts": {
"start": "node ./command/server.js",
"server": "hap server",
"postinstall": "hap postinstall",
"debug": "hap debug",
Expand All @@ -15,28 +14,20 @@
"watch": "hap watch"
},
"devDependencies": {
"address": "^1.0.3",
"hap-toolkit": "0.0.35-beta.2",
"babel-cli": "^6.10.1",
"babel-core": "^6.26.0",
"babel-eslint": "^8.2.1",
"babel-loader": "^7.1.4",
"babel-plugin-syntax-jsx": "^6.18.0",
"chalk": "^2.4.1",
"child_process": "^1.0.2",
"cross-env": "^5.1.4",
"css-what": "^2.1.0",
"hap-toolkit": "0.0.34",
"koa": "^2.3.0",
"koa-body": "^2.5.0",
"koa-router": "^7.2.1",
"koa-send": "^4.1.1",
"koa-static": "^4.0.1",
"less": "^3.8.0",
"less-loader": "^4.1.0",
"opn": "^5.3.0",
"portfinder": "^1.0.17",
"shelljs": "^0.8.2",
"koa-body": "^2.5.0",
"koa-router": "^7.2.1",
"socket.io": "^2.1.0",
"webpack": "^3.11.0"
}
}
}
10 changes: 7 additions & 3 deletions src/assets/styles/common.less
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
font-size: 5 * @size-factor;
}
.wrapper-padding {
padding: 0 5 * @size-factor;
padding: 0 6 * @size-factor;
}

.title {
color: #212121;
color: @black;
font-size: 6 * @size-factor;
font-weight: bold;
text-align: center;
margin-top: 5 * @size-factor;
}
.desc {
color: #454545;
color: @grey-black;
margin-top: 4 * @size-factor;
font-size: 4.5 * @size-factor;
}
Expand All @@ -26,4 +26,8 @@
border-radius: 40px;
margin-top: 6 * @size-factor;
margin-bottom: 6 * @size-factor;
}

.external-link {
color: @brand;
}
1 change: 1 addition & 0 deletions src/assets/styles/variables.less
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
@white: #ffffff;
@black: #000000;
@grey: #7a8ba9;
@grey-black: #454545;
@border-grey: #eaeaea;
@white-grey: #f4f6fa;

Expand Down
122 changes: 122 additions & 0 deletions src/components/Particle.ux
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<template>
<div class="nice-particle">
<canvas id="niceCanvas" class="nice-canvas"></canvas>
</div>
</template>

<script>
import $device from '@system.device'
import $app from '@system.app'
const appInfo = $app.getInfo()

export default {
// 页面级组件的数据模型,影响传入数据的覆盖机制:private内定义的属性不允许被覆盖
private: {
},

public: {
title: appInfo.name,
icon: appInfo.icon,
mCanvas: null,
mContext: null,
mParticleNum: 20,
mParticleIndex: 0,
mParticles: {},
mWidth: 800,
mHeight: 300
},

onInit () {
},

onReady () {
setTimeout(() => {
this.startNiceDraw()
}, 1000)
},

onShow () {
},

createParticle () {
const that = {}
that.posX = this.mWidth / 2; // position X
that.posY = this.mHeight / 8; // position Y
that.vx = Math.random() * 10 - 5; // velocity X
that.vy = Math.random() * 10 - 5; // velocity Y
that.width = 1; // particle width
that.height = Math.random() * 6 - 3; // particle height

this.mParticleIndex++
this.mParticles[this.mParticleIndex] = that
that.id = this.mParticleIndex

that.life = 0;
that.death = 140;

// random color
that.colors = [
"rgba(100, 100, 100," + (Math.random() + .5) + ")",
"rgba(52, 152, 200," + (Math.random() + .5) + ")",
"rgba(41, 128, 250," + (Math.random() + .5) + ")"];
that.color = that.colors[Math.floor(Math.random() * 3)];
},

drawParticleCanvas(target) {
const gravity = 0.7
target.posX += target.vx;
target.posY += target.vy;

target.life++;

if (target.life >= target.death) {
delete this.mParticles[target.id];
}
if (target.posY > (this.mCanvas.height - 5)) {
target.vx *= 0.8;
target.vy *= -0.5;
target.posY = (this.mCanvas.height - 5);
}

target.vy += gravity;

this.mContext.fillStyle = target.color;
this.mContext.fillRect(target.posX, target.posY, target.width, target.height);
},

startNiceDraw() {
this.mCanvas = this.$element('niceCanvas')
this.mContext = this.mCanvas.getContext("2d")
// this.mCanvas.width = $device.screenWidth || 800
// this.mCanvas.height = $device.screenHeight || 300
this.mContext.fillRect(0, 0, this.mWidth, this.mHeight);
setInterval(() => {
this.mContext.fillStyle = "rgba(0, 0, 0, 0.8)";
this.mContext.fillRect(0, 0, this.mWidth, this.mHeight);

for(let i = 0; i < this.mParticleNum; i++){
this.createParticle()
}

for(let i in this.mParticles){
this.drawParticleCanvas(this.mParticles[i])
}
}, 1000)
}
}
</script>

<style lang="less">
@import './../assets/styles/style.less';
.nice-particle {
.flex-box-mixins(column, center, center);
width: 100%;
height: 300px;
background-color: rgba(0,0,0,0.4);
.nice-canvas {
width: 100%;
height: 300px;
/* background-color: rgba(0,222,0,0.4); */
}
}
</style>
142 changes: 142 additions & 0 deletions src/components/WaterWorld.ux
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<template>
<div class="nice-particle">
<canvas id="waterCanvas" class="water-canvas"></canvas>
</div>
</template>

<script>
import { $util } from './../helper'
let tim, bai, a, b, c, s, p, r, x, y, z, x1, y1, max, p2, han, h, pt, hei;
const colorRandomList = [
[87, 209, 201, 0.3],
[221, 10, 53, 0.3],
[66, 33, 142, 0.3],
[43, 187, 216, 0.3],
[92,242,232, 0.3],
[135,226,147, 0.3],
[0, 255, 0, 0.3],
[255, 0, 0, 0.3],
[245, 223, 101, 0.3],
[17, 141, 240, 0.3],
[31, 138, 255, 0.3]
]
export default {
// 页面级组件的数据模型,影响传入数据的覆盖机制:private内定义的属性不允许被覆盖;
protected: {
mCanvas: null,
mContext: null,
mWidth: 800,
mHeight: 300,
randomIndex: 0
},

onInit () {
this.randomIndex = $util.getRandomInt(0, 10)
},

onReady () {
setTimeout(() => {
this.startNiceDraw()
}, 10)
},

onShow () {
},

startNiceDraw () {
this.mCanvas = this.$element('waterCanvas')
this.mContext =this.mCanvas.getContext('2d');
this.mCanvas.width = 800;
this.mCanvas.height = 400;
setInterval(() => {
this.drawParticleCanvas()
}, 60)
},

drawParticleCanvas() {
this.mContext.globalCompositeOperation = "source-over";
this.mContext.fillStyle = "rgb(0, 0, 0)";
this.mContext.fillRect(0, 0, this.mCanvas.width, this.mCanvas.height);
this.mContext.globalCompositeOperation = "lighter";
tim = new Date().getTime() / 10;
const colorStr = colorRandomList[this.randomIndex].join(',')
this.mContext.strokeStyle = this.mContext.fillStyle = `hsla(${colorStr})`;

bai = 0.6 + Math.sin(tim / 5) * 0.2;
hei = 0.9 + Math.sin(tim / 100) * 0.3;

pt = [];
for (c = 0; c < 180; c++) {
s = (c + 1) / 180;
han = Math.cos(s * Math.PI / 2);
h = Math.sin(s * Math.PI / 2);
s = 1 - s;
p = [];
max = (100 * han) | 0;
r = 0;
for (a = 0; a < max; a++) {
x = Math.cos(r) * han;
y = Math.sin(r) * han;
z = this.nami(r, s);
x -= z * s;
p.push([x, y, hei + z * s + h]);
r += Math.PI * 2 / max;
}
pt.push(p);
}
for (c = 0; c < 100; c++) {
s = (c + 1) / 100;
p = [];
max = 100 * s;
r = 0;
for (a = 0; a < max; a++) {
x = Math.cos(r) * s;
y = Math.sin(r) * s;
z = this.nami(r, s);
x -= z * s;
p.push([x, y, hei + z * s]);
r += Math.PI * 2 / max;
}
pt.push(p);
}
for (c = 0; c < pt.length; c++) {
p = pt[c];
p2 = [];
for (a = 0; a < p.length; a++) {
x = p[a][0];
y = p[a][1];
z = p[a][2];
b = Math.pow(1.5, y / 2);
x1 = x * b * 150 + 200;
y1 = z * b * 200 - hei * 200 + 150;
p2.push([x1, y1]);
}
this.mContext.beginPath();
for (a = 0; a < p2.length; a++) this.mContext.lineTo(p2[a][0], p2[a][1]);
this.mContext.closePath();
this.mContext.stroke();
}
// requestAnimationFrame(this.drawParticleCanvas)
},
nami(r, s) {
var a = Math.sin(r * 2 - tim / 13 + s * 13) / 20 + Math.sin(r * 5 - tim / 17 + s * 13) / 20 + Math.sin(r * 7 - tim / 19 + s * 13) / 40;
return a * bai;
}
}
</script>

<style lang="less">
@import './../assets/styles/style.less';
.nice-particle {
.flex-box-mixins(column, center, center);
width: 100%;
height: 369px;
background-color: @black;
.water-canvas {
background-color: @black;
width: 400px;
height: 369px;
}
}
</style>

9 changes: 8 additions & 1 deletion src/helper/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,17 @@ function queryString (url, query) {
return paramStr ? `${url}?${paramStr}` : url
}

function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min
}

export default {
showMenu,
createShortcut,
serverUrl,
queryString,
setCurrentDate
setCurrentDate,
getRandomInt
}
3 changes: 2 additions & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
"name": "倾城之链",
"versionName": "1.0.1",
"versionCode": "2",
"minPlatformVersion": "1010",
"minPlatformVersion": "1020",
"icon": "/assets/images/logo.png",
"features": [
{ "name": "system.prompt" },
{ "name": "system.router" },
{ "name": "system.shortcut" },
{ "name": "system.webview" },
{ "name": "service.share" },
{ "name": "system.device" },
{ "name": "system.fetch" }
],
"permissions": [
Expand Down
Loading

0 comments on commit 7581863

Please sign in to comment.