javascript玫瑰花代码
时间: 2023-11-13 20:53:40 浏览: 124
好的,以下是JavaScript实现玫瑰花的代码:
```
function drawRoses() {
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var width = canvas.width;
var height = canvas.height;
var petals = 24;
var radius = (width/2)*0.8;
var centerX = width/2;
var centerY = height/2;
var angle = (2*Math.PI)/petals;
ctx.strokeStyle = "red";
ctx.lineWidth = 5;
ctx.beginPath();
for(var i=0; i<petals; i++) {
var petalAngle = i*angle;
var x = centerX + radius * Math.sin(petalAngle);
var y = centerY - radius * Math.cos(petalAngle);
ctx.moveTo(centerX, centerY);
ctx.quadraticCurveTo(x, y, centerX, centerY);
}
ctx.stroke();
}
```
该代码将在HTML文档中具有ID为“canvas”的画布元素中绘制一朵玫瑰花。
阅读全文