html5 - canvas绘制五角形
PHP中文网
PHP中文网 2017-04-17 13:36:54
0
3
442

在网上看到的视频,不理解是怎样通过for循环和Math.PI / 5*4实现的五角形,大神帮忙解析下

function create5Star(context) {
            var n = 0;
            var dx = 100;
            var dy = 0;
            var s = 50;
            context.BeginPath;
            context.fillStyle = 'rgba(255,0,0,0.5)';
            var x = Math.sin(0);
            var y = Math.cos(0);
            var dig = Math.PI / 5*4;
            for (var i=0;i<5;i++) {
                var x = Math.sin(i*dig);
                var y = Math.cos(i*dig);
                context.lineTo(dx+x*s,dy+y*s);
            }
            context.closePath();
        }
PHP中文网
PHP中文网

认证0级讲师

reply all(3)
迷茫

2π/5 divides 5 points equally. Because the five-pointed star needs to connect one point to the next point, it is multiplied by 2. We get what you said (2π/5)*2 = π/5*4

巴扎黑

I only know a little bit about cavans, but I am very curious about this question. Can you post the address of the video tutorial? I'll go take a look.

左手右手慢动作
context.stroke();

You can see the trajectory

In fact, it is the coordinates of the five vertices you are looking for. Start from the bottom vertex and walk counterclockwise along the trajectory

s is the radius of the circumcircle from the vertex to the pentagram, plus simple sine and cosine knowledge.

However, because the lower right side of the canvas is upright, the five-pointed star is actually upside down

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!