用js+css 模拟九宫格精灵

<pre name="code" class="javascript">__setScale9Sprite :function(data)
	{
		var _this = this;
		if(data.wraperId != _this.m_id) {
			return;
		}

		//最外层的div
		var scale9Sprite = _this.m_viewElement.querySelector('#scale9Sprite');
		if(!scale9Sprite) {
			scale9Sprite = document.createElement('div');
			scale9Sprite.id = 'scale9Sprite';
			scale9Sprite.style.position = 'absolute';
			scale9Sprite.style.border = 'solid 1px black';
			_this.m_viewElement.appendChild(scale9Sprite);
		}
		if (data.key) {
			switch(data.key){
				case 'file':
						var resInfo = gameEditor.getProject().getFileInfoByID(data.resId);
						_this.scale9SpriteResID = data.resId;
						if(resInfo) {
							var url = global.TAG_BASE_URL_RES_HOST + resInfo.resURL.substr(1);
							var width = resInfo.width;
							var height = resInfo.height;
							//通过循环创建9个img,用来存放裁切来的图片
							for (var i = 1; i <= 9 ; i++) {
								var imgSprite = '#img'+i;
								var img = scale9Sprite.querySelector(imgSprite);
								if (!img) {
									var img = document.createElement("img");
									//img.style.border = 'solid 1px black';
									img.id = 'img'+ i;
									//通过绝对定位确定位置
									img.style.position = 'absolute';
									//img.style.top = Math.ceil(i/3)*height/3 +'px';
									//img.style.left = (i-1)%3*width/3 + "px";
									img.style.top = -height/2 +'px';
									img.style.left = -width/2 + 'px';
									img.style.width = width +'px';
									img.style.height = height + 'px';
									//对img进行裁切
									//img.style.clip =  "rect("+ (Math.ceil(i/3)-1)*height/3+"px"+","+(i-1)%3*width/3+"px"+","+width/3+"px"+","+height/3+"px" +")";
									//最终剪裁的矩形的上边距离原始元素的上边缘 
									//剪裁矩形的右边缘距离原元素左边缘的距离
									//剪裁矩形的下边缘距离原元素顶部的距离 
									//剪裁矩形的左边缘距离原元素左边缘的距离
									img.style.clip =  "rect("+ (Math.ceil(i/3)-1)*height/3+"px"+","+((i-1)%3+1)*width/3+"px"+","+((i-1)%3+Math.ceil(i/3)*height/3)+"px"+","+((i-1)%3)*width/3+"px" +")";
									//对每个img的图片的填充
									img.width = width ;
									img.height = height ;
									scale9Sprite.appendChild(img);
								};
								img.setAttribute('src', url);
								
							};
							
						}
				break;
				case 'scaleAreaX':
					var resInfo = gameEditor.getProject().getFileInfoByID(this.scale9SpriteResID);

					var wraper = gameEditor.GetWraper(data.wraperId);
					var component = wraper.getComponents('Scale9Sprite')[0];

					if (!resInfo && !wraper  && !component) {
						return;
					};
					var spW = Number( component.getPropertyValue('scale9SpriteW'))|| resInfo.width;
					var spH =  Number(component.getPropertyValue("scale9SpriteH"))|| resInfo.height;
					var picX = Number(component.getPropertyValue("scaleAreaX")) || spW/3;
					var picY = Number(component.getPropertyValue("scaleAreaY")) || spH/3;
					var picW = Number(component.getPropertyValue("scaleAreaW")) || spW/3;
					var picH = Number(component.getPropertyValue("scaleAreaH")) || spH/3;
					//初始化裁切
					this.onScale9SapriteRefresh(picX,picY,picW,picH,spW,spH);
					this.onScale9SapriteChange(picX,picY,picW,picH,spW,spH);







				break;
				case 'scaleAreaY':

				
				var resInfo = gameEditor.getProject().getFileInfoByID(this.scale9SpriteResID);

				var wraper = gameEditor.GetWraper(data.wraperId);
				var component = wraper.getComponents('Scale9Sprite')[0];

				if (!resInfo && !wraper  && !component) {
						return;
					};
				
				var spW = Number( component.getPropertyValue('scale9SpriteW'))|| resInfo.width;
				var spH =  Number(component.getPropertyValue("scale9SpriteH"))|| resInfo.height;
				var picX = Number(component.getPropertyValue("scaleAreaX")) || spW/3;
				var picY = Number(component.getPropertyValue("scaleAreaY")) || spH/3;
				var picW = Number(component.getPropertyValue("scaleAreaW")) || spW/3;
				var picH = Number(component.getPropertyValue("scaleAreaH")) || spH/3;
				//初始化裁切
				this.onScale9SapriteRefresh(picX,picY,picW,picH,spW,spH);
				this.onScale9SapriteChange(picX,picY,picW,picH,spW,spH);


				break;
				case 'scaleAreaW':
				var resInfo = gameEditor.getProject().getFileInfoByID(this.scale9SpriteResID);

				var wraper = gameEditor.GetWraper(data.wraperId);
				var component = wraper.getComponents('Scale9Sprite')[0];

				if (!resInfo && !wraper  && !component) {
						return;
					};
				
				var spW = Number( component.getPropertyValue('scale9SpriteW'))|| resInfo.width;
				var spH =  Number(component.getPropertyValue("scale9SpriteH"))|| resInfo.height;
				var picX = Number(component.getPropertyValue("scaleAreaX")) || spW/3;
				var picY = Number(component.getPropertyValue("scaleAreaY")) || spH/3;
				var picW = Number(component.getPropertyValue("scaleAreaW")) || spW/3;
				var picH = Number(component.getPropertyValue("scaleAreaH")) || spH/3;
				//初始化裁切
				this.onScale9SapriteRefresh(picX,picY,picW,picH,spW,spH);
				this.onScale9SapriteChange(picX,picY,picW,picH,spW,spH);


				break;
				case 'scaleAreaH':
					var resInfo = gameEditor.getProject().getFileInfoByID(this.scale9SpriteResID);

					var wraper = gameEditor.GetWraper(data.wraperId);
					var component = wraper.getComponents('Scale9Sprite')[0];

					if (!resInfo && !wraper  && !component) {
						return;
					};
					
					var spW = Number( component.getPropertyValue('scale9SpriteW'))|| resInfo.width;
					var spH =  Number(component.getPropertyValue("scale9SpriteH"))|| resInfo.height;
					var picX = Number(component.getPropertyValue("scaleAreaX")) || spW/3;
					var picY = Number(component.getPropertyValue("scaleAreaY")) || spH/3;
					var picW = Number(component.getPropertyValue("scaleAreaW")) || spW/3;
					var picH = Number(component.getPropertyValue("scaleAreaH")) || spH/3;
						//初始化裁切
					this.onScale9SapriteRefresh(picX,picY,picW,picH,spW,spH);
					this.onScale9SapriteChange(picX,picY,picW,picH,spW,spH);
				break;
				case 'scale9SpriteW':
			
					var resInfo = gameEditor.getProject().getFileInfoByID(this.scale9SpriteResID);

					var wraper = gameEditor.GetWraper(data.wraperId);
					var component = wraper.getComponents('Scale9Sprite')[0];
					if (!resInfo && !wraper  && !component) {
						return;
					};

					
					var spW = Number( component.getPropertyValue('scale9SpriteW'))|| resInfo.width;
					var spH =  Number(component.getPropertyValue("scale9SpriteH"))|| resInfo.height;
					var picX = Number(component.getPropertyValue("scaleAreaX")) || spW/3;
					var picY = Number(component.getPropertyValue("scaleAreaY")) || spH/3;
					var picW = Number(component.getPropertyValue("scaleAreaW")) || spW/3;
					var picH = Number(component.getPropertyValue("scaleAreaH")) || spH/3;
					//初始化裁切
					this.onScale9SapriteRefresh(picX,picY,picW,picH,spW,spH);
					this.onScale9SapriteChange(picX,picY,picW,picH,spW,spH);
				break;
				case 'scale9SpriteH':
				
					var resInfo = gameEditor.getProject().getFileInfoByID(this.scale9SpriteResID);

					var wraper = gameEditor.GetWraper(data.wraperId);
					var component = wraper.getComponents('Scale9Sprite')[0];
					if (!resInfo && !wraper  && !component) {
						return;
					};

					
					var spW = Number( component.getPropertyValue('scale9SpriteW'))|| resInfo.width;
					var spH =  Number(component.getPropertyValue("scale9SpriteH"))|| resInfo.height;
					var picX = Number(component.getPropertyValue("scaleAreaX")) || spW/3;
					var picY = Number(component.getPropertyValue("scaleAreaY")) || spH/3;
					var picW = Number(component.getPropertyValue("scaleAreaW")) || spW/3;
					var picH = Number(component.getPropertyValue("scaleAreaH")) || spH/3;
					//初始化裁切
					this.onScale9SapriteRefresh(picX,picY,picW,picH,spW,spH);
					this.onScale9SapriteChange(picX,picY,picW,picH,spW,spH);
				break;
				case 'delete':
						var img = _this.m_viewElement.querySelector('#scale9Sprite');
						if(img) {
							_this.m_viewElement.removeChild(img);
						}
						eventCenter.raise('NOTIFY_OBJECT_SIZE_CHANGED');
				break;
			}
		};

		eventCenter.raise('NOTIFY_OBJECT_SIZE_CHANGED');


	},
	//当九宫格发生改变
	onScale9SapriteChange: function(picX,picY,picW,picH,spW,spH)
	{

		//最终剪裁的矩形的上边距离原始元素的上边缘 
		//剪裁矩形的右边缘距离原元素左边缘的距离
		//剪裁矩形的下边缘距离原元素顶部的距离 
		//剪裁矩形的左边缘距离原元素左边缘的距离

		//获取放大缩小倍数
		var resInfo = gameEditor.getProject().getFileInfoByID(this.scale9SpriteResID);
		if (resInfo) {
			var resWidth = resInfo.width;
			var resHeight = resInfo.height;

			var ratioX = (spW - picX - (resWidth - picX - picW))/(resWidth- picX -(resWidth - picX - picW));
			var ratioY = (spH- picY - (resHeight - picY - picH))/(resHeight - picY - (resHeight - picY - picH));

			var	_this = this;
			var scale9Sprite = _this.m_viewElement.querySelector('#scale9Sprite');
			if (scale9Sprite) {

				var img1 = scale9Sprite.querySelector('#img1');
				if (img1) {
					img1.style.clip =  "rect("+ 0 +"px"+","+ picX +"px"+","+ (resHeight - picX - picH)  +"px"+","+ 0+"px" +")";
				};
				var img2 = scale9Sprite.querySelector('#img2');
				if (img2) {
				//x轴放大
					img2.style.width = resWidth * ratioX +'px';
					img2.style.left  = parseInt(img2.style.left) - (picX*ratioX - picX)  + 'px';
				//对齐中心点
					img2.style.clip =  "rect("+ 0 +"px"+","+ (picX*ratioX + picW*ratioX) +"px"+","+ (resHeight - picX - picH) +"px"+","+ picX*ratioX +"px" +")";
				};
				var img3 = scale9Sprite.querySelector('#img3');
				if (img3) {
				//需要对齐坐标
					img3.style.left = parseInt(img3.style.left) + (spW - parseInt(img3.style.width))  +"px";
					img3.style.clip =  "rect("+ 0 +"px"+","+ resWidth +"px"+","+ (resHeight - picY - picH) +"px"+","+ ( picW + picX) +"px" +")";
				};
				var img4 = scale9Sprite.querySelector('#img4');
				if (img4) {
				//Y轴压缩
					img4.style.height = resHeight * ratioY + 'px';
					img4.style.top = parseInt(img4.style.top) - (picY*ratioY - picY)  + 'px';

					img4.style.clip =  "rect("+ (parseInt(img4.style.height) - picY*ratioY - picH*ratioY) +"px"+","+ picX +"px"+","+ ((resHeight - picY - picH)*ratioY + picH*ratioY) +"px"+","+ 0+"px" +")";
				};
				var img5 = scale9Sprite.querySelector('#img5');
				if (img5) {

					img5.style.width = resWidth * ratioX +'px';
					img5.style.height = resHeight * ratioY + 'px';
				//中心点对齐
				//img5.style.top = parseInt(img5.style.top) + (spH - resHeight) + 'px';
				//img5.style.left = parseInt(img5.style.left)  - ((spW - parseInt(img5.style.width)) + picX) +"px";
					img5.style.top =   parseInt(img5.style.top) - (picY*ratioY - picY)  + 'px';
					img5.style.left =  parseInt(img5.style.left) - (picX*ratioX - picX)  + 'px';

					img5.style.clip =  "rect("+ (parseInt(img5.style.height) - picY*ratioY - picH*ratioY) +"px"+","+ (picX*ratioX + picW*ratioX)+"px"+","+ ((resHeight - picY - picH)*ratioY + picH*ratioY) +"px"+","+ picX*ratioX+"px" +")";
				};

				var img6 = scale9Sprite.querySelector('#img6');
				if (img6) {
				//Y轴压缩
					img6.style.height = resHeight * ratioY + 'px';
				//对齐坐标
					img6.style.left = parseInt(img6.style.left) + (spW - parseInt(img6.style.width))+"px";
					img6.style.top = parseInt(img6.style.top) - (picY*ratioY - picY)  + 'px';

				//img6.style.left = parseInt(img6.style.left) + (parseInt(img6.style.width) - resWidth)/2 + 'px';
				//img6.style.left = parseInt(img6.style.left) + (parseInt(img6.style.width) - resWidth) + 'px';
					img6.style.clip =  "rect("+ (parseInt(img5.style.height) - picY*ratioY - picH*ratioY) +"px"+","+ parseInt(img6.style.width) +"px"+","+ ((resHeight - picY - picH)*ratioY + picH*ratioY) +"px"+","+ (parseInt(img6.style.width) - (resWidth- picX - picW))+"px" +")";
				};

				var img7 = scale9Sprite.querySelector('#img7');
				if (img7) {
					img7.style.top = parseInt(img7.style.top) +  (spH - parseInt(img7.style.height)) +  'px';
					img7.style.clip =  "rect("+ (resHeight- picY) +"px"+","+ picX +"px"+","+ resHeight +"px"+","+ 0+"px" +")";
				
				};
				var img8 = scale9Sprite.querySelector('#img8');
				if (img8) {
				//X轴缩放
					img8.style.width = resWidth * ratioX +'px';
				//对齐坐标
					img8.style.top = parseInt(img8.style.top) + (spH - resHeight) + 'px';
					img8.style.left = img8.style.left  = parseInt(img8.style.left) - (parseInt(img8.style.width)/3 - picX) + 'px';

					img8.style.clip =  "rect("+ (resHeight - picY) +"px"+","+ (picX*ratioX + picW*ratioX) +"px"+","+ resHeight +"px"+","+ picX*ratioX+"px" +")";
				//img8.style.clip =  "rect("+ (resHeight - picY) +"px"+","+ (parseInt(img8.style.width) - (resWidth - picX- picW)) +"px"+","+ resHeight +"px"+","+ picX+"px" +")";
				};	
				var img9 = scale9Sprite.querySelector('#img9');
				if (img9) {
				//需要对齐坐标
					img9.style.top = parseInt(img9.style.top) +  (spH - parseInt(img9.style.height)) +  'px';
					img9.style.left = parseInt(img9.style.left) + (spW - parseInt(img9.style.width)) +"px";
					img9.style.clip =  "rect("+ (resHeight - picY) +"px"+","+ resWidth +"px"+","+ resHeight +"px"+","+ ( picW + picX)+"px" +")";
				};
			};

		};
	},
	//对九宫格重新布局
	onScale9SapriteRefresh : function(picX,picY,picW,picH,spW,spH)
	{
		var	_this = this;
		var scale9Sprite = _this.m_viewElement.querySelector('#scale9Sprite');
		var resInfo = gameEditor.getProject().getFileInfoByID(this.scale9SpriteResID);
		if (resInfo) {
			var width = spW || resInfo.width;
			var height = spH || resInfo.height;
			if (scale9Sprite) {
				for (var i = 1; i <= 9; i++) {
				
					var key = '#img' + i;
					var img = scale9Sprite.querySelector(key);
					if (img) {
						img.style.top = -height/2 +'px';
						img.style.left = -width/2 + 'px';
					//img.style.width = width +'px';
					//img.style.height = height + 'px';
						img.style.clip =  "rect("+ (Math.ceil(i/3)-1)*height/3+"px"+","+((i-1)%3+1)*width/3+"px"+","+((i-1)%3+Math.ceil(i/3)*height/3)+"px"+","+((i-1)%3)*width/3+"px" +")";
					};
				}
			};
		//最终剪裁的矩形的上边距离原始元素的上边缘 
		//剪裁矩形的右边缘距离原元素左边缘的距离
		//剪裁矩形的下边缘距离原元素顶部的距离 
		//剪裁矩形的左边缘距离原元素左边缘的距离

			var scale9Sprite = _this.m_viewElement.querySelector('#scale9Sprite');
			if (scale9Sprite) {
				var img1 = scale9Sprite.querySelector('#img1');
				if (img1) {
					img1.style.clip =  "rect("+ 0 +"px"+","+ picX +"px"+","+ (spH - picY - picH)  +"px"+","+ 0+"px" +")";
				};
				var img2 = scale9Sprite.querySelector('#img2');
				if (img2) {
					img2.style.clip =  "rect("+ 0 +"px"+","+ (picX+ picW) +"px"+","+ (spH - picY - picH) +"px"+","+ picX +"px" +")";
				};
				var img3 = scale9Sprite.querySelector('#img3');
				if (img3) {
					img3.style.clip =  "rect("+ 0 +"px"+","+ spW +"px"+","+ (spH - picY - picH) +"px"+","+ (picW + picX) +"px" +")";
				};
				var img4 = scale9Sprite.querySelector('#img4');
				if (img4) {
					img4.style.clip =  "rect("+ (spH- picY - picH) +"px"+","+ picX +"px"+","+ (spH - picY) +"px"+","+ 0+"px" +")";
				};
				var img5 = scale9Sprite.querySelector('#img5');
				if (img5) {
					img5.style.clip =  "rect("+ (spH - picY - picH) +"px"+","+ (picX + picW) +"px"+","+ (spH - picY) +"px"+","+ picX+"px" +")";
				};

				var img6 = scale9Sprite.querySelector('#img6');
				if (img6) {
					img6.style.clip =  "rect("+ (spH - picY - picH) +"px"+","+ spW +"px"+","+  (spH - picY) +"px"+","+ (picX+picW)+"px" +")";
				};

				var img7 = scale9Sprite.querySelector('#img7');
				if (img7) {
					img7.style.clip =  "rect("+ (spH - picY) +"px"+","+ picX +"px"+","+ spH +"px"+","+ 0+"px" +")";
				};
				var img8 = scale9Sprite.querySelector('#img8');
				if (img8) {
					img8.style.clip =  "rect("+ (spH - picY) +"px"+","+  (picX + picW) +"px"+","+ spH +"px"+","+ picX+"px" +")";
				};
				var img9 = scale9Sprite.querySelector('#img9');
				if (img9) {
					img9.style.clip =  "rect("+ (spH - picY) +"px"+","+ spW +"px"+","+ spH +"px"+","+ ( picW + picX)+"px" +")";
				};
			};

		};
	},



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值