-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDrawer.php
47 lines (39 loc) · 1.61 KB
/
Drawer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
// My pattern drawer class isn't working! Please help!
// 1. Fix the errors so the pattern will be displayed on the screen
// 2. If a width and height is passed it should not be random
// 3. If a no width and height is passed it should be random
class CoolPatternDrawer
{
private function draw(?int $width, ?int $height): string
{
$canvasWidth = rand(300, 500);
$canvasHeight = rand(300, 500);
return <<<HTML
<html>
<head>
<title>Cool Pattern</title>
</head>
<body>
<canvas id='coolCanvas' width='$canvasWidth' height='$canvasHeight' style='border:1px solid #000;'></canvas>
<script>
var canvas = document.getElementById('coolCanvas');
var ctx = canvas.getContext('2d');
// Draw a random geometric pattern
for (var i = 0; i < 100; i++) {
var x = Math.random() * $canvasWidth;
var y = Math.random() * $canvasHeight;
var size = Math.random() * 20;
var red = Math.floor(Math.random() * 256);
var green = Math.floor(Math.random() * 256);
var blue = Math.floor(Math.random() * 256);
ctx.fillStyle = 'rgb(' + red + ',' + green + ',' + blue + ')';
ctx.fillRect(x, y, size, size);
}
</script>
</body>
</html>
HTML;
}
}
CoolPatternDrawer::draw(width: 300, height: 300);