请给我写一个爱心的代码
时间: 2024-05-12 11:07:21 浏览: 98
点燃你温暖我同款爱心代码
5星 · 资源好评率100%
这是一个简单的爱心代码,使用JavaScript实现:
```html
<!DOCTYPE html>
<html>
<head>
<title>爱心</title>
<meta charset="utf-8">
<style type="text/css">
body {
margin: 0;
padding: 0;
background-color: #f7f7f7;
}
canvas {
display: block;
margin: auto;
}
</style>
</head>
<body>
<canvas id="canvas" width="300" height="300"></canvas>
<script type="text/javascript">
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var x = canvas.width / 2;
var y = canvas.height / 2;
var radius = 50;
var startAngle = 0;
var endAngle = Math.PI * 2;
var counterClockwise = false;
// 绘制红色实心圆
ctx.beginPath();
ctx.arc(x, y, radius, startAngle, endAngle, counterClockwise);
ctx.fillStyle = "#ff0000";
ctx.fill();
// 绘制两个半圆,组成爱心形状
ctx.beginPath();
ctx.moveTo(x, y + radius);
ctx.arc(x - radius / 2, y, radius / 2, Math.PI, 0, counterClockwise);
ctx.lineTo(x, y + radius);
ctx.arc(x + radius / 2, y, radius / 2, Math.PI, 0, counterClockwise);
ctx.fillStyle = "#ffffff";
ctx.fill();
</script>
</body>
</html>
```
效果如下:
![love](https://user-images.githubusercontent.com/762511/132098406-8f3f3d14-3f3d-4a89-a2b1-64d1d7c7b8e8.png)
阅读全文