Created
October 23, 2016 16:55
-
-
Save krakjoe/76653e7e485b769d5178ca666acbc952 to your computer and use it in GitHub Desktop.
Starfield canvas from: http://codentronix.com/2011/07/22/html5-canvas-3d-starfield/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use UI\App; | |
use UI\Window; | |
use UI\Point; | |
use UI\Size; | |
use UI\Area; | |
use UI\Controls\Box; | |
use UI\Draw\Pen; | |
use UI\Draw\Brush; | |
use UI\Draw\Path; | |
use UI\Draw\Color; | |
$app = new App(); | |
$win = new Window($app, "Starfield", new Size(640, 480), false); | |
$box = new Box(Box::Vertical); | |
$win->add($box); | |
$stars = new class(512, 32) extends Area { | |
public function onDraw(UI\Draw\Pen $pen, UI\Size $size, UI\Point $clip, UI\Size $clipSize) { | |
$hWidth = $size->width / 2; | |
$hHeight = $size->height / 2; | |
$path = new Path(Path::Winding); | |
$path->addRectangle( | |
new Point(0, 0), $size); | |
$path->end(); | |
$pen->fill($path, new Brush(Brush::Solid, new Color(0, 1))); | |
foreach ($this->stars as $idx => &$star) { | |
$star[1] -= 0.2; | |
if ($star[1] <= 0) { | |
$star[0]->setX(mt_rand(-25, 25)); | |
$star[0]->setY(mt_rand(-25, 25)); | |
$star[1] = $this->depth; | |
} | |
$k = 128 / $star[1]; | |
$px = $star[0]->getX() * $k + $hWidth; | |
$py = $star[0]->getY() * $k + $hHeight; | |
if ($px >= 0 && $px <= $size->width && $py >= 0 && $py <= $size->height) { | |
$starSize = (1 - $star[1] / 32) * 5; | |
$path = new Path(Path::Winding); | |
$path->addRectangle(new Point($px, $py), new Size($starSize, $starSize)); | |
$path->end(); | |
$color = new Color(0, 1); | |
$color->setChannel(Color::Red, $starSize); | |
$color->setChannel(Color::Green, $starSize); | |
$color->setChannel(Color::Blue, $starSize); | |
$pen->fill($path, new Brush(Brush::Solid, $color)); | |
} | |
} | |
} | |
public function __construct($max, $depth) { | |
$this->max = $max; | |
$this->depth = $depth; | |
for ($i = 0; $i < $this->max; $i++) { | |
$this->stars[] = [ | |
new Point(mt_rand(-25, 25), mt_rand(-25, 25)), | |
mt_rand(1, $this->depth) | |
]; | |
} | |
} | |
}; | |
$box->append($stars, true); | |
$win->show(); | |
do { | |
$app->run(true, false); | |
$stars->redraw(); | |
} while(true); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment