【飘逸的线条~

本文通过一个前端代码实例展示了使用JavaScript实现的Web动画,包括绳子模拟效果,并详细解析了相关类的构造和方法。同时,文章还提供了前端面试题解析,涵盖了Web核心知识点和实际项目经验,适合前端开发者学习和提升。
摘要由CSDN通过智能技术生成

window.mozRequestAnimationFrame ||

window.oRequestAnimationFrame ||

window.msRequestAnimationFrame ||

function(callback) {

window.setTimeout(callback);

}

);

});

function init(elemid) {

let canvas = document.getElementById(elemid),

c = canvas.getContext(“2d”),

w = (canvas.width = window.innerWidth),

h = (canvas.height = window.innerHeight);

c.fillStyle = “rgba(30,30,30,1)”;

c.fillRect(0, 0, w, h);

return {c:c,canvas:canvas};

}

《大厂前端面试题解析+Web核心总结学习笔记+企业项目实战源码+最新高清讲解视频》无偿开源 徽信搜索公众号【编程进阶路】

style.css

body,

html {

margin: 0px;

padding: 0px;

position: fixed;

background: rgb(30, 30, 30);

cursor: none;

}

script.js

window.onload = function () {

//functions definition

//class definition

class segm {

constructor(x, y, l) {

this.b = Math.random()*1.9+0.1;

this.x0 = x;

this.y0 = y;

this.a = Math.random() * 2 * Math.PI;

this.x1 = this.x0 + l * Math.cos(this.a);

this.y1 = this.y0 + l * Math.sin(this.a);

this.l = l;

}

update(x, y) {

this.x0 = x;

this.y0 = y;

this.a = Math.atan2(this.y1 - this.y0, this.x1 - this.x0);

this.x1 = this.x0 + this.l * Math.cos(this.a);

this.y1 = this.y0 + this.l * Math.sin(this.a);

}

}

class rope {

constructor(tx, ty, l, b, slq) {

this.res = l / slq;

this.l = l;

this.segm = [];

this.segm.push(new segm(tx, ty, this.l / this.res));

for (let i = 1; i < this.res; i++) {

this.segm.push(

new segm(this.segm[i - 1].x1, this.segm[i - 1].y1, this.l / this.res)

);

}

this.b = b;

}

update(t) {

this.segm[0].update(t.x, t.y);

for (let i = 1; i < this.res; i++) {

this.segm[i].update(this.segm[i - 1].x1, this.segm[i - 1].y1);

}

}

show(t) {

if(t == “l”){

c.beginPath();

for (let i = 0; i < this.segm.length; i++) {

c.lineTo(this.segm[i].x0, this.segm[i].y0);

}

c.lineTo(

this.segm[this.segm.length - 1].x1,

this.segm[this.segm.length - 1].y1

);

c.strokeStyle = “white”;

c.lineWidth = this.b;

c.stroke();

c.beginPath();

c.arc(this.segm[0].x0, this.segm[0].y0, 1, 0, 2 * Math.PI);

c.fillStyle = “white”;

c.fill();

c.beginPath();

c.arc(

this.segm[this.segm.length - 1].x1,

this.segm[this.segm.length - 1].y1,

2,

0,

2 * Math.PI

);

c.fillStyle = “white”;

c.fill();

}else{

for (let i = 0; i < this.segm.length; i++) {

c.beginPath();

c.arc(this.segm[i].x0, this.segm[i].y0, this.segm[i].b, 0, 2*Math.PI);

c.fillStyle = “white”;

c.fill();

}

c.beginPath();

c.arc(

this.segm[this.segm.length - 1].x1,

this.segm[this.segm.length - 1].y1,

2, 0, 2*Math.PI

);

c.fillStyle = “white”;

c.fill();

}

}

}

//setting up canvas

let c = init(“canvas”).c,

canvas = init(“canvas”).canvas,

w = (canvas.width = window.innerWidth),

h = (canvas.height = window.innerHeight),

ropes = [];

//variables definition

let nameOfVariable = “value”,

mouse = {},

last_mouse = {},

rl = 50,

randl = [],

target = { x: w/2, y: h/2 },

last_target = {},

t = 0,

q = 10,

da = [],

type = “l”;

for (let i = 0; i < 100; i++) {

ropes.push(

new rope(

w / 2,

h / 2,

(Math.random() * 1 + 0.5) * 500,

Math.random() * 0.4 + 0.1,

Math.random()*15+5

)

);

randl.push(Math.random() * 2 - 1);

da.push(0);

}

//place for objects in animation

function draw() {

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值