[AS3]as3.0对图片进行不规则切割源代码实例
[AS3]as3.0对图片进行不规则切割源代码实例
[AS3]as3.0对图片进行不规则切割源代码实例
- import flash.display.Sprite;
- import flash.display.Loader;
- import flash.net.URLRequest;
- import flash.display.BitmapData;
- import flash.events.Event;
- import flash.events.MouseEvent;
- import flash.filters.GlowFilter;
- import flash.filters.DropShadowFilter;
- var spArray:Array = new Array();
- var rows,cols,w,h,i,j,m,n:Number;
- var filterArray:Array = new Array();
- //定义行数列数
- rows = cols = 5;
- //画曲线的重要参数,curWidth为贝塞尔曲线的参数
- var curWidth:Number;
- var ellipseA:Number;
- var ellipseB:Number;
- //加载图片
- var url = new URLRequest("1.jpg");
- var _loader = new Loader();
- _loader.load(url);
- _loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onImgLoad);
- //定义滤镜
- var color1:Number = 0x33CCFF;
- var alp1:Number = 0.8;
- var blurX1:Number = 2;
- var blurY1:Number = 2;
- var strength1:Number = 2;
- var quality1:Number = 3;
- var inner1:Boolean = false;
- var knockout1:Boolean = false;
- var filter1:GlowFilter = new GlowFilter(color,alp1,blurX1,blurY1,strength1,quality1,inner1,knockout1);
- filterArray.push(filter1);
- var splArray:Array = new Array();
- var distance:Number = 3;
- var angleInDegrees:Number = 3;
- var color:Number = 0x000033;
- var alp:Number = 0.8;
- var blurX:Number = 3;
- var blurY:Number = 3;
- var strength:Number = 1;
- var quality:Number = 3;
- var inner:Boolean = false;
- var knockout:Boolean = false;
- var hideObject:Boolean = false;
- var filter:DropShadowFilter = new DropShadowFilter(distance,angleInDegrees,color,alp,blurX,blurY,strength,quality,inner,knockout,hideObject);
- filterArray.push(filter);
- //对每个格子的形状初始化,如果left, right, up, down全部为true表示四面有填充的椭圆
- for (i = 0; i < rows; i ++)
- {
- for (j = 0; j < cols; j++)
- {
- var ms:MySplit = new MySplit(i,j);
- //首先调用随机数,确认本格右方和下方是否填充
- m = int(Math.random() * 2);
- n = int(Math.random() * 2);
- if (m == 1 && j < cols - 1)
- {
- ms.right = true;
- }
- if (n == 1 && i < rows - 1)
- {
- ms.down = true;
- }
- //cuplayer.com检测本格左方是否需要填充
- if (j > 0 &&! splArray[i * cols + j - 1].right)
- {
- ms.left = true;
- }
- //cuplayer.com检测本格上方是否需要填充
- if (i > 0 && ! splArray[(i - 1) * cols + j].down)
- {
- ms.up = true;
- }
- splArray.push(ms);
- }
- }
- //cuplayer.com主处理函数
- function onImgLoad(event:Event):void
- {
- var bitmap:BitmapData = new BitmapData(_loader.width,_loader.height);
- w = _loader.width / rows;
- curWidth = w / 4;
- h = _loader.height / cols;
- //ellipseA始终较大,这样可以适应竖着或横着的椭圆
- ellipseA = h > w ? h / 5:w / 5;
- ellipseB = h<w? h / 5: w /5;
- //cuplayer.com逐个分割图片
- for (i = 0; i < rows; i ++)
- {
- for (j = 0; j < cols; j++)
- {
- bitmap.draw(_loader);
- var sampleSprite:Sprite = new Sprite();
- sampleSprite.graphics.lineStyle();
- sampleSprite.graphics.beginBitmapFill(bitmap);
- myDraw(sampleSprite, splArray[i*cols + j]);
- sampleSprite.graphics.endFill();
- if (w*rows > 1000)
- {
- sampleSprite.scaleX = 1000 / (w * rows);
- sampleSprite.scaleY = 1000/(w*rows)
- }
- sampleSprite.x = 2 * j;
- sampleSprite.y = 2 * i;
- addChild(sampleSprite);
- sampleSprite.filters = filterArray;
- spArray.push(sampleSprite);
- sampleSprite.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
- sampleSprite.addEventListener(MouseEvent.MOUSE_UP, onUp);
- }
- }
- }
- //cuplayer.com画图函数
- function myDraw(sprite:Sprite, ms:MySplit)
- {
- i = ms.row;
- j = ms.col;
- sprite.graphics.drawRoundRect(j * w, i * h, w, h, 5, 5);
- if (ms.right)
- {
- sprite.graphics.moveTo(w*(j+1), i*h);
- sprite.graphics.curveTo(w*(j+1) - curWidth, i * h + h / 2, w*(j+1), (i+1)*h);
- sprite.graphics.drawEllipse(w*(j+1)-0.5*curWidth, i*h+0.5*h-0.5*ellipseA,ellipseB,ellipseA);
- }
- else if(j < 4)
- {
- sprite.graphics.moveTo(w*(j+1), i*h);
- sprite.graphics.curveTo(w*(j+1) + curWidth, i * h + h / 2, w*(j+1), (i+1)*h);
- sprite.graphics.drawEllipse(w*(j+1)+0.5*curWidth - ellipseB, i*h+0.5*h-0.5*ellipseA,ellipseB,ellipseA);
- }
- if (ms.left)
- {
- sprite.graphics.moveTo(w*j, i*h);
- sprite.graphics.curveTo(w*j + curWidth, i * h + h / 2, w*j, (i+1)*h);
- sprite.graphics.drawEllipse(w*j+0.5*curWidth - ellipseB, i*h+0.5*h-0.5*ellipseA,ellipseB,ellipseA);
- }
- else if (j > 0)
- {
- sprite.graphics.drawEllipse(w*j-0.5*curWidth, i*h+0.5*h-0.5*ellipseA,ellipseB,ellipseA);
- sprite.graphics.moveTo(w*j, i*h);
- sprite.graphics.curveTo(w*j - curWidth, i * h + h / 2, w*j, (i+1)*h);
- }
- if (ms.down)
- {
- sprite.graphics.moveTo(w*j, (i+1)*h);
- sprite.graphics.curveTo(w*j+0.5*w, (i+1) * h - curWidth, (j+1)*w, (i+1)*h);
- sprite.graphics.drawEllipse(w*j+0.5*w - 0.5*ellipseA, (i+1)*h-0.5*curWidth, ellipseA,ellipseB);
- }
- else if(i < 4)
- {
- sprite.graphics.moveTo(w*j, (i+1)*h);
- sprite.graphics.curveTo(w*j+0.5*w, (i+1) * h + curWidth, (j+1)*w, (i+1)*h);
- sprite.graphics.drawEllipse(w*j+0.5*w - 0.5*ellipseA, (i+1)*h+0.5*curWidth - ellipseB,ellipseA,ellipseB);
- }
- if (ms.up)
- {
- sprite.graphics.moveTo(w*j, i*h);
- sprite.graphics.curveTo(w*j+0.5*w, i * h + curWidth, (j+1)*w, i*h);
- sprite.graphics.drawEllipse(w*j+0.5*w - 0.5*ellipseA, i*h+0.5*curWidth - ellipseB,ellipseA,ellipseB);
- }
- else if(i > 0)
- {
- sprite.graphics.moveTo(w*j, i*h);
- sprite.graphics.curveTo(w*j+0.5*w, i * h - curWidth, (j+1)*w, i*h);
- sprite.graphics.drawEllipse(w*j+0.5*w - 0.5*ellipseA, i*h-0.5*curWidth,ellipseA,ellipseB);
- }
- }
- //cuplayer.com拖曳处理函数
- function onDown(e:MouseEvent)
- {
- e.target.startDrag();
- }
- function onUp(e:MouseEvent)
- {
- e.target.stopDrag();
- }
热门文章推荐
- [HLS]做自己的m3u8点播系统使用HTTP Live Streaming(HLS技术)
- [FMS]FMS流媒体服务器配置与使用相关的介绍
- [AS3]什么是M3U8,与HTML5的区别是什么
- AS2.0 让flash自适应全屏,并且不自动缩放
- [AS3]as3.0的sound类常用技巧整理
- [AS3]as3与ByteArray详解、ByteArray介绍、ByteArray用法
- 关于RTMP,RTMPT,RTMPS,RTMPE,RTMPTE协议的介绍
- [JS]分享浏览器弹出窗口不被拦截JS示例
请稍候...