javascript绘制心形图案代码实例

快乐打工仔 分类:实例代码

本章节分享一段代码实例,它实现了绘制心形图案的效果。

代码实例如下:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.pipipi.net/" />
<title>前端教程网</title>
<style>
div {
  position: absolute;
}
.xx-box {
  left: 50%;
  top: 50%;
  margin-left: -250px;
  margin-top: -250px;
  width: 500px;
  height: 500px;
  background-color: #000;
}
.xx-box .text {
  top: 30%;
  height: 48px;
  line-height: 48px;
  color: #f00;
  text-shadow: 3px 3px 4px #f00;
  font-size: 36px;
  font-weight: bold;
  width: 100%;
  text-align: center;
  font-family: Tangerine,Tahoma,Arial,"f6c1ae2ded1b80f53","b8bf53";
}
.xx-box .item {
  width: 2px;
  height: 20px;
  overflow: hidden;
}
</style>
<script>
function createPoint(x, y, c) {
  var div = document.createElement("div");
  div.className = "item";
  div.style.left = x + "px";
  div.style.top = y + "px";
  div.style.backgroundColor = c;
  document.getElementById("xx-box").appendChild(div);
}
function heartShape(r, dx, dy, c) {//r:大小;dx:水平偏移;dy:垂直偏移;c:颜色
  var m, n, x, y, i;
  for (i = 0; i <= 200; i += 0.04) {
    m = i;
    n=-r*(((Math.sin(i)*Math.sqrt(Math.abs(Math.cos(i))))/(Math.sin(i)+1.4))-2*Math.sin(i)+2);
    x = n * Math.cos(m) + dx;
    y = n * Math.sin(m) + dy;
    createPoint(x, y, c);
  }
}
window.onload = function () {
  heartShape(80, 250, 100, "#f00");
}
</script>
</head>
<body>
<div class="xx-box" id="xx-box">
  <div class="text">前端教程网</div>
</div>
</body>
</html>

javascript绘制心形图案代码实例,这样的场景在实际项目中还是用的比较多的,关于javascript绘制心形图案代码实例就介绍到这了。

javascript绘制心形图案代码实例属于前端实例代码,有关更多实例代码大家可以查看

回复

我来回复
  • 暂无回复内容