Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library ball;
- import 'dart:async';
- import 'dart:html';
- import 'dart:math';
- void main() {
- CanvasElement canvas = querySelector("#area");
- scheduleMicrotask(new Game(canvas).start);
- }
- class Ball {
- num x;
- num y;
- num radius;
- String color;
- Ball(this.x, this.y, this.radius, this.color) {
- this.X = x;
- this.Y = y;
- this.Color = color;
- this.Radius = radius;
- }
- void draw(CanvasRenderingContext2D context) {
- context..lineWidth = 0.5
- ..fillStyle = color
- ..strokeStyle = color;
- context
- ..beginPath()
- ..arc(x, y, radius, 0, PI * 2, false)
- ..fill()
- ..closePath();
- }
- num get X => this.x;
- set X(x) => this.x = x;
- num get Y => this.y;
- set Y(y) => this.y = y;
- num get Radius => radius;
- set Radius(r) => radius = r;
- String get Color => this.color;
- set Color(c) => this.color = c;
- }
- class Game {
- CanvasElement canvas;
- final List<Ball> balls = <Ball>[];
- var rng = new Random();
- num width;
- num height;
- Game(this.canvas);
- start() {
- Rectangle rect = canvas.parent.client;
- width = rect.width;
- height = rect.height;
- canvas.width = width;
- canvas.height = height;
- for (var i = 0; i < 100; ++i) {
- final Ball ball = new Ball(rng.nextInt(500) + 100, rng.nextInt(500) + 100, rng.nextInt(50), '#' + rng.nextInt(1000).toString());
- balls.add(ball);
- }
- requestDraw();
- }
- void drawBackground(CanvasRenderingContext2D context) {
- context.clearRect(0, 0, width, height);
- }
- void drawBalls(CanvasRenderingContext2D context) {
- for (var i in balls) {
- i.draw(context);
- }
- }
- void draw(num _) {
- var context = canvas.context2D;
- drawBackground(context);
- drawBalls(context);
- requestDraw();
- }
- void requestDraw() {
- window.requestAnimationFrame(draw);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement