move($x, $y); } public function move($x, $y){ $this->x = $x; $this->y = $y; } public abstract function generateDrawCode(); public function scale($factor){ $this->x = $this->x * $factor; $this->y = $this->y * $factor; } } class Circle extends DrawingObject { protected $radius; public function __construct($x, $y, $radius){ parent::__construct($x, $y); $this->radius = $radius; } public function generateDrawCode(){ echo 'ctx.beginPath();'; echo 'ctx.arc('.$this->x.', '.$this->y.', '.$this->radius.', 0, Math.PI*2, true);'; echo 'ctx.closePath();'; echo 'ctx.stroke();'; } public function scale($factor){ parent::scale($factor); $this->radius = $this->radius * $factor; } } class Rectangle extends DrawingObject { protected $width; protected $height; public function __construct($x, $y, $width, $height){ parent::__construct($x, $y); $this->width = $width; $this->height = $height; } public function generateDrawCode(){ echo 'ctx.strokeRect('.$this->x.', '.$this->y.', '.$this->width.', '.$this->height.');'; } public function scale($factor){ parent::scale($factor); $this->width = $this->width * $factor; $this->height = $this->height * $factor; } } class Container extends DrawingObject { protected $objects = array(); public function __construct($x, $y){ parent::__construct($x, $y); } public function add($object){ $this->objects[] = $object; } public function generateDrawCode(){ foreach ($this->objects as $object) { $x = $object->x; $y = $object->y; $object->move($this->x + $x, $this->y + $y); $object->generateDrawCode(); $object->move($x, $y); } } public function scale($factor){ parent::scale($factor); foreach ($this->objects as $object) { $object->scale($factor); } } } $sun=new Circle(10,4,2); $train=new Container(1,0); $cabin=new Rectangle(0,6,3,3); $body=new Rectangle(0,9,7,3); $chimney=new Rectangle(5,7,1,2); $wheel1=new Circle(2,12,1); $wheel2=new Circle(5,12,1); $train->add($cabin); $train->add($body); $train->add($chimney); $train->add($wheel1); $train->add($wheel2); $drawingBoard = new Container(0, 0); $drawingBoard->add($sun); $drawingBoard->add($train); ?> Canvas