public static void Main() { renderer = Pixi.AutoDetectRenderer(Width, Height, new RendererOptions { BackgroundColor = 0x1099bb }); Document.Body.AppendChild(renderer["view"].As <HTMLCanvasElement>()); Textures = new TexturePool(); SceneManager = new SceneManager(); SceneManager.Open <MainMenu>(); Window.RequestAnimationFrame(Animate); }
public Sandbox() { Stage = new Stage(0x00FFFF); Renderer = Pixi.AutoDetectRenderer(1024, 768); Global.document.body.appendChild(Renderer.View); var texture = Texture.FromImage("assets/spacesloth.png"); Sloth = new Sprite(texture); Sloth.Anchor.X = Sloth.Anchor.Y = 0.5f; Sloth.Position.X = 512; Sloth.Position.Y = 384; Stage.AddChild(Sloth); Js.de.requestAnimationFrame(new Action(Render)); }
//both params needs to be below 100 and above 0, its a percantage. Threshold defines how close the color should be to the original color private void tryToDraw(Bitmap cpyImage, double threshold, double changeChance) //This method attempts to draw the base image, not a genetics algorithm just randomly gets values for colors. if the color is "close enough" skips that pixel next time. { Invoke((MethodInvoker) delegate //Disable buttons when entering the method { button1.Enabled = false; bCopyTest.Enabled = false; }); ArrayList pixelsToChange = new ArrayList(); int changedPixels = 0; Random rnd = new Random(); Bitmap cMap = cpyImage; for (int i = 0; i < cMap.Width; i++) //Add all pixels to the list { for (int j = 0; j < cMap.Height; j++) { Point p = new Point(i, j); Pixi pi = new Pixi(p); pixelsToChange.Add(pi); } } Invoke((MethodInvoker) delegate //Create an invoker to access controls from inside a thread. { pbCopy.Maximum = (pixelsToChange.Count) - (pixelsToChange.Count * (100 - cThreshold) / 100); // Set the new maxiumum for the progress bar }); //Pixels that needs changing = (pixelsToChange.Count) - (pixelsToChange.Count * (100 -cThreshold)/100) while (changedPixels <= (pixelsToChange.Count) - (pixelsToChange.Count * (100 - cThreshold) / 100)) //While changed pixels amount not equal to pixels that needs changing, 2nd part of the equation creates a threshold value which allows program to stop before completing the task { foreach (Pixi e in pixelsToChange) //go through every pixel { if (!e.isChanged) { if (rnd.Next(0, 100) <= changeChance) //if random chance is ok change it to some random color { Color cReplace = Color.FromArgb((rnd.Next(255) + 255) % 255, (rnd.Next(255) + 255) % 255, (rnd.Next(255) + 255) % 255); double cDiff = Math.Sqrt(Math.Pow((cReplace.R - getPixel(cMap, e.point.X, e.point.Y).R), 2) + Math.Pow((cReplace.G - getPixel(cMap, e.point.X, e.point.Y).G), 2) + Math.Pow((cReplace.B - getPixel(cMap, e.point.X, e.point.Y).B), 2)); //Distance between colors formula (percantage) Gonna use this alot double cDiffPercant = 100 * cDiff / Math.Sqrt(3 * Math.Pow(255, 2)); if (cDiffPercant < threshold) //if the color is good enough add that Point to the other list and draws it! { drawPixel(cReplace, e.point.X, e.point.Y); e.isChanged = true; Invoke((MethodInvoker) delegate //Create an invoker to access controls from inside a thread. { pbCopy.Value = changedPixels; //Updates the progress bar lbProgressB.Text = "Progress: " + pbCopy.Value + " / " + pbCopy.Maximum; //Prints out the progress }); changedPixels++; if (changedPixels >= (pixelsToChange.Count) - (pixelsToChange.Count * (100 - cThreshold) / 100)) //Safe-check to make sure program stops when required threshold is reached! { Invoke((MethodInvoker) delegate //Reanble buttons after job is done { lbProgressB.Text = "Progress: " + pbCopy.Value + " / " + pbCopy.Maximum + " !Done!"; button1.Enabled = true; bCopyTest.Enabled = true; }); break; } } } } } } }