摘要:个人博客封装方法,增加点击,移动效果,配置颜色线条粗细功能在线查看传到了,以后也许用得着,地址先上效果点击效果默认生成随机大小的个小点点鼠标移动,修改鼠标小点点的轴坐标鼠标点击,增加随机大小小点点增加离屏优化性能修复鼠标移出,去除鼠标效果的
</>复制代码
个人博客 http://taoquns.com/paper/59ba...
canvas 封装方法,增加点击,移动效果,配置颜色线条粗细功能
在线查看 http://jsrun.net/3PkKp/edit
传到github了,以后也许用得着,地址: https://github.com/Taoqun/can...
先上效果:
点击效果:
</>复制代码
默认生成随机大小的100个小点点
鼠标移动,修改鼠标小点点的xy轴坐标
鼠标点击,增加随机大小小点点
增加离屏canvas 优化性能
修复鼠标移出,去除鼠标效果的小点点
增加暂停开始功能
index.html
</>复制代码
index.js
</>复制代码
var root = document.querySelector("#root")
var a = new CanvasAnimate(root,{length:80,clicked:true,moveon:true})
a.Run() // 开始运行
a.pause() // 暂停
CanvasAnimate.js
</>复制代码
说明,配置参数
length Number 生成的小点点数量
RoundColor String 小点点颜色
LineColor String 线条颜色
LineWeight Number 线条宽度
clicked Boolean 鼠标点击是否增加连接小点点
moveon Boolean 鼠标移动是否增加连接效果
</>复制代码
方法
Run 开始运行
pause 暂停动画 或者 开始动画 切换
</>复制代码
function CanvasAnimate(Dom,options){
options = options || {}
this.Element = Dom
this.cvs = Dom.getContext("2d")
this.off_cvs = document.createElement("canvas")
this.off_cvs.width = Dom.width
this.off_cvs.height = Dom.height
this.Dom = this.off_cvs.getContext("2d")
this.width = Dom.width
this.height = Dom.height
this.length = options.length || 100
this.RoundColor = options.RoundColor || "#999"
this.RoundDiameter = options.RoundDiameter || 2
this.LineColor = options.LineColor || "#ccc"
this.LineWeight = options.LineWeight || 1
this.clicked = options.clicked || false
this.moveon = options.moveon || false
this.list = []
this.paused = true
}
CanvasAnimate.prototype.Run = function(){
if( this.clicked ){
this.Element.addEventListener( "click",this.Clicked.bind(this) )
}
if( this.moveon ){
this.Element.addEventListener( "mousemove",this.moveXY.bind(this) )
this.Element.addEventListener( "mouseout",this.moveoutXY.bind(this) )
}
this.Draw( this.getLength() )
}
CanvasAnimate.prototype.getLength=function(){
let arr = []
for(let i=0;i< this.length ;i++){
let obj = {}
obj.x = parseInt( Math.random() * this.width )
obj.y = parseInt( Math.random() * this.height )
obj.r = parseInt( Math.random()*10 )
obj.controlX = parseInt( Math.random()*10 ) > 5 ? "left":"right"
obj.controlY = parseInt( Math.random()*10 ) > 5 ? "bottom":"top"
arr.push(obj)
}
return arr
}
CanvasAnimate.prototype.Draw = function(list){
let new_arr = []
let line_arr = []
list.map((item,index)=>{
let xy = this.ControlXY(item)
let obj = this.ControlRound(xy)
new_arr.push( obj )
});
new_arr.map((item1,index1)=>{
new_arr.map((item2,index2)=>{
if(item1 !== item2){
let x = item1.x - item2.x
let y = item1.y - item2.y
if( Math.abs(x)< 100 && Math.abs(y)<100 ){
let obj = {
x1:item1.x,
y1:item1.y,
x2:item2.x,
y2:item2.y,
}
line_arr.push(obj)
}
}
})
})
this.drawLine(line_arr)
new_arr.map((item)=>{
this.drawRound(item)
})
this.list = new_arr
this.cvs.drawImage(this.off_cvs,0,0,this.width,this.height)
setTimeout(()=>{
if(this.paused){
this.next()
}
},50)
}
CanvasAnimate.prototype.next = function(){
this.cvs.clearRect( 0,0,this.width,this.height )
this.Dom.clearRect( 0,0,this.width,this.height )
this.Draw( this.list )
}
CanvasAnimate.prototype.drawRound = function(obj){
let {x,y,r} = obj
this.Dom.beginPath()
this.Dom.arc( x,y,r, 0, 2*Math.PI )
this.Dom.fillStyle = this.RoundColor
this.Dom.fill()
this.Dom.closePath()
}
CanvasAnimate.prototype.drawLine = function(list){
list.map( (item)=>{
this.Dom.beginPath()
this.Dom.moveTo( item.x1,item.y1 )
this.Dom.lineTo( item.x2,item.y2 )
this.Dom.lineWidth = this.LineWeight
this.Dom.strokeStyle = this.LineColor
this.Dom.stroke()
this.Dom.closePath()
})
}
CanvasAnimate.prototype.ControlXY = function(obj){
if(obj.x >= (this.width - obj.r) ){
obj.controlX = "left"
}else if( obj.x <= parseInt(obj.r/2) ){
obj.controlX = "right"
}
if( obj.y >= (this.height - obj.r) ){
obj.controlY = "bottom"
}else if( obj.y <= parseInt(obj.r/2) ){
obj.controlY = "top"
}
return obj
}
CanvasAnimate.prototype.ControlRound = function(obj){
switch(obj.controlX){
case "right":
obj.x++;
break;
case "left":
obj.x--;
break;
}
switch(obj.controlY){
case "top":
obj.y++;
break;
case "bottom":
obj.y--;
break;
}
return obj
}
CanvasAnimate.prototype.Clicked = function(event){
let obj = {}
obj.x = event.clientX
obj.y = event.clientY
obj.r = parseInt( Math.random()*10 )
obj.controlX = parseInt( Math.random()*10 ) > 5 ? "left" :"right"
obj.controlY = parseInt( Math.random()*10 ) > 5 ? "bottom" :"top"
this.list.push(obj)
}
CanvasAnimate.prototype.moveXY = function(event){
let obj = {}
obj.x = event.clientX
obj.y = event.clientY
obj.r = 0
obj.move = true
if( this.list[0]["move"] ){
this.list[0]["x"] = obj.x
this.list[0]["y"] = obj.y
this.list[0]["r"] = 1
}else{
this.list.unshift(obj)
}
}
CanvasAnimate.prototype.moveoutXY = function(event){
this.list.shift()
}
CanvasAnimate.prototype.pause = function(){
this.paused = !this.paused
if( this.paused){
this.Draw(this.list)
}
}
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/82454.html
摘要:可以绘制动态效果,除了常用的规则动画之外,还可以采用粒子的概念来实现较复杂的动效,本文分别采用普通动效与粒子特效实现了一个简单的时钟。普通时钟普通动效即利用的,实现有规则的图案动画。表示保存当前环境的状态,在此基础上进行绘制。 canvas用于在网页上绘制图像、动画,可以将其理解为画布,在这个画布上构建想要的效果。 canvas可以绘制动态效果,除了常用的规则动画之外,还可以采用粒子的...
摘要:前言之前有发过这个项目的文章了,根据朋友的建议改变了博客的样式,也增加了一些新功能,下面完整地介绍这个博客项目。 前言 之前有发过这个项目的文章了,根据朋友的建议改变了博客的样式,也增加了一些新功能,下面完整地介绍这个博客项目。文末附前端实习求职简历 项目简介 简要介绍:一个前后端分离的项目主要技术栈:vue全家桶 + node.js + Express + Mongodbgithub...
摘要:前言之前有发过这个项目的文章了,根据朋友的建议改变了博客的样式,也增加了一些新功能,下面完整地介绍这个博客项目。 前言 之前有发过这个项目的文章了,根据朋友的建议改变了博客的样式,也增加了一些新功能,下面完整地介绍这个博客项目。文末附前端实习求职简历 项目简介 简要介绍:一个前后端分离的项目主要技术栈:vue全家桶 + node.js + Express + Mongodbgithub...
摘要:点都构建完毕了,就要构建点与点之间的连线了,我们用到双重遍历,把两个点捆绑成一组,放到数组中。最后加入鼠标移动事件,启动定时器大功告成 废话不说先上图:showImg(https://segmentfault.com/img/bVOH83?w=1312&h=586); 关于这个效果我第一次见是在https://www.mengxiaozhu.cn/后来知乎登录页也开始用了https:/...
摘要:点都构建完毕了,就要构建点与点之间的连线了,我们用到双重遍历,把两个点捆绑成一组,放到数组中。最后加入鼠标移动事件,启动定时器大功告成 废话不说先上图:showImg(https://segmentfault.com/img/bVOH83?w=1312&h=586); 关于这个效果我第一次见是在https://www.mengxiaozhu.cn/后来知乎登录页也开始用了https:/...
阅读 1950·2021-11-11 16:55
阅读 829·2019-08-30 15:53
阅读 3679·2019-08-30 15:45
阅读 808·2019-08-30 14:10
阅读 3367·2019-08-30 12:46
阅读 2196·2019-08-29 13:15
阅读 2110·2019-08-26 13:48
阅读 1000·2019-08-26 12:23