function siriwave(opt) { this.opt = opt || {}; this.k = 1; this.f = 6; this.speed = this.opt.speed || 0.1; this.noise = this.opt.noise || 0; this.phase = this.opt.phase || 0; if (!devicepixelratio) devicepixelratio = 1; this.width = devicepixelratio * (this.opt.width || 320); this.height = devicepixelratio * (this.opt.height || 200); this.max = (this.height / 2) - 4; this.canvas = document.createelement('canvas'); this.canvas.id = "xin_wave"; this.canvas.width = this.width; this.canvas.height = this.height; this.canvas.style.width = (this.width / devicepixelratio) + 'px'; this.canvas.style.height = (this.height / devicepixelratio) + 'px'; if (document.getelementsbyclassname("xin_dong").length != 0) { for (var i = 0; i < document.getelementsbyclassname("xin_dong").length; i++) { document.getelementsbyclassname("xin_dong")[i].appendchild(this.canvas); } } this.ctx = this.canvas.getcontext('2d'); this.run = false; } siriwave.prototype = { _globalattenuationfn: function(x) { return math.pow(this.k * 4 / (this.k * 4 + math.pow(x, 4)), this.k * 2); }, _drawline: function(attenuation, color, width) { this.ctx.moveto(0, 0); this.ctx.beginpath(); this.ctx.strokestyle = color; this.ctx.linewidth = width || 0.5; var x, y; for (var i = -this.k; i <= this.k; i += 0.01) { x = this.width * ((i + this.k) / (this.k * 2)); y = this.height / 2 + this.noise * this._globalattenuationfn(i) * (1 / attenuation) * math.sin(this.f * i - this.phase); this.ctx.lineto(x, y); } this.ctx.stroke(); }, _clear: function() { this.ctx.globalcompositeoperation = 'destination-out'; this.ctx.fillrect(0, 0, this.width, this.height); this.ctx.globalcompositeoperation = 'source-over'; }, _draw: function() { if (!this.run) return; this.phase = (this.phase + this.speed) % (math.pi * 64); this._clear(); this._drawline(-5, 'rgba(0,0,0,0.04)'); this._drawline(-4, 'rgba(0,0,0,0.06)'); this._drawline(-3, 'rgba(0,0,0,0.08)'); this._drawline(-2, 'rgba(0,0,0,0.1)'); this._drawline(-1.6, 'rgba(0,0,0,0.2)'); requestanimationframe(this._draw.bind(this), 1000); }, start: function() { this.phase = 0; this.run = true; this._draw(); }, stop: function() { this.run = false; this._clear(); }, setnoise: function(v) { this.noise = math.min(v, 1) * this.max; }, setspeed: function(v) { this.speed = v; }, set: function(noise, speed) { this.setnoise(noise); this.setspeed(speed); }, }; var sw = new siriwave({ width: 1920, height: 200 }); sw.setspeed(0.03); sw.setnoise(0.7); sw.start();