html5canvas效果跳一跳小游戏,HTML5 Canvas在水面波浪中起伏跳跃的文本动画效果

JavaScript

语言:

JaveScriptBabelCoffeeScript

确定

const canvas = document.querySelector("canvas");

const context = canvas.getContext("2d");

const text = "Hello World";

const multiplier = 1000;

const amplitude = 30;

function frame(t) {

const colors = [

"#f77194",

"#ebf771",

"#90f771",

"#71f7eb",

"#7187f7",

"#f7187f",

"#e071f7"

];

const m = 0.005;

context.clearRect(0,0,canvas.width,canvas.height);

context.save();

context.translate(canvas.width >> 1, canvas.height >> 1);

context.font = "72px monospace";

context.textAlign = "center";

context.textBaseline = "middle";

const { width } = context.measureText(text);

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

const character = text.charAt(i);

const { width: x } = context.measureText(text.substr(0,i));

const { width: cw } = context.measureText(character);

const p = t + (i * multiplier);

const y = Math.sin(p * m) * amplitude;

context.save();

context.translate(x - (width >> 1) + (cw >> 1),y);

context.rotate(Math.cos(p * m) * 0.5);

//context.fillText(character, -x, 0); // I LOVE THIS

context.fillStyle = colors[i % colors.length];

context.fillText(character, 0, 0); // I LOVE THIS

context.restore();

}

context.beginPath();

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

const p = t + (i * multiplier);

const pn = t + ((i + 1) * multiplier);

const y = Math.sin(p * m) * amplitude;

const yn = Math.sin(pn * m) * amplitude;

const character = text.charAt(i);

const { width: x } = context.measureText(text.substr(0,i));

const { width: cw } = context.measureText(character);

if (i === 0) {

context.moveTo(

x - (width >> 1) + (cw >> 1),

y + 64

);

} else {

context.quadraticCurveTo(

x - (width >> 1) + (cw >> 1),

y + 64,

x - (width >> 1) + cw,

y + ((yn - y) * 0.5) + 64

);

}

}

context.lineWidth = 5;

context.strokeStyle = "#71f7eb";

context.stroke();

context.beginPath();

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

const p = t + (i * multiplier);

const pn = t + ((i + 1) * multiplier);

const y = Math.sin(p * m) * amplitude;

const yn = Math.sin(pn * m) * amplitude;

const character = text.charAt(i);

const { width: x } = context.measureText(text.substr(0,i));

const { width: cw } = context.measureText(character);

if (i === 0) {

context.moveTo(

x - (width >> 1) + (cw >> 1),

y + 80

);

} else {

context.quadraticCurveTo(

x - (width >> 1) + (cw >> 1),

y + 80,

x - (width >> 1) + cw,

y + ((yn - y) * 0.5) + 80

);

}

}

context.lineWidth = 5;

context.strokeStyle = "#71f7eb";

context.stroke();

context.beginPath();

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

const p = t + (i * multiplier);

const pn = t + ((i + 1) * multiplier);

const y = Math.sin(p * m) * amplitude;

const yn = Math.sin(pn * m) * amplitude;

const character = text.charAt(i);

const { width: x } = context.measureText(text.substr(0,i));

const { width: cw } = context.measureText(character);

if (i === 0) {

context.moveTo(

x - (width >> 1) + (cw >> 1),

y + 96

);

} else {

context.quadraticCurveTo(

x - (width >> 1) + (cw >> 1),

y + 96,

x - (width >> 1) + cw,

y + ((yn - y) * 0.5) + 96

);

}

}

context.lineWidth = 5;

context.strokeStyle = "#71f7eb";

context.stroke();

context.restore();

requestAnimationFrame(frame);

}

function resize() {

canvas.width = canvas.offsetWidth;

canvas.height = canvas.offsetHeight;

}

function start() {

addEventListener("resize", resize);

dispatchEvent(new Event("resize"));

requestAnimationFrame(frame);

}

start();

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值