php 声波图,Canvas 美丽的声波图

JavaScript

语言:

JaveScriptBabelCoffeeScript

确定

//object

var Obj = {

circle: new Array(10),

radius: 200,

noise: 20,

speed: 0.3,

size: 700,

//color a = background color; color b = object color

color: {

a: 'hsla(280, 95%, 5%, 1)',

b: 'hsla(255, 255%, 255%, .8)'

},

//X & Y positions

X: function(x) {

return Obj.c.width / 2 + x;

},

Y: function(y) {

return Obj.c.height / 2 - y;

},

//behavior

Circle: function(i) {

this.r = Obj.radius - i * Obj.radius / Obj.circle.length;

this.e = i % 2 ? true : false;

this.max = Math.random() * Obj.noise;

this.min = -Math.random() * Obj.noise;

this.val = Math.random() * (this.max - this.min) + this.min;

},

//clearing

Clear: function() {

Obj.$.fillStyle = Obj.color.a;

Obj.$.fillRect(0, 0, Obj.c.width, Obj.c.height);

},

//shape changing

Change: function(C) {

for (var i = 0; i < Obj.size; i++) {

var a = i * Math.PI * 2 / Obj.size;

var x = Math.cos(a) * (C.r - C.val * Math.cos(i / 4));

var y = Math.sin(a) * (C.r - C.val * Math.cos(i / 4));

Obj.$.fillStyle = Obj.color.b;

Obj.$.fillRect(Obj.X(x), Obj.Y(y), 1, 1);

}

Obj.Check(C);

},

//noise level checks

Check: function(C) {

C.val = C.e ? C.val + Obj.speed : C.val - Obj.speed;

if (C.val < C.min) {

C.e = true;

C.max = Math.random() * Obj.noise;

}

if (C.val > C.max) {

C.e = false;

C.min = -Math.random() * Obj.noise;

}

},

//update object

Update: function() {

Obj.Clear();

for (var i = 0; i < Obj.circle.length; i++) {

Obj.Change(Obj.circle[i]);

}

},

//draw object

Draw: function() {

Obj.Update();

requestAnimationFrame(Obj.Draw, Obj.c);

},

//set circles

Set: function() {

for (var i = 0; i < Obj.circle.length; i++) {

Obj.circle[i] = new Obj.Circle(i);

}

},

//size control

Size: function() {

Obj.c.width = window.innerWidth / 2;

Obj.c.height = window.innerHeight / 2;

},

//get canvas

Run: function() {

Obj.c = document.querySelector('canvas');

Obj.$ = Obj.c.getContext('2d');

window.addEventListener('resize', Obj.size, false);

},

//start

Init: function() {

Obj.Run();

Obj.Size();

Obj.Set();

Obj.Draw();

}

};

Obj.Init();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值