html5 canvas绘制爱心的方法示例


本文摘自PHP中文网,作者黄舟,侵删。

第一种方法


代码实现的一种方法

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

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>使用桃心形方程绘制爱心</title>

</head>

<body>

    <canvas></canvas>

    <script>

        var canvas = document.querySelector('canvas');

        var ctx = canvas.getContext('2d');

        canvas.width = window.innerWidth;

        canvas.height = window.innerHeight;

        var Heart = function(x, y) {

            this.x = x;

            this.y = y;

            this.vertices = [];

            for(let i=0; i<30; i++) {

                var step = i / 30 * (Math.PI * 2);//设置心上面两点之间的角度,具体分成多少份,好像需要去试。

                var vector = {

                    x : (15 * Math.pow(Math.sin(step), 3)),

                    y : -(13 * Math.cos(step) - 5 * Math.cos(2 * step) - 2 * Math.cos(3 * step) - Math.cos(4 * step))

                }

                this.vertices.push(vector);

            }

        }

        Heart.prototype.draw = function() {

            ctx.translate(-1000,this.y);//这一步跟ctx.shadowOffsetX必须一起使用,不明白为啥?

            ctx.beginPath();

            for(let i=0; i<30; i++) {

                var vector = this.vertices[i];

                ctx.lineTo(vector.x, vector.y);

            }

            ctx.shadowColor = "red";

            ctx.shadowOffsetX = this.x+1000;

            ctx.fill();

        }

        canvas.onmousedown = function(e) {

            var x = e.offsetX;

            var y = e.offsetY;

            var heart = new Heart(x, y);

            heart.draw();

        }

    </script>

</body>

</html>

以上就是html5 canvas绘制爱心的方法示例的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

html5编码怎么设置

canvas绘制饼图的方法介绍(代码)

h5视频中背景音乐如何自动播放

【h5开发工具】2017最好用的10种html5应用开发工具推荐

html5返回音频/视频的当前网络状态(activity)的属性networkstate

html5语义化标签及兼容性处理详解

html5实现一个简单的多人飞机游戏实例详解

html5 虚拟键盘出现挡住输入框怎么办

h5+js实现本地文件读取和写入

什么是html h1标签?html h1标签使用方法的详细介绍

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




打赏

取消

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

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

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

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

评论

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