Randomly Colored Elastic Stripes


/ Published in: ActionScript 3
Save to your folder(s)

see demo at http://adamcoulombe.info/lab/as3/stripes.html.

This demonstrates the use of k-lib, Kuler API library for AS3. As well as Traer Physics for to handle the springiness.


Copy this code and paste it in your HTML
  1. //Get Traer.Physics for AS3 at http://blog.sqrtof5.com/?p=53
  2. import traer.physics.Attraction;
  3. import traer.physics.Particle;
  4. import traer.physics.ParticleSystem;
  5. import traer.physics.Spring;
  6.  
  7. //get k-lib Kuler API Library at http://code.google.com/p/k-lib/
  8. import com.adobe.kuler.events.GetResultEvent;
  9. import com.adobe.kuler.params.SearchParams;
  10. import com.adobe.kuler.swatches.swatch.color.Color;
  11. import com.adobe.kuler.KLibService;
  12.  
  13. import flash.geom.Vector3D;
  14.  
  15. //
  16. //
  17. //
  18. //
  19. // Get a Kuler API key at http://kuler.adobe.com/api/
  20. //
  21. // BE SURE TO ENTER YOUR API KEY HERE FOR THIS TO WORK
  22. // ||
  23. // ||
  24. // ||
  25. // \/
  26.  
  27. var YOUR_KULER_API_KEY = "";
  28.  
  29.  
  30.  
  31. var s:ParticleSystem = new ParticleSystem(new Vector3D(0, 0, 0), .2);
  32. var waterMolecules = new Array();
  33. var waterParticles = new Array();
  34. var sky = new Array();
  35. var earth = new Array();
  36. var magnet = s.makeParticle(0.12, new Vector3D(0, 0, 0));
  37. var hover=new Molecule();
  38. var points = new Array();
  39. var water = new Sprite();
  40. var lastColor = "000000";
  41. var container = new Sprite();
  42. var stripes = new Array();
  43. var colors = new Array();
  44. var totalWidth = 0;
  45. var swatch;
  46.  
  47.  
  48. var klib:KLibService = new KLibService(YOUR_KULER_API_KEY);
  49. klib.addEventListener(GetResultEvent.GET_RESULTS, onResults);
  50. klib.getRecent();
  51.  
  52.  
  53. function onResults(e){
  54. swatch = e.results.swatches[0];
  55. trace(swatch.colors[ int(Math.random() * swatch.colors.length -1) ]);
  56. init();
  57. }
  58.  
  59.  
  60.  
  61. function init(){
  62. magnet.makeFixed();
  63. magnet.position.y=200;
  64. var i=0;
  65. while(totalWidth<stage.stageWidth){
  66.  
  67. colors[i] = lastColor;
  68. while(colors[i]==lastColor){
  69. colors[i]= swatch.colors[ int(Math.random() * swatch.colors.length -1) ].swatchHexColor;
  70. trace(colors[i]);
  71. }
  72. sky[i]=s.makeParticle(10, new Vector3D(totalWidth, 100, 0)); sky[i].makeFixed();
  73. waterParticles[i]=s.makeParticle(0.5, new Vector3D(totalWidth, 200, 0));
  74. earth[i]=s.makeParticle(10, new Vector3D(totalWidth, 300, 0)); earth[i].makeFixed();
  75. var thisWidth = Math.random()*20 + 5;
  76. totalWidth += thisWidth;
  77. stripes[i]=new Sprite();
  78. addChild(stripes[i]);
  79.  
  80. if(i>0){
  81. s.makeSpring(waterParticles[i-1], waterParticles[i], 0.1 , .05, 0);
  82. s.makeSpring(sky[i], waterParticles[i], 0.05 , .05, 0);
  83. s.makeSpring(earth[i], waterParticles[i], 0.05 , .05, 0);
  84.  
  85. s.makeAttraction(magnet, waterParticles[i], -15000 , 10);
  86.  
  87. }
  88. lastColor = colors[i];
  89. i++
  90. }
  91.  
  92. waterParticles[0].makeFixed();
  93. waterParticles[waterParticles.length-1].makeFixed();
  94. waterParticles[waterParticles.length-1].position.x=stage.stageWidth;
  95.  
  96. stage.addEventListener(Event.ENTER_FRAME,loop);
  97. }
  98.  
  99. function loop(e){
  100. s.tick(1);
  101. magnet.position.x = hover.x = mouseX;
  102.  
  103. for(var i in waterParticles){
  104. stripes[i].graphics.clear();
  105. stripes[i].graphics.beginFill("0x"+colors[i]);
  106. if(i<waterParticles.length){
  107. stripes[i].graphics.moveTo(waterParticles[i].position.x,0);
  108. stripes[i].graphics.lineTo(waterParticles[i].position.x,stage.stageHeight);
  109. }
  110. if(i>0){
  111. stripes[i].graphics.lineTo(waterParticles[i-1].position.x,stage.stageHeight);
  112. stripes[i].graphics.lineTo(waterParticles[i-1].position.x,0);
  113. }
  114. stripes[i].graphics.endFill();
  115.  
  116. }
  117.  
  118. }

URL: http://adamcoulombe.info/lab/as3/stripes.html

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.