如何使用 HTML5 Canvas 制作水波纹效果

JavaScript018

如何使用 HTML5 Canvas 制作水波纹效果,第1张

1、首先:html

<!DOCTYPE html>

<html>

<head>

<meta charset=utf-8 />

<title>Water drops effect</title>

<link rel="stylesheet" href="css/main.css" type="text/css" />

<script src="js/vector2d.js" type="text/javascript" charset="utf-8"></script>

<script src="js/waterfall.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>

<div class="example">

<h3><a href="#">Water drops effect</a></h3>

<canvas id="water">HTML5 compliant browser required</canvas>

<div id="switcher">

<img onclick='watereff.changePicture(this.src)' src="data_images/underwater1.jpg" />

<img onclick='watereff.changePicture(this.src)' src="data_images/underwater2.jpg" />

</div>

<div id="fps"></div>

</div>

</body>

</html> 

2、然后是css样式

body{background:#eeemargin:0padding:0}

.example{background:#FFFwidth:600pxborder:1px #000 solidmargin:20px autopadding:15px-moz-border-radius: 3px-webkit-border-radius: 3px}

#water {

width:500px

height:400px

display: block

margin:0px auto

cursor:pointer

}

#switcher {

text-align:center

overflow:hidden

margin:15px

}

#switcher img {

width:160px

height:120px

}

3、再然后是js代码

function drop(x, y, damping, shading, refraction, ctx, screenWidth, screenHeight){

this.x = x

this.y = y

this.shading = shading

this.refraction = refraction

this.bufferSize = this.x * this.y

this.damping = damping

this.background = ctx.getImageData(0, 0, screenWidth, screenHeight).data

this.imageData = ctx.getImageData(0, 0, screenWidth, screenHeight)

this.buffer1 = []

this.buffer2 = []

for (var i = 0i <this.bufferSizei++){

this.buffer1.push(0)

this.buffer2.push(0)

}

this.update = function(){

for (var i = this.x + 1, x = 1i <this.bufferSize - this.xi++, x++){

if ((x <this.x)){

this.buffer2[i] = ((this.buffer1[i - 1] + this.buffer1[i + 1] + this.buffer1[i - this.x] + this.buffer1[i + this.x]) / 2) - this.buffer2[i]

this.buffer2[i] *= this.damping

} else x = 0

}

var temp = this.buffer1

this.buffer1 = this.buffer2

this.buffer2 = temp

}

this.draw = function(ctx){

var imageDataArray = this.imageData.data

for (var i = this.x + 1, index = (this.x + 1) * 4i <this.bufferSize - (1 + this.x)i++, index += 4){

var xOffset = ~~(this.buffer1[i - 1] - this.buffer1[i + 1])

var yOffset = ~~(this.buffer1[i - this.x] - this.buffer1[i + this.x])

var shade = xOffset * this.shading

var texture = index + (xOffset * this.refraction + yOffset * this.refraction * this.x) * 4

imageDataArray[index] = this.background[texture] + shade

imageDataArray[index + 1] = this.background[texture + 1] + shade

imageDataArray[index + 2] = 50 + this.background[texture + 2] + shade

}

ctx.putImageData(this.imageData, 0, 0)

}

}

var fps = 0

var watereff = {

// variables

timeStep : 20,

refractions : 2,

shading : 3,

damping : 0.99,

screenWidth : 500,

screenHeight : 400,

pond : null,

textureImg : null,

interval : null,

backgroundURL : 'data_images/underwater1.jpg',

// initialization

init : function() {

var canvas = document.getElementById('water')

if (canvas.getContext){

// fps countrt

fps = 0

setInterval(function() {

document.getElementById('fps').innerHTML = fps / 2 + ' FPS'

fps = 0

}, 2000)

canvas.onmousedown = function(e) {

var mouse = watereff.getMousePosition(e).sub(new vector2d(canvas.offsetLeft, canvas.offsetTop))

watereff.pond.buffer1[mouse.y * watereff.pond.x + mouse.x ] += 200

}

canvas.onmouseup = function(e) {

canvas.onmousemove = null

}

canvas.width = this.screenWidth

canvas.height = this.screenHeight

this.textureImg = new Image(256, 256)

this.textureImg.src = this.backgroundURL

canvas.getContext('2d').drawImage(this.textureImg, 0, 0)

this.pond = new drop(

this.screenWidth,

this.screenHeight,

this.damping,

this.shading,

this.refractions,

canvas.getContext('2d'),

this.screenWidth, this.screenHeight

)

if (this.interval != null){

clearInterval(this.interval)

}

this.interval = setInterval(watereff.run, this.timeStep)

}

},

// change image func

changePicture : function(url){

this.backgroundURL = url

this.init()

},

// get mouse position func

getMousePosition : function(e){

if (!e){

var e = window.event

}

if (e.pageX || e.pageY){

return new vector2d(e.pageX, e.pageY)

} else if (e.clientX || e.clientY){

return new vector2d(e.clientX, e.clientY)

}

},

// loop drawing

run : function(){

var ctx = document.getElementById('water').getContext('2d')

watereff.pond.update()

watereff.pond.draw(ctx)

fps++

}

}

window.onload = function(){

watereff.init()

}

4、然后就没然后了

由于传输线的特性阻抗不匹配引起的故障现象,这种现象的表现形式是在监视器的画面上产生若干条间距相等的竖条干扰,干扰信号的频率基本上是行频的整数倍。这是由于视频传输线的特性阻抗不是75Ω而导致阻抗失配造成的。

另外,值得注意的是,在视频传输距离很短时(一般为 150米以内),使用上述阻抗失配和分布参数过大的视频电缆不一定会出现上述的干扰现象。解决上述问题的根本办法是在选购视频电缆时,一定要保证质量。必要时应对电缆进行抽样检测。

扩展资料

红外半球系列机器晚上出现图像照度差、发白或有亮白色光圈现象。该现象是机器装配不当导致的,装配时感光器件(光敏电阻)离半球距离过远会导致红外灯启动不完全造成机器夜间照度差;

出现图像发白或亮白色光圈现象主要因红外发光管发出的红外光通过球罩折射到镜头所致,解决此现象就是避免让红外光折射到镜头表面,通常采用海绵圈进行镜头与红外光的隔离,在装配时一定要将球罩紧贴海绵圈。

参考资料来源:百度百科-监控摄像头