用JavaScript画一个心形
时间: 2023-11-03 20:06:29 浏览: 26
以下是用JavaScript画一个心形的代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>Heart Shape</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
canvas {
border: 1px solid black;
}
</style>
</head>
<body>
<canvas id="myCanvas" width="300" height="300"></canvas>
<script>
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var x = canvas.width / 2;
var y = canvas.height / 2;
var radius = 80;
ctx.beginPath();
ctx.moveTo(x, y);
ctx.bezierCurveTo(x + radius, y - radius, x + radius, y + (radius * 2), x, y + (radius * 2));
ctx.bezierCurveTo(x - radius, y + (radius * 2), x - radius, y - radius, x, y);
ctx.fillStyle = "red";
ctx.fill();
</script>
</body>
</html>
```
这段代码使用了HTML5 Canvas元素和JavaScript绘制了一个红色的心形。