|
|

楼主 |
发表于 2023-6-17 12:22
|
显示全部楼层
代码
- <style>
- #mydiv {
- width: 200px;
- height: 200px;
- background: rebeccapurple;
- border-radius: 50%;
- }
- .papa > div, .papa > p { margin: 20px 0; }
- .papa label, .papa input { margin: 0 8px; }
- </style>
- <div class="papa">
- <p>
- <input id="btnReset" type="button" value="重置" />
- <label for="numLen">宽高:</label>
- <input id="numLen" type="number" value="200" min="100" max="1000" />
- <label for="numPoint">边数:</label>
- <input id="numPoint" type="number" value="3" min="3" max="360" />
- <label for="numThick">边厚:</label>
- <input id="numThick" type="number" value="10" min="5" max="40" />
- <input id="btnOk" type="button" value="裁剪" />
- </p>
- <div id="mydiv"></div>
- <div id="msgBox"></div>
- </div>
- <script>
- let clipBox = (xx,points,thick) => {
- if(points < 3) points = 3;
- let x0 = y0 = r = xx / 2, pointsAr = [`200px 0, 0 0, 0 ${xx}px, ${xx}px ${xx}px, ${xx}px ${xx/2}px`];
- for(let i = 0; i <= points; i ++) {
- let hudu = Math.PI / 180 * 360 / points * i;
- let x1 = x0 + Math.cos(hudu) * (r - thick), y1 = y0 + Math.sin(hudu) * (r - thick);
- pointsAr.push(x1 + 'px ' + y1 + 'px');
- }
- pointsAr.push(`${xx}px ${xx/2}px`);
- pointsAr.push(`${xx}px 0px`);
- return `polygon(${pointsAr.join(',')})`;
- }
- let limit = (box,min,max) => {
- if(box.value < min) box.value = min;
- if(box.value > max) box.value = max;
- }
- numPoint.onchange = () => limit(numPoint,3,360);
- numLen.onchange = () => limit(numLen,40,600);
- numThick.onchange = () => limit(numThick,5,40);
- btnOk.onclick = () => {
- mydiv.style.width = numLen.value + 'px';
- mydiv.style.height = numLen.value + 'px';
- mydiv.style.clipPath = clipBox(numLen.value, numPoint.value, numThick.value);
- msgBox.innerText = 'clip-path: ' + mydiv.style.clipPath + ';';
- }
- btnReset.onclick = () => {
- mydiv.style.clipPath = '';
- mydiv.style.width = '200px';
- mydiv.style.height = '200px';
- numLen.value = '200';
- numPoint.value = '3';
- numThick.value = '10';
- msgBox.innerText = '';
- }
- </script>
复制代码
|
评分
-
| 参与人数 1 | 威望 +50 |
金钱 +100 |
经验 +50 |
收起
理由
|
红影
| + 50 |
+ 100 |
+ 50 |
赞一个! |
查看全部评分
|