html5使用canvas实现跟随光标跳动的火焰效果


本文摘自PHP中文网,作者不言,侵删。

本示例通过Javascript使用HTML5的canvas元素在屏幕上显示一个跳动的火焰,火焰会跟随光标跳动

本效果的完整代码如下,把代码保存到HTML文件中打开也能查看效果,火焰会跟随光标:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

<!DOCTYPE HTML>

<head>

<meta charset=utf-8" />

<title>HTML5 Canvas火焰效果</title>

<style type="text/css">

body{margin: 0; padding: 0;}

#canvas-keleyi-com {display: block;}

</style>

</head>

<body>

<canvas id="canvas-keleyi-com"></canvas>

<script type="text/javascript">

window.onload = function(){

var keleyi_canvas = document.getElementById("canvas-kel"+"eyi-com");

var ctx = keleyi_canvas.getContext("2d");

var W = window.innerWidth, H = window.innerHeight;

keleyi_canvas.width = W;

keleyi_canvas.height = H;</p>

<p>var particles = [];

var mouse = {};</p>

<p>//Lets create some particles now

var particle_count = 100;

for(var i = 0; i < particle_count; i++)

{

particles.push(new particle());

}

keleyi_canvas.addEventListener('mousemove', track_mouse, false);</p>

<p>function track_mouse(e)

{

mouse.x = e.pageX;

mouse.y = e.pageY;

}</p>

<p>function particle()

{

this.speed = {x: -2.5+Math.random()*5, y: -15+Math.random()*10};

//location = mouse coordinates

//Now the flame follows the mouse coordinates

if(mouse.x && mouse.y)

{

this.location = {x: mouse.x, y: mouse.y};

}

else

{

this.location = {x: W/2, y: H/2};

}

//radius range = 10-30

this.radius = 10+Math.random()*20;

//life range = 20-30

this.life = 20+Math.random()*10;

this.remaining_life = this.life;

//colors

this.r = Math.round(Math.random()*255);

this.g = Math.round(Math.random()*255);

this.b = Math.round(Math.random()*255);

}</p>

<p>function draw()

{

ctx.globalCompositeOperation = "source-over";

ctx.fillStyle = "black";

ctx.fillRect(0, 0, W, H);

ctx.globalCompositeOperation = "lighter";</p>

<p>for(var i = 0; i < particles.length; i++)

{

var p = particles[i];

ctx.beginPath();

p.opacity = Math.round(p.remaining_life/p.life*100)/100

var gradient = ctx.createRadialGradient(p.location.x, p.location.y, 0, p.location.x, p.location.y, p.radius);

gradient.addColorStop(0, "rgba("+p.r+", "+p.g+", "+p.b+", "+p.opacity+")");

gradient.addColorStop(0.5, "rgba("+p.r+", "+p.g+", "+p.b+", "+p.opacity+")");

gradient.addColorStop(1, "rgba("+p.r+", "+p.g+", "+p.b+", 0)");

ctx.fillStyle = gradient;

ctx.arc(p.location.x, p.location.y, p.radius, Math.PI*2, false);

ctx.fill();</p>

<p>

p.remaining_life--;

p.radius--;

p.location.x += p.speed.x;

p.location.y += p.speed.y;</p>

<p>if(p.remaining_life < 0 || p.radius < 0)

{

particles[i] = new particle();

}

}

}</p>

<p>setInterval(draw, 86);

}

</script>

</body>

</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

使用canvas画“哆啦A梦”时钟的代码

关于html5.2 dialog的介绍

以上就是html5使用canvas实现跟随光标跳动的火焰效果的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

html5 canvas标签是什么意思?canvas标签使用方法介绍

html5 canvas实现瀑布流文字效果代码

用html5 canvas来绘制三角形和矩形等多边形的方法

使用canvas画“哆啦a梦”时钟的代码

canvas 学习 3---画坐标系

高德地图+canvas画图结合实现一个小项目

借助todataurl实现将html5 canvas的内容保存为图片

使用canvas压缩图片的具体步骤

html5 canvas实现画直线与设置线条的样式

图文详解如何用canvas画实心圆和空心圆

更多相关阅读请进入《canvas》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...