好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

html5 canvas绘制爱心的方法示例

第一种方法@H_ 406 _1@


代码实现的一种方法

<!DOCTY PE  ht ML >
<html lang="en">
<head>
    < ;m eta charset="UTF-8">
    <t IT le>使用桃心形方程 绘制 爱心</title>
</head>
<body>
    <canvas></canvas>
    <script>
         VAR  canvas = document.querySelector(& # 39;canvas');
        var ctx = canvas.getContext('2d');
        canvas.width = window.innerWidth;
        canvas.h ei ght = 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. PR ototype.draw = function() {
            ctx.translate(-1000,this.y);//这一步跟ctx.shadowOffsetX必须一起使用,不明白为啥?
            ctx.be gin Path();
            for(let i=0; i<30; i++) {
                var vector = this.vertices[i];
                ctx.l inet o(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绘制爱心的方法示例 全部内容,希望文章能够帮你解决 html5 canvas绘制爱心的方法示例 所遇到的问题。

如果觉得 网站内容还不错, 推荐好友。

查看更多关于html5 canvas绘制爱心的方法示例的详细内容...

  阅读:19次