html贝塞尔曲线在线,HTML5/Canvas 贝塞尔曲线轨迹动画

JavaScript

语言:

JaveScriptBabelCoffeeScript

确定

(function() {

var lastTime = 0;

var vendors = ['ms', 'moz', 'webkit', 'o'];

for (var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {

window.requestAnimationFrame = window[vendors[x] + 'RequestAnimationFrame'];

window.cancelAnimationFrame = window[vendors[x] + 'CancelAnimationFrame'] || window[vendors[x] + 'CancelRequestAnimationFrame'];

}

if (!window.requestAnimationFrame)

window.requestAnimationFrame = function(callback, element) {

var currTime = new Date().getTime();

var timeToCall = Math.max(0, 16 - (currTime - lastTime));

var id = window.setTimeout(function() {

callback(currTime + timeToCall);

},

timeToCall);

lastTime = currTime + timeToCall;

return id;

};

if (!window.cancelAnimationFrame)

window.cancelAnimationFrame = function(id) {

clearTimeout(id);

};

}());

// ends requestAnimationFrame polyfill

var c = document.getElementById("c");

var ctx = c.getContext("2d");

var cw = c.width = window.innerWidth;

var ch = c.height = window.innerHeight;

ctx.globalAlpha = .1;

ctx.lineWidth = 2;

var kappa = 0;

var T = 0;

var bezierRy = [{

color: "Tomato",

r: 550,

x0: -150,

y0: -150,

x1: cw / 4,

y1: ch,

x2: cw / 2,

y2: -ch / 2,

x3: cw,

y3: ch / 2

}, {

color: "Chartreuse",

r: 300,

x0: -300,

y0: ch - 250,

x1: 4 * cw / 5,

y1: -2 * ch / 3,

x2: cw / 2,

y2: 2 * ch / 3,

x3: cw,

y3: ch / 2

},

{

color: "gold",

r: 200,

x0: -200,

y0: 450,

x1: cw / 4,

y1: -2 * ch / 3,

x2: cw / 2,

y2: 2 * ch / 3,

x3: cw,

y3: ch / 2

}, {

color: "LightSkyBlue",

r: 150,

x0: -100,

y0: 250,

x1: cw / 2,

y1: 2 * ch / 3,

x2: cw / 4,

y2: -2 * ch / 3,

x3: cw,

y3: ch / 2

}

]

var increment = .4 / bezierRy.length;

function shamrock(w, h, x, y) {

kappa = -1.25;

var ox = (w / 2) * kappa; // desplasamiento horizontal (offset x);

var oy = (h / 2) * kappa; // desplazamiento vertical (offset y);

var xf = x + w; // x final

var yf = y + h; // y final

var xm = x + w / 2; // x medio

var ym = y + h / 2; // y medio

ctx.beginPath();

ctx.moveTo(x, ym);

ctx.bezierCurveTo(x, ym - oy, xm - ox, y, xm, y);

ctx.bezierCurveTo(xm + ox, y, xf, ym - oy, xf, ym);

ctx.bezierCurveTo(xf, ym + oy, xm + ox, yf, xm, yf);

ctx.bezierCurveTo(xm - ox, yf, x, ym + oy, x, ym);

ctx.closePath();

ctx.stroke();

console.log("sham")

}

function DrawShamrock(i) {

if (T > 100) {

T = 0;

} else {

T += increment;

};

var w = bezierRy[i].r;

var h = bezierRy[i].r;

var x0 = -bezierRy[i].r;

var y0 = bezierRy[i].y0;

var x1 = bezierRy[i].x1;

var y1 = bezierRy[i].y1;

var x2 = bezierRy[i].x2;

var y2 = bezierRy[i].y2;

var x3 = bezierRy[i].x3;

var y3 = bezierRy[i].y3;

//ctx.strokeStyle = "white";

ctx.strokeStyle = bezierRy[i].color;

ctx.lineWidth = 1; //bezierRy[i].lineWidth;

//point on a Bézier curve

var t = T / 100; // t toma valores entre 0 - 1

var x = (1 - t) * (1 - t) * (1 - t) * x0 + 3 * (1 - t) * (1 - t) * t * x1 + 3 * (1 - t) * t * t * x2 + t * t * t * x3;

var y = (1 - t) * (1 - t) * (1 - t) * y0 + 3 * (1 - t) * (1 - t) * t * y1 + 3 * (1 - t) * t * t * y2 + t * t * t * y3;

shamrock(w, h, x, y);

//ctx.globalCompositeOperation="xor";

}

function Draw() {

for (var i = 0; i < bezierRy.length; i++) {

DrawShamrock(i);

}

requestId = window.requestAnimationFrame(Draw);

}

function start() {

console.log("start")

requestId = window.requestAnimationFrame(Draw);

stopped = false;

}

function stopAnim() {

if (requestId) {

window.cancelAnimationFrame(requestId);

}

stopped = true;

}

window.addEventListener("load", start, false);

window.setInterval(stopAnim, cw * 10);

c.addEventListener("click", function() {

(stopped == true) ? start(): stopAnim();

}, false);

//window.onresize = function(){ location.reload(); }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值