首页 > 分享 > 七个基于JavaScript实现的情人节表白特效

七个基于JavaScript实现的情人节表白特效

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

<div class="page page-thunder-to-text">

    <input id="input" type="text" maxlength="24" placeholder="I love you!">

    <canvas id="canvas"></canvas>

</div>

<script>

let canvas, ctx, w, h, thunder, text, particles, input;

function Thunder(options) {

    options = options || {};

    this.lifespan = options.lifespan || Math.round(Math.random() * 10 + 10);

    this.maxlife = this.lifespan;

    this.color = options.color || '#fefefe';

    this.glow = options.glow || '#2323fe';

    this.x = options.x || Math.random() * w;

    this.y = options.y || Math.random() * h;

    this.width = options.width || 2;

    this.direct = options.direct || Math.random() * Math.PI * 2;

    this.max = options.max || Math.round(Math.random() * 10 + 20);

    this.segments = [...new Array(this.max)].map(() => {

        return {

            direct: this.direct + (Math.PI * Math.random() * 0.2 - 0.1),

            length: Math.random() * 20 + 80,

            change: Math.random() * 0.04 - 0.02

        };

    });

    this.update = function(index, array) {

        this.segments.forEach(s => { (s.direct += s.change) && Math.random() > 0.96 && (s.change *= -1) });

        (this.lifespan > 0 && this.lifespan--) || this.remove(index, array);

    }

    this.render = function(ctx) {

        if (this.lifespan <= 0) return;

        ctx.beginPath();

        ctx.globalAlpha = this.lifespan / this.maxlife;

        ctx.strokeStyle = this.color;

        ctx.lineWidth = this.width;

        ctx.shadowBlur = 32;

        ctx.shadowColor = this.glow;

        ctx.moveTo(this.x, this.y);

        let prev = { x: this.x, y: this.y };

        this.segments.forEach(s => {

            const x = prev.x + Math.cos(s.direct) * s.length;

            const y = prev.y + Math.sin(s.direct) * s.length;

            prev = { x: x, y: y };

            ctx.lineTo(x, y);

        });

        ctx.stroke();

        ctx.closePath();

        ctx.shadowBlur = 0;

        const strength = Math.random() * 80 + 40;

        const light = ctx.createRadialGradient(this.x, this.y, 0, this.x, this.y, strength);

        light.addColorStop(0, 'rgba(250, 200, 50, 0.6)');

        light.addColorStop(0.1, 'rgba(250, 200, 50, 0.2)');

        light.addColorStop(0.4, 'rgba(250, 200, 50, 0.06)');

        light.addColorStop(0.65, 'rgba(250, 200, 50, 0.01)');

        light.addColorStop(0.8, 'rgba(250, 200, 50, 0)');

        ctx.beginPath();

        ctx.fillStyle = light;

        ctx.arc(this.x, this.y, strength, 0, Math.PI * 2);

        ctx.fill();

        ctx.closePath();

    }

    this.remove = function(index, array) {

        array.splice(index, 1);

    }

}

相关知识

JavaScript实现情人节红玫瑰(附完整源码)
HTML5七夕情人节表白网页制作【HTML5庆祝生日蛋糕烟花特效】HTML+CSS+JavaScript
HTML5之七夕情人节送花动画特效
javascript实现玫瑰花
520表白网页,程序员浪漫表白,求婚倒计时网站制作 HTML+CSS+JS
基于HTML实现浪漫情人节表白代码(附源代码)
jquery+css3实现网页背景花瓣随机飘落特效【转】
HTML5七夕情人节表白网页制作 (浪漫的求婚动画) HTML+CSS+JavaScript
JavaScript实现的风飓风数据可视化分析
七夕情人节送花告白动画(HTML+CSS+JavaScript)

网址: 七个基于JavaScript实现的情人节表白特效 https://m.huajiangbk.com/newsview792839.html

所属分类:花卉
上一篇: HTML5七夕情人节表白网页制作
下一篇: 又到一年情人节,用Html和Py