xiangpei001
Flash/Flex构架师 / 广东
积分 0

AS3文档类制作变色球


2010-08-28 点击:


  1. package {  
  2.  
  3.  
  4.  
  5.         import flash.display.Sprite;  
  6.  
  7.  
  8.  
  9.         [SWF(backgroundColor="#FFFFFF"width="465"height="465"frameRate="30")]  
  10.  
  11.  
  12.  
  13.         public class Main extends Sprite {  
  14.  
  15.                 private var aqua:Aqua;  
  16.  
  17.                 private var wheel:Wheel;  
  18.  
  19.  
  20.  
  21.                 public function Main() {  
  22.  
  23.                           
  24.  
  25.                         init();  
  26.  
  27.                 }  
  28.  
  29.  
  30.  
  31.                 private function init():void {  
  32.  
  33.                         aqua = new Aqua(160, 230);  
  34.  
  35.                         addChild(aqua);  
  36.  
  37.                         aqua.x = 232;  
  38.  
  39.                         aqua.y = 392;  
  40.  
  41.                         var wheel:Wheel = new Wheel();  
  42.  
  43.                         wheel.x = 420;  
  44.  
  45.                         wheel.y = 420;  
  46.  
  47.                         addChild(wheel);  
  48.  
  49.                         wheel.init({zero: 0, angle :230});  
  50.  
  51.                         wheel.addEventListener(CompoEvent.CHANGE, change, false, 0, true);  
  52.  
  53.                 }  
  54.  
  55.                 private function change(evt:CompoEvent):void {  
  56.  
  57.                         aqua.colorize(evt.value);  
  58.  
  59.                 }  
  60.  
  61.  
  62.  
  63.         }  
  64.  
  65.  
  66.  
  67. }  
  68.  
  69.  
  70.  
  71.  
  72.  
  73. import flash.display.Sprite;  
  74.  
  75. import flash.display.Shape;  
  76.  
  77. import flash.filters.DropShadowFilter;  
  78.  
  79. import flash.filters.GlowFilter;  
  80.  
  81. import flash.filters.BlurFilter;  
  82.  
  83. import flash.geom.Matrix;  
  84.  
  85. import flash.display.GradientType;  
  86.  
  87. import flash.display.SpreadMethod;  
  88.  
  89. import flash.display.InterpolationMethod;  
  90.  
  91. import flash.geom.ColorTransform;  
  92.  
  93. import frocessing.color.ColorHSV;  
  94.  
  95.  
  96.  
  97. class Aqua extends Sprite {  
  98.  
  99.         private var radius:uint;  
  100.  
  101.         private var hue:Number;  
  102.  
  103.         private var light:Shape;  
  104.  
  105.         private var inner:Shape;  
  106.  
  107.         private var base:Shape;  
  108.  
  109.         private var shade:Shape;  
  110.  
  111.         private static var bColor:uint = 0xFFFFFF;  
  112.  
  113.         private static var sColor:uint = 0x000000;  
  114.  
  115.         private var shaded:Boolean;  
  116.  
  117.  
  118.  
  119.         public function Aqua(r:uint, h:Number, s:Boolean = true) {  
  120.  
  121.                 rradius = r;  
  122.  
  123.                 hhue = h;  
  124.  
  125.                 sshaded = s;  
  126.  
  127.                 draw();  
  128.  
  129.         }  
  130.  
  131.  
  132.  
  133.         private function draw():void {  
  134.  
  135.                 if (shaded) {  
  136.  
  137.                         shade = new Shape();  
  138.  
  139.                         addChild(shade);  
  140.  
  141.                         createShade();  
  142.  
  143.                 }  
  144.  
  145.                 base = new Shape();  
  146.  
  147.                 addChild(base);  
  148.  
  149.                 base.y = -radius;  
  150.  
  151.                 inner = new Shape();  
  152.  
  153.                 addChild(inner);  
  154.  
  155.                 inner.y = -radius;  
  156.  
  157.                 light = new Shape();  
  158.  
  159.                 addChild(light);  
  160.  
  161.                 light.y = -radius;  
  162.  
  163.                 createLight();  
  164.  
  165.                 colorize(hue);  
  166.  
  167.         }  
  168.  
  169.         public function colorize(h:Number):void {  
  170.  
  171.                 h %= 360;  
  172.  
  173.                 if (h < 0) h += 360;  
  174.  
  175.                 hue = Math.floor(h);  
  176.  
  177.                 createBase();  
  178.  
  179.                 createInner();  
  180.  
  181.         }  
  182.  
  183.         private function createBase():void {  
  184.  
  185.                 base.graphics.clear();  
  186.  
  187.                 //var color:Number = ColorConversion.HSBtoHEX24(hue, 75, 78);  
  188.  
  189.                 var hsv:ColorHSV = new ColorHSV(hue, 0.75, 0.78);  
  190.  
  191.                 base.graphics.beginFill(hsv.value);  
  192.  
  193.                 base.graphics.drawCircle(0, 0, radius);  
  194.  
  195.                 base.graphics.endFill();  
  196.  
  197.                 var _hue:Number = (hue + 6)%360;  
  198.  
  199.                 //var gColor:Number = ColorConversion.HSBtoHEX24(_hue, 50, 53);  
  200.  
  201.                 var ghsv:ColorHSV = new ColorHSV(hue, 0.5, 0.53);  
  202.  
  203.                 var glow:GlowFilter = new GlowFilter(ghsv.value, 1, radius*0.5, radius*0.5, 2, 3, true, false); 
  204.  
  205.                 base.filters = [glow];  
  206.  
  207.         }  
  208.  
  209.         private function createShade():void {  
  210.  
  211.                 shade.graphics.beginFill(sColor);  
  212.  
  213.                 shade.graphics.drawEllipse(-radius*0.75, -radius*0.1775, radius*1.5, radius*0.375);  
  214.  
  215.                 shade.graphics.endFill();  
  216.  
  217.                 var shadow:DropShadowFilter = new DropShadowFilter(0, 90, sColor, 0.5, radius*0.15, radius*0.15, 1, 3, false, false, true); 
  218.  
  219.                 shade.filters = [shadow];  
  220.  
  221.         }  
  222.  
  223.         private function createLight():void {  
  224.  
  225.                 var colors:Array = [bColor, bColor];  
  226.  
  227.                 var alphas:Array = [0.7, 0];  
  228.  
  229.                 var ratios:Array = [0, 191];  
  230.  
  231.                 var matrix:Matrix = new Matrix();  
  232.  
  233.                 var w:Number = radius*1.44;  
  234.  
  235.                 var h:Number = radius*1.35;  
  236.  
  237.                 var yOffset:Number = radius*0.95;  
  238.  
  239.                 matrix.createGradientBox(w, h, 0.5*Math.PI, -w*0.5, -yOffset);  
  240.  
  241.                 light.graphics.beginGradientFill(GradientType.LINEAR, colors, alphas, ratios, matrix, SpreadMethod.PAD, InterpolationMethod.RGB, 0); 
  242.  
  243.                 light.graphics.drawEllipse(-w*0.5, -yOffset, w, h);  
  244.  
  245.                 light.graphics.endFill();  
  246.  
  247.         }  
  248.  
  249.         private function createInner():void {  
  250.  
  251.                 inner.graphics.clear();  
  252.  
  253.                 var colors:Array = [bColor, bColor];  
  254.  
  255.                 var alphas:Array = [1, 0];  
  256.  
  257.                 var ratios:Array = [0, 191];  
  258.  
  259.                 var matrix:Matrix = new Matrix();  
  260.  
  261.                 var w:Number = radius*1.44;  
  262.  
  263.                 var h:Number = radius*1.35;  
  264.  
  265.                 var yOffset:Number = radius*0.45;  
  266.  
  267.                 matrix.createGradientBox(w, h, -0.5*Math.PI, -w*0.5, -yOffset);  
  268.  
  269.                 inner.graphics.beginGradientFill(GradientType.LINEAR, colors, 
  270.  
  271. alphas, ratios, matrix, SpreadMethod.PAD, InterpolationMethod.RGB, 0); 
  272.  
  273.                 inner.graphics.drawEllipse(-w*0.5, -yOffset, w, h);  
  274.  
  275.                 inner.graphics.endFill();  
  276.  
  277.                 var colorTrans:ColorTransform = new ColorTransform();  
  278.  
  279.                 var _hue:Number = (hue - 30)%360;  
  280.  
  281.                 if (_hue < 0) _hue += 360;  
  282.  
  283.                 //var color:Number = ColorConversion.HSBtoHEX24(_hue, 100, 100);  
  284.  
  285.                 var color:ColorHSV = new ColorHSV(hue, 1, 1);  
  286.  
  287.                 colorcolorTrans.color = color.value;  
  288.  
  289.                 inner.transform.colorTransform = colorTrans;  
  290.  
  291.                 var b:Number = radius*0.15;  
  292.  
  293.                 var blur:BlurFilter = new BlurFilter(b, b, 3);  
  294.  
  295.                 inner.filters = [blur];  
  296.  
  297.         }  
  298.  
  299.  
  300.  
  301. }  
  302.  
  303.  
  304.  
  305.  
  306.  
  307. import flash.display.Sprite;  
  308.  
  309. import flash.display.Shape;  
  310.  
  311. import flash.filters.DropShadowFilter;  
  312.  
  313. import flash.geom.ColorTransform;  
  314.  
  315. import flash.events.Event;  
  316.  
  317. import flash.events.MouseEvent;  
  318.  
  319.  
  320.  
  321. class Wheel extends Sprite {  
  322.  
  323.         private var base:Sprite;  
  324.  
  325.         private var thumb:Sprite;  
  326.  
  327.         private var point:Shape;  
  328.  
  329.         private var hit:Shape;  
  330.  
  331.         private static var bColor:uint = 0xFFFFFF;  
  332.  
  333.         private static var cColor:uint = 0x999999;  
  334.  
  335.         private static var sColor:uint = 0x000000;  
  336.  
  337.         private static var pColor:uint = 0x666666;  
  338.  
  339.         private static var offColor:uint = 0xCCCCCC;  
  340.  
  341.         private static var cColorTrans:ColorTransform;  
  342.  
  343.         private static var pColorTrans:ColorTransform;  
  344.  
  345.         private static var offColorTrans:ColorTransform;  
  346.  
  347.         private var zero:Number = 0;  
  348.  
  349.         private var angle:Number = 0;  
  350.  
  351.         private var shade:DropShadowFilter;  
  352.  
  353.         private var initValue:Number;  
  354.  
  355.         private var value:Number;  
  356.  
  357.         private var _enabled:Boolean = true;  
  358.  
  359.  
  360.  
  361.         public function Wheel() {  
  362.  
  363.         }  
  364.  
  365.  
  366.  
  367.         public function init(option:Object):void {  
  368.  
  369.                 if (option.zero != undefined) zero = option.zero;  
  370.  
  371.                 if (option.angle != undefined) angle = option.angle;  
  372.  
  373.                 value = initValue = angle;  
  374.  
  375.                 draw();  
  376.  
  377.         }  
  378.  
  379.         private function draw():void {  
  380.  
  381.                 shade = new DropShadowFilter(1, 90, sColor, 0.4, 4, 4, 2, 3, false, false);  
  382.  
  383.                 cColorTrans = new ColorTransform();  
  384.  
  385.                 cColorcColorTrans.color = cColor;  
  386.  
  387.                 pColorTrans = new ColorTransform();  
  388.  
  389.                 pColorpColorTrans.color = pColor;  
  390.  
  391.                 offColorTrans = new ColorTransform();  
  392.  
  393.                 offColoroffColorTrans.color = offColor;  
  394.  
  395.                 base = new Sprite();  
  396.  
  397.                 thumb = new Sprite();  
  398.  
  399.                 point = new Shape();  
  400.  
  401.                 hit = new Shape();  
  402.  
  403.                 addChild(base);  
  404.  
  405.                 base.addChild(thumb);  
  406.  
  407.                 thumb.addChild(point);  
  408.  
  409.                 thumb.addChild(hit);  
  410.  
  411.                 addChild(thumb);  
  412.  
  413.                 base.x = thumb.x = 0;  
  414.  
  415.                 base.y = thumb.y = 0;  
  416.  
  417.                 createDonut(base, 30, 10);  
  418.  
  419.                 base.filters = [shade];  
  420.  
  421.                 point.x = 20;  
  422.  
  423.                 createCircle(point, 5, bColor, 1);  
  424.  
  425.                 hit.x = 20;  
  426.  
  427.                 createCircle(hit, 10, bColor, 0);  
  428.  
  429.                 reset();  
  430.  
  431.                 _up();  
  432.  
  433.                 enabled = true;  
  434.  
  435.                 thumb.mouseChildren = false;  
  436.  
  437.         }  
  438.  
  439.         private function rollOver(evt:MouseEvent):void {  
  440.  
  441.                 _over();  
  442.  
  443.         }  
  444.  
  445.         private function rollOut(evt:MouseEvent):void {  
  446.  
  447.                 _up();  
  448.  
  449.         }  
  450.  
  451.         private function press(evt:MouseEvent):void {  
  452.  
  453.                 _down();  
  454.  
  455.                 thumb.addEventListener(MouseEvent.MOUSE_UP, release, false, 0, true);  
  456.  
  457.                 stage.addEventListener(MouseEvent.MOUSE_UP, releaseOutside, false, 0, true); 
  458.  
  459.                 stage.addEventListener(MouseEvent.MOUSE_MOVE, change, false, 0, true);  
  460.  
  461.                 stage.addEventListener(Event.MOUSE_LEAVE, leave, false, 0, true);  
  462.  
  463.         }  
  464.  
  465.         private function release(evt:MouseEvent):void {  
  466.  
  467.                 _up();  
  468.  
  469.                 checkValue();  
  470.  
  471.                 var e:CompoEvent = new CompoEvent(CompoEvent.SELECT, value);  
  472.  
  473.                 dispatchEvent(e);  
  474.  
  475.                 thumb.removeEventListener(MouseEvent.MOUSE_UP, release);  
  476.  
  477.                 stage.removeEventListener(MouseEvent.MOUSE_UP, releaseOutside);  
  478.  
  479.                 stage.removeEventListener(MouseEvent.MOUSE_MOVE, change);  
  480.  
  481.                 stage.removeEventListener(Event.MOUSE_LEAVE, leave);  
  482.  
  483.         }  
  484.  
  485.         private function releaseOutside(evt:MouseEvent):void {  
  486.  
  487.                 _up();  
  488.  
  489.                 checkValue();  
  490.  
  491.                 var e:CompoEvent = new CompoEvent(CompoEvent.SELECT, value);  
  492.  
  493.                 dispatchEvent(e);  
  494.  
  495.                 thumb.removeEventListener(MouseEvent.MOUSE_UP, release);  
  496.  
  497.                 stage.removeEventListener(MouseEvent.MOUSE_UP, releaseOutside);  
  498.  
  499.                 stage.removeEventListener(MouseEvent.MOUSE_MOVE, change);  
  500.  
  501.                 stage.removeEventListener(Event.MOUSE_LEAVE, leave);  
  502.  
  503.         }  
  504.  
  505.         private function leave(evt:Event):void {  
  506.  
  507.                 _up();  
  508.  
  509.                 checkValue();  
  510.  
  511.                 var e:CompoEvent = new CompoEvent(CompoEvent.SELECT, value);  
  512.  
  513.                 dispatchEvent(e);  
  514.  
  515.                 thumb.removeEventListener(MouseEvent.MOUSE_UP, release);  
  516.  
  517.                 stage.removeEventListener(MouseEvent.MOUSE_UP, releaseOutside);  
  518.  
  519.                 stage.removeEventListener(MouseEvent.MOUSE_MOVE, change);  
  520.  
  521.                 stage.removeEventListener(Event.MOUSE_LEAVE, leave);  
  522.  
  523.         }  
  524.  
  525.         private function _up():void {  
  526.  
  527.                 point.transform.colorTransform = cColorTrans;  
  528.  
  529.         }  
  530.  
  531.         private function _over():void {  
  532.  
  533.                 point.transform.colorTransform = pColorTrans;  
  534.  
  535.         }  
  536.  
  537.         private function _down():void {  
  538.  
  539.                 point.transform.colorTransform = pColorTrans;  
  540.  
  541.         }  
  542.  
  543.         private function _off():void {  
  544.  
  545.                 point.transform.colorTransform = offColorTrans;  
  546.  
  547.         }  
  548.  
  549.         private function change(evt:MouseEvent):void {  
  550.  
  551.                 _down();  
  552.  
  553.                 thumb.rotation = Math.round(Math.atan2(base.mouseY, base.mouseX)/Math.PI*180); 
  554.  
  555.                 evt.updateAfterEvent();  
  556.  
  557.                 checkValue();  
  558.  
  559.                 var e:CompoEvent = new CompoEvent(CompoEvent.CHANGE, value);  
  560.  
  561.                 dispatchEvent(e);  
  562.  
  563.         }  
  564.  
  565.         private function checkValue():void {  
  566.  
  567.                 value = (thumb.rotation + 360 + 90 - zero)%360;  
  568.  
  569.         }  
  570.  
  571.         public function get enabled():Boolean {  
  572.  
  573.                 return _enabled;  
  574.  
  575.         }  
  576.  
  577.         public function set enabled(param:Boolean):void {  
  578.  
  579.                 _enabled = param;  
  580.  
  581.                 if (!_enabled) _off();  
  582.  
  583.                 thumb.buttonMode = _enabled;  
  584.  
  585.                 thumb.mouseEnabled = _enabled;  
  586.  
  587.                 thumb.useHandCursor = _enabled;  
  588.  
  589.                 if (_enabled) {  
  590.  
  591.                 thumb.addEventListener(MouseEvent.MOUSE_OVER, rollOver, false, 0, true);  
  592.  
  593.                 thumb.addEventListener(MouseEvent.MOUSE_OUT, rollOut, false, 0, true);  
  594.  
  595.                 thumb.addEventListener(MouseEvent.MOUSE_DOWN, press, false, 0, true);  
  596.  
  597.                 } else {  
  598.  
  599.                 thumb.removeEventListener(MouseEvent.MOUSE_OVER, rollOver);  
  600.  
  601.                 thumb.removeEventListener(MouseEvent.MOUSE_OUT, rollOut);  
  602.  
  603.                 thumb.removeEventListener(MouseEvent.MOUSE_DOWN, press);  
  604.  
  605.                 }  
  606.  
  607.         }  
  608.  
  609.         public function reset():void {  
  610.  
  611.                 value = initValue;  
  612.  
  613.                 thumb.rotation = value - 90 + zero;  
  614.  
  615.         }  
  616.  
  617.         private function createCircle(target:Shape, r:uint, color:uint, alpha:Number):void {  
  618.  
  619.                 target.graphics.beginFill(color, alpha);  
  620.  
  621.                 target.graphics.drawCircle(0, 0, r);  
  622.  
  623.                 target.graphics.endFill();  
  624.  
  625.         }  
  626.  
  627.         private function createDonut(target:Sprite, outer:uint, inner:uint):void {  
  628.  
  629.                 target.graphics.beginFill(bColor);  
  630.  
  631.                 target.graphics.drawCircle(0, 0, outer);  
  632.  
  633.                 target.graphics.drawCircle(0, 0, inner);  
  634.  
  635.                 target.graphics.endFill();  
  636.  
  637.         }  
  638.  
  639.  
  640.  
  641. }  
  642.  
  643.  
  644.  
  645.  
  646.  
  647. import flash.events.Event;  
  648.  
  649.  
  650.  
  651. class CompoEvent extends Event {  
  652.  
  653.         public static const SELECT:String = "select";  
  654.  
  655.         public static const CHANGE:String = "change";  
  656.  
  657.         public var value:*;  
  658.  
  659.  
  660.  
  661.         public function CompoEvent(type:String, value:*) {  
  662.  
  663.                 super(type);  
  664.  
  665.                 this.value = value;  
  666.  
  667.         }  
  668.  
  669.  
  670.  
  671.         public override function clone():Event {  
  672.  
  673.                 return new CompoEvent(type, value);  
  674.  
  675.         }  
  676.  
  677.  
  678.  

 


    xiangpei001  版权所有
    禁止任何用途(禁止转载、商用和个人使用)


所属分类:技术经验分享

本文标签:AS3文档类 变色球

各位正在潜水的同学请注意,有0位无聊人士 在EBIBI附近出没!







    点击我更换图片 看不清
    评论内容 (*必填):

    (Ctrl + Enter 快速提交)