• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com专业计算机教程网站
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • html/xhtml
  • html5
  • CSS
  • XML/XSLT
  • Dreamweaver教程
  • Frontpage教程
  • 心得技巧
  • bootstrap
  • vue
  • AngularJS
  • HBuilder教程
  • css3
  • 浏览器兼容
  • div/css
  • 网页编辑器
  • axure
您的位置:首页 > 网页设计 >html5 > JCanvas库 开发接口

JCanvas库 开发接口

作者:匿名 字体:[增加 减小] 来源:互联网 时间:2018-12-03

本文主要包含JCanvas, 开发接口 等相关知识,匿名希望在学习及工作中可以帮助到您
最新动态:/content/10088155.html
把以下内容复制到文本文档中,另存为JCanvas.js

/**
 * @author 智圆行方
 */

/**
 * DisplayObject 类
 * 可视对象(抽象)
 * 所有的可视对象均继承于此类
 * 定义了所有可视对象最基本得五项属性
 */
function DisplayObject() {
	this.x=0;//横坐标
	this.y=0;//纵坐标
	this.width=0;//宽度
	this.height=0;//高度
	this.stage=null;//对应的舞台对象(舞台对象对应的舞台对象为null)
};

/**
 * InteractiveObject 类
 * 交互对象(抽象)
 * 所有用来与用户交互的对象均继承于此类
 * 定义了相关的事件操作方法、属性
 */
function InteractiveObject() {
	DisplayObject.call(this);//继承
	this.eventListner=new Object();//事件侦听器列表
};

InteractiveObject.prototype.addEventListner= function(type,func) {//添加事件侦听器(同一类型的事件可有多个侦听器)
	if(this.eventListner[type]==null || this.eventListner[type]==undefined) {//如果为空或未定义
		this.eventListner[type]=new Array();//定义为数组对象
	}
	this.eventListner[type].push(func);//添加一个事件侦听器到该类型
};
InteractiveObject.prototype.removeEventListner= function(type,func) {//移除事件侦听器(同一类型事件的某一侦听器)
	if(this.eventListner[type]==null || this.eventListner[type]==undefined) {//如果本来就没有
		return;//返回
	}
	for (var i=0; i < this.eventListner[type].length; i++) {//有,就循环
		if(this.eventListner[type][i]==func) {//判断一下,哪一个是指定的侦听器函数
			delete this.eventListner[type][i];//删除它
			this.eventListner[type].splice(i,1);//在数组中移除他
		}
	};
	if(this.eventListner[type].length==0) {//如果该类型已经没有侦听器
		delete this.eventListner[type];//清除该类型的侦听器对象
	}
};
InteractiveObject.prototype.removeAllEventListner= function(type) {//移除某类型事件的所有侦听器
	if(this.eventListner[type]==null || this.eventListner[type]==undefined) {//本来为空,不理他
		return;
	}
	this.eventListner[type].splice();//移除
	delete this.eventListner[type];//删除
};
InteractiveObject.prototype.hasEventListner= function(type) {//是否有事件侦听器
	return (this.eventListner[type]!=null && this.eventListner[type]!=undefined && this.eventListner[type].length>0);
};

/**
 * DisplayObjectContainer 类
 * 可视对象容器(具体)
 * 所有可以拥有子可视对象的对象均继承于此类
 * 定义了操作子可视对象的属性、方法
 */
function DisplayObjectContainer(ctx) {
	InteractiveObject.call(this);//继承
	this.ctx=ctx;//具体类将涉及到canvas的上下文context,需要提供该参数并记录到属性中
	this.childs=new Array();//子对象数组
	this.maxWidth=0;//根据子对象宽高记录最大宽高
	this.maxHeight=0;
	this.moveChild=new Array();//当前鼠标悬停子成员数组(由于对象重叠关系,悬停子成员并非一个,所以用数组)。此属性在处理事件时起作用。
};

DisplayObjectContainer.prototype=new InteractiveObject();//继承交互对象的原型
DisplayObjectContainer.prototype.getContext= function() {//取上下文,完全可以直接操作属性
	return this.ctx;
};
DisplayObjectContainer.prototype.addChild= function(child) {//添加子对象
	if(this.maxWidth<child.x+child.width) {
		this.maxWidth=child.x+child.width;//自动处理最大宽高
	}
	if(this.maxHeight<child.y+child.height) {
		this.maxHegiht=child.y+child.height;
	}
	this.childs.push(child);//加入
	child.stage=this;//子对象的舞台stage属性自动赋值
};
DisplayObjectContainer.prototype.addChildAt= function(child,index) {//按照索引添加子对象
	if(this.maxWidth<child.x+child.width) {
		this.maxWidth=child.x+child.width;//自动处理最大宽高
	}
	if(this.maxHeight<child.y+child.height) {
		this.maxHegiht=child.y+child.height;
	}
	this.childs.splice(index,0,child);//在index索引处插入子对象
	child.stage=this;//子对象的舞台stage属性自动赋值
};
DisplayObjectContainer.prototype.removeChild= function(child) {//移除子对象
	this.childs.splice(this.getChildIndex(child),1);//移除
	if(this.maxWidth==child.x+child.width) {//处理最大宽高
		this.maxWidth=0;
		for (var i=0; i < this.childs.length; i++) {
			if(this.childs[i].x+this.childs[i].width>this.maxWidth) {
				this.maxWidth=this.childs[i].x+this.childs[i].width;
			}
		};
	}
	if(this.maxHeight==child.y+child.height) {
		this.maxHeight=0;
		for (var i=0; i < this.childs.length; i++) {
			if(this.childs[i].y+this.childs[i].height>this.maxHeight) {
				this.maxHeight=this.childs[i].y+this.childs[i].height;
			}
		};
	}
	child.stage=null;//已移除,故子对象舞台stage为空
};
DisplayObjectContainer.prototype.removeChildAt= function(index) {//根据索引移除子对象
	this.childs[index].stage=null;//已移除,故子对象舞台stage为空
	this.childs.splice(index,1);//移除
	if(this.maxWidth==child.x+child.width) {//处理最大宽高
		this.maxWidth=0;
		for (var i=0; i < this.childs.length; i++) {
			if(this.childs[i].x+this.childs[i].width>this.maxWidth) {
				this.maxWidth=this.childs[i].x+this.childs[i].width;
			}
		};
	}
	if(this.maxHeight==child.y+child.height) {
		this.maxHeight=0;
		for (var i=0; i < this.childs.length; i++) {
			if(this.childs[i].y+this.childs[i].height>this.maxHeight) {
				this.maxHeight=this.childs[i].y+this.childs[i].height;
			}
		};
	}
};
DisplayObjectContainer.prototype.getChildAt= function(index) {//根据索引取出子对象
	return this.childs[index];
};
DisplayObjectContainer.prototype.contains= function(child) {//判断某对象是否为该stage舞台的子对象
	return (this.getChildIndex(child)!=-1);
};
DisplayObjectContainer.prototype.getChildIndex= function(child) {//根据子对象取出索引
	for (var i=0; i < this.childs.length; i++) {
		if(this.childs[i]==child) {
			return i;
		}
	};
	return -1;
};
DisplayObjectContainer.prototype.setChildIndex= function(child,index) {//重设子对象索引(未测试)
	this.removeChild(child);
	this.addChildAt(child,index);
};
DisplayObjectContainer.prototype.swapChildren= function(child1,child2) {//交换子对象索引(未测试)
	this.setChildIndex(child1,this.getChildIndex(child2));
	this.setChildIndex(child2,this.getChildIndex(child1));
};
DisplayObjectContainer.prototype.dispatchMouseEvent= function(type,x,y) {//调度鼠标事件
	var mouseX=x;
	var mouseY=y;
	var newMoveChild=new Array();//当前鼠标悬浮子对象数组
	for (var i=0; i < this.childs.length; i++) {
		if(this.childs[i].dispatchMouseEvent!=null && this.childs[i].dispatchMouseEvent!=undefined) {
			this.childs[i].dispatchMouseEvent(type,mouseX-this.childs[i].x,mouseY-this.childs[i].y);//如果子对象仍有调度函数,也需调用
		}
		//↓ 与子对象相交
		if(mouseX>this.childs[i].x && mouseX<this.childs[i].x+this.childs[i].width && mouseY>this.childs[i].y && mouseY<this.childs[i].y+this.childs[i].height) {
			if(type=="onmousemove" ) {
				newMoveChild.push(this.childs[i]);//如果是鼠标移动事件,记录悬浮对象
			}
			if(this.childs[i].eventListner[type]==null || this.childs[i].eventListner[type]==undefined) {
				continue;//如果子对象没有相应事件侦听器,跳过
			}
			for (var j=0; j < this.childs[i].eventListner[type].length; j++) {
				this.childs[i].eventListner[type][j](mouseX-this.childs[i].x,mouseY-this.childs[i].y);//否则循环执行一遍侦听器
			}
		}
	};
	if(type!="onmousemove") {
		return;//如果不是鼠标移动事件,就无事了
	}
	for (var j=0; j < this.moveChild.length; j++) {//循环寻找 原先悬浮子对象中有,新悬浮子对象中没有的
		var has=false;
		for (var i=0; i < newMoveChild.length; i++) {
			if(this.moveChild[j]==newMoveChild[i]) {
				has=true;
			}
		};
		if(has==false) {//没有了
			if(this.moveChild[j].eventListner["onmouseout"]) {//即鼠标移出了它,为他处理onmouseout事件
				for (var i=0; i < this.moveChild[j].eventListner["onmouseout"].length; i++) {
					this.moveChild[j].eventListner["onmouseout"][i](mouseX-this.moveChild[j].x,mouseY-this.moveChild[j].y);
				}
			}
			delete this.moveChild[j];
			this.moveChild[j]=undefined;
		}
	};
	for (var i=0; i < newMoveChild.length; i++) {//
  


 

您可能想查找下面的文章:

  • JCanvas库 开发接口

相关文章

  • 2018-12-03基于html5 DeviceOrientation 实现微信摇一摇功能_html5教程技巧
  • 2018-12-03还要多少年, 前端开发才能像客户端开发那样轻松?
  • 2018-12-03H5的语义化标签
  • 2017-08-06HTML5的hidden属性兼容老浏览器的方法
  • 2018-12-03利用H5制作一个倒计时demo的实例教程
  • 2017-08-06HTML 5.1来了 9月份正式发布 更新内容预览
  • 2018-12-03HTML5引入的新数组TypedArray介绍_html5教程技巧
  • 2018-12-03有关HTML5服务器推送事件的讲解
  • 2018-12-03利用SurfaceView实现下雨与下雪动画的效果
  • 2018-12-03HTML5实现文件断点续传的方法

文章分类

  • html/xhtml
  • html5
  • CSS
  • XML/XSLT
  • Dreamweaver教程
  • Frontpage教程
  • 心得技巧
  • bootstrap
  • vue
  • AngularJS
  • HBuilder教程
  • css3
  • 浏览器兼容
  • div/css
  • 网页编辑器
  • axure

最近更新的内容

    • HTML5学习笔记简明版(3):新元素之hgroup,header,footer
    • HTML5实战与剖析之焦点管理(activeElement和hasFocus)
    • 一款利用html5和css3动画排列人物头像的实例演示
    • 详解H5的自定义属性data-*
    • 利用Canvas模仿百度贴吧客户端loading小球的方法
    • html5新增标签有哪些?html5新增的标签应用
    • html5 touch事件实现页面上下滑动效果【附代码】
    • html5 兼容IE6结构的实现代码
    • 详解HTML5地理定位与第三方工具百度地图的应用
    • html5 worker 实例(一) 为什么测试不到效果

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有