-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathch2ex10.dart
82 lines (68 loc) · 2.7 KB
/
ch2ex10.dart
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
library ch2.ex10;
import 'dart:html';
import 'dart:math' as math;
// The dart print() command will automatically print to the console
// and shouldn't throw an exception so no need to worry about creating
// our own debugger. For more complicated logging the logger package exists.
// Since dart supports class inheritance and library namespaces
// we don't need to keep our methods/properties in a local scope.
class CanvasApp {
CanvasElement theCanvas;
CanvasRenderingContext2D context;
CanvasApp() {
// Initialize the canvas and context
theCanvas = document.querySelector('#canvas');
context = theCanvas.getContext('2d');
}
void drawScreen() {
// Content goes here.
// Draw a red square and rotate it
var angleInRadians = 45 * (math.PI / 180);
var x = 50;
var y = 100;
var height = 40;
var width = 40;
context..setTransform(1, 0, 0, 1, 0, 0) // Required before rotating
..translate(x + (width/2), y + (height/2)) // Change our origin point.
..rotate(angleInRadians) // Rotate before drawing
..fillStyle = 'red'
..fillRect(-width/2, -height/2, width, height);
// Set new values and draw it again.
angleInRadians = 75 * (math.PI / 180);
x = 100;
y = 100; // not strictly needed
height = 40; // not strictly needed
width = 40; // Not strictly needed
context..setTransform(1, 0, 0, 1, 0, 0) // Required before rotating
..translate(x + (width/2), y + (height/2)) // Change our origin point.
..rotate(angleInRadians) // Rotate before drawing
..fillStyle = 'red'
..fillRect(-width/2, -height/2, width, height);
// Set new values and draw it again.
angleInRadians = 90 * (math.PI / 180);
x = 150;
y = 100; // not strictly needed
height = 40; // not strictly needed
width = 40; // Not strictly needed
context..setTransform(1, 0, 0, 1, 0, 0) // Required before rotating
..translate(x + (width/2), y + (height/2)) // Change our origin point.
..rotate(angleInRadians) // Rotate before drawing
..fillStyle = 'red'
..fillRect(-width/2, -height/2, width, height);
// Set new values and draw it again.
angleInRadians = 120 * (math.PI / 180);
x = 200;
y = 100; // not strictly needed
height = 40; // not strictly needed
width = 40; // Not strictly needed
context..setTransform(1, 0, 0, 1, 0, 0) // Required before rotating
..translate(x + (width/2), y + (height/2)) // Change our origin point.
..rotate(angleInRadians) // Rotate before drawing
..fillStyle = 'red'
..fillRect(-width/2, -height/2, width, height);
}
}
void main() {
var canvasApp = new CanvasApp();
canvasApp.drawScreen();
}