查看Tween算法的曲线
最近在学习javascript动画的实现,自己写的时候用setInterval()来做,结果发现这个函数和setTimeOut一样,延时不准确,而且还要计算每次移动的距离,每次移动以后的偏差等一堆参数,够麻烦的。后来在网上发现了根据时间计算偏移量的方法,茅塞顿开啊。果然闭门造车造不出好车。
然后就知道了Tween算法,本来想用Tween算法实现动画,结果看了半天Tween算法的资料。发现有人将Tween算法的曲线画出来,觉得很好玩。模仿的做了一个。修正了被参考者那里画曲线时有一些断开的现象。参考地址在文章最后。
TWeen类型:
Linear Quad Cubic Quart Quint Sine Expo Circ Elastic Back Bounce
Linear Quad Cubic Quart Quint Sine Expo Circ Elastic Back Bounce
ease类型:
easeIn easeOut easeInOut
easeIn easeOut easeInOut
这个是画线的代码,Tween算法网上有很多,找不到的话可以右键->查看源代码:
function $g(_canvas,fn){
var canvas = (typeof _canvas === "string")?document.getElementById(_canvas):_canvas;
var cache = [];
var width = canvas.clientWidth;
var height = canvas.clientHeight;
var _this = {};
var color = "red";
_this.setColor = function(_color){
if(_color){color = _color;}
else {return color;}
}
_this.Draw = function(){
var y = 0,_y = 0;
for(var x = 0; x <= width; x++){
y = parseInt(fn(x,0,height,width));
//补齐y轴坐标上的点
if(Math.abs(y - _y) > 1){
var flag = ((y - _y) > 0);
flag?_y++:_y--;
while(_y != y){
createPoint(x,height - _y);
flag?_y++:_y--;
}
}
createPoint(x,height - _y);
_y = y;
}
paint();
return _this;
}
function createPoint(x,y){
cache.push("<div style='width:1px; height:1px; font-size:1px;border-width:0px;margin:0px; padding:0px; background-color:"+color+"; position:absolute;left:" + x + "px; top:" + y + "px'></div>");
}
function paint(){
canvas.innerHTML = cache.join("");
cache = [];
}
return _this;
}
var canvas = (typeof _canvas === "string")?document.getElementById(_canvas):_canvas;
var cache = [];
var width = canvas.clientWidth;
var height = canvas.clientHeight;
var _this = {};
var color = "red";
_this.setColor = function(_color){
if(_color){color = _color;}
else {return color;}
}
_this.Draw = function(){
var y = 0,_y = 0;
for(var x = 0; x <= width; x++){
y = parseInt(fn(x,0,height,width));
//补齐y轴坐标上的点
if(Math.abs(y - _y) > 1){
var flag = ((y - _y) > 0);
flag?_y++:_y--;
while(_y != y){
createPoint(x,height - _y);
flag?_y++:_y--;
}
}
createPoint(x,height - _y);
_y = y;
}
paint();
return _this;
}
function createPoint(x,y){
cache.push("<div style='width:1px; height:1px; font-size:1px;border-width:0px;margin:0px; padding:0px; background-color:"+color+"; position:absolute;left:" + x + "px; top:" + y + "px'></div>");
}
function paint(){
canvas.innerHTML = cache.join("");
cache = [];
}
return _this;
}
参考地址:
http://greatghoul.javaeye.com/blog/612232
http://www.cnblogs.com/cloudgamer/archive/2009/01/06/Tween.html
http://www.cnblogs.com/idche/archive/2010/06/13/1758006.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构