JavaScript利用多彩线条摆出心形效果的示例代码

作者:肥学 时间:2024-04-16 10:29:49 

演示

JavaScript利用多彩线条摆出心形效果的示例代码

源码展示

创建画布

<canvas width="300" height="300" style="width:100%;height:100vh;" id="c"></canvas>

基础样式设置

overflow 语法: overflow:{1,2}= visible | hidden | scroll | auto

默认值:visible

取值:

visible:不剪切内容。hidden:将超出对象尺寸的内容进行裁剪,将不出现滚动条。scroll:将超出对象尺寸的内容进行裁剪,并以滚动条的方式显示超出的内容。auto:在需要时剪切内容并添加滚动条,此为body对象和textarea的默认值。

padding:[ | ]{1,4}

默认值:看每个独立属性

相关属性:[ padding-top ] || [ padding-right ] || [ padding-bottom ] || [padding-left ]

取值: :用长度值来定义内补白。不允许负值:用百分比来定义内补白。不允许负值

说明: 检索或设置对象四边的内部边距。

如果提供全部四个参数值,将按上、右、下、左的顺序作用于四边。 如果只提供一个,将用于全部的四边。

如果提供两个,第一个用于上、下,第二个用于左、右。 如果提供三个,第一个用于上,第二个用于左、右,第三个用于下。

内联对象可以使用该属性设置左、右两边的内补丁;若要设置上、下两边的内补丁,必须先使该对象表现为块级或内联块级。

对应的脚本特性为padding。

html,body{
           border: 0;
           padding: 0;
           margin: 0;
           overflow: hidden;
           background: #000;
}

.info{
 z-index:999;
 position : absolute;
 left:0;
 top:0;
 padding:10px;
 color:#fff;
 background: rgba(0,0,0,0.5);
 text-transform:capitalize;
}

用js来设计动画效果

定义变量

var canvas = document.getElementById('c');
var ctx = canvas.getContext("2d");
var height = void 0,width = void 0,innerpoints = [],outerpoints = [],particles = [];

var noofpoints = 200,trashold = 10;
var x = void 0,y = void 0,p = void 0,n = void 0,point = void 0,dx = void 0,dy = void 0,color = void 0;
deltaangle = Math.PI * 2 / noofpoints,
r = Math.min(height, width) * 0.5;

var distance = function distance(x1, y1, x2, y2) {
 return Math.sqrt(Math.pow(y2 - y1, 2) + Math.pow(x2 - x1, 2));
};
var mapVal = function mapVal(num, in_min, in_max, out_min, out_max) {
 return (num - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
};

设置画布resize

var resize = function resize() {
 height = ctx.canvas.clientHeight;
 width = ctx.canvas.clientWidth;

if (ctx.canvas.clientWidth !== canvas.width ||
 ctx.canvas.clientHeight !== canvas.height)
 {
   console.log("resized");
   canvas.width = width;
   canvas.height = height;
   ctx.translate(canvas.width / 2, canvas.height / 2);
   ctx.rotate(-Math.PI);
   innerpoints = [];
   r = 10;
   for (var i = deltaangle; i <= Math.PI * 2; i += deltaangle) {
     x = r * 16 * Math.pow(Math.sin(i), 3);
     y = r * (13 * Math.cos(i) - 5 * Math.cos(2 * i) - 2 * Math.cos(3 * i) - Math.cos(4 * i));
     innerpoints.push({
       x: x,
       y: y });

x = 10 * r * 16 * Math.pow(Math.sin(i), 3);
     y = 10 * r * (13 * Math.cos(i) - 5 * Math.cos(2 * i) - 2 * Math.cos(3 * i) - Math.cos(4 * i));
     outerpoints.push({
       x: x,
       y: y });

var step = random(0.001, 0.003, true);
     particles.push({
       step: step,
       x: x,
       y: y });

}
 }
};

对线条设计

var draw = function draw() {
 ctx.fillStyle = "rgba(0,0,0,0.03)";
 ctx.fillRect(-width, -height, width * 2, height * 2);
 ctx.beginPath();

for (var i = 0; i < innerpoints.length; i++) {
   s = outerpoints[i];
   d = innerpoints[i];
   point = particles[i];

if (distance(point.x, point.y, d.x, d.y) > 10) {
     dx = d.x - s.x;
     dy = d.y - s.y;

point.x += dx * point.step;
     point.y += dy * point.step;
     color = distance(0, 0, point.x, point.y);
     ctx.beginPath();
     ctx.fillStyle = "hsl(" + color % 360 + ",100%,50%)";
     ctx.arc(point.x, point.y, 2, 0, Math.PI * 2, false);
     ctx.closePath();
     ctx.fill();
   } else {
     point.x = d.x;
     point.y = d.y;
     ctx.beginPath();
     ctx.arc(point.x, point.y, 2, 0, Math.PI * 2, false);
     ctx.closePath();
     ctx.fill();
     particles[i].x = s.x;
     particles[i].y = s.y;
     particles[i].step = random(0.001, 0.003, true);
   }
 }

};

来源:https://blog.csdn.net/jiahuiandxuehui/article/details/125118374

标签:JavaScript,线条,心形
0
投稿

猜你喜欢

  • js实现鼠标悬浮给图片加边框的方法

    2024-04-18 09:40:22
  • CSS3创建惊艳多重边框色

    2010-07-23 10:13:00
  • Tensorflow2.4使用Tuner选择模型最佳超参详解

    2023-07-19 19:46:22
  • 详解python日期时间处理

    2021-08-20 17:07:53
  • 详解如何修改jupyter notebook的默认目录和默认浏览器

    2022-07-01 14:34:54
  • js获取select标签选中值的两种方式

    2024-04-19 09:50:18
  • SQL SERVER日志进行收缩的图文教程

    2024-01-27 13:09:16
  • UltraEdit编辑器免费激活方法

    2023-09-14 22:19:33
  • pytorch tensorboard可视化的使用详解

    2022-09-27 01:01:51
  • MySQL存储过程中游标循环的跳出和继续操作示例

    2024-01-25 05:32:04
  • 在CMD中操作mysql数据库出现中文乱码解决方案

    2024-01-19 10:38:03
  • Firefox 下 innerHTML 的一个 BUG

    2008-08-05 18:19:00
  • 不同浏览器的兼容一些写法

    2009-03-26 12:58:00
  • RC4文件加密的python实现方法

    2023-12-21 20:25:28
  • 使用VSCode如何从github拉取项目的实现

    2023-02-02 10:32:35
  • Python函数装饰器实现方法详解

    2023-08-10 12:33:16
  • Python中字符串对象语法分享

    2022-04-19 14:48:34
  • Python内置函数—vars的具体使用方法

    2021-05-07 04:09:57
  • PyQt5实现用户登录GUI界面及登录后跳转

    2021-04-08 07:50:03
  • Python实战之基于OpenCV的美颜挂件制作

    2022-08-30 20:46:30
  • asp之家 网络编程 m.aspxhome.com