Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'dart:html';
- import 'package:vector_math/vector_math.dart';
- class Demo {
- final CanvasElement _canvas;
- final Camera _camera;
- CanvasRenderingContext2D _context;
- Demo() : _canvas = document.createElement('canvas') as CanvasElement,
- _camera = new Camera(new Vector2(0.0, 0.0)) {
- _context = _canvas.getContext('2d') as CanvasRenderingContext2D;
- _canvas.width = 640;
- _canvas.height = 640;
- document.body.append(_canvas);
- }
- void start() {
- window.requestAnimationFrame(processTick);
- }
- void processTick(num delta) {
- update();
- render();
- }
- void update() {}
- void render() {
- var tileWidth = 32;
- var tileHeight = 32;
- for (var y = 0; y < 20; y++) {
- for (var x = 0; x < 20; x++) {
- var pos = new Vector2(x.toDouble(), y.toDouble());
- var relativePosition = _camera.getRelativePosition(pos);
- var colour = '#00ff00';
- if (((x + y) % 8) == 0)
- colour = '#ff00ff';
- else if (((x + y) % 4) == 0)
- colour = '#0000ff';
- else if (((x + y) % 2) == 0)
- colour = '#ff0000';
- _context.fillStyle = colour;
- _context.fillRect(x * tileWidth + relativePosition.x, y * tileHeight + relativePosition.y, tileWidth, tileHeight);
- }
- }
- }
- }
- class Camera {
- final Vector2 _position;
- Camera(this._position);
- Vector2 getRelativePosition(Vector2 position) {
- return new Vector2(position.x - _position.x, position.y - _position.y);
- }
- void moveTo(Vector2 position) {
- _position.x = position.x;
- _position.y = position.y;
- }
- void move(Vector2 position) {
- _position.x += position.x;
- _position.y += position.y;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement