面条html5,利用HTML5 Canvas实现一碗面条特效
特效描述:利用HTML5 Canvas实现一碗面条特效。利用HTML5 Canvas实现一碗面条特效
代码结构
1. HTML代码
// Initiate Canvas
let can = document.getElementById('can'),
ctx = can.getContext('2d'),
cRad = 250;
can.width = cRad * 2;
can.height = cRad * 2;
ctx.translate(cRad, cRad);
ctx.lineCap = 'round';
ctx.lineJoin = 'round';
// Mouse listeners
let mouse = {
x: -cRad,
y: -cRad
};
can.onmousemove = (e) => {
mouse.x = e.clientX - cRad;
mouse.y = e.clientY - cRad;
};
// World variables
let noodles = [];
const bowlRad = cRad - 50,
mouseRad = 20;
// Object: Node - single Noodle joint
function Node(initX, initY, rad) {
this.p = {x: initX, y: initY};
this.v = {x: 0, y: 0};
this.f = {x: 0, y: 0};
this.r = rad;
this.friction = 0.15;
}
Node.prototype.applyForce = function(dx, dy) {
this.f.x += dx;
this.f.y += dy;
}
Node.prototype.step = function() {
// Stay in Bowl
let centerDis = Math.sqrt((this.p.x * this.p.x) + (this.p.y * this.p.y));
if (centerDis > bowlRad) {
let ang = Math.atan2(this.p.y, this.p.x);
this.applyForce(
(bowlRad - centerDis) * Math.cos(ang),
(bowlRad - centerDis) * Math.sin(ang)
);
}
// Mouse interaction
let mouseDis = Math.sqrt(Math.pow(mouse.x - this.p.x, 2) + Math.pow(mouse.y - this.p.y, 2));
if (mouseDis < this.r + mouseRad) {
let ang = Math.atan2(this.p.y - mouse.y, this.p.x - mouse.x);
this.applyForce(
Math.sqrt(this.r + mouseRad / mouseDis) * Math.cos(ang),
Math.sqrt(this.r + mouseRad / mouseDis) * Math.sin(ang)
);
}
// Apply Movement
this.applyForce(this.v.x * -this.friction, this.v.y * -this.friction);
this.v.x += this.f.x;
this.v.y += this.f.y;
this.p.x += this.v.x;
this.p.y += this.v.y;
this.f = {x: 0, y: 0};
}
// Object: Noodle - a string of connected nodes
function Noodle(initX, initY, length, thickness) {
this.nodes = [];
this.elastic = 0.4;
this.thickness = thickness;
// Random colour
let lightness = (50 + Math.round(Math.random() * 40));
this.color = 'hsl(48, 93%, ' + lightness + '%)';
this.colorOutline = 'hsl(48, 93%, ' + (lightness - 20) + '%)';
// Initiate nodes, slightly out of line
let nodeNum = length / thickness,
offsetY = 0;
for(let i = 0; i < nodeNum; i++) {
this.nodes.push(new Node(
initX + (i * thickness),
initY + offsetY,
thickness / 2
));
offsetY += (Math.random() * thickness * 2) - thickness;
}
noodles.push(this);
}
// Draw noodle as curve connecting nodes
Noodle.prototype.draw = function() {
ctx.beginPath();
ctx.moveTo(this.nodes[0].p.x, this.nodes[0].p.y);
let n = 1;
for (; n < this.nodes.length - 2; n++) {
let xc = (this.nodes[n].p.x + this.nodes[n + 1].p.x) / 2,
yc = (this.nodes[n].p.y + this.nodes[n + 1].p.y) / 2;
ctx.quadraticCurveTo(this.nodes[n].p.x, this.nodes[n].p.y, xc, yc);
}
ctx.quadraticCurveTo(
this.nodes[n].p.x,
this.nodes[n].p.y,
this.nodes[n + 1].p.x,
this.nodes[n + 1].p.y
);
ctx.strokeStyle = this.colorOutline;
ctx.lineWidth = this.thickness + 2;
ctx.stroke();
ctx.strokeStyle = this.color;
ctx.lineWidth = this.thickness;
ctx.stroke();
}
// Apply restoration force to all nodes to keep them in line
Noodle.prototype.step = function() {
for(let i = 0; i < this.nodes.length; i++) {
let n = this.nodes[i];
if (i > 0) {
// Find closest distance between previous node
let nPrev = this.nodes[i - 1],
ang = Math.atan2(nPrev.p.y - n.p.y, nPrev.p.x - n.p.x),
nearN = {
x: n.p.x + (Math.cos(ang) * n.r),
y: n.p.y + (Math.sin(ang) * n.r)
},
nearNp = {
x: nPrev.p.x + (Math.cos(ang + Math.PI) * n.r),
y: nPrev.p.y + (Math.sin(ang + Math.PI) * n.r)
};
n.applyForce(
(nearNp.x - nearN.x) * this.elastic,
(nearNp.y - nearN.y) * this.elastic
);
nPrev.applyForce(
(nearN.x - nearNp.x) * this.elastic,
(nearN.y - nearNp.y) * this.elastic
);
}
n.step();
}
}
// World loop
function step() {
ctx.clearRect(-cRad, -cRad, cRad * 2, cRad * 2);
noodles.forEach(n => n.step());
// Draw Bowl
ctx.beginPath();
ctx.arc(0, 0, bowlRad + 25, 0, Math.PI * 2);
ctx.fillStyle = '#e2d3ad';
ctx.fill();
ctx.strokeStyle = '#333';
ctx.lineWidth = 32;
ctx.stroke();
ctx.strokeStyle = '#ddd';
ctx.lineWidth = 30;
ctx.stroke();
// Draw Noodles
noodles.forEach(n => n.draw());
window.requestAnimationFrame(step);
}
// Initiate Noodles
for(let i = 0; i < 150; i++) {
let initX = Math.round(-bowlRad + (Math.random() * bowlRad)),
initY = Math.round(-bowlRad + (Math.random() * bowlRad * 2)),
length = 100 + (Math.random() * 250),
thick = Math.round(10 + (Math.random() * 15));
new Noodle(initX, initY, length, thick);
}
// Initiate World
step();
面条html5,利用HTML5 Canvas实现一碗面条特效相关推荐
- bubble html5,利用HTML5 Canvas创建交互式Bubble Chart
Josh Marinacci也在其博客中详细地介绍了"如何利用HTML5 Canvas创建可以在移动桌面上运行的交互式图表以及如何利用真实数据填充图表."下面让我们跟随作者学习如何 ...
- 全景效果图html5,利用html5实现的360度全景图浏览(带天地)
[实例简介]利用html5实现的360度全景图浏览(带天地) [实例截图] [核心代码] var camera, scene, renderer; var texture_placeholder, i ...
- 网页游戏制作html5,利用HTML5 Canvas制作一个简单的打飞机游戏
之前在当耐特的DEMO里看到个打飞机的游戏,然后就把他的图片和音频扒了了下来....自己凭着玩的心情重新写了一个.仅供娱乐哈......我没有用框架,所有js都是自己写的......所以就可以来当个简 ...
- 刮刮乐html5效果擦除,利用HTML5的画布Canvas实现刮刮卡效果
先给大家展示效果: 你玩过刮刮卡么?一不小心可以中奖的那种.今天我给大家分享一个基于HTML5技术实现的刮刮卡效果,在PC上只需按住鼠标,在手机上你只需按住指头,轻轻刮去图层就可以模拟真实的刮奖效果. ...
- 在html利用canvas蚂蚁,html5 利用canvas实现简单的人物走动
最近在学习html5,其中涉及到很关键的元素canvas-画布,在网上下载了一些游戏源代码,虽然能看懂,但是想单独地针对某个功能提取出来还是有难处的,于是乎自己又上网查找了一些例子,才将超级玛丽简单的 ...
- html5 实现波浪效果图,利用HTML5实现Canvas流动的波浪特效
特效描述:利用HTML5实现 Canvas 流动的 波浪特效.利用HTML5实现Canvas流动的波浪特效 代码结构 1. 引入JS 2. HTML代码 'use strict'; var gui = ...
- php代码输出笑脸,利用HTML5中的Canvas绘制笑脸的代码
这篇文章主要介绍了利用HTML5中的Canvas绘制一张笑脸的教程,使用Canvas进行绘图是HTML5中的基本功能,需要的朋友可以参考下 今天,你将学习一项称为Canvas(画布)的web技术,以及 ...
- html打飞机游戏代码,利用HTML5 Canvas实现打飞机游戏
这篇文章主要介绍了利用HTML5 Canvas制作一个简单的打飞机游戏,作者也给出了相关的Javascript代码,需要的朋友可以参考下 之前在当耐特的DEMO里看到个打飞机的游戏,然后就把他的图片和 ...
- html 5抽奖特效,利用HTML5实现Canvas大转盘抽奖特效
特效描述:利用HTML5实现 Canvas 大转盘 抽奖特效.利用HTML5实现Canvas大转盘抽奖特效 代码结构 1. 引入JS 2. HTML代码 当前浏览器版本过低,请使用其他浏览器尝试 va ...
最新文章
- 鱼和熊掌兼得:同时使用 JPA 和 Mybatis
- 面试两个月,我吐了(软件测试岗面试经验)
- 为什么大多Virtual Globe程序纵向旋转效率比较低
- 每天一道LeetCode-----实现二叉搜索树的迭代器
- Linux系统下怎样配置多个Tomcat
- 计算机模拟分子设计,计算机模拟分子材料.pdf
- mysql加索引快很多
- Ubuntu16.04 开启多个终端,一个终端多个小窗口
- 志邦橱柜坑爹,志邦橱柜大忽悠,志邦橱柜欺骗
- android开发案例1---拦截电话,拯救史迪仔,有序广播
- Python 贪吃蛇小游戏
- 论文中常见的argmin,argmax是什么意思?
- 百度分享如何自定义分享url和内容?
- wx.chooseImage上传本地PC端和移动端区别
- 【Copy攻城狮日志】飞浆学院强化学习7日打卡营-学习笔记
- mysql查询除某一列外的其他列
- Python数据挖掘—电力窃漏电用户自动识别
- matlab ccd采集,CCD数据采集.doc
- wms仓储管理绩效评价指标的制定
- 揭秘橙子钱包拥有40万+用户的背后
热门文章
- 晶体基片提供 Ta2NiSe5晶体/CuInP2S6晶体/Cr2Ge2Te6晶体/MnPS3晶体/CrI3碘化铬晶体
- 如何写好SCI文章(关于NLP论文)
- 敏捷就是快速交付吗?
- 魔百盒CM211-2,长虹代工,MV310芯,强刷固件
- BUUCTF Web [BSidesCF 2019]Kookie1 [BSidesCF 2019]Futurella1
- C语言消灭星星算法,消灭星星算法思路内容 算法过程解析
- 1.1.14 Electron 监听网络状态
- 解决:DreamScene 切换背景时 Shell 意外崩溃
- 解释this指针指向与bind()方法:用bind方法永久绑定this的指向
- N个开源的管理系统,接私活必备