以下是一个简单的JavaScript程序,用于绘制情人节红玫瑰。源代码如下:
var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); var petals = 20; // 玫瑰花瓣数量 var angle = 0; // 角度 var petalSize = 10; // 玫瑰花瓣大小 var centerX = canvas.width / 2; // 玫瑰中心X坐标 var centerY = canvas.height / 2; // 玫瑰中心Y坐标 var radius = 100; // 玫瑰半径 ctx.fillStyle = "red"; // 玫瑰花瓣颜色 // 绘制玫瑰花瓣 for (var i = 0; i < petals; i++) { var x = centerX + Math.cos(angle) * radius; var y = centerY + Math.sin(angle) * radius; ctx.beginPath(); ctx.arc(x, y, petalSize, 0, Math.PI * 2); ctx.fill(); angle += Math.PI / (petals / 2); petalSize += 2; // 玫瑰花瓣逐渐扩大 }
123456789101112131415161718192021222324这段代码会在HTML中创建一个canvas元素,并在canvas上绘制情人节红玫瑰。</