ytkns/src/Effects/ZoomTextEffect.php
2020-06-10 16:03:13 +00:00

58 lines
1.8 KiB
PHP

<?php
namespace YTKNS\Effects;
use YTKNS\HtmlTag;
use YTKNS\HtmlText;
use YTKNS\PageBuilder;
use YTKNS\PageEffectInterface;
use YTKNS\PageEffectException;
class ZoomTextEffect implements PageEffectInterface {
private const TEXT_MIN = 1;
private const TEXT_MAX = 1000;
private $text = '';
public function getEffectName(): string {
return 'Zoom Text';
}
public function getEffectProperties(): array {
return [
[
'name' => 'txt',
'title' => 'Text',
'type' => [
'name' => 'string',
'min' => self::TEXT_MIN,
'max' => self::TEXT_MAX,
],
],
];
}
public function setEffectParams(array $vars, bool $quiet = false): void {
if(isset($vars['txt']) && is_string($vars['txt'])) {
if(!$quiet && (mb_strlen($vars['txt']) < self::TEXT_MIN || mb_strlen($vars['txt']) > self::TEXT_MAX))
throw new PageEffectException('Your text is too long or too short.');
$this->text = $vars['txt'];
}
}
public function getEffectParams(): array {
return [
'txt' => $this->text,
];
}
public function applyEffect(PageBuilder $builder): void {
$tags = [];
for($i = 1; $i <= 50; $i++) {
$tags[] = new HtmlTag('div', ['class' => 'ZoomText_Child', 'style' => sprintf('font-size: %1$dpt; top: %1$dpx; left: %2$dpx; color: rgb(%3$d, %3$d, %3$d);', $i * 2, $i, $i === 50 ? 0 : (4 * $i))], [new HtmlText($this->text)]);
}
$builder->getContainer()->appendChild(new HtmlTag('div', ['class' => 'ZoomText'], $tags));
}
}