About Math commonly used method

1. The common usage of Math

Math.random () // 0-1 random number

Math.round () // rounded to integer

Math.ceil () // rounded up

Math.floor () // rounded down

Math.abs () // absolute value

Math.max (num1, num2 ....) // compare the maximum value

Math.min (num1, num2 ....) // compare the minimum

Math.PI // p 3.1415926 - 3.1415927

Math.pow (x, y) // x power of y

Math.sqrt () // square root

Math.sin

Math.cos

Math.tan

Note:

Used to represent the angle in radians

eg: b = c * sin (x); // x represents a degree. Represents the angle (2 * Math.PI / 360) * x in radians;

2. Case

a. Make a hexagon

 Bridesmaid sh = 150 / Mathktn ((2 * Mathkpi / 360) * 30);
        for (var i = 0; i < 6; i++) {
            box.innerHTML += '<div style="transform-origin: center center '+(-z)+'px;transform: translateZ('+z+'px) rotateY('+(i*60)+'deg);background:'+randomColor()+';"></div>';
        }

        // random color (color value in hexadecimal notation.)
        function randomColor() {
            var str = "0123456789abcdef"
            var color = '#';
            for (var i = 0; i < 6; i++) {
                // each time a random subscript 0-15
                color += str.charAt((Math.random() * 15));
            }
           return color;
        }

    </script>
effect:
 

 

 

b. parabola Case

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0
        }
        div:nth-of-type(1) {
            width: 600px;
            height: 2px;
            background: red;
            position: absolute;
            top: 300px;
        }
        div:nth-of-type(2) {
            width: 2px;
            height: 600px;
            background: blue;
            position: absolute;
            left: 300px;
        }
        span {
            display: block;
            width: 2px;
            height: 2px;
            position: absolute;
            border-radius: 1px;
            background: red;
        }
    </style>
</head>
<body>
    <div></div>
    <div></div>
    <script>
        for(var x= -300; x<=300;x++) {
            var y = 0.01 * Math.pow (x, 2);
            document.write('<span style="left:' +(x+300)+ 'px;top:' +(y+300)+'px;"></span>');
        }
    </script>
</body>
</html>
display effect:

 

 

 

 

Guess you like

Origin www.cnblogs.com/lxz123/p/11489705.html