css线条伸缩_Canvas 线条伸缩动画

本文介绍了如何使用JavaScript和Canvas创建线条伸缩动画。通过设置canvas尺寸,创建顶点和线条,利用 easing 函数更新线条位置,实现了线条的动态变化效果。文章详细展示了代码实现过程,包括线条和点的定义,以及各种缓动函数的应用。
摘要由CSDN通过智能技术生成

JavaScript

语言:

JaveScriptBabelCoffeeScript

确定

// canvas settings

var viewWidth = 512,

viewHeight = 512,

canvas = document.getElementById("drawing_canvas"),

ctx,

timeStep = (1/60),

time = 0;

var vertices = [],

lines = [];

window.onload = function() {

initCanvas();

createLines();

requestAnimationFrame(loop);

};

function initCanvas() {

canvas.width = viewWidth;

canvas.height = viewHeight;

ctx = canvas.getContext('2d');

}

function createLines() {

var count = 256,

step = 4;

// create points

var phi = Math.PI * (3 - Math.sqrt(5)),

theta, r, x, y;

for (var i = 0; i < count; i+=step) {

theta = i * phi;

r = Math.sqrt(i) / Math.sqrt(count);

x = r * Math.cos(theta) * 196;

y = r * Math.sin(theta) * 196;

vertices.push(new Point(x, y));

}

// create lines

var start, end, color,

lineCount = (count / step) - 2;

for (var j = 1; j < lineCount; j++) {

start = vertices[j];

end = getNext(start, vertices);

color = 'hsl(' + Math.floor(j / lineCount * 360) + ',100%,50%)';

lines[j] = new Line(start, end, color, Ease.easeOutBack, Ease.easeOutCubic);

}

}

function update() {

lines.forEach(function(l) {

l.update();

if (l.complete) {

l.time = 0;

l.complete = false;

var temp = l.start;

l.start = l.end;

l.end = temp;

}

});

}

function draw() {

ctx.clearRect(0, 0, viewWidth, viewHeight);

ctx.save();

ctx.translate(256, 256);

ctx.rotate((time % 24) / 24 * Math.PI * 2);

ctx.lineWidth = 8;

ctx.lineCap = 'round';

ctx.lineJoin = 'round';

lines.forEach(function(l) {

l.draw();

});

ctx.restore();

}

function loop() {

update();

draw();

time += timeStep;

requestAnimationFrame(loop);

}

//

// Line and Point

//

function Line(p0, p1, color, ease0, ease1) {

this.start = p0;

this.end = p1;

this.p0 = new Point();

this.p1 = new Point();

this.color = color;

this.ease0 = ease0;

this.ease1 = ease1;

this.duration = 1;

this.complete = false;

}

Line.prototype = {

update:function() {

if (this.time + timeStep <= this.duration) {

this.time += timeStep;

}

else {

this.time = this.duration;

this.complete = true;

}

},

draw:function() {

var t0 = this.ease0(this.time, 0, 1, this.duration);

var t1 = this.ease1(this.time, 0, 1, this.duration);

lerp(this.start, this.end, t0, this.p0);

lerp(this.start, this.end, t1, this.p1);

ctx.strokeStyle = this.color;

ctx.beginPath();

ctx.moveTo(this.p0.x, this.p0.y);

ctx.lineTo(this.p1.x, this.p1.y);

ctx.stroke();

}

};

function Point(x, y) {

this.x = x || 0;

this.y = y || 0;

}

//

// utils

//

function lerp(p0, p1, t, tp) {

tp = tp || new Point();

tp.x = p0.x + t * (p1.x - p0.x);

tp.y = p0.y + t * (p1.y - p0.y);

return tp;

}

function getNext(item, array) {

var i = array.indexOf(item) + 1;

if (i === array.length) i = 0;

return array[i];

}

//

// EASING (functions from jQuery github)

//

Ease = {

easeInQuad: function (t, b, c, d) {

return c*(t/=d)*t + b;

},

easeOutQuad: function (t, b, c, d) {

return -c *(t/=d)*(t-2) + b;

},

easeInOutQuad: function (t, b, c, d) {

if ((t/=d/2) < 1) return c/2*t*t + b;

return -c/2 * ((--t)*(t-2) - 1) + b;

},

easeInCubic: function (t, b, c, d) {

return c*(t/=d)*t*t + b;

},

easeOutCubic: function (t, b, c, d) {

return c*((t=t/d-1)*t*t + 1) + b;

},

easeInOutCubic: function (t, b, c, d) {

if ((t/=d/2) < 1) return c/2*t*t*t + b;

return c/2*((t-=2)*t*t + 2) + b;

},

easeInQuart: function (t, b, c, d) {

return c*(t/=d)*t*t*t + b;

},

easeOutQuart: function (t, b, c, d) {

return -c * ((t=t/d-1)*t*t*t - 1) + b;

},

easeInOutQuart: function (t, b, c, d) {

if ((t/=d/2) < 1) return c/2*t*t*t*t + b;

return -c/2 * ((t-=2)*t*t*t - 2) + b;

},

easeInQuint: function (t, b, c, d) {

return c*(t/=d)*t*t*t*t + b;

},

easeOutQuint: function (t, b, c, d) {

return c*((t=t/d-1)*t*t*t*t + 1) + b;

},

easeInOutQuint: function (t, b, c, d) {

if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;

return c/2*((t-=2)*t*t*t*t + 2) + b;

},

easeInSine: function (t, b, c, d) {

return -c * Math.cos(t/d * (Math.PI/2)) + c + b;

},

easeOutSine: function (t, b, c, d) {

return c * Math.sin(t/d * (Math.PI/2)) + b;

},

easeInOutSine: function (t, b, c, d) {

return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;

},

easeInExpo: function (t, b, c, d) {

return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;

},

easeOutExpo: function (t, b, c, d) {

return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;

},

easeInOutExpo: function (t, b, c, d) {

if (t==0) return b;

if (t==d) return b+c;

if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;

return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;

},

easeInCirc: function (t, b, c, d) {

return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;

},

easeOutCirc: function (t, b, c, d) {

return c * Math.sqrt(1 - (t=t/d-1)*t) + b;

},

easeInOutCirc: function (t, b, c, d) {

if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;

return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;

},

easeInElastic: function (t, b, c, d) {

var s=1.70158;var p=0;var a=c;

if (t==0) return b; if ((t/=d)==1) return b+c; if (!p) p=d*.3;

if (a < Math.abs(c)) { a=c; var s=p/4; }

else var s = p/(2*Math.PI) * Math.asin (c/a);

return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;

},

easeOutElastic: function (t, b, c, d) {

var s=1.70158;var p=0;var a=c;

if (t==0) return b; if ((t/=d)==1) return b+c; if (!p) p=d*.3;

if (a < Math.abs(c)) { a=c; var s=p/4; }

else var s = p/(2*Math.PI) * Math.asin (c/a);

return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;

},

easeInOutElastic: function (t, b, c, d) {

var s=1.70158;var p=0;var a=c;

if (t==0) return b; if ((t/=d/2)==2) return b+c; if (!p) p=d*(.3*1.5);

if (a < Math.abs(c)) { a=c; var s=p/4; }

else var s = p/(2*Math.PI) * Math.asin (c/a);

if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;

return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;

},

easeInBack: function (t, b, c, d, s) {

if (s == undefined) s = 1.70158;

return c*(t/=d)*t*((s+1)*t - s) + b;

},

easeOutBack: function (t, b, c, d, s) {

if (s == undefined) s = 1.70158;

return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;

},

easeInOutBack: function (t, b, c, d, s) {

if (s == undefined) s = 1.70158;

if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;

return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;

},

easeInBounce: function (t, b, c, d) {

return c - Ease.easeOutBounce (d-t, 0, c, d) + b;

},

easeOutBounce: function (t, b, c, d) {

if ((t/=d) < (1/2.75)) {

return c*(7.5625*t*t) + b;

} else if (t < (2/2.75)) {

return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;

} else if (t < (2.5/2.75)) {

return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;

} else {

return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;

}

},

easeInOutBounce: function (t, b, c, d) {

if (t < d/2) return Ease.easeInBounce (t*2, 0, c, d) * .5 + b;

return Ease.easeOutBounce (t*2-d, 0, c, d) * .5 + c*.5 + b;

}

};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
js代码 [removed] ;(function() { 'use strict'; var c = document.getElementById('c'); var ctx = c.getContext('2d'); var w = c.width = window.innerWidth; var h = c.height = window.innerHeight; var cx = w / 2; var cy = h / 2; var fl = 1000; function prj(obj) { var cz = obj.z fl; if(cz === 0) return; var scl = fl / cz; obj.p.x = cx obj.x * scl; obj.p.y = cy obj.y * scl; obj.s = scl; } var P = function(x, y, z) { this.x = x; this.y = y; this.z = z; this.s = 1; this.cl = 0; this.p = { x: 0, y: 0 }; }; P.prototype = { constructor: P, update: function() { this.z -= 30; }, render: function(ctx) { if(this.z <= -fl) return; ctx.save(); ctx.translate(this.p.x, this.p.y); ctx.scale(this.s, this.s); ctx.fillStyle = 'hsla(' this.cl ', 100%, 50%, 0.5)'; ctx.beginPath(); ctx.arc(0, 0, 2, 0, Math.PI * 2); ctx.fill(); ctx.restore(); } }; var M = function(x, y, z) { this.list = []; this.max = 100; this.x = x; this.y = y; this.z = z; this.s = 1; this.p = { x: 0, y: 0 }; this.ax = Math.random() * (Math.PI * 2); this.ay = Math.random() * (Math.PI * 2); this.rx = Math.random() * 100; this.ry = Math.random() * 100; this.cl = Math.random() * 360; this.cls = Math.random(); }; M.prototype = { constructor: M, update: function() { this.cl = this.cls; this.ax = Math.random() * 0.1 - 0.02; this.ay = Math.random() * 0.1 - 0.02; this.x = Math.cos(this.ax) * 100; this.y = Math.sin(this.ay) * 100; this.z = 10; if(this.z > fl) this.z = fl; if(this.list.length < this.max) { if(Math.random() * 100 < 50) { var pp = new P(this.x, this.y, this.z); pp.cl = this.cl; this.list.push(pp); } } else { var pp = this.list.shift(); pp.x = this.x; pp.y = this.y; pp.z = this.z; pp.cl = this.cl; this.list.push(pp); } }, render: function(ctx) { if(this.z <= -fl) return; ctx.save(); ctx.translate(this.p.x, this.p.y); ctx.fillStyle = 'green'; ctx.beginPath(); ctx.arc(0, 0, 2, 0, Math.PI * 2); ctx.fill(); ctx.restore(); } }; function update(mv, list) { for(var i = 0; i < list.length; i ) { var p = list[i]; p.update(); prj(p); p.render(ctx); } for(var i = list.length-1; i >= 0; i--) { var p = list[i]; if(p.z <= -fl) continue; if(i === list.length - 1) { ctx.lineWidth = Math.random(); ctx.strokeStyle = 'hsl(' mv.cl ', 100%, 50%)'; ctx.beginPath(); ctx.moveTo(p.p.x, p.p.y); } else { ctx.lineTo(p.p.x, p.p.y); } } ctx.stroke(); } var ms = []; for(var i = 0; i < 10; i ) { ms.push(new M( Math.random() * 400 - 200, Math.random() * 400 - 200, Math.random() * 400 - 200)); } requestAnimationFrame(function loop() { requestAnimationFrame(loop); ctx.clearRect(0, 0, w, h); for(var i = 0; i < ms.length; i ) { var m = ms[i]; m.update(); prj(m); update(m, m.list); } }); })(); [removed] 这是一款基于HTML5 Canvas绘制的炫酷3D线条延伸动画特效,多彩颜色变幻,非常漂亮!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值