$pig = new Canvas(); if (!$pig) { die("Error while creating canvas pig\n"); } $display = new Display($canvas); if (!$display) { die("Error while attaching canvas to display\n"); } $pig->setColorANSI(AnsiColor::LIGHTMAGENTA, AnsiColor::TRANSPARENT); $pig->importString($pig_str, "text"); $display->setDisplayTime(20000); $x = $y = 0; $ix = $iy = 1; while (!$display->getEvent(EventType::KEY_PRESS)) { // In case of resize ... if ($x + $pig->getWidth() - 1 >= $canvas->getWidth() || $x < 0) { $x = 0; } if ($y + $pig->getHeight() - 1 >= $canvas->getHeight() || $y < 0) { $y = 0; } $canvas->Clear(); // Draw $canvas->Blit($x, $y, $pig, NULL); $canvas->setColorANSI(AnsiColor::LIGHTBLUE, AnsiColor::BLACK); $canvas->putStr($canvas->getWidth() / 2 - 10, $canvas->getHeight() / 2, "Powered by libcaca " . Libcaca::getVersion()); $display->refresh(); // Move cursor $x += $ix; $y += $iy; if ($x + $pig->getWidth() >= $canvas->getWidth() || $x < 0) {
/** * @brief Render the chart in 3D * * @return Canvas */ private function render3D() { $c = new Canvas($this->width, $this->height, rgb($this->getProperty('background', '#FFFFFF'))); $radiusx = 180; $radiusy = 90; $cx = $c->getWidth() / 2; $cy = $c->getHeight() / 2; $explode = $this->getProperty('explode', 0); $palette = $this->getProperty('palette'); list($label, $vals) = $this->dataset->getSeries(0); $labels = $this->dataset->getLabels(); $sum = $vals->getSum(); $ci = 0; $sa = 0; for ($n = 0; $n < $vals->getCount(); $n++) { list($val, $key) = $vals->getValue($n); $a = 360 / $sum * $val; // Get angle $ea = $sa + $a; $ch = rgb($palette[$ci]); $cs = hsv($ch); $cs->value = $cs->value - 30; if (arr::hasKey($labels, $n)) { $l = $labels[$n]; } else { $l = $n; } $data[] = array('key' => $key, 'label' => $l, 'c1' => $ch, 'c2' => $cs, 'sa' => $sa, 'ea' => $ea); $sa = $ea; $ci++; } $offs = array(); foreach ($data as $id => $slice) { $avg = ($slice['sa'] + $slice['ea']) / 2; $data[$id]['ox'] = cos(($avg - 90) % 360 * PI / 180) * $explode; $data[$id]['oy'] = sin(($avg - 90) % 360 * PI / 180) * $explode; $data[$id]['dx'] = cos(($avg - 180) % 360 * PI / 180); $data[$id]['dy'] = sin(($avg - 180) % 360 * PI / 180); } $f = new BitmapFont(3); $f->setTextEffect(BitmapFont::EFFECT_OUTLINE, rgb(255, 255, 255)); $p = $c->getPainter(); for ($yp = 20; $yp >= 0; $yp--) { foreach ($data as $slice) { $ox = $slice['ox']; $oy = $slice['oy']; $p->drawFilledArc($cx + $ox, $cy + $oy + $yp, $radiusx * 2, $radiusy * 2, $slice['sa'], $slice['ea'], $yp == 0 ? $slice['c1'] : $slice['c2']); } } for ($yp = 20; $yp >= 0; $yp--) { foreach ($data as $slice) { // TODO: Labels $m = $f->measure($slice['label']); $dx = $slice['dx']; $dy = $slice['dy']; $c->drawText($f, rgb(0, 0, 0), $cx + $dx * $radiusx / 1.5 - $m['width'] / 2, $cx + $dy * $radiusy / 1.5 - $m['height'] / 2, $slice['label']); } } $this->renderObjects($c); // Return the canvas return $c; }